KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > tree > TreeModelBaseImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.admingui.tree;
25
26 import com.iplanet.jato.RequestManager;
27 import com.iplanet.jato.RequestContext;
28 import com.iplanet.jato.ModelManager;
29 import com.iplanet.jato.model.TreeModelBase;
30 import com.iplanet.jato.model.ModelControlException;
31 import com.iplanet.jato.model.ValidationException;
32 import com.iplanet.jato.util.StringTokenizer2;
33
34 import com.sun.enterprise.tools.admingui.util.Util;
35 import com.sun.enterprise.tools.guiframework.view.ViewDescriptorManager;
36
37 import java.util.List JavaDoc;
38 import java.util.ListIterator JavaDoc;
39 import java.lang.Thread JavaDoc;
40 import java.io.InputStream JavaDoc;
41 import java.io.Serializable JavaDoc;
42 import javax.servlet.*;
43
44
45 public class TreeModelBaseImpl extends TreeModelBase implements IndexTreeModel, Serializable JavaDoc {
46     
47     public TreeModelBaseImpl(String JavaDoc treeFileName, String JavaDoc stateDataName) {
48     super();
49         this.treeFileName = treeFileName;
50         this.stateDataName = stateDataName;
51     }
52     
53     public String JavaDoc getStateDataName() {
54         return stateDataName;
55     }
56     
57     public String JavaDoc getName() {
58     return name;
59     }
60     
61     public void setName(String JavaDoc value) {
62     name=value;
63     }
64     
65     public void root() throws ModelControlException {
66         if (root == null) {
67             throw new RuntimeException JavaDoc("The root node is null!!!");
68         }
69     setCurrentNode(root);
70     setNodeLevel(ROOT_NODE_LEVEL);
71     setIterationComplete(false);
72     }
73     
74     public String JavaDoc getNodeName() throws ModelControlException {
75     IndexTreeNode node=(IndexTreeNode)getCurrentNode();
76     if (node!=null)
77         return node.getName();
78     else
79         return null;
80     }
81     
82     public String JavaDoc getNodeType() throws ModelControlException {
83     IndexTreeNode node=(IndexTreeNode)getCurrentNode();
84     if (node!=null)
85         return node.getType();
86     else
87         return null;
88     }
89     
90     public IndexTreeNode getNode(String JavaDoc nodePath) throws NodeNotFoundException {
91     if (Util.isLoggableFINEST()) {
92         Util.logFINEST("IndexTreeModelImpl.getNode : " + nodePath);
93     }
94
95     String JavaDoc[] nodeIDs=StringTokenizer2.tokenize(nodePath,".");
96     IndexTreeNode node = getRoot();
97     for (int i = 1; i < nodeIDs.length; i++) {
98         for (int j=0; j < node.getChildren().size(); j++) {
99         IndexTreeNode child=(IndexTreeNode)node.getChildren().get(j);
100         if (String.valueOf(child.getID()).equals(nodeIDs[i])) {
101             node = child;
102             break;
103         }
104         }
105     }
106     // Sanity check
107
if (!node.getPath().equals(nodePath)) {
108         throw new NodeNotFoundException("Incorrect node (" + node.getPath() + " != "+nodePath+")");
109     }
110     return node;
111     }
112     
113     public IndexTreeNode getNodeByName(String JavaDoc nodeNamePath) {
114     String JavaDoc[] nodeIDs=StringTokenizer2.tokenize(nodeNamePath,".");
115     IndexTreeNode node = getRoot();
116     for (int i = 1; i < nodeIDs.length; i++) {
117         for (int j=0; j < node.getChildren().size(); j++) {
118         IndexTreeNode child=(IndexTreeNode)node.getChildren().get(j);
119         if (String.valueOf(child.getName()).equals(nodeIDs[i])) {
120             node = child;
121             break;
122         }
123         }
124     }
125     // Sanity check
126
if (!node.getNamePath().equals(nodeNamePath)) {
127         throw new NodeNotFoundException("Incorrect node (" +
128         node.getNamePath() + " != "+nodeNamePath+")");
129     }
130     return node;
131     }
132
133     public IndexTreeNode getNodeByUniqueID(String JavaDoc id, String JavaDoc childName, IndexTreeNode node) {
134         if (node == null)
135             node = getRoot();
136         String JavaDoc uniqueID = node.getUniqueID();
137         //System.out.print("UNIQUE-ID: "+id+" "+uniqueID+" "+node.getName());
138
if (uniqueID == null) {
139             // stop looking.
140
return null;
141         }
142         List JavaDoc children = node.getChildren();
143         if (uniqueID != null && uniqueID.equals(id)) {
144             if (childName != null) {
145                 IndexTreeNode childNode = node.getChild(childName);
146                 if (childNode != null)
147                     return childNode;
148             } else {
149                 return node;
150             }
151         }
152         // keep looking; next look at the kids.
153
for (int i=0; i < children.size(); i++) {
154             IndexTreeNode child = (IndexTreeNode)children.get(i);
155             IndexTreeNode childNode = getNodeByUniqueID(id, childName, child);
156             if (childNode != null) {
157                 return childNode;
158             }
159         }
160     return null;
161     }
162     
163     public void beforeRoot() throws ModelControlException {
164     setCurrentNode((Object JavaDoc)null);
165     setNodeLevel(UNDEFINED_NODE_LEVEL);
166     setIterationComplete(false);
167     }
168     
169     public Object JavaDoc getCurrentNode() {
170     return super.getCurrentNode();
171     }
172     
173     public void setCurrentNode(String JavaDoc nodeID) throws ModelControlException, Exception JavaDoc {
174     setCurrentNode(getNode(nodeID));
175     }
176     
177     public void setCurrentNode(IndexTreeNode node) {
178         super.setCurrentNode(node);
179     }
180     
181     public String JavaDoc getNodeID() {
182     IndexTreeNode node=(IndexTreeNode)getCurrentNode();
183     if (node!=null)
184         return node.getPath();
185     else
186         return null;
187     }
188     
189     public String JavaDoc getNodeHID() {
190     // returns the highligh ID for the current node.
191
IndexTreeNode node=(IndexTreeNode)getCurrentNode();
192     if (node != null)
193         return node.getHighlightID();
194     else
195         return "0";
196     }
197     
198     public String JavaDoc getIconName(boolean expanded) {
199     // returns the icon file name for the current node.
200
IndexTreeNode node=(IndexTreeNode)getCurrentNode();
201     if (node != null)
202         return node.getIconName(expanded);
203     else
204         return "props.gif";
205     }
206     
207     public boolean isParentNode() {
208     IndexTreeNode node=(IndexTreeNode)getCurrentNode();
209     if (node!=null)
210         return node.getChildren().size()>0;
211     else
212         return false;
213     }
214     
215     public boolean isChildNode() {
216     IndexTreeNode node=(IndexTreeNode)getCurrentNode();
217     if (node!=null)
218         return node.getParent()!=null;
219     else
220         return false;
221     }
222     
223     public boolean firstChild() throws ModelControlException {
224     IndexTreeNode node=(IndexTreeNode)getCurrentNode();
225     if (node!=null && isParentNode()) {
226         setCurrentNode(node.getChildren().get(0));
227         incrementNodeLevel();
228         return true;
229     }
230     else {
231         return false;
232     }
233     }
234     
235     public boolean parent() throws ModelControlException {
236     IndexTreeNode node=(IndexTreeNode)getCurrentNode();
237     if (node!=null && isChildNode()) {
238         setCurrentNode(node.getParent());
239         decrementNodeLevel();
240         return true;
241     }
242     else {
243         return false;
244     }
245     }
246     
247     public boolean nextSibling() throws ModelControlException {
248     IndexTreeNode node=(IndexTreeNode)getCurrentNode();
249     if (node!=null) {
250         IndexTreeNode nextNode=node.getNextSibling();
251         if (nextNode!=null) {
252         setCurrentNode(node.getNextSibling());
253         return true;
254         }
255         else
256         return false;
257     }
258     else {
259         return false;
260     }
261     }
262     
263     public Object JavaDoc getValue(String JavaDoc name) {
264     Object JavaDoc[] values=getValues(name);
265     if (values==null || values.length==0)
266         return null;
267     else
268         return values[0];
269     }
270     
271     public void setValue(String JavaDoc name, Object JavaDoc value)
272         throws ValidationException {
273     setValues(name,new Object JavaDoc[] {value});
274     }
275     
276     public Object JavaDoc[] getValues(String JavaDoc name) {
277         if (getCurrentNode()!=null) {
278             if (name.equals("name")) {
279                 return new Object JavaDoc[] {
280                     ((IndexTreeNode)getCurrentNode()).getName()};
281             } else {
282                 
283                 return new Object JavaDoc[] {
284                     ((IndexTreeNode)getCurrentNode()).getAttribute(name)};
285             }
286         }
287         else
288             return null;
289         
290     }
291     
292     public void setValues(String JavaDoc name, Object JavaDoc[] values) throws ValidationException {
293     if (getCurrentNode() != null)
294         ((IndexTreeNode)getCurrentNode()).setAttribute(name,values);
295     }
296     
297     
298     protected void createIndexData() {
299     try {
300             // try loading from the classpath with the call loader. This is cached.
301
InputStream JavaDoc inStream = getClass().getClassLoader().getResourceAsStream(treeFileName);
302             if (inStream == null) {
303                 // try getting the file from the context root. This is not cached.
304
inStream = RequestManager.getRequestContext().getServletContext().
305             getResourceAsStream(treeFileName);
306         }
307
308             TreeReader reader = new TreeReader(inStream,
309                 ViewDescriptorManager.getInstance().getDTDURLBase());
310         reader.populate(this); // this call will set the root node
311
root();
312     } catch (Exception JavaDoc ex) {
313             if (Util.isLoggableINFO()) {
314                 Util.logINFO("ERROR IN READING TREE FILE: "+treeFileName+" "+ex.getMessage());
315                 ex.printStackTrace();
316             }
317     }
318     }
319     
320     
321     public int getNextNodeID() {
322     return nodeCount++;
323     }
324     
325     public boolean isInitialExpand() {
326     boolean b = initialExpand;
327     initialExpand = false;
328     return b;
329     }
330     
331     public String JavaDoc getType(String JavaDoc string) {
332     IndexTreeNode node=(IndexTreeNode)getCurrentNode();
333     if (node != null)
334         return node.getType();
335     else
336             return ROOT;
337     }
338     
339     public IndexTreeNode getRoot() {
340     return root;
341     }
342         
343     public void setRoot(IndexTreeNode node) {
344     root = node;
345     }
346         
347     private String JavaDoc treeFileName;
348     private String JavaDoc stateDataName;
349     private boolean initialExpand = true;
350     private String JavaDoc name;
351     private int nodeCount = 0;
352     private IndexTreeNode root = null; // tree root
353
}
354
Popular Tags