Class: PPCurses::TableView

Inherits:
View show all
Defined in:
lib/ppcurses/table_view.rb

Overview

Instance Attribute Summary collapse

Attributes inherited from View

#frame

Attributes inherited from ResponderManager

#first_responder

Attributes inherited from Responder

#next_responder

Instance Method Summary collapse

Methods inherited from View

#setFrameOrigin, #setFrameSize

Methods inherited from ResponderManager

#accepts_first_responder, #make_first_responder

Methods inherited from Responder

#accepts_first_responder, #become_first_responder, isa, #resign_first_responder

Constructor Details

#initializeTableView

Returns a new instance of TableView.



21
22
23
24
25
26
27
# File 'lib/ppcurses/table_view.rb', line 21

def initialize
  super
  origin = Point.new( 2, 2 )
  setFrameOrigin(origin)
  
  @table_columns = []
end

Instance Attribute Details

#data_sourceObject

Returns the value of attribute data_source.



18
19
20
# File 'lib/ppcurses/table_view.rb', line 18

def data_source
  @data_source
end

#selected_rowObject (readonly)

Returns the value of attribute selected_row.



19
20
21
# File 'lib/ppcurses/table_view.rb', line 19

def selected_row
  @selected_row
end

Instance Method Details

#add_table_column(col) ⇒ Object

Column Management



141
142
143
144
# File 'lib/ppcurses/table_view.rb', line 141

def add_table_column( col )
  col.table_view = self
  @table_columns.push(col)    
end

#display(screen) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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) ⇒ Object

NSTableViewSelectionDidChangeNotification

NSNotificationCentre


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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_columnsObject



128
129
130
131
132
133
134
135
136
137
# 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