Class: RubyCurses::Tree

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

Overview

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

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, #key_label, #parent_component, #row_offset, #rows_panned, #state

Instance Method Summary collapse

Methods included from ListScrollable

#_convert_index_to_printable_row, #_convert_index_to_visible_row, #_find_next, #_find_prev, #ask_search, #backward_word, #bounds_check, #find_more, #find_next, #find_prev, #focussed_index, #forward_char, #forward_word, #goto_bottom, #goto_last_position, #goto_top, #highlight_focussed_row, #install_keys, #is_visible?, #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

#action_manager, #changed, #click, #color_pair, #destroy, #enter, #event_list, #focus, #get_preferred_size, #getvalue_for_paint, #height, #height=, #hide, #leave, #modified?, #move, #on_leave, #override_graphic, #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

#__create_footer_window, #clear_this, #get_file, #print_this, #rb_getchar, #rb_gets, #rbgetstr, #warn

Methods included from Utils

#OLDdefine_key, #_process_key, #bind_key, #bind_keys, #clean_string!, #define_key, #define_prefix_command, #display_app_help, #get_attrib, #get_color, #keycode_tos, #last_line, #one_line_window, #parse_formatted_text, #print_key_bindings, #repeatm, #run_command, #shell_out, #shell_output, #suspend, #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.



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
97
98
99
100
# File 'lib/rbcurse/core/widgets/rtree.rb', line 62

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
  @one_key_selection = false # use vim keys 
  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  
  # FIXME the above is dangerous if user set his own renderer with some values XXX
  init_vars
  bordertitle_init

  #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.



44
45
46
# File 'lib/rbcurse/core/widgets/rtree.rb', line 44

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



57
58
59
# File 'lib/rbcurse/core/widgets/rtree.rb', line 57

def one_key_selection
  @one_key_selection
end

#selected_indexObject (readonly)

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



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

def selected_index
  @selected_index
end

#toprowObject (readonly)

dsl_accessor :title dsl_property :title_attrib # bold, reverse, normal dsl_accessor :border_attrib, :border_color # FIXME not used currently



40
41
42
# File 'lib/rbcurse/core/widgets/rtree.rb', line 40

def toprow
  @toprow
end

#treemodelObject (readonly)

returns treemodel for further actions 2011-10-2



60
61
62
# File 'lib/rbcurse/core/widgets/rtree.rb', line 60

def treemodel
  @treemodel
end

Instance Method Details

#_listObject

private, for use by repaint



205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/rbcurse/core/widgets/rtree.rb', line 205

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



365
366
367
368
369
370
371
372
373
374
# File 'lib/rbcurse/core/widgets/rtree.rb', line 365

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



355
356
357
358
359
360
361
362
363
# File 'lib/rbcurse/core/widgets/rtree.rb', line 355

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



348
349
350
351
352
353
354
# File 'lib/rbcurse/core/widgets/rtree.rb', line 348

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



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

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



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

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



599
600
601
602
603
604
605
# File 'lib/rbcurse/core/widgets/rtree.rb', line 599

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



665
666
667
668
669
670
671
# File 'lib/rbcurse/core/widgets/rtree.rb', line 665

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

#command(*args, &block) ⇒ Object

default block

Since:

  • 1.5.0 2011-11-22



751
752
753
# File 'lib/rbcurse/core/widgets/rtree.rb', line 751

def command *args, &block
  bind :TREE_WILL_EXPAND_EVENT, *args, &block
end

#convert_to_list(tree) ⇒ Object



217
218
219
220
221
# File 'lib/rbcurse/core/widgets/rtree.rb', line 217

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



416
417
418
# File 'lib/rbcurse/core/widgets/rtree.rb', line 416

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.



235
236
237
# File 'lib/rbcurse/core/widgets/rtree.rb', line 235

def current_row
  @list[@current_index]
end

#data(alist = nil) ⇒ Object

pass data to create this tree model used to be list



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rbcurse/core/widgets/rtree.rb', line 171

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



632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/rbcurse/core/widgets/rtree.rb', line 632

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



592
593
594
595
596
597
598
# File 'lib/rbcurse/core/widgets/rtree.rb', line 592

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.



620
621
622
623
624
625
626
627
# File 'lib/rbcurse/core/widgets/rtree.rb', line 620

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 ###



279
280
281
282
283
284
# File 'lib/rbcurse/core/widgets/rtree.rb', line 279

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



697
698
699
700
701
702
703
# File 'lib/rbcurse/core/widgets/rtree.rb', line 697

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



723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
# File 'lib/rbcurse/core/widgets/rtree.rb', line 723

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



285
286
287
# File 'lib/rbcurse/core/widgets/rtree.rb', line 285

def get_window
  @graphic 
end

#getvalueObject

END FOR scrollable ### override widgets text



290
291
292
# File 'lib/rbcurse/core/widgets/rtree.rb', line 290

def getvalue
  selected_row()
end

#goto_parent(node = :current_index) ⇒ Object



