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