Class: Canis::DefaultTreeModel

Inherits:
Object
  • Object
show all
Includes:
EventHandler
Defined in:
lib/canis/core/widgets/tree/treemodel.rb

Overview

< TreeModel

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventHandler

#bind, #event?, #event_list, #fire_handler, #fire_property_change, #register_events

Constructor Details

#initialize(node = nil, asks_allow_children = false, &block) ⇒ DefaultTreeModel

Returns a new instance of DefaultTreeModel.



14
15
16
17
18
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 14

def initialize node=nil, asks_allow_children=false, &block
  @root_visible        = true
  root(node, asks_allow_children) if node
  instance_eval &block if block_given?
end

Instance Attribute Details

#asks_allow_childrenObject (readonly)

Returns the value of attribute asks_allow_children.



12
13
14
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 12

def asks_allow_children
  @asks_allow_children
end

#root_visibleObject

Returns the value of attribute root_visible.



13
14
15
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 13

def root_visible
  @root_visible
end

Instance Method Details

#add(nodechild, allows_children = true, &block) ⇒ TreeNode Also known as: <<

add a node to root passing a block optionally

Parameters:

  • node/s (String, TreeNode, Array, Hash)

    to add

  • allow (Boolean)

    children to be added

Returns:

  • (TreeNode)

    node just added to root (NOT self)

See Also:



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 50

def add nodechild, allows_children=true, &block
  # calling TreeNode.add
  $log.debug " XXX def add of DTM #{nodechild} to root "
  node = @root.add nodechild, allows_children, &block
  if @handler # only if someone is listening, won't fire when being prepared
    tme = TreeModelEvent.new(row, row,:ALL_COLUMNS,  self, :INSERT)
    fire_handler :TREE_MODEL_EVENT, tme
  end
  #return @root
  return node
end

#branch(node, &block) ⇒ Object



64
65
66
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 64

def branch node, &block
  add node, true, &block
end

#child_at(parent, index) ⇒ Object



74
75
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 74

def child_at parent, index
end

#child_count(node) ⇒ Object



78
79
80
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 78

def child_count node
  node.children.size
end

#data=(data) ⇒ Object

for those quick cases when you wish to replace all the data and not have an event per row being generated



161
162
163
164
165
166
167
168
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 161

def data=(data)
  raise "not yet used"
  raise "Data nil or invalid" if data.nil? or data.size == 0
  delete_all
  @data = data
  tme = TreeModelEvent.new(0, @data.length-1,:ALL_COLUMNS,  self, :INSERT)
  fire_handler :TREE_MODEL_EVENT, tme
end

#delete(obj) ⇒ Object

create tablemodelevent and fire_table_changed for all listeners



110
111
112
113
114
115
116
117
118
119
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 110

def delete obj
  raise "not yet used"
  row = @data.index obj
  return if row.nil?
  ret = @data.delete obj
  tme = TreeModelEvent.new(row, row,:ALL_COLUMNS,  self, :DELETE)
  fire_handler :TREE_MODEL_EVENT, tme
  # create tablemodelevent and fire_table_changed for all listeners
  return ret
end

#delete_allObject

added 2009-01-17 21:36 Use with caution, does not call events per row



151
152
153
154
155
156
157
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 151

def delete_all
  raise "not yet used"
  len = @data.length-1
  @data=[]
  tme = TreeModelEvent.new(0, len,:ALL_COLUMNS,  self, :DELETE)
  fire_handler :TREE_MODEL_EVENT, tme
end

#delete_at(row) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 120

def delete_at row
  raise "not yet used"
  if !$multiplier or $multiplier == 0 
    @delete_buffer = @data.delete_at row
  else
    @delete_buffer = @data.slice!(row, $multiplier)
  end
  $multiplier = 0
  #ret = @data.delete_at row
  # create tablemodelevent and fire_table_changed for all listeners 
  # we don;t pass buffer to event as in listeditable. how to undo later?
  tme = TreeModelEvent.new(row, row+@delete_buffer.length,:ALL_COLUMNS,  self, :DELETE)
  fire_handler :TREE_MODEL_EVENT, tme
  return @delete_buffer
end

#get_value_at(row, col) ⇒ Object

please avoid directly hitting this. Suggested to use get_value_at of jtable since columns could have been switched.



96
97
98
99
100
101
102
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 96

def get_value_at row, col
  raise "not yet used"
  #$log.debug " def get_value_at #{row}, #{col} "

  raise "IndexError get_value_at #{row}, #{col}" if @data.nil? or row >= @data.size
  return @data[row][ col]
end

#index_of_child(parent, child) ⇒ Object



