Jakarta > Tapestry
Jakarta
 

Tree

Renders hierarchical data as a tree.

The Tree component uses the lower level components TreeView (handles the tree model), TreeDataView (handles the tree data), TreeNodeView (handles the presentation of the data).

See also: TreeView, TreeDataView, TreeNodeView

Parameters

Name Type Direction Required Default Description
sessionStateManager ITreeSessionStateManager custom no   Manages the state of the tree (i.e. which nodes are open, closed or selected).
sessionStoreManager ISessionStoreManager custom no   Allows storing the tree (or parts of it) to the session.
treeModel ITreeModel custom yes   Defines a model that can provide the nodes of the tree.
treeStateListener ITreeStateListener custom no   If specified, this object will receive events generated by the tree. Such events are: SELECTED_NODE_CHANGED, NODE_EXPANDED and NODE_COLLAPSED.
nodeRenderFactory INodeRenderFactory custom no (SimpleNodeRenderFactory) Allows custom rendering of each node.

This parameter defines a factory responsible for creating IRender instances which will be used to perform the actual render of each node.

The default factory returns RenderString instances that output each node's toString().

showNodeImages boolean custom no (true) If node images are shown.
makeNodeDirect boolean custom no (true) If true, renders a link around nodes that contain children. This allows expanding and collapsing the node.
showRootNode boolean custom no (true) If root node should be visible.

Body: allowed

Informal parameters: allowed

Reserved parameters: none

Examples

The following sample code shows how to create and render a simple tree.

<span jwcid="@contrib:Tree" treeModel="ognl:treeModel"/>

public abstract class TreePage extends BasePage {   
    private ITreeModel model;     
    
    public ITreeModel getTreeModel() {
        if (model == null) {
            TreeNode root = new MyNode("root");
            TreeNode child1 = new MyNode("child1");
            TreeNode child2 = new MyNode("child2");
            TreeNode child1of1 = new MyNode("child1of1");
            TreeNode child2of1 = new MyNode("child2of1");
            root.insert(child1);
            root.insert(child2);
            child1.insert(child1of1);
            child1.insert(child2of1);
            
            SimpleTreeDataModel dataModel = new SimpleTreeDataModel(root);
            model = new SimpleTreeModel(dataModel);
        }        
        return model;        
    }    
}

class MyNode extends TreeNode {
    protected String value;
    
    MyNode(String val) {
        super(null);
        value = val;
    }
    
    public String toString() {
        return value;
    }
    
    public int hashCode() {
        return value.hashCode();
    }

    public boolean equals(Object objTarget) {
        if(objTarget == this)
            return true;
        if(! (objTarget instanceof MyNode))
            return false;

        MyNode objTargetNode = (MyNode)objTarget;
        return value.equals(objTargetNode.value);
    }    
}