KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > TreeParentNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.tax;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24
25 /**
26  * This tree object own a list of its children accessible by getChildNodes().
27  *
28  * @author Libor Kramolis
29  * @version 0.1
30  */

31 public abstract class TreeParentNode extends TreeChild {
32
33     /** */
34     public static final String JavaDoc PROP_CHILD_LIST = "childList"; // NOI18N
35

36     /** */
37     private TreeObjectList childList;
38
39
40     //
41
// init
42
//
43

44     /** Creates new TreeParentNode. */
45     protected TreeParentNode () {
46         super ();
47         
48         this.childList = new TreeObjectList (createChildListContentManager ());
49     }
50     
51     /** Creates new TreeParentNode -- copy constructor. */
52     protected TreeParentNode (TreeParentNode parentNode, boolean deep) {
53         super (parentNode);
54         
55         this.childList = new TreeObjectList (createChildListContentManager ());
56         if (deep) {
57             this.childList.addAll ((TreeObjectList)parentNode.childList.clone ());
58         }
59     }
60     
61     
62     //
63
// from TreeObject
64
//
65

66     /** Clone depply tree object.
67      * @return deep clone of this node
68      */

69     public abstract Object JavaDoc clone (boolean deep);
70     
71     
72     /** Call clone (true).
73      * @return deep clone of this node.
74      */

75     public final Object JavaDoc clone () {
76         return clone (true);
77     }
78     
79     /**
80      */

81     public boolean equals (Object JavaDoc object, boolean deep) {
82         if (!!! super.equals (object, deep))
83             return false;
84         
85         TreeParentNode peer = (TreeParentNode) object;
86         if (!!! Util.equals (this.childList, peer.childList)) {
87             return false;
88         }
89         
90         return true;
91     }
92     
93     /*
94      * Merges childlist.
95      */

96     public void merge (TreeObject treeObject) throws CannotMergeException {
97         super.merge (treeObject);
98         
99         TreeParentNode peer = (TreeParentNode) treeObject;
100         
101         childList.merge (peer.childList);
102     }
103     
104     
105     //
106
// itself
107
//
108

109     /**
110      */

111     public boolean isAssignableChild (TreeChild child) {
112         return childList.isAssignableObject (child);
113     }
114     
115     /**
116      * @return <b>reference</b> to kids
117      */

118     public final TreeObjectList getChildNodes () {
119         return childList;
120     }
121     
122     
123     //
124
// read only
125
//
126

127     
128     /**
129      */

130     protected void setReadOnly (boolean newReadOnly) {
131         super.setReadOnly (newReadOnly);
132         
133         childList.setReadOnly (newReadOnly);
134     }
135     
136     
137     //
138
// Children manipulation
139
//
140

141     /**
142      */

143     public final TreeChild getFirstChild () {
144         if ( childList.size () == 0 ) {
145             return null;
146         }
147         return (TreeChild)childList.get (0);
148     }
149     
150     /**
151      */

152     public final TreeChild getLastChild () {
153         if ( childList.size () == 0 ) {
154             return null;
155         }
156         return (TreeChild)childList.get (childList.size () - 1);
157     }
158     
159     
160     /**
161      * @throws ReadOnlyException
162      */

163     public final void insertBefore (TreeChild newChild, TreeChild refChild) throws ReadOnlyException {
164         /*
165         if (refChild == null) {
166             // For semantic compatibility with DOM.
167             appendChild(newChild);
168             return;
169         }
170          */

171         childList.checkReadOnly ();
172         int index = childList.indexOf (refChild);
173         if (index < 0) {
174             return;
175         }
176         childList.add (index, newChild);
177     }
178     
179     /**
180      * @throws ReadOnlyException
181      */

182     public final void replaceChild (TreeChild oldChild, TreeChild newChild) throws ReadOnlyException {
183         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\nTreeParentNode::replaceChild: oldChild = " + oldChild); // NOI18N
184
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" ::replaceChild: newChild = " + newChild); // NOI18N
185

186         childList.checkReadOnly ();
187         int index = childList.indexOf (oldChild);
188         
189         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" ::replaceChild: childList [oldChild] = " + index); // NOI18N
190

191         if (index < 0) {
192             return;
193         }
194         childList.set (index, newChild);
195     }
196     
197     /**
198      * @throws ReadOnlyException
199      */

200     public final void removeChild (TreeChild oldChild) throws ReadOnlyException {
201         childList.checkReadOnly ();
202         childList.remove (oldChild);
203     }
204     
205     /**
206      * @throws ReadOnlyException
207      */