76
77
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 76

def index_of_child parent, child
end

#insert(row, obj) ⇒ Object



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
101
102
103
104
105
106
107
108
109
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 68

def insert row, obj
  @data.insert row, obj
  if @handler # only if someone is listening, won't fire when being prepared
    tme = TreeModelEvent.new(row, row,:ALL_COLUMNS,  self, :INSERT)
    fire_handler :TREE_MODEL_EVENT, tme
  end
def child_at parent, index
end
def index_of_child parent, child
end
def child_count node
  node.children.size
end

def row_count
  @data.length
end
# 
def set_value_at row, col, val
  # if editing allowed
  raise "not yet used"
  @data[row][col] = val
  tme = TreeModelEvent.new(row, row, col, self, :UPDATE)
  fire_handler :TREE_MODEL_EVENT, tme
end
##
# please avoid directly hitting this. Suggested to use get_value_at of jtable
# since columns could have been switched.
def get_value_at row, col
  raise "not yet used"
  #$log.debug " def get_value_at #{row}, #{col} "

  raise "IndexError get_value_at #{row}, #{col}" if @data.nil? or row >= @data.size
  return @data[row][ col]
end
#def << obj
  #@data << obj
  #tme = TreeModelEvent.new(@data.length-1,@data.length-1, :ALL_COLUMNS, self, :INSERT)
  #fire_handler :TREE_MODEL_EVENT, tme
#end
  # create tablemodelevent and fire_table_changed for all listeners 
end

#insert_node_into(nodechild, nodeparent, index) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 36

def insert_node_into nodechild, nodeparent, index
  $log.debug " TODO remove from existing parent to avoid bugs XXX"
  nodeparent.insert nodechild, index
  if @handler # only if someone is listening, won't fire when being prepared
    tme = TreeModelEvent.new(row, row,:ALL_COLUMNS,  self, :INSERT)
    fire_handler :TREE_MODEL_EVENT, tme
  end
  self
end

#leaf(node, &block) ⇒ Object



61
62
63
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 61

def leaf node, &block
  add node, false, &block
end

#root(*args, &block) ⇒ Object

insert a node the old sucky java pain in the butt way private sets node as root def root node, asks_allow_children=false, &block



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 23

def root *args, &block
  return @root if args.empty?
  node = args[0]
  @asks_allow_children = args[1]
  if !node.is_a? TreeNode
    n = TreeNode.new node
    node = n
  end
  @root = node
  $log.debug " XXX def root created root with #{node} "
  #add node, true, &block 
  instance_eval &block if block_given?
end

#row_countObject



82
83
84
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 82

def row_count
  @data.length
end

#set_value_at(row, col, val) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 86

def set_value_at row, col, val
  # if editing allowed
  raise "not yet used"
  @data[row][col] = val
  tme = TreeModelEvent.new(row, row, col, self, :UPDATE)
  fire_handler :TREE_MODEL_EVENT, tme
end

#traverse(node = @root, level = 0) ⇒ Object

def ask_search_forward regex = get_string “Enter regex to search for:” ix = get_list_data_model.find_match regex if ix.nil? alert(“No matching data for: #regex”) else set_focus_on(ix) end end continues previous search

def find_match regex, ix0=0, ix1=row_count() $log.debug “ find_match got #regex #ix0 #ix1” next if ix < ix0 break if ix > ix1 if row.grep(/#regex/) != [] if !row.match(regex).nil? return ix end end return nil end def find_prev regex=@last_regex, start = @search_found_ix raise “No previous search” if @last_regex.nil? $log.debug “ find_prev #@search_found_ix : #@current_index” start -= 1 unless start == 0 start.downto(0) do |ix| row = @data if row.grep(/#regex/) != [] return ix end end return nil return find_match @last_regex, start, @search_end_ix end dtm findnext def find_next raise “No more search” if @last_regex.nil? start = @search_found_ix && @search_found_ix+1 || 0 return find_match @last_regex, start, @search_end_ix end just a test program



219
220
221
222
223
224
225
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 219

def traverse node=@root, level=0
  icon = node.is_leaf? ? "-" : "+"
  puts "%*s %s" % [ level+1, icon,  node.user_object ]
  node.children.each do |e| 
    traverse e, level+1
  end
end

#undo(where) ⇒ Object

a quick method to undo deletes onto given row. More like paste



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 136

def undo where
  raise "not yet used"
  return unless @delete_buffer
  case @delete_buffer[0]
  when Array
    @delete_buffer.each do |r| 
      insert where, r 
    end
  else
    insert where, @delete_buffer
  end
end