1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.myfaces.custom.tree2;
20
21 import java.util.StringTokenizer;
22 import java.util.ArrayList;
23
24 /**
25 * Model class for the tree component. It provides random access to nodes in a tree
26 * made up of instances of the {@link TreeNode} class.
27 *
28 * @author Sean Schofield
29 * @author Hans Bergsten (Some code taken from an example in his O'Reilly JavaServer Faces book. Copied with permission)
30 * @version $Revision: 168205 $ $Date: 2005-05-04 18:59:21 -0400 (Wed, 04 May 2005) $
31 */
32 public class TreeModelBase implements TreeModel
33 {
34
35 private static final long serialVersionUID = 3969414475396945742L;
36 // private static final Log log = LogFactory.getLog(TreeModelBase.class);
37
38 private TreeNode root;
39 private TreeState treeState = new TreeStateBase();
40
41
42 /**
43 * Constructor
44 * @param root The root TreeNode
45 */
46 public TreeModelBase(TreeNode root)
47 {
48 this.root = root;
49 }
50
51 // see interface
52 public TreeState getTreeState()
53 {
54 return treeState;
55 }
56
57 // see interface
58 public void setTreeState(TreeState treeState)
59 {
60 this.treeState = treeState;
61 }
62
63
64
65 /**
66 * Gets an array of String containing the ID's of all of the {@link TreeNode}s in the path to
67 * the specified node. The path information will be an array of <code>String</code> objects
68 * representing node ID's. The array will starting with the ID of the root node and end with
69 * the ID of the specified node.
70 *
71 * @param nodeId The id of the node for whom the path information is needed.
72 * @return String[]
73 */
74 public String[] getPathInformation(String nodeId)
75 {
76 if (nodeId == null)
77 {
78 throw new IllegalArgumentException("Cannot determine parents for a null node.");
79 }
80
81 ArrayList pathList = new ArrayList();
82 pathList.add(nodeId);
83
84 while (nodeId.lastIndexOf(SEPARATOR) != -1)
85 {
86 nodeId = nodeId.substring(0, nodeId.lastIndexOf(SEPARATOR));
87 pathList.add(nodeId);
88 }
89
90 String[] pathInfo = new String[pathList.size()];
91
92 for (int i=0; i < pathInfo.length; i++)
93 {
94 pathInfo[i] = (String)pathList.get(pathInfo.length - i - 1);
95 }
96
97 return pathInfo;
98 }
99
100 /**
101 * Indicates whether or not the specified {@link TreeNode} is the last child in the <code>List</code>
102 * of children. If the node id provided corresponds to the root node, this returns <code>true</code>.
103 *
104 * @param nodeId The ID of the node to check
105 * @return boolean
106 */
107 public boolean isLastChild(String nodeId)
108 {
109 if (nodeId.lastIndexOf(SEPARATOR) == -1)
110 {
111 // root node considered to be the last child
112 return true;
113 }
114
115 //first get the id of the parent
116 String parentId = nodeId.substring(0, nodeId.lastIndexOf(SEPARATOR));
117 String childString = nodeId.substring(nodeId.lastIndexOf(SEPARATOR) + 1);
118 int childId = Integer.parseInt(childString);
119 TreeNode parentNode = getNodeById(parentId);
120
121 return childId + 1== parentNode.getChildCount();
122 }
123
124 public TreeNode getNodeById(String nodeId)
125 {
126 if (nodeId == null)
127 return null;
128
129 TreeNode node = null;
130 StringTokenizer st = new StringTokenizer(nodeId, SEPARATOR);
131
132 while (st.hasMoreTokens())
133 {
134 int nodeIndex = Integer.parseInt(st.nextToken());
135 if(node == null)
136 {
137 node = root;
138 }
139 else
140 {
141 // don't worry about invalid index, that exception will be caught later and dealt with
142 node = (TreeNode)node.getChildren().get(nodeIndex);
143 }
144 }
145
146 return node;
147 }
148
149 // see interface
150 public TreeWalker getTreeWalker()
151 {
152 return new TreeWalkerBase();
153 }
154 }