2
|
1 |
/*
|
|
2 |
* Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package javax.swing.tree;
|
|
27 |
|
|
28 |
import java.util.*;
|
|
29 |
import java.beans.ConstructorProperties;
|
|
30 |
import java.io.*;
|
|
31 |
import javax.swing.event.*;
|
|
32 |
|
|
33 |
/**
|
|
34 |
* A simple tree data model that uses TreeNodes.
|
|
35 |
* For further information and examples that use DefaultTreeModel,
|
|
36 |
* see <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html">How to Use Trees</a>
|
|
37 |
* in <em>The Java Tutorial.</em>
|
|
38 |
* <p>
|
|
39 |
* <strong>Warning:</strong>
|
|
40 |
* Serialized objects of this class will not be compatible with
|
|
41 |
* future Swing releases. The current serialization support is
|
|
42 |
* appropriate for short term storage or RMI between applications running
|
|
43 |
* the same version of Swing. As of 1.4, support for long term storage
|
|
44 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
|
45 |
* has been added to the <code>java.beans</code> package.
|
|
46 |
* Please see {@link java.beans.XMLEncoder}.
|
|
47 |
*
|
|
48 |
* @author Rob Davis
|
|
49 |
* @author Ray Ryan
|
|
50 |
* @author Scott Violet
|
|
51 |
*/
|
|
52 |
public class DefaultTreeModel implements Serializable, TreeModel {
|
|
53 |
/** Root of the tree. */
|
|
54 |
protected TreeNode root;
|
|
55 |
/** Listeners. */
|
|
56 |
protected EventListenerList listenerList = new EventListenerList();
|
|
57 |
/**
|
|
58 |
* Determines how the <code>isLeaf</code> method figures
|
|
59 |
* out if a node is a leaf node. If true, a node is a leaf
|
|
60 |
* node if it does not allow children. (If it allows
|
|
61 |
* children, it is not a leaf node, even if no children
|
|
62 |
* are present.) That lets you distinguish between <i>folder</i>
|
|
63 |
* nodes and <i>file</i> nodes in a file system, for example.
|
|
64 |
* <p>
|
|
65 |
* If this value is false, then any node which has no
|
|
66 |
* children is a leaf node, and any node may acquire
|
|
67 |
* children.
|
|
68 |
*
|
|
69 |
* @see TreeNode#getAllowsChildren
|
|
70 |
* @see TreeModel#isLeaf
|
|
71 |
* @see #setAsksAllowsChildren
|
|
72 |
*/
|
|
73 |
protected boolean asksAllowsChildren;
|
|
74 |
|
|
75 |
|
|
76 |
/**
|
|
77 |
* Creates a tree in which any node can have children.
|
|
78 |
*
|
|
79 |
* @param root a TreeNode object that is the root of the tree
|
|
80 |
* @see #DefaultTreeModel(TreeNode, boolean)
|
|
81 |
*/
|
|
82 |
@ConstructorProperties({"root"})
|
|
83 |
public DefaultTreeModel(TreeNode root) {
|
|
84 |
this(root, false);
|
|
85 |
}
|
|
86 |
|
|
87 |
/**
|
|
88 |
* Creates a tree specifying whether any node can have children,
|
|
89 |
* or whether only certain nodes can have children.
|
|
90 |
*
|
|
91 |
* @param root a TreeNode object that is the root of the tree
|
|
92 |
* @param asksAllowsChildren a boolean, false if any node can
|
|
93 |
* have children, true if each node is asked to see if
|
|
94 |
* it can have children
|
|
95 |
* @see #asksAllowsChildren
|
|
96 |
*/
|
|
97 |
public DefaultTreeModel(TreeNode root, boolean asksAllowsChildren) {
|
|
98 |
super();
|
|
99 |
this.root = root;
|
|
100 |
this.asksAllowsChildren = asksAllowsChildren;
|
|
101 |
}
|
|
102 |
|
|
103 |
/**
|
|
104 |
* Sets whether or not to test leafness by asking getAllowsChildren()
|
|
105 |
* or isLeaf() to the TreeNodes. If newvalue is true, getAllowsChildren()
|
|
106 |
* is messaged, otherwise isLeaf() is messaged.
|
|
107 |
*/
|
|
108 |
public void setAsksAllowsChildren(boolean newValue) {
|
|
109 |
asksAllowsChildren = newValue;
|
|
110 |
}
|
|
111 |
|
|
112 |
/**
|
|
113 |
* Tells how leaf nodes are determined.
|
|
114 |
*
|
|
115 |
* @return true if only nodes which do not allow children are
|
|
116 |
* leaf nodes, false if nodes which have no children
|
|
117 |
* (even if allowed) are leaf nodes
|
|
118 |
* @see #asksAllowsChildren
|
|
119 |
*/
|
|
120 |
public boolean asksAllowsChildren() {
|
|
121 |
return asksAllowsChildren;
|
|
122 |
}
|
|
123 |
|
|
124 |
/**
|
|
125 |
* Sets the root to <code>root</code>. A null <code>root</code> implies
|
|
126 |
* the tree is to display nothing, and is legal.
|
|
127 |
*/
|
|
128 |
public void setRoot(TreeNode root) {
|
|
129 |
Object oldRoot = this.root;
|
|
130 |
this.root = root;
|
|
131 |
if (root == null && oldRoot != null) {
|
|
132 |
fireTreeStructureChanged(this, null);
|
|
133 |
}
|
|
134 |
else {
|
|
135 |
nodeStructureChanged(root);
|
|
136 |
}
|
|
137 |
}
|
|
138 |
|
|
139 |
/**
|
|
140 |
* Returns the root of the tree. Returns null only if the tree has
|
|
141 |
* no nodes.
|
|
142 |
*
|
|
143 |
* @return the root of the tree
|
|
144 |
*/
|
|
145 |
public Object getRoot() {
|
|
146 |
return root;
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* Returns the index of child in parent.
|
|
151 |
* If either the parent or child is <code>null</code>, returns -1.
|
|
152 |
* @param parent a note in the tree, obtained from this data source
|
|
153 |
* @param child the node we are interested in
|
|
154 |
* @return the index of the child in the parent, or -1
|
|
155 |
* if either the parent or the child is <code>null</code>
|
|
156 |
*/
|
|
157 |
public int getIndexOfChild(Object parent, Object child) {
|
|
158 |
if(parent == null || child == null)
|
|
159 |
return -1;
|
|
160 |
return ((TreeNode)parent).getIndex((TreeNode)child);
|
|
161 |
}
|
|
162 |
|
|
163 |
/**
|
|
164 |
* Returns the child of <I>parent</I> at index <I>index</I> in the parent's
|
|
165 |
* child array. <I>parent</I> must be a node previously obtained from
|
|
166 |
* this data source. This should not return null if <i>index</i>
|
|
167 |
* is a valid index for <i>parent</i> (that is <i>index</i> >= 0 &&
|
|
168 |
* <i>index</i> < getChildCount(<i>parent</i>)).
|
|
169 |
*
|
|
170 |
* @param parent a node in the tree, obtained from this data source
|
|
171 |
* @return the child of <I>parent</I> at index <I>index</I>
|
|
172 |
*/
|
|
173 |
public Object getChild(Object parent, int index) {
|
|
174 |
return ((TreeNode)parent).getChildAt(index);
|
|
175 |
}
|
|
176 |
|
|
177 |
/**
|
|
178 |
* Returns the number of children of <I>parent</I>. Returns 0 if the node
|
|
179 |
* is a leaf or if it has no children. <I>parent</I> must be a node
|
|
180 |
* previously obtained from this data source.
|
|
181 |
*
|
|
182 |
* @param parent a node in the tree, obtained from this data source
|
|
183 |
* @return the number of children of the node <I>parent</I>
|
|
184 |
*/
|
|
185 |
public int getChildCount(Object parent) {
|
|
186 |
return ((TreeNode)parent).getChildCount();
|
|
187 |
}
|
|
188 |
|
|
189 |
/**
|
|
190 |
* Returns whether the specified node is a leaf node.
|
|
191 |
* The way the test is performed depends on the
|
|
192 |
* <code>askAllowsChildren</code> setting.
|
|
193 |
*
|
|
194 |
* @param node the node to check
|
|
195 |
* @return true if the node is a leaf node
|
|
196 |
*
|
|
197 |
* @see #asksAllowsChildren
|
|
198 |
* @see TreeModel#isLeaf
|
|
199 |
*/
|
|
200 |
public boolean isLeaf(Object node) {
|
|
201 |
if(asksAllowsChildren)
|
|
202 |
return !((TreeNode)node).getAllowsChildren();
|
|
203 |
return ((TreeNode)node).isLeaf();
|
|
204 |
}
|
|
205 |
|
|
206 |
/**
|
|
207 |
* Invoke this method if you've modified the {@code TreeNode}s upon which
|
|
208 |
* this model depends. The model will notify all of its listeners that the
|
|
209 |
* model has changed.
|
|
210 |
*/
|
|
211 |
public void reload() {
|
|
212 |
reload(root);
|
|
213 |
}
|
|
214 |
|
|
215 |
/**
|
|
216 |
* This sets the user object of the TreeNode identified by path
|
|
217 |
* and posts a node changed. If you use custom user objects in
|
|
218 |
* the TreeModel you're going to need to subclass this and
|
|
219 |
* set the user object of the changed node to something meaningful.
|
|
220 |
*/
|
|
221 |
public void valueForPathChanged(TreePath path, Object newValue) {
|
|
222 |
MutableTreeNode aNode = (MutableTreeNode)path.getLastPathComponent();
|
|
223 |
|
|
224 |
aNode.setUserObject(newValue);
|
|
225 |
nodeChanged(aNode);
|
|
226 |
}
|
|
227 |
|
|
228 |
/**
|
|
229 |
* Invoked this to insert newChild at location index in parents children.
|
|
230 |
* This will then message nodesWereInserted to create the appropriate
|
|
231 |
* event. This is the preferred way to add children as it will create
|
|
232 |
* the appropriate event.
|
|
233 |
*/
|
|
234 |
public void insertNodeInto(MutableTreeNode newChild,
|
|
235 |
MutableTreeNode parent, int index){
|
|
236 |
parent.insert(newChild, index);
|
|
237 |
|
|
238 |
int[] newIndexs = new int[1];
|
|
239 |
|
|
240 |
newIndexs[0] = index;
|
|
241 |
nodesWereInserted(parent, newIndexs);
|
|
242 |
}
|
|
243 |
|
|
244 |
/**
|
|
245 |
* Message this to remove node from its parent. This will message
|
|
246 |
* nodesWereRemoved to create the appropriate event. This is the
|
|
247 |
* preferred way to remove a node as it handles the event creation
|
|
248 |
* for you.
|
|
249 |
*/
|
|
250 |
public void removeNodeFromParent(MutableTreeNode node) {
|
|
251 |
MutableTreeNode parent = (MutableTreeNode)node.getParent();
|
|
252 |
|
|
253 |
if(parent == null)
|
|
254 |
throw new IllegalArgumentException("node does not have a parent.");
|
|
255 |
|
|
256 |
int[] childIndex = new int[1];
|
|
257 |
Object[] removedArray = new Object[1];
|
|
258 |
|
|
259 |
childIndex[0] = parent.getIndex(node);
|
|
260 |
parent.remove(childIndex[0]);
|
|
261 |
removedArray[0] = node;
|
|
262 |
nodesWereRemoved(parent, childIndex, removedArray);
|
|
263 |
}
|
|
264 |
|
|
265 |
/**
|
|
266 |
* Invoke this method after you've changed how node is to be
|
|
267 |
* represented in the tree.
|
|
268 |
*/
|
|
269 |
public void nodeChanged(TreeNode node) {
|
|
270 |
if(listenerList != null && node != null) {
|
|
271 |
TreeNode parent = node.getParent();
|
|
272 |
|
|
273 |
if(parent != null) {
|
|
274 |
int anIndex = parent.getIndex(node);
|
|
275 |
if(anIndex != -1) {
|
|
276 |
int[] cIndexs = new int[1];
|
|
277 |
|
|
278 |
cIndexs[0] = anIndex;
|
|
279 |
nodesChanged(parent, cIndexs);
|
|
280 |
}
|
|
281 |
}
|
|
282 |
else if (node == getRoot()) {
|
|
283 |
nodesChanged(node, null);
|
|
284 |
}
|
|
285 |
}
|
|
286 |
}
|
|
287 |
|
|
288 |
/**
|
|
289 |
* Invoke this method if you've modified the {@code TreeNode}s upon which
|
|
290 |
* this model depends. The model will notify all of its listeners that the
|
|
291 |
* model has changed below the given node.
|
|
292 |
*
|
|
293 |
* @param node the node below which the model has changed
|
|
294 |
*/
|
|
295 |
public void reload(TreeNode node) {
|
|
296 |
if(node != null) {
|
|
297 |
fireTreeStructureChanged(this, getPathToRoot(node), null, null);
|
|
298 |
}
|
|
299 |
}
|
|
300 |
|
|
301 |
/**
|
|
302 |
* Invoke this method after you've inserted some TreeNodes into
|
|
303 |
* node. childIndices should be the index of the new elements and
|
|
304 |
* must be sorted in ascending order.
|
|
305 |
*/
|
|
306 |
public void nodesWereInserted(TreeNode node, int[] childIndices) {
|
|
307 |
if(listenerList != null && node != null && childIndices != null
|
|
308 |
&& childIndices.length > 0) {
|
|
309 |
int cCount = childIndices.length;
|
|
310 |
Object[] newChildren = new Object[cCount];
|
|
311 |
|
|
312 |
for(int counter = 0; counter < cCount; counter++)
|
|
313 |
newChildren[counter] = node.getChildAt(childIndices[counter]);
|
|
314 |
fireTreeNodesInserted(this, getPathToRoot(node), childIndices,
|
|
315 |
newChildren);
|
|
316 |
}
|
|
317 |
}
|
|
318 |
|
|
319 |
/**
|
|
320 |
* Invoke this method after you've removed some TreeNodes from
|
|
321 |
* node. childIndices should be the index of the removed elements and
|
|
322 |
* must be sorted in ascending order. And removedChildren should be
|
|
323 |
* the array of the children objects that were removed.
|
|
324 |
*/
|
|
325 |
public void nodesWereRemoved(TreeNode node, int[] childIndices,
|
|
326 |
Object[] removedChildren) {
|
|
327 |
if(node != null && childIndices != null) {
|
|
328 |
fireTreeNodesRemoved(this, getPathToRoot(node), childIndices,
|
|
329 |
removedChildren);
|
|
330 |
}
|
|
331 |
}
|
|
332 |
|
|
333 |
/**
|
|
334 |
* Invoke this method after you've changed how the children identified by
|
|
335 |
* childIndicies are to be represented in the tree.
|
|
336 |
*/
|
|
337 |
public void nodesChanged(TreeNode node, int[] childIndices) {
|
|
338 |
if(node != null) {
|
|
339 |
if (childIndices != null) {
|
|
340 |
int cCount = childIndices.length;
|
|
341 |
|
|
342 |
if(cCount > 0) {
|
|
343 |
Object[] cChildren = new Object[cCount];
|
|
344 |
|
|
345 |
for(int counter = 0; counter < cCount; counter++)
|
|
346 |
cChildren[counter] = node.getChildAt
|
|
347 |
(childIndices[counter]);
|
|
348 |
fireTreeNodesChanged(this, getPathToRoot(node),
|
|
349 |
childIndices, cChildren);
|
|
350 |
}
|
|
351 |
}
|
|
352 |
else if (node == getRoot()) {
|
|
353 |
fireTreeNodesChanged(this, getPathToRoot(node), null, null);
|
|
354 |
}
|
|
355 |
}
|
|
356 |
}
|
|
357 |
|
|
358 |
/**
|
|
359 |
* Invoke this method if you've totally changed the children of
|
|
360 |
* node and its childrens children... This will post a
|
|
361 |
* treeStructureChanged event.
|
|
362 |
*/
|
|
363 |
public void nodeStructureChanged(TreeNode node) {
|
|
364 |
if(node != null) {
|
|
365 |
fireTreeStructureChanged(this, getPathToRoot(node), null, null);
|
|
366 |
}
|
|
367 |
}
|
|
368 |
|
|
369 |
/**
|
|
370 |
* Builds the parents of node up to and including the root node,
|
|
371 |
* where the original node is the last element in the returned array.
|
|
372 |
* The length of the returned array gives the node's depth in the
|
|
373 |
* tree.
|
|
374 |
*
|
|
375 |
* @param aNode the TreeNode to get the path for
|
|
376 |
*/
|
|
377 |
public TreeNode[] getPathToRoot(TreeNode aNode) {
|
|
378 |
return getPathToRoot(aNode, 0);
|
|
379 |
}
|
|
380 |
|
|
381 |
/**
|
|
382 |
* Builds the parents of node up to and including the root node,
|
|
383 |
* where the original node is the last element in the returned array.
|
|
384 |
* The length of the returned array gives the node's depth in the
|
|
385 |
* tree.
|
|
386 |
*
|
|
387 |
* @param aNode the TreeNode to get the path for
|
|
388 |
* @param depth an int giving the number of steps already taken towards
|
|
389 |
* the root (on recursive calls), used to size the returned array
|
|
390 |
* @return an array of TreeNodes giving the path from the root to the
|
|
391 |
* specified node
|
|
392 |
*/
|
|
393 |
protected TreeNode[] getPathToRoot(TreeNode aNode, int depth) {
|
|
394 |
TreeNode[] retNodes;
|
|
395 |
// This method recurses, traversing towards the root in order
|
|
396 |
// size the array. On the way back, it fills in the nodes,
|
|
397 |
// starting from the root and working back to the original node.
|
|
398 |
|
|
399 |
/* Check for null, in case someone passed in a null node, or
|
|
400 |
they passed in an element that isn't rooted at root. */
|
|
401 |
if(aNode == null) {
|
|
402 |
if(depth == 0)
|
|
403 |
return null;
|
|
404 |
else
|
|
405 |
retNodes = new TreeNode[depth];
|
|
406 |
}
|
|
407 |
else {
|
|
408 |
depth++;
|
|
409 |
if(aNode == root)
|
|
410 |
retNodes = new TreeNode[depth];
|
|
411 |
else
|
|
412 |
retNodes = getPathToRoot(aNode.getParent(), depth);
|
|
413 |
retNodes[retNodes.length - depth] = aNode;
|
|
414 |
}
|
|
415 |
return retNodes;
|
|
416 |
}
|
|
417 |
|
|
418 |
//
|
|
419 |
// Events
|
|
420 |
//
|
|
421 |
|
|
422 |
/**
|
|
423 |
* Adds a listener for the TreeModelEvent posted after the tree changes.
|
|
424 |
*
|
|
425 |
* @see #removeTreeModelListener
|
|
426 |
* @param l the listener to add
|
|
427 |
*/
|
|
428 |
public void addTreeModelListener(TreeModelListener l) {
|
|
429 |
listenerList.add(TreeModelListener.class, l);
|
|
430 |
}
|
|
431 |
|
|
432 |
/**
|
|
433 |
* Removes a listener previously added with <B>addTreeModelListener()</B>.
|
|
434 |
*
|
|
435 |
* @see #addTreeModelListener
|
|
436 |
* @param l the listener to remove
|
|
437 |
*/
|
|
438 |
public void removeTreeModelListener(TreeModelListener l) {
|
|
439 |
listenerList.remove(TreeModelListener.class, l);
|
|
440 |
}
|
|
441 |
|
|
442 |
/**
|
|
443 |
* Returns an array of all the tree model listeners
|
|
444 |
* registered on this model.
|
|
445 |
*
|
|
446 |
* @return all of this model's <code>TreeModelListener</code>s
|
|
447 |
* or an empty
|
|
448 |
* array if no tree model listeners are currently registered
|
|
449 |
*
|
|
450 |
* @see #addTreeModelListener
|
|
451 |
* @see #removeTreeModelListener
|
|
452 |
*
|
|
453 |
* @since 1.4
|
|
454 |
*/
|
|
455 |
public TreeModelListener[] getTreeModelListeners() {
|
|
456 |
return (TreeModelListener[])listenerList.getListeners(
|
|
457 |
TreeModelListener.class);
|
|
458 |
}
|
|
459 |
|
|
460 |
/**
|
|
461 |
* Notifies all listeners that have registered interest for
|
|
462 |
* notification on this event type. The event instance
|
|
463 |
* is lazily created using the parameters passed into
|
|
464 |
* the fire method.
|
|
465 |
*
|
|
466 |
* @param source the source of the {@code TreeModelEvent};
|
|
467 |
* typically {@code this}
|
|
468 |
* @param path the path to the parent of the nodes that changed; use
|
|
469 |
* {@code null} to identify the root has changed
|
|
470 |
* @param childIndices the indices of the changed elements
|
|
471 |
* @param children the changed elements
|
|
472 |
*/
|
|
473 |
protected void fireTreeNodesChanged(Object source, Object[] path,
|
|
474 |
int[] childIndices,
|
|
475 |
Object[] children) {
|
|
476 |
// Guaranteed to return a non-null array
|
|
477 |
Object[] listeners = listenerList.getListenerList();
|
|
478 |
TreeModelEvent e = null;
|
|
479 |
// Process the listeners last to first, notifying
|
|
480 |
// those that are interested in this event
|
|
481 |
for (int i = listeners.length-2; i>=0; i-=2) {
|
|
482 |
if (listeners[i]==TreeModelListener.class) {
|
|
483 |
// Lazily create the event:
|
|
484 |
if (e == null)
|
|
485 |
e = new TreeModelEvent(source, path,
|
|
486 |
childIndices, children);
|
|
487 |
((TreeModelListener)listeners[i+1]).treeNodesChanged(e);
|
|
488 |
}
|
|
489 |
}
|
|
490 |
}
|
|
491 |
|
|
492 |
/**
|
|
493 |
* Notifies all listeners that have registered interest for
|
|
494 |
* notification on this event type. The event instance
|
|
495 |
* is lazily created using the parameters passed into
|
|
496 |
* the fire method.
|
|
497 |
*
|
|
498 |
* @param source the source of the {@code TreeModelEvent};
|
|
499 |
* typically {@code this}
|
|
500 |
* @param path the path to the parent the nodes were added to
|
|
501 |
* @param childIndices the indices of the new elements
|
|
502 |
* @param children the new elements
|
|
503 |
*/
|
|
504 |
protected void fireTreeNodesInserted(Object source, Object[] path,
|
|
505 |
int[] childIndices,
|
|
506 |
Object[] children) {
|
|
507 |
// Guaranteed to return a non-null array
|
|
508 |
Object[] listeners = listenerList.getListenerList();
|
|
509 |
TreeModelEvent e = null;
|
|
510 |
// Process the listeners last to first, notifying
|
|
511 |
// those that are interested in this event
|
|
512 |
for (int i = listeners.length-2; i>=0; i-=2) {
|
|
513 |
if (listeners[i]==TreeModelListener.class) {
|
|
514 |
// Lazily create the event:
|
|
515 |
if (e == null)
|
|
516 |
e = new TreeModelEvent(source, path,
|
|
517 |
childIndices, children);
|
|
518 |
((TreeModelListener)listeners[i+1]).treeNodesInserted(e);
|
|
519 |
}
|
|
520 |
}
|
|
521 |
}
|
|
522 |
|
|
523 |
/**
|
|
524 |
* Notifies all listeners that have registered interest for
|
|
525 |
* notification on this event type. The event instance
|
|
526 |
* is lazily created using the parameters passed into
|
|
527 |
* the fire method.
|
|
528 |
*
|
|
529 |
* @param source the source of the {@code TreeModelEvent};
|
|
530 |
* typically {@code this}
|
|
531 |
* @param path the path to the parent the nodes were removed from
|
|
532 |
* @param childIndices the indices of the removed elements
|
|
533 |
* @param children the removed elements
|
|
534 |
*/
|
|
535 |
protected void fireTreeNodesRemoved(Object source, Object[] path,
|
|
536 |
int[] childIndices,
|
|
537 |
Object[] children) {
|
|
538 |
// Guaranteed to return a non-null array
|
|
539 |
Object[] listeners = listenerList.getListenerList();
|
|
540 |
TreeModelEvent e = null;
|
|
541 |
// Process the listeners last to first, notifying
|
|
542 |
// those that are interested in this event
|
|
543 |
for (int i = listeners.length-2; i>=0; i-=2) {
|
|
544 |
if (listeners[i]==TreeModelListener.class) {
|
|
545 |
// Lazily create the event:
|
|
546 |
if (e == null)
|
|
547 |
e = new TreeModelEvent(source, path,
|
|
548 |
childIndices, children);
|
|
549 |
((TreeModelListener)listeners[i+1]).treeNodesRemoved(e);
|
|
550 |
}
|
|
551 |
}
|
|
552 |
}
|
|
553 |
|
|
554 |
/**
|
|
555 |
* Notifies all listeners that have registered interest for
|
|
556 |
* notification on this event type. The event instance
|
|
557 |
* is lazily created using the parameters passed into
|
|
558 |
* the fire method.
|
|
559 |
*
|
|
560 |
* @param source the source of the {@code TreeModelEvent};
|
|
561 |
* typically {@code this}
|
|
562 |
* @param path the path to the parent of the structure that has changed;
|
|
563 |
* use {@code null} to identify the root has changed
|
|
564 |
* @param childIndices the indices of the affected elements
|
|
565 |
* @param children the affected elements
|
|
566 |
*/
|
|
567 |
protected void fireTreeStructureChanged(Object source, Object[] path,
|
|
568 |
int[] childIndices,
|
|
569 |
Object[] children) {
|
|
570 |
// Guaranteed to return a non-null array
|
|
571 |
Object[] listeners = listenerList.getListenerList();
|
|
572 |
TreeModelEvent e = null;
|
|
573 |
// Process the listeners last to first, notifying
|
|
574 |
// those that are interested in this event
|
|
575 |
for (int i = listeners.length-2; i>=0; i-=2) {
|
|
576 |
if (listeners[i]==TreeModelListener.class) {
|
|
577 |
// Lazily create the event:
|
|
578 |
if (e == null)
|
|
579 |
e = new TreeModelEvent(source, path,
|
|
580 |
childIndices, children);
|
|
581 |
((TreeModelListener)listeners[i+1]).treeStructureChanged(e);
|
|
582 |
}
|
|
583 |
}
|
|
584 |
}
|
|
585 |
|
|
586 |
/**
|
|
587 |
* Notifies all listeners that have registered interest for
|
|
588 |
* notification on this event type. The event instance
|
|
589 |
* is lazily created using the parameters passed into
|
|
590 |
* the fire method.
|
|
591 |
*
|
|
592 |
* @param source the source of the {@code TreeModelEvent};
|
|
593 |
* typically {@code this}
|
|
594 |
* @param path the path to the parent of the structure that has changed;
|
|
595 |
* use {@code null} to identify the root has changed
|
|
596 |
*/
|
|
597 |
private void fireTreeStructureChanged(Object source, TreePath path) {
|
|
598 |
// Guaranteed to return a non-null array
|
|
599 |
Object[] listeners = listenerList.getListenerList();
|
|
600 |
TreeModelEvent e = null;
|
|
601 |
// Process the listeners last to first, notifying
|
|
602 |
// those that are interested in this event
|
|
603 |
for (int i = listeners.length-2; i>=0; i-=2) {
|
|
604 |
if (listeners[i]==TreeModelListener.class) {
|
|
605 |
// Lazily create the event:
|
|
606 |
if (e == null)
|
|
607 |
e = new TreeModelEvent(source, path);
|
|
608 |
((TreeModelListener)listeners[i+1]).treeStructureChanged(e);
|
|
609 |
}
|
|
610 |
}
|
|
611 |
}
|
|
612 |
|
|
613 |
/**
|
|
614 |
* Returns an array of all the objects currently registered
|
|
615 |
* as <code><em>Foo</em>Listener</code>s
|
|
616 |
* upon this model.
|
|
617 |
* <code><em>Foo</em>Listener</code>s are registered using the
|
|
618 |
* <code>add<em>Foo</em>Listener</code> method.
|
|
619 |
*
|
|
620 |
* <p>
|
|
621 |
*
|
|
622 |
* You can specify the <code>listenerType</code> argument
|
|
623 |
* with a class literal,
|
|
624 |
* such as
|
|
625 |
* <code><em>Foo</em>Listener.class</code>.
|
|
626 |
* For example, you can query a
|
|
627 |
* <code>DefaultTreeModel</code> <code>m</code>
|
|
628 |
* for its tree model listeners with the following code:
|
|
629 |
*
|
|
630 |
* <pre>TreeModelListener[] tmls = (TreeModelListener[])(m.getListeners(TreeModelListener.class));</pre>
|
|
631 |
*
|
|
632 |
* If no such listeners exist, this method returns an empty array.
|
|
633 |
*
|
|
634 |
* @param listenerType the type of listeners requested; this parameter
|
|
635 |
* should specify an interface that descends from
|
|
636 |
* <code>java.util.EventListener</code>
|
|
637 |
* @return an array of all objects registered as
|
|
638 |
* <code><em>Foo</em>Listener</code>s on this component,
|
|
639 |
* or an empty array if no such
|
|
640 |
* listeners have been added
|
|
641 |
* @exception ClassCastException if <code>listenerType</code>
|
|
642 |
* doesn't specify a class or interface that implements
|
|
643 |
* <code>java.util.EventListener</code>
|
|
644 |
*
|
|
645 |
* @see #getTreeModelListeners
|
|
646 |
*
|
|
647 |
* @since 1.3
|
|
648 |
*/
|
|
649 |
public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
|
|
650 |
return listenerList.getListeners(listenerType);
|
|
651 |
}
|
|
652 |
|
|
653 |
// Serialization support.
|
|
654 |
private void writeObject(ObjectOutputStream s) throws IOException {
|
|
655 |
Vector values = new Vector();
|
|
656 |
|
|
657 |
s.defaultWriteObject();
|
|
658 |
// Save the root, if its Serializable.
|
|
659 |
if(root != null && root instanceof Serializable) {
|
|
660 |
values.addElement("root");
|
|
661 |
values.addElement(root);
|
|
662 |
}
|
|
663 |
s.writeObject(values);
|
|
664 |
}
|
|
665 |
|
|
666 |
private void readObject(ObjectInputStream s)
|
|
667 |
throws IOException, ClassNotFoundException {
|
|
668 |
s.defaultReadObject();
|
|
669 |
|
|
670 |
Vector values = (Vector)s.readObject();
|
|
671 |
int indexCounter = 0;
|
|
672 |
int maxCounter = values.size();
|
|
673 |
|
|
674 |
if(indexCounter < maxCounter && values.elementAt(indexCounter).
|
|
675 |
equals("root")) {
|
|
676 |
root = (TreeNode)values.elementAt(++indexCounter);
|
|
677 |
indexCounter++;
|
|
678 |
}
|
|
679 |
}
|
|
680 |
|
|
681 |
|
|
682 |
} // End of class DefaultTreeModel
|