KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nextime > ion > backoffice > tree > TreeControlNode


1 /*
2  * $Header: /home/cvs/jakarta-tomcat-4.0/webapps/admin/WEB-INF/classes/org/apache/webapp/admin/TreeControlNode.java,v 1.2 2002/03/07 02:48:54 craigmcc Exp $
3  * $Revision: 1.2 $
4  * $Date: 2002/03/07 02:48:54 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 2001 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowlegement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowlegement may appear in the software itself,
30  * if and wherever such third-party acknowlegements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Group.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  */

61
62 package org.nextime.ion.backoffice.tree;
63
64 import java.io.Serializable JavaDoc;
65 import java.util.ArrayList JavaDoc;
66
67 /**
68  * <p>An individual node of a tree control represented by an instance of
69  * <code>TreeControl</code>, and rendered by an instance of
70  * <code>TreeControlTag</code>.</p>
71  *
72  * @author Jazmin Jonson
73  * @author Craig R. McClanahan
74  * @version $Revision: 1.2 $ $Date: 2002/03/07 02:48:54 $
75  */

76
77 public class TreeControlNode implements Serializable JavaDoc {
78
79     // ----------------------------------------------------------- Constructors
80

81     /**
82      * Construct a new TreeControlNode with the specified parameters.
83      *
84      * @param name Internal name of this node (must be unique within
85      * the entire tree)
86      * @param icon Pathname of the image file for the icon to be displayed
87      * when this node is visible, relative to the image directory
88      * for our images
89      * @param label The label that will be displayed to the user if
90      * this node is visible
91      * @param action The hyperlink to be selected if the user
92      * selects this node, or <code>null</code> if this node's label should
93      * not be a hyperlink
94      * @param target The window target in which the <code>action</code>
95      * hyperlink's results will be displayed, or <code>null</code> for
96      * the current window
97      * @param expanded Should this node be expanded?
98      */

99     public TreeControlNode(
100         String JavaDoc name,
101         String JavaDoc icon,
102         String JavaDoc label,
103         String JavaDoc action,
104         String JavaDoc target,
105         boolean expanded) {
106
107         super();
108         this.name = name;
109         this.icon = icon;
110         this.label = label;
111         this.action = action;
112         this.target = target;
113         this.expanded = expanded;
114
115     }
116
117     // ----------------------------------------------------- Instance Variables
118

119     /**
120      * The set of child <code>TreeControlNodes</code> for this node, in the
121      * order that they should be displayed.
122      */

123     protected ArrayList JavaDoc children = new ArrayList JavaDoc();
124
125     // ------------------------------------------------------------- Properties
126

127     /**
128      * The hyperlink to which control will be directed if this node
129      * is selected by the user.
130      */

131     protected String JavaDoc action = null;
132
133     public String JavaDoc getAction() {
134         return (this.action);
135     }
136
137     /**
138      * Is this node currently expanded?
139      */

140     protected boolean expanded = false;
141
142     public boolean isExpanded() {
143         return (this.expanded);
144     }
145
146     public void setExpanded(boolean expanded) {
147         this.expanded = expanded;
148     }
149
150     /**
151      * The pathname to the icon file displayed when this node is visible,
152      * relative to the image directory for our images.
153      */

154     protected String JavaDoc icon = null;
155
156     public String JavaDoc getIcon() {
157         return (this.icon);
158     }
159
160     /**
161      * The label that will be displayed when this node is visible.
162      */

163     protected String JavaDoc label = null;
164
165     public String JavaDoc getLabel() {
166         return (this.label);
167     }
168
169     /**
170      * Is this the last node in the set of children for our parent node?
171      */

172     protected boolean last = false;
173
174     public boolean isLast() {
175         return (this.last);
176     }
177
178     void setLast(boolean last) {
179         this.last = last;
180     }
181
182     /**
183      * Is this a "leaf" node (i.e. one with no children)?
184      */

185     public boolean isLeaf() {
186         synchronized (children) {
187             return (children.size() < 1);
188         }
189     }
190
191     public void up() {
192         TreeControlNode parent = getParent();
193         ArrayList JavaDoc brothers = parent.children;
194         int myIndex = brothers.indexOf(this);
195         if (myIndex<brothers.size()-1) {
196             TreeControlNode nextOne =
197                 (TreeControlNode) brothers.get(myIndex + 1);
198             brothers.set(myIndex, nextOne);
199             brothers.set(myIndex + 1, this);
200         }
201         rebuildLast(brothers);
202     }
203     
204     protected void rebuildLast( ArrayList JavaDoc nodes ) {
205         for( int i=1; i<=nodes.size(); i++ ) {
206             TreeControlNode node = (TreeControlNode)nodes.get(i-1);
207             if( i==nodes.size() ) {
208                 node.setLast(true);
209             } else {
210                 node.setLast(false);
211             }
212         }
213     }
214     
215     public void rebuildLast() {
216         ArrayList JavaDoc brothers = parent.children;
217         rebuildLast(brothers);
218     }
219     
220     public void rebuildLastChildren() {
221         ArrayList JavaDoc brothers = children;
222         rebuildLast(brothers);
223     }
224     
225     public void down() {
226         TreeControlNode parent = getParent();
227         ArrayList JavaDoc brothers = parent.children;
228         int myIndex = brothers.indexOf(this);
229         if (myIndex>0) {
230             TreeControlNode nextOne =
231                 (TreeControlNode) brothers.get(myIndex - 1);
232             brothers.set(myIndex, nextOne);
233             brothers.set(myIndex - 1, this);
234         }
235         rebuildLast(brothers);
236     }
237     
238
239     /**
240      * The unique (within the entire tree) name of this node.
241      */

242     protected String JavaDoc name = null;
243
244     public String JavaDoc getName() {
245         return (this.name);
246     }
247
248     /**
249      * The parent node of this node, or <code>null</code> if this
250      * is the root node.
251      */

252     protected TreeControlNode parent = null;
253
254     public TreeControlNode getParent() {
255         return (this.parent);
256     }
257
258     void setParent(TreeControlNode parent) {
259         this.parent = parent;
260         if (parent == null)
261             width = 1;
262         else
263             width = parent.getWidth() + 1;
264     }
265
266     /**
267      * Is this node currently selected?
268      */

269     protected boolean selected = false;
270
271     public boolean isSelected() {
272         return (this.selected);
273     }
274
275     public void setSelected(boolean selected) {
276         this.selected = selected;
277     }
278
279     /**
280      * The window target for the hyperlink identified by the
281      * <code>action</code> property, if this node is selected
282      * by the user.
283      */

284     protected String JavaDoc target = null;
285
286     public String JavaDoc getTarget() {
287         return (this.target);
288     }
289
290     /**
291      * The <code>TreeControl</code> instance representing the
292      * entire tree.
293      */

294     protected TreeControl tree = null;
295
296     public TreeControl getTree() {
297         return (this.tree);
298     }
299
300     void setTree(TreeControl tree) {
301         this.tree = tree;
302     }
303
304     /**
305      * The display width necessary to display this item (if it is visible).
306      * If this item is not visible, the calculated width will be that of our
307      * most immediately visible parent.
308      */

309     protected int width = 0;
310
311     public int getWidth() {
312         return (this.width);
313     }
314
315     // --------------------------------------------------------- Public Methods
316

317     /**
318      * Add a new child node to the end of the list.
319      *
320      * @param child The new child node
321      *
322      * @exception IllegalArgumentException if the name of the new child
323      * node is not unique
324      */

325     public void addChild(TreeControlNode child)
326         throws IllegalArgumentException JavaDoc {
327
328         tree.addNode(child);
329         child.setParent(this);
330         synchronized (children) {
331             int n = children.size();
332             if (n > 0) {
333                 TreeControlNode node = (TreeControlNode) children.get(n - 1);
334                 node.setLast(false);
335             }
336             child.setLast(true);
337             children.add(child);
338         }
339
340     }
341
342     /**
343      * Add a new child node at the specified position in the child list.
344      *
345      * @param offset Zero-relative offset at which the new node
346      * should be inserted
347      * @param child The new child node
348      *
349      * @exception IllegalArgumentException if the name of the new child
350      * node is not unique
351      */

352     public void addChild(int offset, TreeControlNode child)
353         throws IllegalArgumentException JavaDoc {
354
355         tree.addNode(child);
356         child.setParent(this);
357         synchronized (children) {
358             children.add(offset, child);
359         }
360
361     }
362
363     /**
364      * Return the set of child nodes for this node.
365      */

366     public TreeControlNode[] findChildren() {
367
368         synchronized (children) {
369             TreeControlNode results[] = new TreeControlNode[children.size()];
370             return ((TreeControlNode[]) children.toArray(results));
371         }
372
373     }
374
375     /**
376      * Remove this node from the tree.
377      */

378     public void remove() {
379
380         if (tree != null) {
381             tree.removeNode(this);
382         }
383
384     }
385
386     /**
387      * Remove the child node (and all children of that child) at the
388      * specified position in the child list.
389      *
390      * @param offset Zero-relative offset at which the existing
391      * node should be removed
392      */

393     public void removeChild(int offset) {
394
395         synchronized (children) {
396             TreeControlNode child = (TreeControlNode) children.get(offset);
397             tree.removeNode(child);
398             child.setParent(null);
399             children.remove(offset);
400         }
401
402     }
403
404     // -------------------------------------------------------- Package Methods
405

406     /**
407      * Remove the specified child node. It is assumed that all of the
408      * children of this child node have already been removed.
409      *
410      * @param child Child node to be removed
411      */

412     void removeChild(TreeControlNode child) {
413
414         if (child == null) {
415             return;
416         }
417         synchronized (children) {
418             int n = children.size();
419             for (int i = 0; i < n; i++) {
420                 if (child == (TreeControlNode) children.get(i)) {
421                     children.remove(i);
422                     return;
423                 }
424             }
425         }
426
427     }
428
429     /**
430      * Sets the label.
431      * @param label The label to set
432      */

433     public void setLabel(String JavaDoc label) {
434         this.label = label;
435     }
436
437
438     /**
439      * Sets the icon.
440      * @param icon The icon to set
441      */

442     public void setIcon(String JavaDoc icon) {
443         this.icon = icon;
444     }
445
446
447 }
448
Popular Tags