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