Class: RubyCurses::Tree

Inherits:
Widget show all
Includes:
ListScrollable
Defined in:
lib/rbcurse/rtree.rb

Overview

a representation of heirarchical data in outline form Currently supports only single selection, and does not allow editing.

Direct Known Subclasses

DirectoryTree

Constant Summary

Constants included from Io

Io::ERROR_COLOR_PAIR, Io::FOOTER_COLOR_PAIR, Io::LINEONE, Io::MAIN_WINDOW_COLOR_PAIR

Instance Attribute Summary collapse

Attributes included from ListScrollable

#find_offset, #find_offset1, #search_found_ix, #show_caret

Attributes inherited from Widget

#_object_created, #col_offset, #cols_panned, #config, #curpos, #focussed, #form, #id, #parent_component, #row_offset, #rows_panned, #state

Instance Method Summary collapse

Methods included from ListScrollable

#_find_next, #_find_prev, #ask_search, #bounds_check, #find_more, #find_next, #find_prev, #focussed_index, #forward_char, #forward_word, #goto_bottom, #goto_last_position, #goto_top, #install_keys, #next_match, #next_row, #previous_row, #sanitize, #scroll_backward, #scroll_forward, #scroll_left, #scroll_right, #selected_item, #set_focus_on, #set_form_row, #set_selection_for_char, #show_caret_func, #truncate

Methods inherited from Widget

#changed, #click, #destroy, #enter, #event_list, #focus, #get_preferred_size, #getvalue_for_paint, #height, #height=, #hide, #leave, #modified?, #move, #on_leave, #override_graphic, #printstring, #process_key, #remove, #repaint_all, #repaint_required, #rowcol, #set_buffer_modified, #set_buffering, #set_form, #set_form_row, #set_modified, #setformrowcol, #setrowcol, #show, #text_variable, #unbind_key, #width, #width=

Methods included from Io

#askchoice, #askyesno, #askyesnocancel, #clear_error, #clear_this, #get_string, #newaskyesno, #old_print_header, #old_print_top_right, #print_action, #print_error, #print_footer_help, #print_header, #print_headers, #print_help, #print_help_page, #print_in_middle, #print_key_labels, #print_key_labels_row, #print_screen_labels, #print_status, #print_this, #print_top_right, #rbgetstr, #warn

Methods included from Utils

#_process_key, #bind_key, #clean_string!, #get_color, #keycode_tos, #repeatm, #view, #wrap_text

Methods included from ConfigSetup

#cget, #config_setup, #configure, #variable_set

Methods included from EventHandler

#bind, #fire_handler, #fire_property_change

Constructor Details

#initialize(form, config = {}, &block) ⇒ Tree

Returns a new instance of Tree.



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/rbcurse/rtree.rb', line 61

def initialize form, config={}, &block
  @focusable = true
  @editable = false
  @row = 0
  @col = 0
  # array representation of tree
  @list = nil
  # any special attribs such as status to be printed in col1, or color (selection)
  @list_attribs = {}
  # hash containing nodes that are expanded or once expanded
  # if value is true, then currently expanded, else once expanded
  # TODO : will need purging under some situations
  @expanded_state = {}
  @suppress_borders = false
  @row_offset = @col_offset = 1
  @current_index = 0
  super
  #@selection_mode ||= :single # default is multiple, anything else given becomes single
  @win = @graphic    # 2010-01-04 12:36 BUFFERED  replace form.window with graphic
  @sanitization_required = true
  @longest_line = 0
  
 
  @win_left = 0
  @win_top = 0
  @_events.push(*[:ENTER_ROW, :LEAVE_ROW, :TREE_COLLAPSED_EVENT, :TREE_EXPANDED_EVENT, :TREE_SELECTION_EVENT, :TREE_WILL_COLLAPSE_EVENT, :TREE_WILL_EXPAND_EVENT])

  
  bind(:PROPERTY_CHANGE){|e| @cell_renderer = nil } # will be recreated if anything changes 2011-09-28 V1.3.1  
  init_vars

  #if [email protected]_index.nil? 
    #set_focus_on @list.selected_index # the new version
  #end
  @keys_mapped = false
end

Instance Attribute Details

