KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > event > TreeModelEvent


1 /*
2  * @(#)TreeModelEvent.java 1.33 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.event;
9
10 import java.util.EventObject JavaDoc;
11 import javax.swing.tree.TreePath JavaDoc;
12
13
14 /**
15  * Encapsulates information describing changes to a tree model, and
16  * used to notify tree model listeners of the change.
17  * For more information and examples see
18  * <a
19  href="http://java.sun.com/docs/books/tutorial/uiswing/events/treemodellistener.html">How to Write a Tree Model Listener</a>,
20  * a section in <em>The Java Tutorial.</em>
21  * <p>
22  * <strong>Warning:</strong>
23  * Serialized objects of this class will not be compatible with
24  * future Swing releases. The current serialization support is
25  * appropriate for short term storage or RMI between applications running
26  * the same version of Swing. As of 1.4, support for long term storage
27  * of all JavaBeans<sup><font size="-2">TM</font></sup>
28  * has been added to the <code>java.beans</code> package.
29  * Please see {@link java.beans.XMLEncoder}.
30  *
31  * @version 1.33 12/19/03
32  * @author Rob Davis
33  * @author Ray Ryan
34  * @author Scott Violet
35  */

36 public class TreeModelEvent extends EventObject JavaDoc {
37     /** Path to the parent of the nodes that have changed. */
38     protected TreePath JavaDoc path;
39     /** Indices identifying the position of where the children were. */
40     protected int[] childIndices;
41     /** Children that have been removed. */
42     protected Object JavaDoc[] children;
43
44     /**
45      * Used to create an event when nodes have been changed, inserted, or
46      * removed, identifying the path to the parent of the modified items as
47      * an array of Objects. All of the modified objects are siblings which are
48      * direct descendents (not grandchildren) of the specified parent.
49      * The positions at which the inserts, deletes, or changes occurred are
50      * specified by an array of <code>int</code>. The indexes in that array
51      * must be in order, from lowest to highest.
52      * <p>
53      * For changes, the indexes in the model correspond exactly to the indexes
54      * of items currently displayed in the UI. As a result, it is not really
55      * critical if the indexes are not in their exact order. But after multiple
56      * inserts or deletes, the items currently in the UI no longer correspond
57      * to the items in the model. It is therefore critical to specify the
58      * indexes properly for inserts and deletes.
59      * <p>
60      * For inserts, the indexes represent the <i>final</i> state of the tree,
61      * after the inserts have occurred. Since the indexes must be specified in
62      * order, the most natural processing methodology is to do the inserts
63      * starting at the lowest index and working towards the highest. Accumulate
64      * a Vector of <code>Integer</code> objects that specify the
65      * insert-locations as you go, then convert the Vector to an
66      * array of <code>int</code> to create the event. When the postition-index
67      * equals zero, the node is inserted at the beginning of the list. When the
68      * position index equals the size of the list, the node is "inserted" at
69      * (appended to) the end of the list.
70      * <p>
71      * For deletes, the indexes represent the <i>initial</i> state of the tree,
72      * before the deletes have occurred. Since the indexes must be specified in
73      * order, the most natural processing methodology is to use a delete-counter.
74      * Start by initializing the counter to zero and start work through the
75      * list from lowest to higest. Every time you do a delete, add the current
76      * value of the delete-counter to the index-position where the delete occurred,
77      * and append the result to a Vector of delete-locations, using
78      * <code>addElement()</code>. Then increment the delete-counter. The index
79      * positions stored in the Vector therefore reflect the effects of all previous
80      * deletes, so they represent each object's position in the initial tree.
81      * (You could also start at the highest index and working back towards the
82      * lowest, accumulating a Vector of delete-locations as you go using the
83      * <code>insertElementAt(Integer, 0)</code>.) However you produce the Vector
84      * of initial-positions, you then need to convert the Vector of <code>Integer</code>
85      * objects to an array of <code>int</code> to create the event.
86      * <p>
87      * <b>Notes:</b><ul>
88      * <li>Like the <code>insertNodeInto</code> method in the
89      * <code>DefaultTreeModel</code> class, <code>insertElementAt</code>
90      * appends to the <code>Vector</code> when the index matches the size
91      * of the vector. So you can use <code>insertElementAt(Integer, 0)</code>
92      * even when the vector is empty.
93      * <ul>To create a node changed event for the root node, specify the parent
94      * and the child indices as <code>null</code>.
95      * </ul>
96      *
97      * @param source the Object responsible for generating the event (typically
98      * the creator of the event object passes <code>this</code>
99      * for its value)
100      * @param path an array of Object identifying the path to the
101      * parent of the modified item(s), where the first element
102      * of the array is the Object stored at the root node and
103      * the last element is the Object stored at the parent node
104      * @param childIndices an array of <code>int</code> that specifies the
105      * index values of the removed items. The indices must be
106      * in sorted order, from lowest to highest
107      * @param children an array of Object containing the inserted, removed, or
108      * changed objects
109      * @see TreePath
110      */

111     public TreeModelEvent(Object JavaDoc source, Object JavaDoc[] path, int[] childIndices,
112               Object JavaDoc[] children)
113     {
114     this(source, new TreePath JavaDoc(path), childIndices, children);
115     }
116
117     /**
118      * Used to create an event when nodes have been changed, inserted, or
119      * removed, identifying the path to the parent of the modified items as
120      * a TreePath object. For more information on how to specify the indexes
121      * and objects, see
122      * <code>TreeModelEvent(Object,Object[],int[],Object[])</code>.
123      *
124      * @param source the Object responsible for generating the event (typically
125      * the creator of the event object passes <code>this</code>
126      * for its value)
127      * @param path a TreePath object that identifies the path to the
128      * parent of the modified item(s)
129      * @param childIndices an array of <code>int</code> that specifies the
130      * index values of the modified items
131      * @param children an array of Object containing the inserted, removed, or
132      * changed objects
133      *
134      * @see #TreeModelEvent(Object,Object[],int[],Object[])
135      */

136     public TreeModelEvent(Object JavaDoc source, TreePath JavaDoc path, int[] childIndices,
137               Object JavaDoc[] children)
138     {
139     super(source);
140     this.path = path;
141     this.childIndices = childIndices;
142     this.children = children;
143     }
144
145     /**
146      * Used to create an event when the node structure has changed in some way,
147      * identifying the path to the root of a modified subtree as an array of
148      * Objects. A structure change event might involve nodes swapping position,
149      * for example, or it might encapsulate multiple inserts and deletes in the
150      * subtree stemming from the node, where the changes may have taken place at
151      * different levels of the subtree.
152      * <blockquote>
153      * <b>Note:</b><br>
154      * JTree collapses all nodes under the specified node, so that only its
155      * immediate children are visible.
156      * </blockquote>
157      *
158      * @param source the Object responsible for generating the event (typically
159      * the creator of the event object passes <code>this</code>
160      * for its value)
161      * @param path an array of Object identifying the path to the root of the
162      * modified subtree, where the first element of the array is
163      * the object stored at the root node and the last element
164      * is the object stored at the changed node
165      * @see TreePath
166      */

167     public TreeModelEvent(Object JavaDoc source, Object JavaDoc[] path)
168     {
169     this(source, new TreePath JavaDoc(path));
170     }
171
172     /**
173      * Used to create an event when the node structure has changed in some way,
174      * identifying the path to the root of the modified subtree as a TreePath
175      * object. For more information on this event specification, see
176      * <code>TreeModelEvent(Object,Object[])</code>.
177      *
178      * @param source the Object responsible for generating the event (typically
179      * the creator of the event object passes <code>this</code>
180      * for its value)
181      * @param path a TreePath object that identifies the path to the
182      * change. In the DefaultTreeModel,
183      * this object contains an array of user-data objects,
184      * but a subclass of TreePath could use some totally
185      * different mechanism -- for example, a node ID number
186      *
187      * @see #TreeModelEvent(Object,Object[])
188      */

189     public TreeModelEvent(Object JavaDoc source, TreePath JavaDoc path)
190     {
191     super(source);
192     this.path = path;
193     this.childIndices = new int[0];
194     }
195
196     /**
197      * For all events, except treeStructureChanged,
198      * returns the parent of the changed nodes.
199      * For treeStructureChanged events, returns the ancestor of the
200      * structure that has changed. This and
201      * <code>getChildIndices</code> are used to get a list of the effected
202      * nodes.
203      * <p>
204      * The one exception to this is a treeNodesChanged event that is to
205      * identify the root, in which case this will return the root
206      * and <code>getChildIndices</code> will return null.
207      *
208      * @return the TreePath used in identifying the changed nodes.
209      * @see TreePath#getLastPathComponent
210      */

211     public TreePath JavaDoc getTreePath() { return path; }
212
213     /**
214      * Convenience method to get the array of objects from the TreePath
215      * instance that this event wraps.
216      *
217      * @return an array of Objects, where the first Object is the one
218      * stored at the root and the last object is the one
219      * stored at the node identified by the path
220      */

221     public Object JavaDoc[] getPath() {
222     if(path != null)
223         return path.getPath();
224     return null;
225     }
226
227     /**
228      * Returns the objects that are children of the node identified by
229      * <code>getPath</code> at the locations specified by
230      * <code>getChildIndices</code>. If this is a removal event the
231      * returned objects are no longer children of the parent node.
232      *
233      * @return an array of Object containing the children specified by
234      * the event
235      * @see #getPath
236      * @see #getChildIndices
237      */

238     public Object JavaDoc[] getChildren() {
239     if(children != null) {
240         int cCount = children.length;
241         Object JavaDoc[] retChildren = new Object JavaDoc[cCount];
242
243         System.arraycopy(children, 0, retChildren, 0, cCount);
244         return retChildren;
245     }
246     return null;
247     }
248
249     /**
250      * Returns the values of the child indexes. If this is a removal event
251      * the indexes point to locations in the initial list where items
252      * were removed. If it is an insert, the indices point to locations
253      * in the final list where the items were added. For node changes,
254      * the indices point to the locations of the modified nodes.
255      *
256      * @return an array of <code>int</code> containing index locations for
257      * the children specified by the event
258      */

259     public int[] getChildIndices() {
260     if(childIndices != null) {
261         int cCount = childIndices.length;
262         int[] retArray = new int[cCount];
263
264         System.arraycopy(childIndices, 0, retArray, 0, cCount);
265         return retArray;
266     }
267     return null;
268     }
269
270     /**
271      * Returns a string that displays and identifies this object's
272      * properties.
273      *
274      * @return a String representation of this object
275      */

276     public String JavaDoc toString() {
277     StringBuffer JavaDoc retBuffer = new StringBuffer JavaDoc();
278
279     retBuffer.append(getClass().getName() + " " +
280              Integer.toString(hashCode()));
281     if(path != null)
282         retBuffer.append(" path " + path);
283     if(childIndices != null) {
284         retBuffer.append(" indices [ ");
285         for(int counter = 0; counter < childIndices.length; counter++)
286         retBuffer.append(Integer.toString(childIndices[counter])+ " ");
287         retBuffer.append("]");
288     }
289     if(children != null) {
290         retBuffer.append(" children [ ");
291         for(int counter = 0; counter < children.length; counter++)
292         retBuffer.append(children[counter] + " ");
293         retBuffer.append("]");
294     }
295     return retBuffer.toString();
296     }
297 }
298
Popular Tags