Source code: javatools/swing/tree/IndexedTypedTreeNode.java
1 /*
2 * IndexedTypedTreeNode.java
3 *
4 * Created on 17 ottobre 2002, 11.44
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.swing.tree;
26
27 import javatools.util.Clippable;
28 import javatools.swing.MoreSwingUtilities;
29
30 /** It is a node that is both typed (with a "type") and indexed (with an index).
31 * @author Antonio Petrelli
32 * @version 0.1.9
33 */
34 public class IndexedTypedTreeNode
35 extends javax.swing.tree.DefaultMutableTreeNode
36 implements Clippable {
37
38 /** Creates a new instance of IndexedTypedTreeNode */
39 public IndexedTypedTreeNode() {
40 expanded = false;
41 deleteWhenCut = false;
42 dupWhenClipped = true;
43 beingCut = false;
44 type = null;
45 ID = null;
46 tree = null;
47 }
48
49 /** Creates a new instance of IndexedTypedTreeNode
50 * @param userObject The string to be displayed in the tree.
51 */
52 public IndexedTypedTreeNode(Object userObject) {
53 super(userObject);
54 tree = null;
55 }
56
57 /** Creates a new IndexedTypedTreeNode.
58 * @param userObject The object to use.
59 * @param allowsChildren <CODE>true</CODE>: allows children;
60 * <CODE>false</CODE>: does not allow children.
61 */
62 public IndexedTypedTreeNode(Object userObject, boolean allowsChildren) {
63 super(userObject, allowsChildren);
64 expanded = false;
65 deleteWhenCut = false;
66 dupWhenClipped = true;
67 beingCut = false;
68 type = null;
69 ID = null;
70 tree = null;
71 }
72
73 /** Creates a new IndexedTypedTreeNode.
74 * @param pTree The tree that contains the node.
75 * @param userObject The string to be displayed in the tree.
76 */
77 public IndexedTypedTreeNode(javax.swing.JTree pTree, Object userObject) {
78 super(userObject);
79 tree = pTree;
80 }
81
82 public IndexedTypedTreeNode(Clippable clip) {
83 super(null);
84 ID = (Object[]) clip.getValue();
85 type = clip.getType();
86 tree = null;
87 }
88
89 public IndexedTypedTreeNode(Clippable clip, Object userObject) {
90 super(userObject);
91 ID = (Object[]) clip.getValue();
92 type = clip.getType();
93 tree = null;
94 }
95
96 /** Creates a new IndexedTypedTreeNode.
97 * @param pTree The tree that contains the node.
98 * @param userObject The string to be displayed in the tree.
99 * @param allowsChildren <CODE>true</CODE>: allows children;
100 * <CODE>false</CODE>: does not allow children.
101 */
102 public IndexedTypedTreeNode(javax.swing.JTree pTree, Object userObject,
103 boolean allowsChildren) {
104 super(userObject, allowsChildren);
105 expanded = false;
106 deleteWhenCut = false;
107 dupWhenClipped = true;
108 beingCut = false;
109 type = null;
110 ID = null;
111 tree = pTree;
112 }
113
114 /** Sets the expanded flag.
115 * @param value The value.
116 */
117 public void setExpanded(boolean value) {
118 expanded = value;
119 }
120
121 /** Returns the expanded flag.
122 * @return The flag.
123 */
124 public boolean isExpanded() {
125 return expanded;
126 }
127
128 /** Sets the ID for the node.
129 * @param pID An array of object representing the ID.
130 */
131 public void setID(Object[] pID) {
132 ID = pID;
133 }
134
135 /** Returns the ID.
136 * @return An array of objects representing the ID.
137 */
138 public Object[] getID() {
139 return ID;
140 }
141
142 /** Sets the type of node.
143 * @param pType The type.
144 */
145 public void setType(String pType) {
146 type = pType;
147 }
148
149 /** Returns the type of the node.
150 * @return The type.
151 */
152 public String getType() {
153 return type;
154 }
155
156 /** Attaches an object to this one.
157 * @param newSon The object to be attached.
158 */
159 public void attach(Clippable newSon) {
160 IndexedTypedTreeNode tempSon;
161 String sonType;
162
163 if (newSon instanceof IndexedTypedTreeNode) {
164 tempSon = (IndexedTypedTreeNode) newSon;
165 this.add(tempSon);
166 }
167 }
168
169 /** Copies this object into the clipboard.
170 * @return The object to be copied.
171 */
172 public Clippable copy() {
173 if (dupWhenClipped)
174 return this.duplicate();
175 else
176 return this;
177 }
178
179 /** Cuts this object into the clipboard.
180 * @return The object to be cut.
181 */
182 public Clippable cut() {
183 beingCut = true;
184 if (deleteWhenCut)
185 this.removeFromParent();
186 return this;
187 }
188
189 /** Returns the value of this object.
190 * @return The value of this object (i.e. the printed string).
191 */
192 public Object getValue() {
193 return ID;
194 }
195
196 /** Pastes this object into another.
197 * @param newFather The object to paste this object into.
198 */
199 public void paste(Clippable newFather) {
200 if (beingCut) {
201 if (!deleteWhenCut)
202 this.removeFromParent();
203 beingCut = false;
204 }
205 }
206
207 /** Checks whether an object can be attached to this one.
208 * @param newSon The object that is going to be attached to this one.
209 * @return <CODE>true</CODE>: the object can be attached;
210 * <CODE>false</CODE>: otherwise.
211 */
212 public boolean canBeAttached(Clippable newSon) {
213 return true;
214 }
215
216 /** Duplicates (clones) this object.
217 * @return The newly created object.
218 */
219 public Clippable duplicate() {
220 IndexedTypedTreeNode tempNode;
221
222 tempNode = new IndexedTypedTreeNode(this.getUserObject(), this.getAllowsChildren());
223 tempNode.ID= ID;
224 tempNode.type = type;
225 tempNode.beingCut = false;
226 return tempNode;
227 }
228
229 /** Resets the status of this object.
230 */
231 public void unclip() {
232 beingCut = false;
233 }
234
235 /** Checks whether this object can be cut or not.
236 * @return <CODE>true</CODE>: the object can be cut;
237 * <CODE>false</CODE>: otherwise.
238 */
239 public boolean canBeCut() {
240 return true;
241 }
242
243 /** Checks whether this object can be copied or not.
244 * @return <CODE>true</CODE>: the object can be copied;
245 * <CODE>false</CODE>: otherwise.
246 */
247 public boolean canBeCopied() {
248 return true;
249 }
250
251 /** Sets the property for "deleting when cut".
252 * @param value <CODE>true</CODE>: this node deletes itself (removes itself from its parent)
253 * when cut;
254 * <CODE>false</CODE>: the opposite.
255 */
256 public void setDeleteWhenCut(boolean value) {
257 deleteWhenCut = value;
258 }
259
260 /** Checks whether the node will be deleted when cut.
261 * @return <CODE>true</CODE>: this node deletes itself (removes itself from its parent)
262 * when cut;
263 * <CODE>false</CODE>: the opposite.
264 */
265 public boolean getDeleteWhenCut() {
266 return deleteWhenCut;
267 }
268
269 /** Sets the "duplicate when clipped" property.
270 * @param value <CODE>true</CODE>: the node duplicates itself when clipped (cut or copied);
271 * <CODE>false</CODE>: the node returns itself.
272 */
273 public void setDuplicateWhenClipped(boolean value) {
274 dupWhenClipped = value;
275 }
276
277 /** Checks whether the node will be duplicated when clipped.
278 * @return <CODE>true</CODE>: the node duplicates itself when clipped (cut or copied);
279 * <CODE>false</CODE>: the node returns itself.
280 */
281 public boolean getDuplicateWhenClipped() {
282 return dupWhenClipped;
283 }
284
285 /** Returns the tree that contains the node.
286 * @return The object representing the tree containing the node.
287 */
288 public Object getPlace() {
289 return tree;
290 }
291
292 /** Sets the tree that contains the node (and all of his subnodes).
293 * @param place The tree that contains the node.
294 */
295 public void setPlace(Object place) {
296 if (place instanceof javax.swing.JTree)
297 tree = (javax.swing.JTree) place;
298 else
299 tree = null;
300 setPlaceForChildren(tree);
301 }
302
303 /** Adds a child to the node.
304 * @param newChild The node to add.
305 */
306 public void add(IndexedTypedTreeNode newChild) {
307 newChild.setPlace(tree);
308 super.add(newChild);
309 }
310
311 /** Inserts a child to the node.
312 * @param newChild The child to be added.
313 * @param childIndex The position in the child list.
314 */
315 public void insert(IndexedTypedTreeNode newChild, int childIndex) {
316 newChild.setPlace(tree);
317 super.insert(newChild, childIndex);
318 }
319
320 /** Removes a child.
321 * @param childIndex The index of the child to remove.
322 */
323 public void remove(int childIndex) {
324 conditionalSetPlace(getChildAt(childIndex), null);
325 super.remove(childIndex);
326 }
327
328 /** Removes a specified child.
329 * @param aChild The child to remove.
330 */
331 public void remove(IndexedTypedTreeNode aChild) {
332 if (isNodeChild(aChild))
333 aChild.setPlace(null);
334 super.remove(aChild);
335 }
336
337 /** Removes all children from this node.
338 */
339 public void removeAllChildren() {
340 setPlaceForChildren(null);
341 super.removeAllChildren();
342 }
343
344 /** Removes this node from his parent node.
345 */
346 public void removeFromParent() {
347 setPlaceForChildren(null);
348 super.removeFromParent();
349 }
350
351 /** Reloads the node.
352 */
353 public void reload() {
354 if (tree != null)
355 MoreSwingUtilities.reloadTree(tree, this);
356 }
357
358 /** Expands the node.
359 */
360 public void expand() {
361 if (tree != null)
362 MoreSwingUtilities.expandLater(tree, this);
363 }
364
365 /** <CODE>true</CODE>: the node is expanded;
366 * <CODE>false</CODE>: the node is not expanded.
367 */
368 protected boolean expanded;
369 /** <CODE>true</CODE>: this node deletes itself (removes itself from its parent)
370 * when cut;
371 * <CODE>false</CODE>: the opposite.
372 */
373 protected boolean deleteWhenCut;
374 /** <CODE>true</CODE>: the node duplicates itself when clipped (cut or copied);
375 * <CODE>false</CODE>: the node returns itself.
376 */
377 protected boolean dupWhenClipped;
378 /** <CODE>true</CODE>: the node is being cut;
379 * <CODE>false</CODE>: otherwise.
380 */
381 protected boolean beingCut;
382 /** The ID of this node.
383 */
384 protected Object[] ID;
385 /** The type of this node.
386 */
387 protected String type;
388
389 /** The tree in which the node is contained.
390 */
391 protected javax.swing.JTree tree;
392
393 /** Sets a node's tree if it is really a JTree. Otherwise it does nothing.
394 * @param node The node to modify.
395 * @param place The new tree.
396 */
397 protected void conditionalSetPlace(javax.swing.tree.TreeNode node, Object place) {
398 if (node instanceof IndexedTypedTreeNode)
399 ((IndexedTypedTreeNode) node).setPlace(place);
400 }
401
402 /** Sets the tree for all children of this node.
403 * @param place The tree to be set.
404 */
405 protected void setPlaceForChildren(Object place) {
406 int i, numChildren;
407
408 numChildren = getChildCount();
409 for (i=0; i < numChildren; i++)
410 conditionalSetPlace(getChildAt(i), place);
411 }
412 }