#current_indexObject

Returns the value of attribute current_index.



43
44
45
# File 'lib/rbcurse/rtree.rb', line 43

def current_index
  @current_index
end

#one_key_selectionObject

dsl_accessor :valign # popup related

will pressing a single key move to first matching row. setting it to false lets us use vim keys



56
57
58
# File 'lib/rbcurse/rtree.rb', line 56

def one_key_selection
  @one_key_selection
end

#selected_indexObject (readonly)

index of row selected, relates to internal representation, not tree. @see selected_row



58
59
60
# File 'lib/rbcurse/rtree.rb', line 58

def selected_index
  @selected_index
end

#toprowObject (readonly)

Returns the value of attribute toprow.



39
40
41
# File 'lib/rbcurse/rtree.rb', line 39

def toprow
  @toprow
end

#treemodelObject (readonly)

returns treemodel for further actions 2011-10-2



59
60
61
# File 'lib/rbcurse/rtree.rb', line 59

def treemodel
  @treemodel
end

Instance Method Details

#_listObject

private, for use by repaint



223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/rbcurse/rtree.rb', line 223

def _list
  if @_structure_changed 
    @list = nil
    @_structure_changed = false
  end
  unless @list
    $log.debug " XXX recreating _list"
    convert_to_list @treemodel
    $log.debug " XXXX list: #{@list.size} : #{@list} "
  end
  return @list
end

#ask_search_backwardObject

gets string to search and calls data models find prev



384
385
386
387
388
389
390
391
392
393
# File 'lib/rbcurse/rtree.rb', line 384

def ask_search_backward
  regex =  get_string("Enter regex to search (backward)")
  @last_regex = regex
  ix = @list.find_prev regex, @current_index
  if ix.nil?
    alert("No matching data for: #{regex}")
  else
    set_focus_on(ix)
  end
end

#ask_search_forwardObject



374
375
376
377
378
379
380
381
382
# File 'lib/rbcurse/rtree.rb', line 374

def ask_search_forward
    regex =  get_string("Enter regex to search")
    ix = @list.find_match regex
    if ix.nil?
      alert("No matching data for: #{regex}")
    else
      set_focus_on(ix)
    end
end

#ask_selection_for_charObject

get a keystroke from user and go to first item starting with that key



367
368
369
370
371
372
373
# File 'lib/rbcurse/rtree.rb', line 367

def ask_selection_for_char
  ch = @graphic.getch
  if ch < 0 || ch > 255
    return :UNHANDLED
  end
  ret = set_selection_for_char ch.chr
end

#cell_renderer(*val) ⇒ Object

getter and setter for cell_renderer



428
429
430
431
432
433
434
# File 'lib/rbcurse/rtree.rb', line 428

def cell_renderer(*val)
  if val.empty?
    @cell_renderer ||= create_default_cell_renderer
  else
    @cell_renderer = val[0] 
  end
end

#collapse_children(node = :current_index) ⇒ Object



663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/rbcurse/rtree.rb', line 663

def collapse_children node=:current_index
  $multiplier = 999 if !$multiplier || $multiplier == 0
  $log.debug " CCCC IINSIDE COLLLAPSE"
  node = row_to_node if node == :current_index
  return if node.children.empty? # or node.is_leaf?
  #node.children.each do |e| 
    #expand_node e # this will keep expanding parents
    #expand_children e
  #end
  node.breadth_each($multiplier) do |e|
    $log.debug "CCC collapsing #{e.user_object}  "
    collapse_node e
  end
  $multiplier = 0
  _structure_changed true
end

#collapse_node(node) ⇒ Object



616
617
618
619
620
621
622
# File 'lib/rbcurse/rtree.rb', line 616

def collapse_node(node)
  $log.debug " collapse called on #{node.user_object} "
  state = false
  fire_handler :TREE_WILL_COLLAPSE_EVENT, node
  set_expanded_state(node, state)
  fire_handler :TREE_COLLAPSED_EVENT, node
end

#collapse_parent(node = :current_index) ⇒ Object

collapse parent can use multiplier. # we need to move up also



682
683
684
685
686
687
688
# File 'lib/rbcurse/rtree.rb', line 682

