class PPCurses::TableView

Based loosely on …

developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTableView_Class/index.html#//apple_ref/occ/cl/NSTableView

Attributes

data_source[RW]
selected_row[R]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/ppcurses/table_view.rb, line 21
def initialize
  super
  origin = Point.new( 2, 2 )
  setFrameOrigin(origin)
  
  @table_columns = []
end

Public Instance Methods

add_table_column( col ) click to toggle source

Column Management

# File lib/ppcurses/table_view.rb, line 141
def add_table_column( col )
  col.table_view = self
  @table_columns.push(col)    
end
data_source=(val) click to toggle source

A data source must implement a formal protocol

  • def number_of_rows_in_table(tableView)

  • def object_value_for(tableView, tableColumn, rowIndex)

# File lib/ppcurses/table_view.rb, line 35
def data_source=(val)
  PPCurses.implements_protocol( val, %w(number_of_rows_in_table object_value_for ))
  @data_source = val
  @selected_row = 0
  
  # Determine frame size from datasource data
  height = @data_source.number_of_rows_in_table(self)
  width = 0    
  for i in 0..@data_source.number_of_rows_in_table(self)-1
    x = @data_source.object_value_for(self, 0, i).length
    if x > width then width = x end
  end
  
  # Add an extra line for the column header
  # and ====== divider
  height += 2
  
  sz = Size.new( width, height )
  setFrameSize( sz )
  
end
display(screen) click to toggle source
# File lib/ppcurses/table_view.rb, line 57
def display(screen)
  
  y = @frame.origin.y
  x = @frame.origin.x
  
  
  # display column header
  screen.setpos(y,x)
  @table_columns.each_with_index do |column,i|
    screen.addstr(column.identifier.center(column.width))
    if i <  @table_columns.length - 1 then screen.addstr(' | ') end
  end
  
  y += 1
  screen.setpos(y,x)
  # Display ================= divider
  @table_columns.each_with_index do |column, i|
    screen.addstr( ''.center(column.width, '=') )  
    if i <  @table_columns.length - 1 then screen.addstr('===') end    
  end
  
  y += 1
  
  for i in 0..@data_source.number_of_rows_in_table(self)-1
    screen.setpos(y,x)
    screen.attron(Curses::A_REVERSE) if i == selected_row
    

    @table_columns.each_with_index do |col,j|
      col_str = @data_source.object_value_for(self, j, i)
      display_string = col_str.ljust(col.width)
      screen.addstr( display_string )
      if j <  @table_columns.length - 1 then screen.addstr(' | ') end
    end
    
    
    screen.attroff(Curses::A_REVERSE) if i == selected_row
    y += 1
  end
end
key_down( key ) click to toggle source

NSTableViewSelectionDidChangeNotification NSNotificationCentre

# File lib/ppcurses/table_view.rb, line 101
def key_down( key )
    
    notary = NotificationCentre.default_centre
    
    if key == KEY_DOWN        
      notary.post_notification(PPTableViewSelectionIsChangingNotification, self) 
      @selected_row += 1
      if @selected_row > @data_source.number_of_rows_in_table(self) - 1 then
        @selected_row = 0
      end
      notary.post_notification(PPTableViewSelectionDidChangeNotification, self)        
    end
    
    if key == KEY_UP
       notary.post_notification(PPTableViewSelectionIsChangingNotification, self) 
       @selected_row -= 1
       if @selected_row < 0 then @selected_row = @data_source.number_of_rows_in_table(self) - 1 end
       notary.post_notification(PPTableViewSelectionDidChangeNotification, self)
    end
    
    if key == ENTER
      notary.post_notification(PPTableViewEnterPressedNotification, self) 
    end
    
    
end
number_of_columns() click to toggle source
# File lib/ppcurses/table_view.rb, line 128
def number_of_columns
  num_col = 0
  
  while @data_source.object_value_for(self, num_col, 0) != nil do
    num_col += 1
  end
  
  
  num_col
end