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. package javax.swing.event;
  8. import java.util.EventObject;
  9. import javax.swing.tree.TreePath;
  10. /**
  11. * Encapsulates information describing changes to a tree model, and
  12. * used to notify tree model listeners of the change.
  13. * For more information and examples see
  14. * <a
  15. href="http://java.sun.com/docs/books/tutorial/uiswing/events/treemodellistener.html">How to Write a Tree Model Listener</a>,
  16. * a section in <em>The Java Tutorial.</em>
  17. * <p>
  18. * <strong>Warning:</strong>
  19. * Serialized objects of this class will not be compatible with
  20. * future Swing releases. The current serialization support is
  21. * appropriate for short term storage or RMI between applications running
  22. * the same version of Swing. As of 1.4, support for long term storage
  23. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  24. * has been added to the <code>java.beans</code> package.
  25. * Please see {@link java.beans.XMLEncoder}.
  26. *
  27. * @version 1.33 12/19/03
  28. * @author Rob Davis
  29. * @author Ray Ryan
  30. * @author Scott Violet
  31. */
  32. public class TreeModelEvent extends EventObject {
  33. /** Path to the parent of the nodes that have changed. */
  34. protected TreePath path;
  35. /** Indices identifying the position of where the children were. */
  36. protected int[] childIndices;
  37. /** Children that have been removed. */
  38. protected Object[] children;
  39. /**
  40. * Used to create an event when nodes have been changed, inserted, or
  41. * removed, identifying the path to the parent of the modified items as
  42. * an array of Objects. All of the modified objects are siblings which are
  43. * direct descendents (not grandchildren) of the specified parent.
  44. * The positions at which the inserts, deletes, or changes occurred are
  45. * specified by an array of <code>int</code>. The indexes in that array
  46. * must be in order, from lowest to highest.
  47. * <p>
  48. * For changes, the indexes in the model correspond exactly to the indexes
  49. * of items currently displayed in the UI. As a result, it is not really
  50. * critical if the indexes are not in their exact order. But after multiple
  51. * inserts or deletes, the items currently in the UI no longer correspond
  52. * to the items in the model. It is therefore critical to specify the
  53. * indexes properly for inserts and deletes.
  54. * <p>
  55. * For inserts, the indexes represent the <i>final</i> state of the tree,
  56. * after the inserts have occurred. Since the indexes must be specified in
  57. * order, the most natural processing methodology is to do the inserts
  58. * starting at the lowest index and working towards the highest. Accumulate
  59. * a Vector of <code>Integer</code> objects that specify the
  60. * insert-locations as you go, then convert the Vector to an
  61. * array of <code>int</code> to create the event. When the postition-index
  62. * equals zero, the node is inserted at the beginning of the list. When the
  63. * position index equals the size of the list, the node is "inserted" at
  64. * (appended to) the end of the list.
  65. * <p>
  66. * For deletes, the indexes represent the <i>initial</i> state of the tree,
  67. * before the deletes have occurred. Since the indexes must be specified in
  68. * order, the most natural processing methodology is to use a delete-counter.
  69. * Start by initializing the counter to zero and start work through the
  70. * list from lowest to higest. Every time you do a delete, add the current
  71. * value of the delete-counter to the index-position where the delete occurred,
  72. * and append the result to a Vector of delete-locations, using
  73. * <code>addElement()</code>. Then increment the delete-counter. The index
  74. * positions stored in the Vector therefore reflect the effects of all previous
  75. * deletes, so they represent each object's position in the initial tree.
  76. * (You could also start at the highest index and working back towards the
  77. * lowest, accumulating a Vector of delete-locations as you go using the
  78. * <code>insertElementAt(Integer, 0)</code>.) However you produce the Vector
  79. * of initial-positions, you then need to convert the Vector of <code>Integer</code>
  80. * objects to an array of <code>int</code> to create the event.
  81. * <p>
  82. * <b>Notes:</b><ul>
  83. * <li>Like the <code>insertNodeInto</code> method in the
  84. * <code>DefaultTreeModel</code> class, <code>insertElementAt</code>
  85. * appends to the <code>Vector</code> when the index matches the size
  86. * of the vector. So you can use <code>insertElementAt(Integer, 0)</code>
  87. * even when the vector is empty.
  88. * <ul>To create a node changed event for the root node, specify the parent
  89. * and the child indices as <code>null</code>.
  90. * </ul>
  91. *
  92. * @param source the Object responsible for generating the event (typically
  93. * the creator of the event object passes <code>this</code>
  94. * for its value)
  95. * @param path an array of Object identifying the path to the
  96. * parent of the modified item(s), where the first element
  97. * of the array is the Object stored at the root node and
  98. * the last element is the Object stored at the parent node
  99. * @param childIndices an array of <code>int</code> that specifies the
  100. * index values of the removed items. The indices must be
  101. * in sorted order, from lowest to highest
  102. * @param children an array of Object containing the inserted, removed, or
  103. * changed objects
  104. * @see TreePath
  105. */
  106. public TreeModelEvent(Object source, Object[] path, int[] childIndices,
  107. Object[] children)
  108. {
  109. this(source, new TreePath(path), childIndices, children);
  110. }
  111. /**
  112. * Used to create an event when nodes have been changed, inserted, or
  113. * removed, identifying the path to the parent of the modified items as
  114. * a TreePath object. For more information on how to specify the indexes
  115. * and objects, see
  116. * <code>TreeModelEvent(Object,Object[],int[],Object[])</code>.
  117. *
  118. * @param source the Object responsible for generating the event (typically
  119. * the creator of the event object passes <code>this</code>
  120. * for its value)
  121. * @param path a TreePath object that identifies the path to the
  122. * parent of the modified item(s)
  123. * @param childIndices an array of <code>int</code> that specifies the
  124. * index values of the modified items
  125. * @param children an array of Object containing the inserted, removed, or
  126. * changed objects
  127. *
  128. * @see #TreeModelEvent(Object,Object[],int[],Object[])
  129. */
  130. public TreeModelEvent(Object source, TreePath path, int[] childIndices,
  131. Object[] children)
  132. {
  133. super(source);
  134. this.path = path;
  135. this.childIndices = childIndices;
  136. this.children = children;
  137. }
  138. /**
  139. * Used to create an event when the node structure has changed in some way,
  140. * identifying the path to the root of a modified subtree as an array of
  141. * Objects. A structure change event might involve nodes swapping position,
  142. * for example, or it might encapsulate multiple inserts and deletes in the
  143. * subtree stemming from the node, where the changes may have taken place at
  144. * different levels of the subtree.
  145. * <blockquote>
  146. * <b>Note:</b><br>
  147. * JTree collapses all nodes under the specified node, so that only its
  148. * immediate children are visible.
  149. * </blockquote>
  150. *
  151. * @param source the Object responsible for generating the event (typically
  152. * the creator of the event object passes <code>this</code>
  153. * for its value)
  154. * @param path an array of Object identifying the path to the root of the
  155. * modified subtree, where the first element of the array is
  156. * the object stored at the root node and the last element
  157. * is the object stored at the changed node
  158. * @see TreePath
  159. */
  160. public TreeModelEvent(Object source, Object[] path)
  161. {
  162. this(source, new TreePath(path));
  163. }
  164. /**
  165. * Used to create an event when the node structure has changed in some way,
  166. * identifying the path to the root of the modified subtree as a TreePath
  167. * object. For more information on this event specification, see
  168. * <code>TreeModelEvent(Object,Object[])</code>.
  169. *
  170. * @param source the Object responsible for generating the event (typically
  171. * the creator of the event object passes <code>this</code>
  172. * for its value)
  173. * @param path a TreePath object that identifies the path to the
  174. * change. In the DefaultTreeModel,
  175. * this object contains an array of user-data objects,
  176. * but a subclass of TreePath could use some totally
  177. * different mechanism -- for example, a node ID number
  178. *
  179. * @see #TreeModelEvent(Object,Object[])
  180. */
  181. public TreeModelEvent(Object source, TreePath path)
  182. {
  183. super(source);
  184. this.path = path;
  185. this.childIndices = new int[0];
  186. }
  187. /**
  188. * For all events, except treeStructureChanged,
  189. * returns the parent of the changed nodes.
  190. * For treeStructureChanged events, returns the ancestor of the
  191. * structure that has changed. This and
  192. * <code>getChildIndices</code> are used to get a list of the effected
  193. * nodes.
  194. * <p>
  195. * The one exception to this is a treeNodesChanged event that is to
  196. * identify the root, in which case this will return the root
  197. * and <code>getChildIndices</code> will return null.
  198. *
  199. * @return the TreePath used in identifying the changed nodes.
  200. * @see TreePath#getLastPathComponent
  201. */
  202. public TreePath getTreePath() { return path; }
  203. /**
  204. * Convenience method to get the array of objects from the TreePath
  205. * instance that this event wraps.
  206. *
  207. * @return an array of Objects, where the first Object is the one
  208. * stored at the root and the last object is the one
  209. * stored at the node identified by the path
  210. */
  211. public Object[] getPath() {
  212. if(path != null)
  213. return path.getPath();
  214. return null;
  215. }
  216. /**
  217. * Returns the objects that are children of the node identified by
  218. * <code>getPath</code> at the locations specified by
  219. * <code>getChildIndices</code>. If this is a removal event the
  220. * returned objects are no longer children of the parent node.
  221. *
  222. * @return an array of Object containing the children specified by
  223. * the event
  224. * @see #getPath
  225. * @see #getChildIndices
  226. */
  227. public Object[] getChildren() {
  228. if(children != null) {
  229. int cCount = children.length;
  230. Object[] retChildren = new Object[cCount];
  231. System.arraycopy(children, 0, retChildren, 0, cCount);
  232. return retChildren;
  233. }
  234. return null;
  235. }
  236. /**
  237. * Returns the values of the child indexes. If this is a removal event
  238. * the indexes point to locations in the initial list where items
  239. * were removed. If it is an insert, the indices point to locations
  240. * in the final list where the items were added. For node changes,
  241. * the indices point to the locations of the modified nodes.
  242. *
  243. * @return an array of <code>int</code> containing index locations for
  244. * the children specified by the event
  245. */
  246. public int[] getChildIndices() {
  247. if(childIndices != null) {
  248. int cCount = childIndices.length;
  249. int[] retArray = new int[cCount];
  250. System.arraycopy(childIndices, 0, retArray, 0, cCount);
  251. return retArray;
  252. }
  253. return null;
  254. }
  255. /**
  256. * Returns a string that displays and identifies this object's
  257. * properties.
  258. *
  259. * @return a String representation of this object
  260. */
  261. public String toString() {
  262. StringBuffer retBuffer = new StringBuffer();
  263. retBuffer.append(getClass().getName() + " " +
  264. Integer.toString(hashCode()));
  265. if(path != null)
  266. retBuffer.append(" path " + path);
  267. if(childIndices != null) {
  268. retBuffer.append(" indices [ ");
  269. for(int counter = 0; counter < childIndices.length; counter++)
  270. retBuffer.append(Integer.toString(childIndices[counter])+ " ");
  271. retBuffer.append("]");
  272. }
  273. if(children != null) {
  274. retBuffer.append(" children [ ");
  275. for(int counter = 0; counter < children.length; counter++)
  276. retBuffer.append(children[counter] + " ");
  277. retBuffer.append("]");
  278. }
  279. return retBuffer.toString();
  280. }
  281. }