def collapse_parent node=:current_index
  node = row_to_node if node == :current_index
  parent = node.parent
  return if parent.nil?
  goto_parent node
  collapse_node parent
end

#convert_to_list(tree) ⇒ Object



235
236
237
238
239
# File 'lib/rbcurse/rtree.rb', line 235

def convert_to_list tree
  @list = get_expanded_descendants(tree.root)
  #$log.debug "XXX convert #{tree.root.children.size} "
  #$log.debug " converted tree to list. #{@list.size} "
end

#create_default_cell_rendererObject



435
436
437
# File 'lib/rbcurse/rtree.rb', line 435

def create_default_cell_renderer
  return RubyCurses::TreeCellRenderer.new "", {"color"=>@color, "bgcolor"=>@bgcolor, "parent" => self, "display_length"=> @width-@internal_width-@left_margin}
end

#current_rowObject Also known as: text

return object under cursor Note: this should not be confused with selected row/s. User may not have selected this. This is only useful since in some demos we like to change a status bar as a user scrolls down

Since:

  • 1.2.0 2010-09-06 14:33 making life easier for others.



253
254
255
# File 'lib/rbcurse/rtree.rb', line 253

def current_row
  @list[@current_index]
end

#data(alist = nil) ⇒ Object

pass data to create this tree model used to be list



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/rbcurse/rtree.rb', line 189

def data alist=nil

  # if nothing passed, print an empty root, rather than crashing
  alist = [] if alist.nil?
  @data = alist # data given by user
  case alist
  when Array
    @treemodel = RubyCurses::DefaultTreeModel.new("/")
    @treemodel.root.add alist
  when Hash
    @treemodel = RubyCurses::DefaultTreeModel.new("/")
    @treemodel.root.add alist
  when TreeNode
    # this is a root node
    @treemodel = RubyCurses::DefaultTreeModel.new(alist)
  when DefaultTreeModel
    @treemodel = alist
  else
    if alist.is_a? DefaultTreeModel
      @treemodel = alist
    else
      raise ArgumentError, "Tree does not know how to handle #{alist.class} "
    end
  end
  # we now have a tree
  raise "I still don't have a tree" unless @treemodel
  set_expanded_state(@treemodel.root, true)
  convert_to_list @treemodel
  
  # added on 2009-01-13 23:19 since updates are not automatic now
  #@list.bind(:LIST_DATA_EVENT) { |e| list_data_changed() }
  #create_default_list_selection_model TODO
end

#expand_children(node = :current_index) ⇒ Object

this expands all the children of a node, recursively we can’t use multiplier concept here since we are doing a preorder enumeration we need to do a breadth first enumeration to use a multiplier



649
650
651
652
653
654
655
656
657
658
659
660
661
662
# File 'lib/rbcurse/rtree.rb', line 649

def expand_children node=:current_index
  $multiplier = 999 if !$multiplier || $multiplier == 0
  node = row_to_node if node == :current_index
  return if node.children.empty? # or node.is_leaf?
  #node.children.each do |e| 
    #expand_node e # this will keep expanding parents
    #expand_children e
  #end
  node.breadth_each($multiplier) do |e|
    expand_node e
  end
  $multiplier = 0
  _structure_changed true
end

#expand_node(node) ⇒ Object



609
610
611
612
613
614
615
# File 'lib/rbcurse/rtree.rb', line 609

def expand_node(node)
  #$log.debug " expand called on #{node.user_object} "
  state = true
  fire_handler :TREE_WILL_EXPAND_EVENT, node
  set_expanded_state(node, state)
  fire_handler :TREE_EXPANDED_EVENT, node
end

#expand_parents(node) ⇒ Object

goes up to root of this node, and expands down to this node this is often required to make a specific node visible such as in a dir listing when current dir is deep in heirarchy.



637
638
639
640
641
642
643
644
# File 'lib/rbcurse/rtree.rb', line 637

def expand_parents node
  _path = node.tree_path
  _path.each do |e| 
    # if already expanded parent then break we should break
    #set_expanded_state(e, true) 
    expand_node(e)
  end
end

#get_contentObject

START FOR scrollable ###



