org.apache.cocoon.forms.formmodel.tree
Class TreeWalker

java.lang.Object
  extended by org.apache.cocoon.forms.formmodel.tree.TreeWalker
All Implemented Interfaces:
Iterator

public class TreeWalker
extends Object
implements Iterator

A helper to crawl a tree and quickly access important node-related information.

It's an Iterator on the "current level" of the tree. This level starts at the root node (and therefore obviously contains only one element), and can then be changed to children of the current node using enterChildren() or popped back to the parent level using leave().

The next() method will return the next node in the iteration, and set the current node used by many convenience methods giving information about that node.

This class was primarily written for page templates containing Trees (see org/apache/cocoon/forms/generation/jx-macros.xml) but can of course be used in other places as well.

Version:
$Id: TreeWalker.html 1304258 2012-03-23 10:09:27Z ilgrosso $

Constructor Summary
TreeWalker(Tree tree)
           
 
Method Summary
 TreeWalker enterChildren()
          Starts iterating the children of the current node.
 int getDepth()
          Get the current depth of this walker (can be used e.g. to compute indentation margins or CSS styles).
 String getIconType()
          Get the "icon type" that should be used for this node, according to the common visual paradigms used to render trees: "leaf" for leaf nodes (will be e.g. a file icon), "expanded" for non-leaf expanded nodes (will be e.g. a "minus" icon) "collapsed" for non-leaf collapsed nodes (will be e.g. a "plus" icon)
 Object getNode()
          Get the current node, which is the result of the last call to next() (except if enterChildren() or leave() where called inbetween.
 TreePath getPath()
          Get the path of the current node.
 String getSelectionType()
          Get the "selection type" that should be used for this node, that can be used e.g. as a CSS class name: "selected" for selected nodes, "unselected" for unselected nodes.
 boolean hasNext()
          Are there more nodes to iterate on at this level?
 boolean isCollapsed()
          Is the current node collapsed?
 boolean isExpanded()
          Is the current node expanded?
 boolean isLeaf()
          Is the current node a leaf?
 boolean isSelected()
          Is the current node selected?
 boolean isVisible()
          Is the current node visible (i.e. its parent is expanded)?
 void leave()
          Go back to the parent node, restoring the iterator at this node.
 Object next()
          Get the next node in the current iteration level.
 void remove()
          Required by the Iterator interface, but not supported here.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TreeWalker

public TreeWalker(Tree tree)
Method Detail

enterChildren

public TreeWalker enterChildren()
Starts iterating the children of the current node. The current iterator is pushed on a stack and will be restored on leave().

Right after calling this method, there is no current node. Calling next() will move to the first child, if any.

Returns:
the current tree walker (i.e. this).

leave

public void leave()
Go back to the parent node, restoring the iterator at this node.


hasNext

public boolean hasNext()
Are there more nodes to iterate on at this level?

Specified by:
hasNext in interface Iterator

next

public Object next()
Get the next node in the current iteration level.

Specified by:
next in interface Iterator

remove

public void remove()
Required by the Iterator interface, but not supported here.

Specified by:
remove in interface Iterator
Throws:
UnsupportedOperationException - whenever called.

getDepth

public int getDepth()
Get the current depth of this walker (can be used e.g. to compute indentation margins or CSS styles). If root node is visible (see Tree.isRootVisible()), depth 0 is for the root. Otherwise, children of the root node are at depth 0.

Returns:
the current depth

getNode

public Object getNode()
Get the current node, which is the result of the last call to next() (except if enterChildren() or leave() where called inbetween.

Returns:
the current node.

getPath

public TreePath getPath()
Get the path of the current node.

Returns:
the path

isLeaf

public boolean isLeaf()
Is the current node a leaf?


isExpanded

public boolean isExpanded()
Is the current node expanded?


isCollapsed

public boolean isCollapsed()
Is the current node collapsed?


isVisible

public boolean isVisible()
Is the current node visible (i.e. its parent is expanded)?


isSelected

public boolean isSelected()
Is the current node selected?


getIconType

public String getIconType()
Get the "icon type" that should be used for this node, according to the common visual paradigms used to render trees:

Returns:
the icon type

getSelectionType

public String getSelectionType()
Get the "selection type" that should be used for this node, that can be used e.g. as a CSS class name:

Returns:
the selection type


Copyright © 1999-2010 The Apache Software Foundation. All Rights Reserved.