1. /*
  2. * @(#)TreeSelectionModel.java 1.24 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.tree;
  8. import javax.swing.event.*;
  9. import java.beans.PropertyChangeListener;
  10. /**
  11. * This interface represents the current state of the selection for
  12. * the tree component.
  13. * For information and examples of using tree selection models,
  14. * see <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html">How to Use Trees</a>
  15. * in <em>The Java Tutorial.</em>
  16. *
  17. * <p>
  18. * The state of the tree selection is characterized by
  19. * a set of TreePaths, and optionally a set of integers. The mapping
  20. * from TreePath to integer is done by way of an instance of RowMapper.
  21. * It is not necessary for a TreeSelectionModel to have a RowMapper to
  22. * correctly operate, but without a RowMapper <code>getSelectionRows</code>
  23. * will return null.
  24. *
  25. * <p>
  26. *
  27. * A TreeSelectionModel can be configured to allow only one
  28. * path (<code>SINGLE_TREE_SELECTION</code>) a number of
  29. * continguous paths (<code>CONTIGUOUS_TREE_SELECTION</code>) or a number of
  30. * discontiguous paths (<code>DISCONTIGUOUS_TREE_SELECTION</code>).
  31. * A <code>RowMapper</code> is used to determine if TreePaths are
  32. * contiguous.
  33. * In the absence of a RowMapper <code>CONTIGUOUS_TREE_SELECTION</code> and
  34. * <code>DISCONTIGUOUS_TREE_SELECTION</code> behave the same, that is they
  35. * allow any number of paths to be contained in the TreeSelectionModel.
  36. *
  37. * <p>
  38. *
  39. * For a selection model of <code>CONTIGUOUS_TREE_SELECTION</code> any
  40. * time the paths are changed (<code>setSelectionPath</code>,
  41. * <code>addSelectionPath</code> ...) the TreePaths are again checked to
  42. * make they are contiguous. A check of the TreePaths can also be forced
  43. * by invoking <code>resetRowSelection</code>. How a set of discontiguous
  44. * TreePaths is mapped to a contiguous set is left to implementors of
  45. * this interface to enforce a particular policy.
  46. *
  47. * <p>
  48. *
  49. * Implementations should combine duplicate TreePaths that are
  50. * added to the selection. For example, the following code
  51. * <pre>
  52. * TreePath[] paths = new TreePath[] { treePath, treePath };
  53. * treeSelectionModel.setSelectionPaths(paths);
  54. * </pre>
  55. * should result in only one path being selected:
  56. * <code>treePath</code>, and
  57. * not two copies of <code>treePath</code>.
  58. *
  59. * <p>
  60. *
  61. * The lead TreePath is the last path that was added (or set). The lead
  62. * row is then the row that corresponds to the TreePath as determined
  63. * from the RowMapper.
  64. *
  65. * @version 1.24 12/19/03
  66. * @author Scott Violet
  67. */
  68. public interface TreeSelectionModel
  69. {
  70. /** Selection can only contain one path at a time. */
  71. public static final int SINGLE_TREE_SELECTION = 1;
  72. /** Selection can only be contiguous. This will only be enforced if
  73. * a RowMapper instance is provided. That is, if no RowMapper is set
  74. * this behaves the same as DISCONTIGUOUS_TREE_SELECTION. */
  75. public static final int CONTIGUOUS_TREE_SELECTION = 2;
  76. /** Selection can contain any number of items that are not necessarily
  77. * contiguous. */
  78. public static final int DISCONTIGUOUS_TREE_SELECTION = 4;
  79. /**
  80. * Sets the selection model, which must be one of SINGLE_TREE_SELECTION,
  81. * CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.
  82. * <p>
  83. * This may change the selection if the current selection is not valid
  84. * for the new mode. For example, if three TreePaths are
  85. * selected when the mode is changed to <code>SINGLE_TREE_SELECTION</code>,
  86. * only one TreePath will remain selected. It is up to the particular
  87. * implementation to decide what TreePath remains selected.
  88. */
  89. void setSelectionMode(int mode);
  90. /**
  91. * Returns the current selection mode, one of
  92. * <code>SINGLE_TREE_SELECTION</code>,
  93. * <code>CONTIGUOUS_TREE_SELECTION</code> or
  94. * <code>DISCONTIGUOUS_TREE_SELECTION</code>.
  95. */
  96. int getSelectionMode();
  97. /**
  98. * Sets the selection to path. If this represents a change, then
  99. * the TreeSelectionListeners are notified. If <code>path</code> is
  100. * null, this has the same effect as invoking <code>clearSelection</code>.
  101. *
  102. * @param path new path to select
  103. */
  104. void setSelectionPath(TreePath path);
  105. /**
  106. * Sets the selection to path. If this represents a change, then
  107. * the TreeSelectionListeners are notified. If <code>paths</code> is
  108. * null, this has the same effect as invoking <code>clearSelection</code>.
  109. *
  110. * @param paths new selection
  111. */
  112. void setSelectionPaths(TreePath[] paths);
  113. /**
  114. * Adds path to the current selection. If path is not currently
  115. * in the selection the TreeSelectionListeners are notified. This has
  116. * no effect if <code>path</code> is null.
  117. *
  118. * @param path the new path to add to the current selection
  119. */
  120. void addSelectionPath(TreePath path);
  121. /**
  122. * Adds paths to the current selection. If any of the paths in
  123. * paths are not currently in the selection the TreeSelectionListeners
  124. * are notified. This has
  125. * no effect if <code>paths</code> is null.
  126. *
  127. * @param paths the new paths to add to the current selection
  128. */
  129. void addSelectionPaths(TreePath[] paths);
  130. /**
  131. * Removes path from the selection. If path is in the selection
  132. * The TreeSelectionListeners are notified. This has no effect if
  133. * <code>path</code> is null.
  134. *
  135. * @param path the path to remove from the selection
  136. */
  137. void removeSelectionPath(TreePath path);
  138. /**
  139. * Removes paths from the selection. If any of the paths in
  140. * <code>paths</code>
  141. * are in the selection, the TreeSelectionListeners are notified.
  142. * This method has no effect if <code>paths</code> is null.
  143. *
  144. * @param paths the path to remove from the selection
  145. */
  146. void removeSelectionPaths(TreePath[] paths);
  147. /**
  148. * Returns the first path in the selection. How first is defined is
  149. * up to implementors, and may not necessarily be the TreePath with
  150. * the smallest integer value as determined from the
  151. * <code>RowMapper</code>.
  152. */
  153. TreePath getSelectionPath();
  154. /**
  155. * Returns the paths in the selection. This will return null (or an
  156. * empty array) if nothing is currently selected.
  157. */
  158. TreePath[] getSelectionPaths();
  159. /**
  160. * Returns the number of paths that are selected.
  161. */
  162. int getSelectionCount();
  163. /**
  164. * Returns true if the path, <code>path</code>, is in the current
  165. * selection.
  166. */
  167. boolean isPathSelected(TreePath path);
  168. /**
  169. * Returns true if the selection is currently empty.
  170. */
  171. boolean isSelectionEmpty();
  172. /**
  173. * Empties the current selection. If this represents a change in the
  174. * current selection, the selection listeners are notified.
  175. */
  176. void clearSelection();
  177. /**
  178. * Sets the RowMapper instance. This instance is used to determine
  179. * the row for a particular TreePath.
  180. */
  181. void setRowMapper(RowMapper newMapper);
  182. /**
  183. * Returns the RowMapper instance that is able to map a TreePath to a
  184. * row.
  185. */
  186. RowMapper getRowMapper();
  187. /**
  188. * Returns all of the currently selected rows. This will return
  189. * null (or an empty array) if there are no selected TreePaths or
  190. * a RowMapper has not been set.
  191. */
  192. int[] getSelectionRows();
  193. /**
  194. * Returns the smallest value obtained from the RowMapper for the
  195. * current set of selected TreePaths. If nothing is selected,
  196. * or there is no RowMapper, this will return -1.
  197. */
  198. int getMinSelectionRow();
  199. /**
  200. * Returns the largest value obtained from the RowMapper for the
  201. * current set of selected TreePaths. If nothing is selected,
  202. * or there is no RowMapper, this will return -1.
  203. */
  204. int getMaxSelectionRow();
  205. /**
  206. * Returns true if the row identified by <code>row</code> is selected.
  207. */
  208. boolean isRowSelected(int row);
  209. /**
  210. * Updates this object's mapping from TreePaths to rows. This should
  211. * be invoked when the mapping from TreePaths to integers has changed
  212. * (for example, a node has been expanded).
  213. * <p>
  214. * You do not normally have to call this; JTree and its associated
  215. * listeners will invoke this for you. If you are implementing your own
  216. * view class, then you will have to invoke this.
  217. */
  218. void resetRowSelection();
  219. /**
  220. * Returns the lead selection index. That is the last index that was
  221. * added.
  222. */
  223. int getLeadSelectionRow();
  224. /**
  225. * Returns the last path that was added. This may differ from the
  226. * leadSelectionPath property maintained by the JTree.
  227. */
  228. TreePath getLeadSelectionPath();
  229. /**
  230. * Adds a PropertyChangeListener to the listener list.
  231. * The listener is registered for all properties.
  232. * <p>
  233. * A PropertyChangeEvent will get fired when the selection mode
  234. * changes.
  235. *
  236. * @param listener the PropertyChangeListener to be added
  237. */
  238. void addPropertyChangeListener(PropertyChangeListener listener);
  239. /**
  240. * Removes a PropertyChangeListener from the listener list.
  241. * This removes a PropertyChangeListener that was registered
  242. * for all properties.
  243. *
  244. * @param listener the PropertyChangeListener to be removed
  245. */
  246. void removePropertyChangeListener(PropertyChangeListener listener);
  247. /**
  248. * Adds x to the list of listeners that are notified each time the
  249. * set of selected TreePaths changes.
  250. *
  251. * @param x the new listener to be added
  252. */
  253. void addTreeSelectionListener(TreeSelectionListener x);
  254. /**
  255. * Removes x from the list of listeners that are notified each time
  256. * the set of selected TreePaths changes.
  257. *
  258. * @param x the listener to remove
  259. */
  260. void removeTreeSelectionListener(TreeSelectionListener x);
  261. }