Class: Canis::TreeNode
- Defined in:
- lib/canis/core/widgets/tree/treemodel.rb
Instance Attribute Summary collapse
-
#allows_children ⇒ Object
readonly
Returns the value of attribute allows_children.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#parent ⇒ Object
extend Forwardable.
-
#user_object ⇒ Object
readonly
Returns the value of attribute user_object.
Instance Method Summary collapse
-
#_add(node, allows_children = true, &block) ⇒ TreeNode
private.
-
#add(node, allows_children = true, &block) ⇒ TreeNode
(also: #<<)
add a node to this node, optionally passing a block for further adding add a node as child to existing node If node is not a TreeNode it will be converted to one.
- #branch(node, &block) ⇒ Object
- #breadth_each(max_depth = 999, &block) ⇒ Object
- #child_after(node) ⇒ Object
- #child_at(node) ⇒ Object
- #child_before(node) ⇒ Object
- #init_vars ⇒ Object
-
#initialize(user_object = nil, allows_children = true, &block) ⇒ TreeNode
constructor
form, config={}, &block.
- #insert(node, index) ⇒ Object
- #is_leaf? ⇒ Boolean
- #leaf(node, &block) ⇒ Object
- #leaf_count ⇒ Object
-
#length ⇒ Object
need by textpad to calculate pad.
- #level ⇒ Object
- #next_node(node) ⇒ Object
- #remove ⇒ Object
- #remove_all_children ⇒ Object
- #remove_from_parent ⇒ Object
- #to_s ⇒ Object
- #traverse_up(&block) ⇒ Object
-
#tree_path ⇒ Array
returns an array of nodes for the current node starting from root, ending in the current one.
-
#user_object_path ⇒ Array
returns an array of user_objects for the current node starting from root, ending in the current one.
Constructor Details
#initialize(user_object = nil, allows_children = true, &block) ⇒ TreeNode
form, config={}, &block
253 254 255 256 257 258 259 260 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 253 def initialize user_object=nil, allows_children=true, &block #form, config={}, &block @allows_children = allows_children @user_object = user_object @children = [] #super instance_eval &block if block_given? init_vars end |
Instance Attribute Details
#allows_children ⇒ Object (readonly)
Returns the value of attribute allows_children.
252 253 254 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 252 def allows_children @allows_children end |
#children ⇒ Object (readonly)
Returns the value of attribute children.
250 251 252 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 250 def children @children end |
#parent ⇒ Object
extend Forwardable
249 250 251 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 249 def parent @parent end |
#user_object ⇒ Object (readonly)
Returns the value of attribute user_object.
251 252 253 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 251 def user_object @user_object end |
Instance Method Details
#_add(node, allows_children = true, &block) ⇒ TreeNode
private
263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 263 def _add node, allows_children=true, &block #raise ArgumentError, "Argument should be a node" if !node.is_a? TreeNode $log.debug " TODO remove from existing parent to avoid bugs XXX" if !node.is_a? TreeNode n = TreeNode.new node, allows_children, &block node = n end node.parent = self @children << node node end |
#add(node, allows_children = true, &block) ⇒ TreeNode Also known as: <<
add a node to this node, optionally passing a block for further adding add a node as child to existing node If node is not a TreeNode it will be converted to one.
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 280 def add node, allows_children=true, &block raise IllegalStateException, "Cannot add a child to this node" unless @allows_children $log.debug " XXX def add of TreeNode #{node} parent #{self} " case node when Array node.each do |e| add e, allows_children, &block end when Hash node.each_pair { |name, val| n = _add name, allows_children, &block n.add val, allows_children, &block } else return _add node, allows_children, &block end self end |
#branch(node, &block) ⇒ Object
301 302 303 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 301 def branch node, &block add node, true, &block end |
#breadth_each(max_depth = 999, &block) ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 372 def breadth_each(max_depth=999,&block) node_queue = [self] # Create a queue with self as the initial entry # Use a queue to do breadth traversal until node_queue.empty? node_to_traverse = node_queue.shift yield node_to_traverse # Enqueue the children from left to right. node_to_traverse.children { |child| node_queue.push child } max_depth -= 1 break if max_depth == 0 end end |
#child_after(node) ⇒ Object
310 311 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 310 def child_after node end |
#child_at(node) ⇒ Object
314 315 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 314 def child_at node end |
#child_before(node) ⇒ Object
312 313 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 312 def child_before node end |
#init_vars ⇒ Object
392 393 394 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 392 def init_vars @repaint_required = true end |
#insert(node, index) ⇒ Object
305 306 307 308 309 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 305 def insert node, index raise ArgumentError, "Argument should be a node. it is #{node.class} " if !node.is_a? TreeNode @children.insert index, node self end |
#is_leaf? ⇒ Boolean
324 325 326 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 324 def is_leaf? @children.size == 0 end |
#leaf(node, &block) ⇒ Object
298 299 300 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 298 def leaf node, &block add node, false, &block end |
#leaf_count ⇒ Object
327 328 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 327 def leaf_count end |
#length ⇒ Object
need by textpad to calculate pad
389 390 391 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 389 def length to_s.length end |
#level ⇒ Object
329 330 331 332 333 334 335 336 337 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 329 def level level = 0 nodeparent = parent() while( nodeparent != nil ) level += 1 nodeparent = nodeparent.parent() end return level end |
#next_node(node) ⇒ Object
316 317 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 316 def next_node node end |
#remove ⇒ Object
318 319 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 318 def remove end |
#remove_all_children ⇒ Object
320 321 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 320 def remove_all_children end |
#remove_from_parent ⇒ Object
322 323 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 322 def remove_from_parent end |
#to_s ⇒ Object
385 386 387 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 385 def to_s @user_object.to_s end |
#traverse_up(&block) ⇒ Object
340 341 342 343 344 345 346 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 340 def traverse_up &block nodeparent = parent() while ( nodeparent != nil ) yield nodeparent nodeparent = nodeparent.parent() end end |
#tree_path ⇒ Array
returns an array of nodes for the current node starting from root, ending in the current one. The last node represents this node.
363 364 365 366 367 368 369 370 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 363 def tree_path arr = [] arr << self traverse_up do |e| arr << e end arr.reverse! end |
#user_object_path ⇒ Array
returns an array of user_objects for the current node starting from root, ending in the current one. The last node represents this node.
351 352 353 354 355 356 357 358 |
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 351 def user_object_path arr = [] arr << self.user_object.to_s traverse_up do |e| arr << e.user_object.to_s end arr.reverse! end |