TreeSelectionEvent.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* TreeSelectionEvent.java --
  2. Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.swing.event;
  32. import java.util.EventObject;
  33. import javax.swing.tree.TreePath;
  34. import javax.swing.tree.TreeSelectionModel;
  35. /**
  36. * An event that carries information about a change to a
  37. * {@link TreeSelectionModel}.
  38. *
  39. * @see TreeSelectionListener
  40. *
  41. * @author Andrew Selkirk
  42. */
  43. public class TreeSelectionEvent extends EventObject
  44. {
  45. /**
  46. * The paths that have been added or removed from the selection.
  47. */
  48. protected TreePath[] paths;
  49. /**
  50. * Flags indicating if the paths were added (<code>true</code>) or removed
  51. * (<code>false</code>) from the selection.
  52. */
  53. protected boolean[] areNew;
  54. /**
  55. * The old lead selection path (may be <code>null</code>).
  56. */
  57. protected TreePath oldLeadSelectionPath;
  58. /**
  59. * The new lead selection path (may be <code>null</code>).
  60. */
  61. protected TreePath newLeadSelectionPath;
  62. /**
  63. * Creates a new <code>TreeSelectionEvent</code>.
  64. *
  65. * @param source the source (usually a {@link TreeSelectionModel},
  66. * <code>null</code> not permitted).
  67. * @param paths an array of the paths that have been added to or removed
  68. * from the selection.
  69. * @param areNew a flag for each path where <code>true</code> indicates the
  70. * corresponding path has been added to the selection and
  71. * <code>false</code> indicates the path has been removed.
  72. * @param oldLeadSelectionPath the old lead selection path (<code>null</code>
  73. * permitted).
  74. * @param newLeadSelectionPath the new lead selection path (<code>null</code>
  75. * permitted).
  76. *
  77. * @throws IllegalArgumentException if <code>source</code> is
  78. * <code>null</code>.
  79. */
  80. public TreeSelectionEvent(Object source, TreePath[] paths,
  81. boolean[] areNew, TreePath oldLeadSelectionPath,
  82. TreePath newLeadSelectionPath)
  83. {
  84. super(source);
  85. this.paths = paths;
  86. this.areNew = areNew;
  87. this.oldLeadSelectionPath = oldLeadSelectionPath;
  88. this.newLeadSelectionPath = newLeadSelectionPath;
  89. }
  90. /**
  91. * Creates a new <code>TreeSelectionEvent</code>.
  92. *
  93. * @param source the event source (usually a {@link TreeSelectionModel},
  94. * <code>null</code> not permitted).
  95. * @param path the path.
  96. * @param isNew <code>true</code> indicates that <code>path</code> has been
  97. * added to the selection, and <code>false</code> indicates that it has
  98. * been removed.
  99. * @param oldLeadSelectionPath the old lead selection path (<code>null</code>
  100. * permitted).
  101. * @param newLeadSelectionPath the new lead selection path (<code>null</code>
  102. * permitted).
  103. *
  104. * @throws IllegalArgumentException if <code>source</code> is
  105. * <code>null</code>.
  106. */
  107. public TreeSelectionEvent(Object source, TreePath path,
  108. boolean isNew, TreePath oldLeadSelectionPath,
  109. TreePath newLeadSelectionPath)
  110. {
  111. super(source);
  112. this.paths = new TreePath[]{path};
  113. this.areNew = new boolean[]{isNew};
  114. this.oldLeadSelectionPath = oldLeadSelectionPath;
  115. this.newLeadSelectionPath = newLeadSelectionPath;
  116. }
  117. /**
  118. * Returns the first path element.
  119. *
  120. * @return The first path element.
  121. *
  122. * @see #getPaths()
  123. */
  124. public TreePath getPath()
  125. {
  126. return paths[0];
  127. }
  128. /**
  129. * Returns an array of the paths that changed in the selection.
  130. *
  131. * @return The paths that changed in the selection.
  132. *
  133. * @see #isAddedPath(TreePath)
  134. */
  135. public TreePath[] getPaths()
  136. {
  137. return (TreePath[]) paths.clone();
  138. }
  139. /**
  140. * Returns <code>true</code> if the path returned by {@link #getPath()} has
  141. * been added to the selection, and <code>false</code> if it has been
  142. * removed.
  143. *
  144. * @return A boolean.
  145. *
  146. * @see #isAddedPath(int)
  147. */
  148. public boolean isAddedPath()
  149. {
  150. return areNew[0];
  151. }
  152. /**
  153. * Returns <code>true</code> if <code>path</code> has been added to the
  154. * selection, and <code>false</code> if the path has been removed from the
  155. * selection.
  156. *
  157. * @param path the path to check.
  158. *
  159. * @return A flag indicating whether the path has been added to, or removed
  160. * from, the selection.
  161. *
  162. * @throw IllegalArgumentException if <code>path</code> is not one of the
  163. * paths in {@link #getPaths()}.
  164. *
  165. * @see #isAddedPath(int)
  166. */
  167. public boolean isAddedPath(TreePath path)
  168. {
  169. for (int i = paths.length - 1; i >= 0; i--)
  170. if (paths[i].equals(path))
  171. return areNew[i];
  172. throw new IllegalArgumentException("Unknown 'path' argument.");
  173. }
  174. /**
  175. * Returns <code>true</code> if the path at the specified index has been
  176. * added to the selection, and <code>false</code> if the path has been
  177. * removed from the selection.
  178. *
  179. * @param index the path index.
  180. *
  181. * @return A flag indicating whether the path has been added to, or removed
  182. * from, the selection.
  183. *
  184. * @since 1.3
  185. *
  186. * @see #isAddedPath(TreePath)
  187. */
  188. public boolean isAddedPath(int index)
  189. {
  190. return areNew[index];
  191. }
  192. /**
  193. * Returns the old lead selection path.
  194. *
  195. * @return The old lead selection path (possibly <code>null</code>).
  196. *
  197. * @see #getNewLeadSelectionPath()
  198. */
  199. public TreePath getOldLeadSelectionPath()
  200. {
  201. return oldLeadSelectionPath;
  202. }
  203. /**
  204. * Returns the new lead selection path.
  205. *
  206. * @return The new lead selection path (possibly <code>null</code>).
  207. *
  208. * @see #getOldLeadSelectionPath()
  209. */
  210. public TreePath getNewLeadSelectionPath()
  211. {
  212. return newLeadSelectionPath;
  213. }
  214. /**
  215. * Creates a shallow copy of this <code>TreeSelectionEvent</code>, replacing
  216. * the source with <code>source</code>.
  217. *
  218. * @param source the new event source (<code>null</code> not permitted).
  219. *
  220. * @return A cloned event with another event source.
  221. *
  222. * @throws IllegalArgumentException if <code>source</code> is
  223. * <code>null</code>.
  224. */
  225. public Object cloneWithSource(Object source)
  226. {
  227. return new TreeSelectionEvent (source, paths, areNew, oldLeadSelectionPath,
  228. newLeadSelectionPath);
  229. }
  230. }