297
298
299
300
301
302
# File 'lib/rbcurse/rtree.rb', line 297

def get_content
  #@list 2008-12-01 23:13 
  @list_variable && @list_variable.value || @list 
  # called by next_match in listscrollable
  @list
end

#get_expanded_descendants(node) ⇒ Object



714
715
716
717
718
719
720
# File 'lib/rbcurse/rtree.rb', line 714

def get_expanded_descendants(node)
  nodes = []
  nodes << node
  traverse_expanded node, nodes
  $log.debug " def get_expanded_descendants(node) #{nodes.size} "
  return nodes
end

#get_node_for_path(user_path) ⇒ Object

To retrieve the node corresponding to a path specified as an array or string Do not mention the root. e.g. “ruby/1.9.2/io/console” or %w[ ruby 1.9.3 io console ]

Since:

  • 1.4.0 2011-10-2



740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
# File 'lib/rbcurse/rtree.rb', line 740

def get_node_for_path(user_path)
  case user_path
  when String
    user_path = user_path.split "/"
  when Array
  else
    raise ArgumentError, "Should be Array or String delimited with /"
  end
  $log.debug "TREE #{user_path} " if $log.debug? 
  root = @treemodel.root
  found = nil
  user_path.each { |e| 
    success = false
    root.children.each { |c| 
      if c.user_object == e
        found = c
        success = true
        root = c
        break
      end
    }
    return false unless success

  }
  return found
end

#get_windowObject



303
304
305
# File 'lib/rbcurse/rtree.rb', line 303

def get_window
  @graphic # 2010-01-04 12:37 BUFFERED
end

#getvalueObject

END FOR scrollable ### override widgets text



308
309
310
# File 'lib/rbcurse/rtree.rb', line 308

def getvalue
  selected_row()
end

#goto_parent(node = :current_index) ⇒ Object



689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'lib/rbcurse/rtree.rb', line 689

def goto_parent node=:current_index
  node = row_to_node if node == :current_index
  parent = node.parent
  return if parent.nil?
  crow = @current_index
  @list.each_with_index { |e,i| 
    if e == parent
      crow = i
      break
    end
  }
  @repaint_required = true
  #set_form_row  # will not work if off form
  set_focus_on crow
end

#handle_key(ch) ⇒ Object

Listbox



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/rbcurse/rtree.rb', line 312

def handle_key(ch)
  return if @list.nil? || @list.empty?
  @current_index ||= 0
  @toprow ||= 0
  map_keys unless @keys_mapped
  h = scrollatrow()
  rc = row_count
  $log.debug " tree got ch #{ch}"
  case ch
  when 27, ?\C-c.getbyte(0)
    #editing_canceled @current_index if @cell_editing_allowed
    #cancel_block # block
    $multiplier = 0
    return 0
  #when ?\C-u.getbyte(0)
    # multiplier. Series is 4 16 64
    # TESTING @multiplier = (@multiplier == 0 ? 4 : @multiplier *= 4)
  #  return 0
  when ?\C-c.getbyte(0)
    @multiplier = 0
    return 0
  else
    ret = :UNHANDLED 
    if ret == :UNHANDLED
      # beware one-key eats up numbers. we'll be wondering why
      if @one_key_selection
        case ch
        #when ?A.getbyte(0)..?Z.getbyte(0), ?a.getbyte(0)..?z.getbyte(0), ?0.getbyte(0)..?9.getbyte(0)
        when ?A.getbyte(0)..?Z.getbyte(0), ?a.getbyte(0)..?z.getbyte(0)
          # simple motion, key press defines motion
          ret = set_selection_for_char ch.chr
        else
          ret = process_key ch, self
          @multiplier = 0
          return :UNHANDLED if ret == :UNHANDLED
        end
      else
        # no motion on single key, we can freak out like in vim, pref f <char> for set_selection
        case ch
        when ?0.getbyte(0)..?9.getbyte(0)
          $multiplier *= 10 ; $multiplier += (ch-48)
          #$log.debug " setting mult to #{$multiplier} in list "
          return 0
        end
        $log.debug " TREE before process key #{ch} "
        ret = process_key ch, self
        $log.debug " TREE after process key #{ch} #{ret} "
        #$multiplier = 0 # 2010-09-02 22:35 this prevents parent from using mult
        return :UNHANDLED if ret == :UNHANDLED
      end
    end
  end
  $multiplier = 0
