class Canis::TreeNode

Attributes

allows_children[R]
children[R]
parent[RW]

extend Forwardable

user_object[R]

Public Class Methods

new(user_object=nil, allows_children=true, &block) click to toggle source
# 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

<<(node, allows_children=true, &block)
Alias for: add
_add(node, allows_children=true, &block) click to toggle source
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(node, allows_children=true, &block) click to toggle source

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
Also aliased as: <<
branch(node, &block) click to toggle source
# File lib/canis/core/widgets/tree/treemodel.rb, line 301
def branch node, &block
  add node, true, &block
end
breadth_each(max_depth=999) { |node_to_traverse| ... } click to toggle source

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
child_after(node) click to toggle source
# File lib/canis/core/widgets/tree/treemodel.rb, line 310
def child_after node
end
child_at(node) click to toggle source
# File lib/canis/core/widgets/tree/treemodel.rb, line 314
def child_at node
end
child_before(node) click to toggle source
# File lib/canis/core/widgets/tree/treemodel.rb, line 312
def child_before node
end
init_vars() click to toggle source
# File lib/canis/core/widgets/tree/treemodel.rb, line 392
def init_vars
   @repaint_required = true
end
insert(node, index) click to toggle source
# 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?() click to toggle source
# File lib/canis/core/widgets/tree/treemodel.rb, line 324
def is_leaf?
  @children.size == 0
end
leaf(node, &block) click to toggle source
# File lib/canis/core/widgets/tree/treemodel.rb, line 298
def leaf node, &block
  add node, false, &block
end
leaf_count() click to toggle source
# File lib/canis/core/widgets/tree/treemodel.rb, line 327
def leaf_count
end
length() click to toggle source

need by textpad to calculate pad

# File lib/canis/core/widgets/tree/treemodel.rb, line 389
def length
  to_s.length
end
level() click to toggle source
# 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) click to toggle source
# File lib/canis/core/widgets/tree/treemodel.rb, line 316
def next_node node
end
remove() click to toggle source
# File lib/canis/core/widgets/tree/treemodel.rb, line 318
def remove
end
remove_all_children() click to toggle source
# File lib/canis/core/widgets/tree/treemodel.rb, line 320
def remove_all_children
end
remove_from_parent() click to toggle source
# File lib/canis/core/widgets/tree/treemodel.rb, line 322
def remove_from_parent
end
to_s() click to toggle source
# File lib/canis/core/widgets/tree/treemodel.rb, line 385
def to_s
  @user_object.to_s
end
traverse_up() { |nodeparent| ... } click to toggle source
# 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() click to toggle source

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
user_object_path() click to toggle source

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