KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > TreeControlNode


1 /*
2  * Copyright 2001,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
17
18 package org.apache.webapp.admin;
19
20
21 import java.io.Serializable JavaDoc;
22 import java.util.ArrayList JavaDoc;
23
24
25 /**
26  * <p>An individual node of a tree control represented by an instance of
27  * <code>TreeControl</code>, and rendered by an instance of
28  * <code>TreeControlTag</code>.</p>
29  *
30  * @author Jazmin Jonson
31  * @author Craig R. McClanahan
32  * @version $Revision: 1.3 $ $Date: 2004/02/27 14:59:01 $
33  */

34
35 public class TreeControlNode implements Serializable JavaDoc {
36
37
38     // ----------------------------------------------------------- Constructors
39

40
41     /**
42      * Construct a new TreeControlNode with the specified parameters.
43      *
44      * @param name Internal name of this node (must be unique within
45      * the entire tree)
46      * @param icon Pathname of the image file for the icon to be displayed
47      * when this node is visible, relative to the image directory
48      * for our images
49      * @param label The label that will be displayed to the user if
50      * this node is visible
51      * @param action The hyperlink to be selected if the user
52      * selects this node, or <code>null</code> if this node's label should
53      * not be a hyperlink
54      * @param target The window target in which the <code>action</code>
55      * hyperlink's results will be displayed, or <code>null</code> for
56      * the current window
57      * @param expanded Should this node be expanded?
58      */

59     public TreeControlNode(String JavaDoc name,
60                            String JavaDoc icon, String JavaDoc label,
61                            String JavaDoc action, String JavaDoc target,
62                            boolean expanded, String JavaDoc domain) {
63
64         super();
65         this.name = name;
66         this.icon = icon;
67         this.label = label;
68         this.action = action;
69         this.target = target;
70         this.expanded = expanded;
71         this.domain = domain;
72
73     }
74
75
76     // ----------------------------------------------------- Instance Variables
77

78
79     /**
80      * The set of child <code>TreeControlNodes</code> for this node, in the
81      * order that they should be displayed.
82      */

83     protected ArrayList JavaDoc children = new ArrayList JavaDoc();
84
85
86     // ------------------------------------------------------------- Properties
87

88
89     /**
90      * The hyperlink to which control will be directed if this node
91      * is selected by the user.
92      */

93     protected String JavaDoc action = null;
94
95     public String JavaDoc getAction() {
96         return (this.action);
97     }
98
99     /**
100      * The domain of this node.
101      */

102     protected String JavaDoc domain = null;
103
104     public String JavaDoc getDomain() {
105         return (this.domain);
106     }
107
108     /**
109      * Is this node currently expanded?
110      */

111     protected boolean expanded = false;
112
113     public boolean isExpanded() {
114         return (this.expanded);
115     }
116
117     public void setExpanded(boolean expanded) {
118         this.expanded = expanded;
119     }
120
121
122     /**
123      * The pathname to the icon file displayed when this node is visible,
124      * relative to the image directory for our images.
125      */

126     protected String JavaDoc icon = null;
127
128     public String JavaDoc getIcon() {
129         return (this.icon);
130     }
131
132
133     /**
134      * The label that will be displayed when this node is visible.
135      */

136     protected String JavaDoc label = null;
137
138     public String JavaDoc getLabel() {
139         return (this.label);
140     }
141
142
143     /**
144      * Is this the last node in the set of children for our parent node?
145      */

146     protected boolean last = false;
147
148     public boolean isLast() {
149         return (this.last);
150     }
151
152     void setLast(boolean last) {
153         this.last = last;
154     }
155
156
157     /**
158      * Is this a "leaf" node (i.e. one with no children)?
159      */

160     public boolean isLeaf() {
161         synchronized (children) {
162             return (children.size() < 1);
163         }
164     }
165
166
167     /**
168      * The unique (within the entire tree) name of this node.
169      */

170     protected String JavaDoc name = null;
171
172     public String JavaDoc getName() {
173         return (this.name);
174     }
175
176
177     /**
178      * The parent node of this node, or <code>null</code> if this
179      * is the root node.
180      */

181     protected TreeControlNode parent = null;
182
183     public TreeControlNode getParent() {
184         return (this.parent);
185     }
186
187     void setParent(TreeControlNode parent) {
188         this.parent = parent;
189         if (parent == null)
190             width = 1;
191         else
192             width = parent.getWidth() + 1;
193     }
194
195
196     /**
197      * Is this node currently selected?
198      */

199     protected boolean selected = false;
200
201     public boolean isSelected() {
202         return (this.selected);
203     }
204
205     public void setSelected(boolean selected) {
206         this.selected = selected;
207     }
208
209
210     /**
211      * The window target for the hyperlink identified by the
212      * <code>action</code> property, if this node is selected
213      * by the user.
214      */

215     protected String JavaDoc target = null;
216
217     public String JavaDoc getTarget() {
218         return (this.target);
219     }
220
221
222     /**
223      * The <code>TreeControl</code> instance representing the
224      * entire tree.
225      */

226     protected TreeControl tree = null;
227
228     public TreeControl getTree() {
229         return (this.tree);
230     }
231
232     void setTree(TreeControl tree) {
233         this.tree = tree;
234     }
235
236
237     /**
238      * The display width necessary to display this item (if it is visible).
239      * If this item is not visible, the calculated width will be that of our
240      * most immediately visible parent.
241      */

242     protected int width = 0;
243
244     public int getWidth() {
245         return (this.width);
246     }
247
248
249     // --------------------------------------------------------- Public Methods
250

251
252     /**
253      * Add a new child node to the end of the list.
254      *
255      * @param child The new child node
256      *
257      * @exception IllegalArgumentException if the name of the new child
258      * node is not unique
259      */

260     public void addChild(TreeControlNode child)
261         throws IllegalArgumentException JavaDoc {
262
263         tree.addNode(child);
264         child.setParent(this);
265         synchronized (children) {
266             int n = children.size();
267             if (n > 0) {
268                 TreeControlNode node = (TreeControlNode) children.get(n - 1);
269                 node.setLast(false);
270             }
271             child.setLast(true);
272             children.add(child);
273         }
274
275     }
276
277
278     /**
279      * Add a new child node at the specified position in the child list.
280      *
281      * @param offset Zero-relative offset at which the new node
282      * should be inserted
283      * @param child The new child node
284      *
285      * @exception IllegalArgumentException if the name of the new child
286      * node is not unique
287      */

288     public void addChild(int offset, TreeControlNode child)
289         throws IllegalArgumentException JavaDoc {
290
291         tree.addNode(child);
292         child.setParent(this);
293         synchronized (children) {
294             children.add(offset, child);
295         }
296
297     }
298
299
300     /**
301      * Return the set of child nodes for this node.
302      */

303     public TreeControlNode[] findChildren() {
304
305         synchronized (children) {
306             TreeControlNode results[] = new TreeControlNode[children.size()];
307             return ((TreeControlNode[]) children.toArray(results));
308         }
309
310     }
311
312
313     /**
314      * Remove this node from the tree.
315      */

316     public void remove() {
317
318         if (tree != null) {
319             tree.removeNode(this);
320         }
321
322     }
323
324
325     /**
326      * Remove the child node (and all children of that child) at the
327      * specified position in the child list.
328      *
329      * @param offset Zero-relative offset at which the existing
330      * node should be removed
331      */

332     public void removeChild(int offset) {
333
334         synchronized (children) {
335             TreeControlNode child =
336                 (TreeControlNode) children.get(offset);
337             tree.removeNode(child);
338             child.setParent(null);
339             children.remove(offset);
340         }
341
342     }
343
344
345     // -------------------------------------------------------- Package Methods
346

347
348     /**
349      * Remove the specified child node. It is assumed that all of the
350      * children of this child node have already been removed.
351      *
352      * @param child Child node to be removed
353      */

354     void removeChild(TreeControlNode child) {
355
356         if (child == null) {
357             return;
358         }
359         synchronized (children) {
360             int n = children.size();
361             for (int i = 0; i < n; i++) {
362                 if (child == (TreeControlNode) children.get(i)) {
363                     children.remove(i);
364                     return;
365                 }
366             }
367         }
368
369     }
370
371
372 }
373
Popular Tags