end

#has_been_expanded(node) ⇒ Object



705
706
707
# File 'lib/rbcurse/rtree.rb', line 705

def has_been_expanded node
  @expanded_state.has_key? node
end

#init_varsObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rbcurse/rtree.rb', line 97

def init_vars
  @repaint_required = true
  @toprow = @pcol = 0
  if @show_selector
    @row_selected_symbol ||= '>'
    @row_unselected_symbol ||= ' '
    @left_margin ||= @row_selected_symbol.length
  end
  @left_margin ||= 0
  @one_key_selection = true if @one_key_selection.nil?
  @height ||= 10
  @width  ||= 30
  @row_offset = @col_offset = 0 if @suppress_borders
  @internal_width = 2 # taking into account borders accounting for 2 cols
  @internal_width = 0 if @suppress_borders # should it be 0 ???

end

#list_data_changedObject



520
521
522
523
524
525
526
527
# File 'lib/rbcurse/rtree.rb', line 520

def list_data_changed
  if row_count == 0 # added on 2009-02-02 17:13 so cursor not hanging on last row which could be empty
    init_vars
    @current_index = 0
    set_form_row
  end
  @repaint_required = true
end

#map_keysObject

maps keys to methods checks @key_map can be :emacs or :vim.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rbcurse/rtree.rb', line 116

def map_keys
  @keys_mapped = true
  $log.debug " cam in XXXX  map keys"
  bind_key(32){ toggle_row_selection() }
  bind_key(KEY_RETURN) { toggle_expanded_state() }
  bind_key(?o) { toggle_expanded_state() }
  bind_key(?f){ ask_selection_for_char() }
  bind_key(?\M-v){ @one_key_selection = !@one_key_selection }
  bind_key(KEY_DOWN){ next_row() }
  bind_key(KEY_UP){ previous_row() }
  bind_key(?O){ expand_children() }
  bind_key(?X){ collapse_children() }
  bind_key(?>, :scroll_right)
  bind_key(?<, :scroll_left)
  bind_key(?\M-l, :scroll_right)
  bind_key(?\M-h, :scroll_left)
  # TODO
  bind_key(?x){ collapse_parent() }
  bind_key(?p){ goto_parent() }
  if $key_map == :emacs
    $log.debug " EMACSam in XXXX  map keys"
    bind_key(?\C-v){ scroll_forward }
    bind_key(?\M-v){ scroll_backward }
    bind_key(?\C-s){ ask_search() }
    bind_key(?\C-n){ next_row() }
    bind_key(?\C-p){ previous_row() }
    bind_key(?\M->){ goto_bottom() }
    bind_key(?\M-<){ goto_top() }
  else # :vim
    $log.debug " VIM cam in XXXX  map keys"
    bind_key(?j){ next_row() }
    bind_key(?k){ previous_row() }
    bind_key(?\C-d){ scroll_forward }
    bind_key(?\C-b){ scroll_backward }
    bind_key(?G){ goto_bottom() }
    bind_key([?g,?g]){ goto_top() }
    bind_key(?/){ ask_search() }
  end
end

#mark_parents_expanded(node) ⇒ Object

this is required to make a node visible, if you wish to start from a node that is not root e.g. you are loading app in a dir somewhere but want to show path from root down. NOTE this sucks since you have to click 2 times to expand it.



626
627
628
629
630
631
632
633
# File 'lib/rbcurse/rtree.rb', line 626

def mark_parents_expanded node
  # i am setting parents as expanded, but NOT firing handlers - XXX separate this into expand_parents
  _path = node.tree_path
  _path.each do |e| 
    # if already expanded parent then break we should break
    set_expanded_state(e, true) 
  end
end

#node_collapsed?(node) ⇒ Boolean

Returns:

  • (Boolean)


711
712
713
# File 'lib/rbcurse/rtree.rb', line 711

def node_collapsed? node
  !node_expanded?(node)
end

#node_expanded?(node) ⇒ Boolean

