KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > tree2 > TreeModel


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.tree2;
17
18 import javax.faces.component.NamingContainer;
19
20 import java.util.StringTokenizer JavaDoc;
21 import java.util.ArrayList JavaDoc;
22
23 /**
24  * Model class for the tree component. It provides random access to nodes in a tree
25  * made up of instances of the {@link TreeNode} class.
26  *
27  * @author Sean Schofield
28  * @author Hans Bergsten (Some code taken from an example in his O'Reilly JavaServer Faces book. Copied with permission)
29  * @version $Revision: 1.3 $ $Date: 2005/02/19 09:03:02 $
30  */

31 public class TreeModel
32 {
33     private final static String JavaDoc SEPARATOR = String.valueOf(NamingContainer.SEPARATOR_CHAR);
34
35     private TreeNode root;
36     private TreeNode currentNode;
37
38     /**
39      * Constructor
40      * @param root The root TreeNode
41      */

42     public TreeModel(TreeNode root)
43     {
44         this.root = root;
45     }
46
47     /**
48      * Gets the current {@link TreeNode} or <code>null</code> if no node ID is selected.
49      * @return The current node
50      */

51     public TreeNode getNode()
52     {
53         return currentNode;
54     }
55
56     /**
57      * Sets the current {@link TreeNode} to the specified node ID, which is a colon-separated list
58      * of node indexes. For instance, "0:0:1" means "the second child node of the first child node
59      * under the root node."
60      *
61      * @param nodeId The id of the node to set
62      */

63     public void setNodeId(String JavaDoc nodeId)
64     {
65         if (nodeId == null)
66         {
67             currentNode = null;
68             return;
69         }
70
71         currentNode = getNodeById(nodeId);
72     }
73
74     /**
75      * Gets an array of String containing the ID's of all of the {@link TreeNode}s in the path to
76      * the specified node. The path information will be an array of <code>String</code> objects
77      * representing node ID's. The array will starting with the ID of the root node and end with
78      * the ID of the specified node.
79      *
80      * @param nodeId The id of the node for whom the path information is needed.
81      * @return String[]
82      */

83     public String JavaDoc[] getPathInformation(String JavaDoc nodeId)
84     {
85         if (nodeId == null)
86         {
87             throw new IllegalArgumentException JavaDoc("Cannot determine parents for a null node.");
88         }
89
90         ArrayList JavaDoc pathList = new ArrayList JavaDoc();
91         pathList.add(nodeId);
92
93         while (nodeId.lastIndexOf(SEPARATOR) != -1)
94         {
95             nodeId = nodeId.substring(0, nodeId.lastIndexOf(SEPARATOR));
96             pathList.add(nodeId);
97         }
98
99         String JavaDoc[] pathInfo = new String JavaDoc[pathList.size()];
100
101         for (int i=0; i < pathInfo.length; i++)
102         {
103             pathInfo[i] = (String JavaDoc)pathList.get(pathInfo.length - i - 1);
104         }
105
106         return pathInfo;
107     }
108
109     /**
110      * Indicates whether or not the specified {@link TreeNode} is the last child in the <code>List</code>
111      * of children. If the node id provided corresponds to the root node, this returns <code>true</code>.
112      *
113      * @param nodeId The ID of the node to check
114      * @return boolean
115      */

116     public boolean isLastChild(String JavaDoc nodeId)
117     {
118         if (nodeId.lastIndexOf(SEPARATOR) == -1)
119         {
120             // root node considered to be the last child
121
return true;
122         }
123
124         //first get the id of the parent
125
String JavaDoc parentId = nodeId.substring(0, nodeId.lastIndexOf(SEPARATOR));
126         String JavaDoc childString = nodeId.substring(nodeId.lastIndexOf(SEPARATOR) + 1);
127         int childId = Integer.parseInt(childString);
128         TreeNode parentNode = getNodeById(parentId);
129
130         return childId + 1== parentNode.getChildCount();
131     }
132
133     private TreeNode getNodeById(String JavaDoc nodeId)
134     {
135         TreeNode node = root;
136
137         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
138         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(nodeId, SEPARATOR);
139         sb.append(st.nextToken()).append(SEPARATOR);
140
141         while (st.hasMoreTokens())
142         {
143             int nodeIndex = Integer.parseInt(st.nextToken());
144             sb.append(nodeIndex);
145
146             try
147             {
148                 node = (TreeNode)node.getChildren().get(nodeIndex);
149             }
150             catch (IndexOutOfBoundsException JavaDoc e)
151             {
152                 String JavaDoc msg = "Node with id " + sb.toString() + ". Failed to parse " + nodeId;
153                 throw new IllegalArgumentException JavaDoc(msg);
154             }
155             sb.append(SEPARATOR);
156         }
157
158         return node;
159     }
160 }
161
Popular Tags