class TheFox::TermKit::TableView

View sub-class.

Provides functionalities to show data in a scrollable table.

Attributes

cells[R]
cells_height_total[R]
cursor_direction[R]
cursor_position[R]
cursor_position_old[R]
data[R]

attr_reader :table

header[R]
header_height[R]
highlighted_cell[RW]
page_begin[R]
page_direction[R]
page_end[R]
page_height[R]

Public Class Methods

new(name = nil) click to toggle source
Calls superclass method TheFox::TermKit::View.new
# File lib/termkit/view/view_table.rb, line 28
def initialize(name = nil)
    # puts "TableView initialize '#{name.inspect}'"
    super(name)
    
    @header = nil
    @header_height = 0
    @data = []
    @cells = []
    @cells_height_total = 0
    @highlighted_cell = nil
    
    @cursor_position = 0
    @cursor_position_old = 0
    @cursor_direction = 0
    
    @page_begin = 0
    @page_end = 0
    @page_height = 0
    @page_direction = 0
    @page_range = nil
    
    @needs_refresh = true
    
    @table = View.new("#{@name}_table")
    @table.is_visible = true
    add_subview(@table)
end

Public Instance Methods

cursor_down() click to toggle source
# File lib/termkit/view/view_table.rb, line 157
def cursor_down
    self.cursor_position = @cursor_position + 1
end
cursor_position=(cursor_position) click to toggle source
# File lib/termkit/view/view_table.rb, line 144
def cursor_position=(cursor_position)
    @cursor_position_old = @cursor_position
    @cursor_position = cursor_position
    
    @needs_refresh = true
    calc_cursor
    calc_page
end
cursor_up() click to toggle source
# File lib/termkit/view/view_table.rb, line 153
def cursor_up
    self.cursor_position = @cursor_position - 1
end
data=(data) click to toggle source
# File lib/termkit/view/view_table.rb, line 97
def data=(data)
    if !data.is_a?(Array)
        raise ArgumentError, "Argument is not a Array -- #{data.class} given"
    end
    
    @data = data
    @cells = []
    
    cell_n = 0
    y_pos = 0
    @data.each do |row|
        cell = nil
        
        row_name = "row_#{cell_n}"
        
        case row
        when String
            text_view = TextView.new(row, "text_#{row_name}")
            text_view.is_visible = true
            # text_view.text = row
            
            cell = CellTableView.new(text_view, "cell_#{row_name}")
        when CellTableView
            cell = row
        else
            raise NotImplementedError, "Class '#{row.class}' not implemented yet"
        end
        
        @cells.push(cell)
        
        #cell.is_visible = false
        cell.is_visible = true
        #cell.position = Point.new(0, y_pos)
        #@table.add_subview(cell)
        
        y_pos += cell.height
        cell_n += 1
    end
    
    @cells_height_total = y_pos
    
    @needs_refresh = true
    calc_page_height
    calc_cursor
    calc_page
end
header=(header) click to toggle source
# File lib/termkit/view/view_table.rb, line 65
def header=(header)
    unless header.is_a?(View)
        raise ArgumentError, "Argument is not a View -- #{header.class} given"
    end
    
    unless @header.nil?
        remove_subview(@header)
    end
    
    @header = header
    unless header.nil?
        @header_height = @header.height
        
        add_subview(@header)
    end
    
    @table.position = Point.new(0, @header_height)
    
    @needs_refresh = true
    calc_page_height
end
is_cursor_at_bottom?() click to toggle source
# File lib/termkit/view/view_table.rb, line 161
def is_cursor_at_bottom?
    @cursor_position == @cells_height_total - 1
end
refresh() click to toggle source
# File lib/termkit/view/view_table.rb, line 174
def refresh
    new_page_range = Range.new(@page_begin, @page_end)
    
    affected_cells = @cells[new_page_range]
    
    y_pos = 0
    cell_n = 0
    affected_cells.each do |cell|
        highlighted = @cursor_position == (cell_n + @page_begin)
        
        # puts "#{@name} -- [+] #{cell} n=#{cell_n} y=#{y_pos} h=#{highlighted ? 'Y' : 'N'}/#{cell.highlighted ? 'Y' : 'N'}"
        cell.highlighted = highlighted
        
        if highlighted
            @highlighted_cell = cell
        end
        
        # cell.size = Size.new(@size.width, nil)
        
        # puts "#{@name} -- [+] #{cell} y=#{y_pos} position"
        cell.position = Point.new(0, y_pos)
        
        unless @table.is_subview?(cell)
            # puts "#{@name} -- [+] #{cell} y=#{y_pos} add_subview"
            @table.add_subview(cell)
        end
        
        # puts "#{@name} -- [+] #{cell} y=#{y_pos} END"
        
        y_pos += cell.height
        cell_n += 1
    end
    
    # Hide out-of-scope cell(s) here. In the best case it's only ONE cell that will
    # be hidden. If you scroll down the top cell will be hidden, if you scroll up
    # only the bottom cell will be hidden.
    (@cells - affected_cells).select{ |cell| cell.is_visible? }.each do |cell|
        # puts "#{@name} -- [-] #{cell} y=#{cell.position.y} r?=#{cell.needs_rendering? ? 'Y' : 'N'}"
        @table.remove_subview(cell)
    end
    
    @page_range = new_page_range
    @needs_refresh = false
end
remove_header() click to toggle source
# File lib/termkit/view/view_table.rb, line 87
def remove_header
    @header = nil
    @header_height = 0
    
    @table.position = Point.new(0, @header_height)
    
    @needs_refresh = true
    calc_page_height
end
render(area = nil) click to toggle source
Calls superclass method TheFox::TermKit::View#render
# File lib/termkit/view/view_table.rb, line 165
def render(area = nil)
    puts "#{@name} -- render r?=#{@needs_refresh ? 'Y' : 'N'}"
    if @needs_refresh
        refresh
    end
    
    super(area)
end
size=(size) click to toggle source
Calls superclass method TheFox::TermKit::View#size=
# File lib/termkit/view/view_table.rb, line 56
def size=(size)
    super(size)
    
    @needs_refresh = true
    calc_page_height
    calc_cursor
    calc_page
end