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