Returns:

  • (Boolean)


708
709
710
# File 'lib/rbcurse/rtree.rb', line 708

def node_expanded? node
  @expanded_state[node] == true
end

#node_to_row(node) ⇒ Object

convert a given node to row



580
581
582
583
584
585
586
587
588
589
# File 'lib/rbcurse/rtree.rb', line 580

def node_to_row node
  crow = nil
  @list.each_with_index { |e,i| 
    if e == node
      crow = i
      break
    end
  }
  crow
end

#on_enterBoolean

please check for error before proceeding

Returns:

  • (Boolean)

    false if no data



396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/rbcurse/rtree.rb', line 396

def on_enter
  if @list.size < 1
    Ncurses.beep
    return false
  end
  on_enter_row @current_index
  set_form_row # added 2009-01-11 23:41 
  #$log.debug " ONE ENTER LIST #{@current_index}, #{@form.row}"
  @repaint_required = true
  super
  #fire_handler :ENTER, self
  true
end

#on_enter_row(arow) ⇒ Object



409
410
411
412
413
414
415
416
# File 'lib/rbcurse/rtree.rb', line 409

def on_enter_row arow
  #$log.debug " Listbox #{self} ENTER_ROW with curr #{@current_index}. row: #{arow} H: #{@handler.keys}"
  #fire_handler :ENTER_ROW, arow
  fire_handler :ENTER_ROW, self
  #@list.on_enter_row self TODO
  #edit_row_at arow
  @repaint_required = true
end

#on_leave_row(arow) ⇒ Object



418
419
420
421
422
423
424
# File 'lib/rbcurse/rtree.rb', line 418

def on_leave_row arow
  #$log.debug " Listbox #{self} leave with (cr: #{@current_index}) #{arow}: list[row]:#{@list[arow]}"
  #$log.debug " Listbox #{self} leave with (cr: #{@current_index}) #{arow}: "
  #fire_handler :LEAVE_ROW, arow
  fire_handler :LEAVE_ROW, self
  #editing_completed arow
end


273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/rbcurse/rtree.rb', line 273

def print_borders
  width = @width
  height = @height-1 # 2010-01-04 15:30 BUFFERED HEIGHT
  window = @graphic  # 2010-01-04 12:37 BUFFERED
  startcol = @col 
  startrow = @row 
  @color_pair = get_color($datacolor)
#      bordercolor = @border_color || $datacolor # changed 2011 dts  
  bordercolor = @border_color || @color_pair # 2011-09-28 V1.3.1 
  borderatt = @border_attrib || Ncurses::A_NORMAL
                       
  window.print_border startrow, startcol, height, width, bordercolor, borderatt
  print_title
end


287
288
289
290
291
292
293
294
295
# File 'lib/rbcurse/rtree.rb', line 287

def print_title
  return unless @title
  _title = @title
  if @title.length > @width - 2
    _title = @title[0..@width-2]
  end
  @color_pair ||= get_color($datacolor)
  @graphic.printstring( @row, @col+(@width-_title.length)/2, _title, @color_pair, @title_attrib) unless @title.nil?
end

#repaintObject

this method chops the data to length before giving it to the renderer, this can cause problems if the renderer does some processing. also, it pans the data horizontally giving the renderer a section of it. FIXME: tree may not be clearing till end see appdirtree after divider movement



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/rbcurse/rtree.rb', line 444

