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