KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > webui > tree > DefaultWebTreeNode


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.webui.tree;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18
19 /**
20  * This class represents a node in a <code>{@link DefaultWebTreeModel}</code>. It has the
21  * attributes most people will need: name, URL, and icon URL. Most of the methods in
22  * <code>DefaultWebTreeModel</code> delegate to this class.
23  *
24  * @author Eric Galluzzo
25  */

26 public class DefaultWebTreeNode
27 {
28     protected static int staticNextID = 0;
29     protected List JavaDoc fieldChildren;
30     protected String JavaDoc fieldIconURL;
31     protected String JavaDoc fieldName;
32     protected boolean fieldLeaf;
33     protected String JavaDoc fieldID;
34     protected DefaultWebTreeNode fieldParent;
35     /**
36      * Create a tree node with the given name, without an icon or link.
37      *
38      * @param inName DOCUMENT ME!
39      */

40     public DefaultWebTreeNode(String JavaDoc inName)
41     {
42         setName(inName);
43         fieldID = String.valueOf(staticNextID++);
44     }
45
46     public DefaultWebTreeNode(String JavaDoc inId, String JavaDoc inName)
47     {
48         setName(inName);
49         fieldID = inId;
50     }
51
52     /**
53      * Get the child at the given position in this node.
54      *
55      * @param inIndex DOCUMENT ME!
56      *
57      * @return
58      */

59     public DefaultWebTreeNode getChild(int inIndex)
60     {
61         return (DefaultWebTreeNode) getChildren().get(inIndex);
62     }
63
64     /**
65      * Get the number of children that this node has.
66      *
67      * @return
68      */

69     public int getChildCount()
70     {
71         return getChildren().size();
72     }
73
74     /**
75      * Gets the children.
76      *
77      * @return Returns a List
78      */

79     public List JavaDoc getChildren()
80     {
81         if (fieldChildren == null)
82         {
83             fieldChildren = new ArrayList JavaDoc();
84         }
85
86         return fieldChildren;
87     }
88
89     /**
90      * Gets the ID of this node, which is unique within the VM for the life of the VM (unless you
91      * do weird things with classloaders).
92      *
93      * @return Returns an int
94      */

95     public String JavaDoc getID()
96     {
97         return fieldID;
98     }
99
100     /**
101      * Sets the icon URL.
102      *
103      * @param iconURL The icon URL to set
104      */

105     public void setIconURL(String JavaDoc iconURL)
106     {
107         fieldIconURL = iconURL;
108     }
109
110     /**
111      * Gets the icon URL.
112      *
113      * @return Returns a String
114      */

115     public String JavaDoc getIconURL()
116     {
117         return fieldIconURL;
118     }
119
120     /**
121      * Get the index of the given child in this node
122      *
123      * @param inChild DOCUMENT ME!
124      *
125      * @return The index, or -1 if the child could not be found
126      */

127     public int getIndexOfChild(DefaultWebTreeNode inChild)
128     {
129         return getChildren().indexOf(inChild);
130     }
131
132     /**
133      * Set whether this node is a leaf.
134      *
135      * @param inLeaf DOCUMENT ME!
136      */

137     public void setLeaf(boolean inLeaf)
138     {
139         fieldLeaf = inLeaf;
140     }
141
142     /**
143      * Determine whether this node is a leaf node.
144      *
145      * @return
146      */

147     public boolean isLeaf()
148     {
149         return fieldLeaf;
150     }
151
152     /**
153      * Sets the name.
154      *
155      * @param name The name to set
156      */

157     public void setName(String JavaDoc name)
158     {
159         fieldName = name;
160     }
161
162     /**
163      * Gets the name.
164      *
165      * @return Returns a String
166      */

167     public String JavaDoc getName()
168     {
169         return fieldName;
170     }
171
172
173     /**
174      * Gets the URL.
175      *
176      * @return Returns a String
177      */

178     public String JavaDoc getURL()
179     {
180         if ( getParent() != null)
181         {
182             String JavaDoc p =getParent().getURL();
183             if ( p.endsWith("/"))
184             {
185                 return p + getName();
186             }
187             else
188             {
189                 return p + "/" + getName();
190             }
191         }
192         else
193         {
194             return getName(); //the root does not need a special URL since it is part of the base path
195
}
196     }
197
198     /**
199      * Add the given child to this node's children.
200      *
201      * @param inNode DOCUMENT ME!
202      */

203     public void addChild(DefaultWebTreeNode inNode)
204     {
205         getChildren().add(inNode);
206         inNode.setParent(this);
207     }
208
209     /**
210      * DOCME
211      */

212     public void reloadChildren()
213     {
214         //nothing to do?
215
}
216     public DefaultWebTreeNode getParent()
217     {
218         return fieldParent;
219     }
220     public void setParent(DefaultWebTreeNode inParent)
221     {
222         fieldParent = inParent;
223     }
224 }
225
Popular Tags