def repaint
  return unless @repaint_required

  my_win = @form ? @form.window : @target_window
  @graphic = my_win unless @graphic
   
  raise " #{@name} neither form, nor target window given TV paint " unless my_win
  raise " #{@name} NO GRAPHIC set as yet                 TV paint " unless @graphic
  @win_left = my_win.left
  @win_top = my_win.top

  $log.debug "rtree repaint  #{@name} graphic #{@graphic}"
  print_borders unless @suppress_borders # do this once only, unless everything changes
  maxlen = @maxlen || @width-@internal_width
  maxlen -= @left_margin # 2011-10-6 
  tm = _list()
  select_default_values
  rc = row_count
  tr = @toprow
  acolor = get_color $datacolor
  h = scrollatrow()
  r,c = rowcol
  @longest_line = @width #maxlen
  0.upto(h) do |hh|
    crow = tr+hh
    if crow < rc
        _focussed = @current_index == crow ? true : false  # row focussed ?
        focus_type = _focussed 
        # added 2010-09-02 14:39 so inactive fields don't show a bright focussed line
        #focussed = false if focussed && !@focussed
        focus_type = :SOFT_FOCUS if _focussed && !@focussed
        selected = row_selected? crow 
        content = tm[crow]   # 2009-01-17 18:37 chomp giving error in some cases says frozen
        if content.is_a? TreeNode
          node = content
          object = content
          leaf = node.is_leaf?
          # content passed is rejected by treecellrenderer 2011-10-6 
          content = node.user_object.to_s # may need to trim or truncate
          expanded = row_expanded? crow  
        elsif content.is_a? String
          $log.warn "Removed this entire block since i don't think it was used XXX  "
          # this block does not set object XXX
        else
          raise "repaint what is the class #{content.class} "
          content = content.to_s
        end
        # this is redundant since data is taken by renderer directly
        #sanitize content if @sanitization_required
        #truncate value
        ## set the selector symbol if requested
        selection_symbol = ''
        if @show_selector
          if selected
            selection_symbol = @row_selected_symbol
          else
            selection_symbol =  @row_unselected_symbol
          end
          @graphic.printstring r+hh, c, selection_symbol, acolor,@attr
        end

        renderer = cell_renderer()
        renderer.display_length(@width-@internal_width-@left_margin) # just in case resizing of listbox
        renderer.pcol = @pcol
        #renderer.repaint @graphic, r+hh, c+@left_margin, crow, content, _focussed, selected
        renderer.repaint @graphic, r+hh, c+@left_margin, crow, object, content, leaf,  focus_type, selected, expanded
        @longest_line = renderer.actual_length if renderer.actual_length > @longest_line 
    else
      # clear rows
      @graphic.printstring r+hh, c, " " * (@width-@internal_width), acolor,@attr
    end
  end
  @table_changed = false
  @repaint_required = false
end

#root(node = nil, asks_allow_children = false, &block) ⇒ Object

Sets the given node as root and returns treemodel. Returns root if no argument given. Now we return root if already set Made node nillable so we can return root.

Raises:

  • ArgumentError if setting a root after its set or passing nil if its not been set.



177
178
179
180
181
182
183
184
185
# File 'lib/rbcurse/rtree.rb', line 177

def root node=nil, asks_allow_children=false, &block
  if @treemodel
    return @treemodel.root unless node
    raise ArgumentError, "Root already set"
  end

  raise ArgumentError, "root: node cannot be nil" unless node
  @treemodel = RubyCurses::DefaultTreeModel.new(node, asks_allow_children, &block)
end

#row_collapsed?(row) ⇒ Boolean

Returns:

  • (Boolean)


601
602
603
# File 'lib/rbcurse/rtree.rb', line 601

def row_collapsed? row
  !row_expanded? row
end

#row_countObject



157
158
159
160
# File 'lib/rbcurse/rtree.rb', line 157

def row_count
  return 0 if @list.nil?
  @list.length
end

#row_expanded?(row) ⇒ TreeNode?

Returns selected node or nil

Returns:

  • (TreeNode, nil)

    returns selected node or nil



597
598
599
600
# File 'lib/rbcurse/rtree.rb', line 597

def row_expanded? row
  node = @list[row]
  node_expanded? node
end

#row_selected?(row) ⇒ Boolean

private related to index in representation, not tree

Returns:

  • (Boolean)


592
593
594
# File 'lib/rbcurse/rtree.rb', line 592

def row_selected? row
  @selected_index == row
end

#row_to_node(row = @current_index) ⇒ Object



576
577
578
# File 'lib/rbcurse/rtree.rb', line 576

def row_to_node row=@current_index
  @list[row]
end

#scrollatrowObject

at what row should scrolling begin



162
163
164
165
166
167
168
# File 'lib/rbcurse/rtree.rb', line 162

def scrollatrow
  if @suppress_borders
    return @height - 1
  else
    return @height - 3
  end
end

#select_default_valuesObject

