Class: Canis::TreeNode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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_childrenObject (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

#childrenObject (readonly)

Returns the value of attribute children.



250
251
252
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 250

def children
  @children
end

#parentObject

extend Forwardable



249
250
251
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 249

def parent
  @parent
end

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

Returns:



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.

Parameters:

  • node/s (TreeNode, Array, Hash)

    to add

  • should (boolean)

    children be allowed

Returns:

  • (TreeNode)

    node last added (NOT self)

Raises:



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_varsObject



392
393
394
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 392

def init_vars
   @repaint_required = true
end

#insert(node, index) ⇒ Object

Raises:

  • (ArgumentError)


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

Returns:

  • (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_countObject



327
328
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 327

def leaf_count
end

#lengthObject

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

#levelObject



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

#removeObject



318
319
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 318

def remove
end

#remove_all_childrenObject



320
321
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 320

def remove_all_children
end

#remove_from_parentObject



322
323
# File 'lib/canis/core/widgets/tree/treemodel.rb', line 322

def remove_from_parent
end

#to_sObject



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_pathArray

returns an array of nodes for the current node starting from root, ending in the current one. The last node represents this node.

Returns:

  • (Array)

    TreeNode[]



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_pathArray

returns an array of user_objects for the current node starting from root, ending in the current one. The last node represents this node.

Returns:

  • (Array)

    Strings[]



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