FileDocCategorySizeDatePackage
TreeModel.javaAPI DocExample1891Tue Jun 08 11:26:42 BST 2004com.mycompany.jsf.model

TreeModel

public class TreeModel extends Object
This class represents a tree of nodes, used as the model for the UITree component.
author
Hans Bergsten, Gefion Software
version
1.0

Fields Summary
private static final String
SEPARATOR
private TreeNode
root
private TreeNode
currentNode
Constructors Summary
public TreeModel(TreeNode root)
Create a model with the provided TreeNode as the root.


                   
       
	this.root = root;
    
Methods Summary
public TreeNodegetNode()
Returns the current node, or null if no node is selected.

	return currentNode;
    
public voidsetNodeId(java.lang.String nodeId)
Selects the node matching the specified node ID, or deselects the current node if the node ID is null. The node ID is a colon-separated list of zero-based node indexes, e.g., the node ID "0:0:1" represents the second subnode of the first node under the root node.

	if (nodeId == null) {
	    currentNode = null;
	    return;
	}
	
	TreeNode node = root;
	StringBuffer sb = new StringBuffer();
	StringTokenizer st = 
	    new StringTokenizer(nodeId, SEPARATOR);

	// Trow away the root index
	sb.append(st.nextToken()).append(SEPARATOR);
	while (st.hasMoreTokens()) {
	    int nodeIndex = Integer.parseInt(st.nextToken());
	    sb.append(nodeIndex);
	    try {
		node = (TreeNode) node.getChildren().get(nodeIndex);
	    }
	    catch (IndexOutOfBoundsException e) {
		String msg = "Node node with ID " + sb.toString() +
		    ". Failed to parse " + nodeId;
		throw new IllegalArgumentException(msg);
	    }
	    sb.append(SEPARATOR);
	}
	currentNode = node;