show default value as selected and fire handler for it This is called in repaint, so can raise an error if called on creation or before repaint. Just set @default_value, and let us handle the rest. Suggestions are welcome.



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/rbcurse/rtree.rb', line 262

def select_default_values
  return if @default_value.nil?
  # NOTE list not yet created
  raise "list has not yet been created" unless @list
  index = node_to_row @default_value
  raise "could not find node #{@default_value}, #{@list}  " unless index
  return unless index
  @current_index = index
  toggle_row_selection
  @default_value = nil
end

#selected_rowObject



534
535
536
# File 'lib/rbcurse/rtree.rb', line 534

def selected_row
  @list[@selected_index].node
end

#set_expanded_state(node, state) ⇒ Object



604
605
606
607
608
# File 'lib/rbcurse/rtree.rb', line 604

def set_expanded_state(node, state)
  @expanded_state[node] = state
  @repaint_required = true
  _structure_changed true
end

#set_form_col(col1 = 0) ⇒ Object



528
529
530
531
532
533
# File 'lib/rbcurse/rtree.rb', line 528

def set_form_col col1=0
  @cols_panned ||= 0 # RFED16 2010-02-17 23:40 
  win_col = 0 
  col2 = win_col + @col + @col_offset + col1 + @cols_panned + @left_margin
  setrowcol nil, col2 # 2010-02-17 23:19 RFED16
end

#toggle_expanded_state(row = @current_index) ⇒ Object



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/rbcurse/rtree.rb', line 561

def toggle_expanded_state row=@current_index
  state = row_expanded? row
  node  = row_to_node
  if node.nil?
    Ncurses.beep
    $log.debug " No such node on row #{row} "
    return
  end
  $log.debug " toggle XXX state #{state} #{node} "
  if state
    collapse_node node
  else
    expand_node node
  end
end

#toggle_row_selectionObject

An event is thrown when a row is selected or deselected. Please note that when a row is selected, another one is automatically deselected. An event is not thrown for that since your may not want to collapse that. Only clicking on a selected row, will send a DESELECT on it since you may want to collapse it. However, the previous selection is also present in the event object, so you can act upon it. This is not used for expanding or collapsing, only for application to show some data in another window or pane based on selection. Maybe there should not be a deselect for current row ?



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/rbcurse/rtree.rb', line 545

def toggle_row_selection
  node = @list[@current_index]
  previous_node = nil
  previous_node = @list[@selected_index] if @selected_index
  if @selected_index == @current_index
    @selected_index = nil
  else
    @selected_index = @current_index
  end
  state = @selected_index.nil? ? :DESELECTED : :SELECTED
  #TreeSelectionEvent = Struct.new(:node, :tree, :state, :previous_node, :row_first)
  @tree_selection_event = TreeSelectionEvent.new(node, self, state, previous_node, @current_index) #if @item_event.nil?
  fire_handler :TREE_SELECTION_EVENT, @tree_selection_event # should the event itself be ITEM_EVENT
  $log.debug " XXX tree selected #{@selected_index}/ #{@current_index} , #{state} "
  @repaint_required = true
end

#traverse(node, level = 0) {|node, level| ... } ⇒ Object

$log.debug “XXX convert #RubyCurses::Tree.treetree.roottree.root.childrentree.root.children.size ” $log.debug “ converted tree to list. #RubyCurses::Tree.@[email protected]

Yields:

  • (node, level)


240
241
242
243
244
245
246
247
248
# File 'lib/rbcurse/rtree.rb', line 240

def traverse node, level=0, &block
  raise "disuse"
  #icon = node.is_leaf? ? "-" : "+"
  #puts "%*s %s" % [ level+1, icon,  node.user_object ]
  yield node, level if block_given?
  node.children.each do |e| 
    traverse e, level+1, &block
  end
end

#traverse_expanded(node, nodes) ⇒ Object



721
722
723
724
725
726
727
728
729
730
731
732
# File 'lib/rbcurse/rtree.rb', line 721

def traverse_expanded node, nodes
  return if !node_expanded? node
  #nodes << node
  node.children.each do |e| 
    nodes << e
    if node_expanded? e
      traverse_expanded e, nodes
    else
      next
    end
  end
end