208     public final void appendChild (TreeChild newChild) throws ReadOnlyException {
209         childList.checkReadOnly ();
210         childList.add (newChild);
211     }
212     
213     
214     /**
215      * Insert child at specified position and set its parent and owner document.
216      * @throws ReadOnlyException
217      */

218     public final void insertChildAt (TreeChild child, int index) throws ReadOnlyException {
219         childList.checkReadOnly ();
220         childList.add (index, child);
221     }
222     
223     
224     /**
225      */

226     public final int indexOf (TreeChild node) {
227         return childList.indexOf (node);
228     }
229     
230     
231     /**
232      */

233     public final TreeChild item (int index) {
234         return (TreeChild)childList.get (index);
235     }
236     
237     /**
238      */

239     public final int getChildrenNumber () {
240         return (childList.size ());
241     }
242     
243     /**
244      */

245     public final boolean hasChildNodes () {
246         return (!!! childList.isEmpty ());
247     }
248     
249     /**
250      */

251     public final boolean hasChildNodes (Class JavaDoc childClass) {
252         return hasChildNodes (childClass, false);
253     }
254     
255     /**
256      */

257     public boolean hasChildNodes (Class JavaDoc childClass, boolean recursive) {
258         Iterator JavaDoc it = getChildNodes ().iterator ();
259         while (it.hasNext ()) {
260             TreeChild child = (TreeChild)it.next ();
261             
262             // add matching leaf node
263

264             if (childClass == null || childClass.isAssignableFrom (child.getClass ())) {
265                 return true;
266             }
267             
268             // do recursive descent into kids
269

270             if ( recursive && (child instanceof TreeParentNode) ) {
271                 if ( ((TreeParentNode)child).hasChildNodes (childClass, true) == true ) {
272                     return true;
273                 }
274             }
275         }
276         return false;
277     }
278     
279     /**
280      * @return copy collection containing references
281      */

282     public final Collection JavaDoc getChildNodes (Class JavaDoc childClass) {
283         return getChildNodes (childClass, false);
284     }
285     
286     /**
287      * @return copy collection containing references
288      */

289     public Collection JavaDoc getChildNodes (Class JavaDoc childClass, boolean recursive) {
290         
291         // new RuntimeException(getClass().toString() + ".getChildNodes(" + childClass.toString() + "," + recursive + ")").printStackTrace(); // NOI18N
292

293         Collection JavaDoc allChildNodes = new LinkedList JavaDoc ();
294         Iterator JavaDoc it = getChildNodes ().iterator ();
295         while (it.hasNext ()) {
296             TreeChild child = (TreeChild)it.next ();
297             
298             // add matching leaf node
299

300             if (childClass == null || childClass.isAssignableFrom (child.getClass ())) {
301                 allChildNodes.add (child);
302             }
303             
304             // do recursive descent into kids
305

306             if ( recursive && (child instanceof TreeParentNode) ) {
307                 allChildNodes.addAll (((TreeParentNode)child).getChildNodes (childClass, true));
308             }
309         }
310         return allChildNodes;
311     }
312     
313     
314     //
315
// TreeObjectList.ContentManager
316
//
317

318     /**
319      */

320     protected abstract TreeObjectList.ContentManager createChildListContentManager ();
321     
322     /**
323      *
324      */

325     protected abstract class ChildListContentManager extends TreeObjectList.ContentManager {
326         
327         /**
328          */

329         public void checkAssignableObject (Object JavaDoc obj) {
330             super.checkAssignableObject (obj);
331             checkAssignableClass (TreeChild.class, obj);
332         }
333         
334         /**
335          */

336         public void objectInserted (TreeObject obj) {
337             ((TreeChild)obj).setParentNode (TreeParentNode.this);
338             TreeParentNode.this.firePropertyChange (TreeParentNode.PROP_CHILD_LIST, TreeParentNode.this.childList, obj);
339         }
340         
341         /**
342          */

343         public void objectRemoved (TreeObject obj) {
344             ((TreeChild)obj).setParentNode (null);
345             TreeParentNode.this.firePropertyChange (TreeParentNode.PROP_CHILD_LIST, TreeParentNode.this.childList, obj);
346         }
347         
348         /**
349          */

350         public void orderChanged (int[] permutation) {
351             TreeParentNode.this.firePropertyChange (TreeParentNode.PROP_CHILD_LIST, TreeParentNode.this.childList, permutation);
352         }
353         
354     } // end: class ChildListContentManager
355

356 }
357
Popular Tags