672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# File 'lib/rbcurse/core/widgets/rtree.rb', line 672

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



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
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
# File 'lib/rbcurse/core/widgets/rtree.rb', line 294

def handle_key(ch)
  return if @list.nil? || @list.empty?
  @current_index ||= 0
  @toprow ||= 0
  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



688
689
690
# File 'lib/rbcurse/core/widgets/rtree.rb', line 688

def has_been_expanded node
  @expanded_state.has_key? node
end

#init_varsObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rbcurse/core/widgets/rtree.rb', line 101

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?
  @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 ???
  map_keys unless @keys_mapped

end

#list_data_changedObject



503
504
505
506
507
508
509
510
# File 'lib/rbcurse/core/widgets/rtree.rb', line 503

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.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rbcurse/core/widgets/rtree.rb', line 119

def map_keys
  @keys_mapped = true
  bind_key(32, 'toggle row selection'){ toggle_row_selection() }
  bind_key(KEY_ENTER, 'toggle expanded state') { toggle_expanded_state() }
  bind_key(?o, 'toggle expanded state') { toggle_expanded_state() }
  bind_key(?f, 'first row starting with char'){ ask_selection_for_char() }
  bind_key(?\M-v, 'one key selection toggle'){ @one_key_selection = !@one_key_selection }
  bind_key(?O, 'expand children'){ expand_children() }
  bind_key(?X, 'collapse children'){ collapse_children() }
  bind_key(?>, :scroll_right)
  bind_key(?<, :scroll_left)
  # TODO
  bind_key(?x, 'collapse parent'){ collapse_parent() }
  bind_key(?p, 'goto parent'){ goto_parent() }
  require 'rbcurse/core/include/listbindings'
  #ListBindings.bindings
  bindings
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.



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

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)


694
695
696
# File 'lib/rbcurse/core/widgets/rtree.rb', line 694

def node_collapsed? node
  !node_expanded?(node)
end

#node_expanded?(node) ⇒ Boolean

Returns:

  • (Boolean)


691
692
693
# File 'lib/rbcurse/core/widgets/rtree.rb', line 691

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

#node_to_row(node) ⇒ Object

convert a given node to row



563
564
565
566
567
568
569
570
571
572
# File 'lib/rbcurse/core/widgets/rtree.rb', line 563

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

#OLDprint_bordersObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/rbcurse/core/widgets/rtree.rb', line 255

def OLDprint_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

#OLDprint_titleObject



269
270
271
272
273
274
275
276
277
# File 'lib/rbcurse/core/widgets/rtree.rb', line 269

def OLDprint_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

#on_enterBoolean

please check for error before proceeding

Returns:

  • (Boolean)

    false if no data



377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/rbcurse/core/widgets/rtree.rb', line 377

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



390
391
392
393
394
395
396
397
# File 'lib/rbcurse/core/widgets/rtree.rb', line 390

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



399
400
401
402
403
404
405
# File 'lib/rbcurse/core/widgets/rtree.rb', line 399

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

#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



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
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
# File 'lib/rbcurse/core/widgets/rtree.rb', line 425

def repaint
  return unless @repaint_required
  @height ||= 10
  @width  ||= 30

  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.



159
160
161
162
163
164
165
166
167
# File 'lib/rbcurse/core/widgets/rtree.rb', line 159

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)


584
585
586
# File 'lib/rbcurse/core/widgets/rtree.rb', line 584

def row_collapsed? row
  !row_expanded? row
end

#row_countObject



139
140
141
142
# File 'lib/rbcurse/core/widgets/rtree.rb', line 139

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



580
581
582
583
# File 'lib/rbcurse/core/widgets/rtree.rb', line 580

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)


575
576
577
# File 'lib/rbcurse/core/widgets/rtree.rb', line 575

def row_selected? row
  @selected_index == row
end

#row_to_node(row = @current_index) ⇒ Object



559
560
561
# File 'lib/rbcurse/core/widgets/rtree.rb', line 559

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

#scrollatrowObject

at what row should scrolling begin



144
145
146
147
148
149
150
# File 'lib/rbcurse/core/widgets/rtree.rb', line 144

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.



244
245
246
247
248
249
250
251
252
253
254
# File 'lib/rbcurse/core/widgets/rtree.rb', line 244

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



517
518
519
# File 'lib/rbcurse/core/widgets/rtree.rb', line 517

def selected_row
  @list[@selected_index].node
end

#set_expanded_state(node, state) ⇒ Object



587
588
589
590
591
# File 'lib/rbcurse/core/widgets/rtree.rb', line 587

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

#set_form_col(col1 = 0) ⇒ Object



511
512
513
514
515
516
# File 'lib/rbcurse/core/widgets/rtree.rb', line 511

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



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

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 ?



528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/rbcurse/core/widgets/rtree.rb', line 528

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)


222
223
224
225
226
227
228
229
230
# File 'lib/rbcurse/core/widgets/rtree.rb', line 222

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



704
705
706
707
708
709
710
711
712
713
714
715
# File 'lib/rbcurse/core/widgets/rtree.rb', line 704

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