1. /*
  2. * @(#)TreeSelectionEvent.java 1.25 03/01/23
  3. *
  4. * Copyright 2003 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. * An event that characterizes a change in the current
  12. * selection. The change is based on any number of paths.
  13. * TreeSelectionListeners will generally query the source of
  14. * the event for the new selected status of each potentially
  15. * changed row.
  16. * <p>
  17. * <strong>Warning:</strong>
  18. * Serialized objects of this class will not be compatible with
  19. * future Swing releases. The current serialization support is
  20. * appropriate for short term storage or RMI between applications running
  21. * the same version of Swing. As of 1.4, support for long term storage
  22. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  23. * has been added to the <code>java.beans</code> package.
  24. * Please see {@link java.beans.XMLEncoder}.
  25. *
  26. * @see TreeSelectionListener
  27. * @see javax.swing.tree.TreeSelectionModel
  28. *
  29. * @version 1.25 01/23/03
  30. * @author Scott Violet
  31. */
  32. public class TreeSelectionEvent extends EventObject
  33. {
  34. /** Paths this event represents. */
  35. protected TreePath[] paths;
  36. /** For each path identifies if that is path is in fact new. */
  37. protected boolean[] areNew;
  38. /** leadSelectionPath before the paths changed, may be null. */
  39. protected TreePath oldLeadSelectionPath;
  40. /** leadSelectionPath after the paths changed, may be null. */
  41. protected TreePath newLeadSelectionPath;
  42. /**
  43. * Represents a change in the selection of a TreeSelectionModel.
  44. * paths identifies the paths that have been either added or
  45. * removed from the selection.
  46. *
  47. * @param source source of event
  48. * @param paths the paths that have changed in the selection
  49. */
  50. public TreeSelectionEvent(Object source, TreePath[] paths,
  51. boolean[] areNew, TreePath oldLeadSelectionPath,
  52. TreePath newLeadSelectionPath)
  53. {
  54. super(source);
  55. this.paths = paths;
  56. this.areNew = areNew;
  57. this.oldLeadSelectionPath = oldLeadSelectionPath;
  58. this.newLeadSelectionPath = newLeadSelectionPath;
  59. }
  60. /**
  61. * Represents a change in the selection of a TreeSelectionModel.
  62. * path identifies the path that have been either added or
  63. * removed from the selection.
  64. *
  65. * @param source source of event
  66. * @param path the path that has changed in the selection
  67. * @param isNew whether or not the path is new to the selection, false
  68. * means path was removed from the selection.
  69. */
  70. public TreeSelectionEvent(Object source, TreePath path, boolean isNew,
  71. TreePath oldLeadSelectionPath,
  72. TreePath newLeadSelectionPath)
  73. {
  74. super(source);
  75. paths = new TreePath[1];
  76. paths[0] = path;
  77. areNew = new boolean[1];
  78. areNew[0] = isNew;
  79. this.oldLeadSelectionPath = oldLeadSelectionPath;
  80. this.newLeadSelectionPath = newLeadSelectionPath;
  81. }
  82. /**
  83. * Returns the paths that have been added or removed from the
  84. * selection.
  85. */
  86. public TreePath[] getPaths()
  87. {
  88. int numPaths;
  89. TreePath[] retPaths;
  90. numPaths = paths.length;
  91. retPaths = new TreePath[numPaths];
  92. System.arraycopy(paths, 0, retPaths, 0, numPaths);
  93. return retPaths;
  94. }
  95. /**
  96. * Returns the first path element.
  97. */
  98. public TreePath getPath()
  99. {
  100. return paths[0];
  101. }
  102. /**
  103. * Returns true if the first path element has been added to the
  104. * selection, a return value of false means the first path has been
  105. * removed from the selection.
  106. */
  107. public boolean isAddedPath() {
  108. return areNew[0];
  109. }
  110. /**
  111. * Returns true if the path identified by path was added to the
  112. * selection. A return value of false means the path was in the
  113. * selection but is no longer in the selection. This will raise if
  114. * path is not one of the paths identified by this event.
  115. */
  116. public boolean isAddedPath(TreePath path) {
  117. for(int counter = paths.length - 1; counter >= 0; counter--)
  118. if(paths[counter].equals(path))
  119. return areNew[counter];
  120. throw new IllegalArgumentException("path is not a path identified by the TreeSelectionEvent");
  121. }
  122. /**
  123. * Returns true if the path identified by <code>index</code> was added to
  124. * the selection. A return value of false means the path was in the
  125. * selection but is no longer in the selection. This will raise if
  126. * index < 0 || >= <code>getPaths</code>.length.
  127. *
  128. * @since 1.3
  129. */
  130. public boolean isAddedPath(int index) {
  131. if (paths == null || index < 0 || index >= paths.length) {
  132. throw new IllegalArgumentException("index is beyond range of added paths identified by TreeSelectionEvent");
  133. }
  134. return areNew[index];
  135. }
  136. /**
  137. * Returns the path that was previously the lead path.
  138. */
  139. public TreePath getOldLeadSelectionPath() {
  140. return oldLeadSelectionPath;
  141. }
  142. /**
  143. * Returns the current lead path.
  144. */
  145. public TreePath getNewLeadSelectionPath() {
  146. return newLeadSelectionPath;
  147. }
  148. /**
  149. * Returns a copy of the receiver, but with the source being newSource.
  150. */
  151. public Object cloneWithSource(Object newSource) {
  152. // Fix for IE bug - crashing
  153. return new TreeSelectionEvent(newSource, paths,areNew,
  154. oldLeadSelectionPath,
  155. newLeadSelectionPath);
  156. }
  157. }