class Canis::TreeNode
Attributes
extend Forwardable
Public Class Methods
# 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
Public Instance Methods
private
@return [TreeNode] just creates node
# 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 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. @param [TreeNode, Array, Hash] node/s to add @param [boolean] should children be allowed @return [TreeNode] node last added (NOT self)
# 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
# File lib/canis/core/widgets/tree/treemodel.rb, line 301 def branch node, &block add node, true, &block end
github.com/evolve75/RubyTree/blob/master/lib/tree.rb
# 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
# File lib/canis/core/widgets/tree/treemodel.rb, line 310 def child_after node end
# File lib/canis/core/widgets/tree/treemodel.rb, line 314 def child_at node end
# File lib/canis/core/widgets/tree/treemodel.rb, line 312 def child_before node end
# File lib/canis/core/widgets/tree/treemodel.rb, line 392 def init_vars @repaint_required = true end
# 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
# File lib/canis/core/widgets/tree/treemodel.rb, line 324 def is_leaf? @children.size == 0 end
# File lib/canis/core/widgets/tree/treemodel.rb, line 298 def leaf node, &block add node, false, &block end
# File lib/canis/core/widgets/tree/treemodel.rb, line 327 def leaf_count end
need by textpad to calculate pad
# File lib/canis/core/widgets/tree/treemodel.rb, line 389 def length to_s.length end
# 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
# File lib/canis/core/widgets/tree/treemodel.rb, line 316 def next_node node end
# File lib/canis/core/widgets/tree/treemodel.rb, line 318 def remove end
# File lib/canis/core/widgets/tree/treemodel.rb, line 320 def remove_all_children end
# File lib/canis/core/widgets/tree/treemodel.rb, line 322 def remove_from_parent end
# File lib/canis/core/widgets/tree/treemodel.rb, line 385 def to_s @user_object.to_s end
# 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
returns an array of nodes for the current node starting from root, ending in the current one. The last node represents this node. @return [Array] TreeNode[]
# 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
returns an array of user_objects for the current node starting from root, ending in the current one. The last node represents this node. @return [Array] Strings[]
# 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