1. /*
  2. * @(#)JTree.java 1.135 00/04/06
  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;
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import java.beans.*;
  14. import java.io.*;
  15. import java.util.*;
  16. import javax.swing.event.*;
  17. import javax.swing.plaf.TreeUI;
  18. import javax.swing.tree.*;
  19. import javax.accessibility.*;
  20. /**
  21. * <a name="jtree_description">
  22. * A control that displays a set of hierarchical data as an outline.
  23. * You can find task-oriented documentation and examples of using trees in
  24. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html">How to Use Trees</a>,
  25. * a section in <em>The Java Tutorial.</em>
  26. * <p>
  27. * A specific node in a tree can be identified either by a
  28. * <code>TreePath</code> (an object
  29. * that encapsulates a node and all of its ancestors), or by its
  30. * display row, where each row in the display area displays one node.
  31. * An <i>expanded</i> node is one displays its children. A <i>collapsed</i>
  32. * node is one which hides them. A <i>hidden</i> node is one which is
  33. * under a collapsed ancestor. All of a <i>viewable</i> nodes parents
  34. * are expanded, but may or may not be displayed. A <i>displayed</i> node
  35. * is both viewable and in the display area, where it can be seen.
  36. * <p>
  37. * The following <code>JTree</code> methods use "visible" to mean "displayed":
  38. * <ul>
  39. * <li><code>isRootVisible()</code>
  40. * <li><code>setRootVisible()</code>
  41. * <li><code>scrollPathToVisible()</code>
  42. * <li><code>scrollRowToVisible()</code>
  43. * <li><code>getVisibleRowCount()</code>
  44. * <li><code>setVisibleRowCount()</code>
  45. * </ul>
  46. * <p>
  47. * The next group of <code>JTree</code> methods use "visible" to mean
  48. * "viewable" (under an expanded parent):
  49. * <ul>
  50. * <li><code>isVisible()</code>
  51. * <li><code>makeVisible()</code>
  52. * </ul>
  53. * <p>
  54. * If you are interested in knowing when the selection changes implement
  55. * the <code>TreeSelectionListener</code> interface and add the instance
  56. * using the method <code>addTreeSelectionListener</code>.
  57. * <code>valueChanged</code> will be invoked when the
  58. * selection changes, that is if the user clicks twice on the same
  59. * node <code>valueChanged</code> will only be invoked once.
  60. * <p>
  61. * If you are interested in detecting either double-click events or when
  62. * a user clicks on a node, regardless of whether or not it was selected,
  63. * we recommend you do the following:
  64. * <pre>
  65. * final JTree tree = ...;
  66. *
  67. * MouseListener ml = new MouseAdapter() {
  68. * public void <b>mousePressed</b>(MouseEvent e) {
  69. * int selRow = tree.getRowForLocation(e.getX(), e.getY());
  70. * TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
  71. * if(selRow != -1) {
  72. * if(e.getClickCount() == 1) {
  73. * mySingleClick(selRow, selPath);
  74. * }
  75. * else if(e.getClickCount() == 2) {
  76. * myDoubleClick(selRow, selPath);
  77. * }
  78. * }
  79. * }
  80. * };
  81. * tree.addMouseListener(ml);
  82. * </pre>
  83. * NOTE: This example obtains both the path and row, but you only need to
  84. * get the one you're interested in.
  85. * <p>
  86. * To use <code>JTree</code> to display compound nodes
  87. * (for example, nodes containing both
  88. * a graphic icon and text), subclass {@link TreeCellRenderer} and use
  89. * {@link #setCellRenderer} to tell the tree to use it. To edit such nodes,
  90. * subclass {@link TreeCellEditor} and use {@link #setCellEditor}.
  91. * <p>
  92. * Like all <code>JComponent</code> classes, you can use {@link InputMap} and
  93. * {@link ActionMap}
  94. * to associate an {@link Action} object with a {@link KeyStroke}
  95. * and execute the action under specified conditions.
  96. * <p>
  97. * For the keyboard keys used by this component in the standard Look and
  98. * Feel (L&F) renditions, see the
  99. * <a href="doc-files/Key-Index.html#JTree">JTree</a> key assignments.
  100. * <p>
  101. * <strong>Warning:</strong>
  102. * Serialized objects of this class will not be compatible with
  103. * future Swing releases. The current serialization support is appropriate
  104. * for short term storage or RMI between applications running the same
  105. * version of Swing. A future release of Swing will provide support for
  106. * long term persistence.
  107. *
  108. * @beaninfo
  109. * attribute: isContainer false
  110. * description: A component that displays a set of hierarchical data as an outline.
  111. *
  112. * @version $I% 04/06/00
  113. * @author Rob Davis
  114. * @author Ray Ryan
  115. * @author Scott Violet
  116. */
  117. public class JTree extends JComponent implements Scrollable, Accessible
  118. {
  119. /**
  120. * @see #getUIClassID
  121. * @see #readObject
  122. */
  123. private static final String uiClassID = "TreeUI";
  124. /**
  125. * The model that defines the tree displayed by this object.
  126. */
  127. transient protected TreeModel treeModel;
  128. /**
  129. * Models the set of selected nodes in this tree.
  130. */
  131. transient protected TreeSelectionModel selectionModel;
  132. /**
  133. * True if the root node is displayed, false if its children are
  134. * the highest visible nodes.
  135. */
  136. protected boolean rootVisible;
  137. /**
  138. * The cell used to draw nodes. If <code>null</code>, the UI uses a default
  139. * <code>cellRenderer</code>.
  140. */
  141. transient protected TreeCellRenderer cellRenderer;
  142. /**
  143. * Height to use for each display row. If this is <= 0 the renderer
  144. * determines the height for each row.
  145. */
  146. protected int rowHeight;
  147. /**
  148. * Maps from <code>TreePath</code> to <code>Boolean</code>
  149. * indicating whether or not the
  150. * particular path is expanded. This ONLY indicates whether a
  151. * given path is expanded, and NOT if it is visible or not. That
  152. * information must be determined by visiting all the parent
  153. * paths and seeing if they are visible.
  154. */
  155. transient private Hashtable expandedState;
  156. /**
  157. * True if handles are displayed at the topmost level of the tree.
  158. * <p>
  159. * A handle is a small icon that displays adjacent to the node which
  160. * allows the user to click once to expand or collapse the node. A
  161. * common interface shows a plus sign (+) for a node which can be
  162. * expanded and a minus sign (-) for a node which can be collapsed.
  163. * Handles are always shown for nodes below the topmost level.
  164. * <p>
  165. * If the <code>rootVisible</code> setting specifies that the root
  166. * node is to be displayed, then that is the only node at the topmost
  167. * level. If the root node is not displayed, then all of its
  168. * children are at the topmost level of the tree. Handles are
  169. * always displayed for nodes other than the topmost.
  170. * <p>
  171. * If the root node isn't visible, it is generally a good to make
  172. * this value true. Otherwise, the tree looks exactly like a list,
  173. * and users may not know that the "list entries" are actually
  174. * tree nodes.
  175. *
  176. * @see #rootVisible
  177. */
  178. protected boolean showsRootHandles;
  179. /**
  180. * Creates a new event and passed it off the
  181. * <code>selectionListeners</code>.
  182. */
  183. protected transient TreeSelectionRedirector selectionRedirector;
  184. /**
  185. * Editor for the entries. Default is <code>null</code>
  186. * (tree is not editable).
  187. */
  188. transient protected TreeCellEditor cellEditor;
  189. /**
  190. * Is the tree editable? Default is false.
  191. */
  192. protected boolean editable;
  193. /**
  194. * Is this tree a large model? This is a code-optimization setting.
  195. * A large model can be used when the cell height is the same for all
  196. * nodes. The UI will then cache very little information and instead
  197. * continually message the model. Without a large model the UI caches
  198. * most of the information, resulting in fewer method calls to the model.
  199. * <p>
  200. * This value is only a suggestion to the UI. Not all UIs will
  201. * take advantage of it. Default value is false.
  202. */
  203. protected boolean largeModel;
  204. /**
  205. * Number of rows to make visible at one time. This value is used for
  206. * the <code>Scrollable</code> interface. It determines the preferred
  207. * size of the display area.
  208. */
  209. protected int visibleRowCount;
  210. /**
  211. * If true, when editing is to be stopped by way of selection changing,
  212. * data in tree changing or other means <code>stopCellEditing</code>
  213. * is invoked, and changes are saved. If false,
  214. * <code>cancelCellEditing</code> is invoked, and changes
  215. * are discarded. Default is false.
  216. */
  217. protected boolean invokesStopCellEditing;
  218. /**
  219. * If true, when a node is expanded, as many of the descendants are
  220. * scrolled to be visible.
  221. */
  222. protected boolean scrollsOnExpand;
  223. /**
  224. * Number of mouse clicks before a node is expanded.
  225. */
  226. protected int toggleClickCount;
  227. /**
  228. * Updates the <code>expandedState</code>.
  229. */
  230. transient protected TreeModelListener treeModelListener;
  231. /**
  232. * Used when <code>setExpandedState</code> is invoked,
  233. * will be a <code>Stack</code> of <code>Stack</code>s.
  234. */
  235. transient private Stack expandedStack;
  236. /**
  237. * Lead selection path, may not be <code>null</code>.
  238. */
  239. private TreePath leadPath;
  240. /**
  241. * Anchor path.
  242. */
  243. private TreePath anchorPath;
  244. /**
  245. * True if paths in the selection should be expanded.
  246. */
  247. private boolean expandsSelectedPaths;
  248. /**
  249. * This is set to true for the life of the setUI call.
  250. */
  251. private boolean settingUI;
  252. /**
  253. * When <code>addTreeExpansionListener</code> is invoked,
  254. * and settingUI is true, this ivar gets set to the passed in
  255. * <code>Listener</code>. This listener is then notified first in
  256. * <code>fireTreeCollapsed</code> and <code>fireTreeExpanded</code>.
  257. * <p>This is an ugly workaround for a way to have the UI listener
  258. * get notified before other listeners.
  259. */
  260. private transient TreeExpansionListener uiTreeExpansionListener;
  261. /**
  262. * Max number of stacks to keep around.
  263. */
  264. private static int TEMP_STACK_SIZE = 11;
  265. //
  266. // Bound propery names
  267. //
  268. /** Bound property name for <code>cellRenderer</code>. */
  269. public final static String CELL_RENDERER_PROPERTY = "cellRenderer";
  270. /** Bound property name for <code>treeModel</code>. */
  271. public final static String TREE_MODEL_PROPERTY = "model";
  272. /** Bound property name for <code>rootVisible</code>. */
  273. public final static String ROOT_VISIBLE_PROPERTY = "rootVisible";
  274. /** Bound property name for <code>showsRootHandles</code>. */
  275. public final static String SHOWS_ROOT_HANDLES_PROPERTY = "showsRootHandles";
  276. /** Bound property name for <code>rowHeight</code>. */
  277. public final static String ROW_HEIGHT_PROPERTY = "rowHeight";
  278. /** Bound property name for <code>cellEditor</code>. */
  279. public final static String CELL_EDITOR_PROPERTY = "cellEditor";
  280. /** Bound property name for <code>editable</code>. */
  281. public final static String EDITABLE_PROPERTY = "editable";
  282. /** Bound property name for <code>largeModel</code>. */
  283. public final static String LARGE_MODEL_PROPERTY = "largeModel";
  284. /** Bound property name for selectionModel. */
  285. public final static String SELECTION_MODEL_PROPERTY = "selectionModel";
  286. /** Bound property name for <code>visibleRowCount</code>. */
  287. public final static String VISIBLE_ROW_COUNT_PROPERTY = "visibleRowCount";
  288. /** Bound property name for <code>messagesStopCellEditing</code>. */
  289. public final static String INVOKES_STOP_CELL_EDITING_PROPERTY = "invokesStopCellEditing";
  290. /** Bound property name for <code>scrollsOnExpand</code>. */
  291. public final static String SCROLLS_ON_EXPAND_PROPERTY = "scrollsOnExpand";
  292. /** Bound property name for <code>toggleClickCount</code>. */
  293. public final static String TOGGLE_CLICK_COUNT_PROPERTY = "toggleClickCount";
  294. /** Bound property name for <code>leadSelectionPath</code>.
  295. * @since 1.3 */
  296. public final static String LEAD_SELECTION_PATH_PROPERTY = "leadSelectionPath";
  297. /** Bound property name for anchor selection path.
  298. * @since 1.3 */
  299. public final static String ANCHOR_SELECTION_PATH_PROPERTY = "anchorSelectionPath";
  300. /** Bound property name for expands selected paths property
  301. * @since 1.3 */
  302. public final static String EXPANDS_SELECTED_PATHS_PROPERTY = "expandsSelectedPaths";
  303. /**
  304. * Creates and returns a sample <code>TreeModel</code>.
  305. * Used primarily for beanbuilders to show something interesting.
  306. *
  307. * @return the default <code>TreeModel</code>
  308. */
  309. protected static TreeModel getDefaultTreeModel() {
  310. DefaultMutableTreeNode root = new DefaultMutableTreeNode("JTree");
  311. DefaultMutableTreeNode parent;
  312. parent = new DefaultMutableTreeNode("colors");
  313. root.add(parent);
  314. parent.add(new DefaultMutableTreeNode("blue"));
  315. parent.add(new DefaultMutableTreeNode("violet"));
  316. parent.add(new DefaultMutableTreeNode("red"));
  317. parent.add(new DefaultMutableTreeNode("yellow"));
  318. parent = new DefaultMutableTreeNode("sports");
  319. root.add(parent);
  320. parent.add(new DefaultMutableTreeNode("basketball"));
  321. parent.add(new DefaultMutableTreeNode("soccer"));
  322. parent.add(new DefaultMutableTreeNode("football"));
  323. parent.add(new DefaultMutableTreeNode("hockey"));
  324. parent = new DefaultMutableTreeNode("food");
  325. root.add(parent);
  326. parent.add(new DefaultMutableTreeNode("hot dogs"));
  327. parent.add(new DefaultMutableTreeNode("pizza"));
  328. parent.add(new DefaultMutableTreeNode("ravioli"));
  329. parent.add(new DefaultMutableTreeNode("bananas"));
  330. return new DefaultTreeModel(root);
  331. }
  332. /**
  333. * Returns a <code>TreeModel</code> wrapping the specified object.
  334. * If the object
  335. * is:<ul>
  336. * <li>an array of <code>Object</code>s,
  337. * <li>a <code>Hashtable</code>, or
  338. * <li>a <code>Vector</code>
  339. * </ul>then a new root node is created with each of the incoming
  340. * objects as children. Otherwise, a new root is created with the
  341. * specified object as its value.
  342. *
  343. * @param value the <code>Object</code> used as the foundation for
  344. * the <code>TreeModel</code>
  345. * @return a <code>TreeModel</code> wrapping the specified object
  346. */
  347. protected static TreeModel createTreeModel(Object value) {
  348. DefaultMutableTreeNode root;
  349. if((value instanceof Object[]) || (value instanceof Hashtable) ||
  350. (value instanceof Vector)) {
  351. root = new DefaultMutableTreeNode("root");
  352. DynamicUtilTreeNode.createChildren(root, value);
  353. }
  354. else {
  355. root = new DynamicUtilTreeNode("root", value);
  356. }
  357. return new DefaultTreeModel(root, false);
  358. }
  359. /**
  360. * Returns a <code>JTree</code> with a sample model.
  361. * The default model used by the tree defines a leaf node as any node
  362. * without children.
  363. *
  364. * @return a <code>JTree</code> with the default model,
  365. * which defines a leaf node as any node without children.
  366. * @see DefaultTreeModel#asksAllowsChildren
  367. */
  368. public JTree() {
  369. this(getDefaultTreeModel());
  370. }
  371. /**
  372. * Returns a <code>JTree</code> with each element of the
  373. * specified array as the
  374. * child of a new root node which is not displayed.
  375. * By default, the tree defines a leaf node as any node without
  376. * children.
  377. *
  378. * @param value an array of <code>Object</code>s
  379. * @return a <code>JTree</code> with the contents of the array as
  380. * children of the root node
  381. * @see DefaultTreeModel#asksAllowsChildren
  382. */
  383. public JTree(Object[] value) {
  384. this(createTreeModel(value));
  385. this.setRootVisible(false);
  386. this.setShowsRootHandles(true);
  387. }
  388. /**
  389. * Returns a <code>JTree</code> with each element of the specified
  390. * <code>Vector</code> as the
  391. * child of a new root node which is not displayed. By default, the
  392. * tree defines a leaf node as any node without children.
  393. *
  394. * @param value a <code>Vector</code>
  395. * @return a <code>JTree</code> with the contents of the
  396. * <code>Vector</code> as children of the root node
  397. * @see DefaultTreeModel#asksAllowsChildren
  398. */
  399. public JTree(Vector value) {
  400. this(createTreeModel(value));
  401. this.setRootVisible(false);
  402. this.setShowsRootHandles(true);
  403. }
  404. /**
  405. * Returns a <code>JTree</code> created from a <code>Hashtable</code>
  406. * which does not display with root.
  407. * Each value-half of the key/value pairs in the <code>HashTable</code>
  408. * becomes a child of the new root node. By default, the tree defines
  409. * a leaf node as any node without children.
  410. *
  411. * @param value a <code>Hashtable</code>
  412. * @return a <code>JTree</code> with the contents of the
  413. * <code>Hashtable</code> as children of the root node
  414. * @see DefaultTreeModel#asksAllowsChildren
  415. */
  416. public JTree(Hashtable value) {
  417. this(createTreeModel(value));
  418. this.setRootVisible(false);
  419. this.setShowsRootHandles(true);
  420. }
  421. /**
  422. * Returns a <code>JTree</code> with the specified TreeNode as its root,
  423. * which displays the root node.
  424. * By default, the tree defines a leaf node as any node without children.
  425. *
  426. * @param root a <code>TreeNode</code> object
  427. * @return a <code>JTree</code> with the specified root node
  428. * @see DefaultTreeModel#asksAllowsChildren
  429. */
  430. public JTree(TreeNode root) {
  431. this(root, false);
  432. }
  433. /**
  434. * Returns a <code>JTree</code> with the specified <code>TreeNode</code>
  435. * as its root, which
  436. * displays the root node and which decides whether a node is a
  437. * leaf node in the specified manner.
  438. *
  439. * @param root a <code>TreeNode</code> object
  440. * @param asksAllowsChildren if false, any node without children is a
  441. * leaf node; if true, only nodes that do not allow
  442. * children are leaf nodes
  443. * @return a <code>JTree</code> with the specified root node
  444. * @see DefaultTreeModel#asksAllowsChildren
  445. */
  446. public JTree(TreeNode root, boolean asksAllowsChildren) {
  447. this(new DefaultTreeModel(root, asksAllowsChildren));
  448. }
  449. /**
  450. * Returns an instance of <code>JTree</code> which displays the root node
  451. * -- the tree is created using the specified data model.
  452. *
  453. * @param newModel the <code>TreeModel</code> to use as the data model
  454. * @return a <code>JTree</code> based on the <code>TreeModel</code>
  455. */
  456. public JTree(TreeModel newModel) {
  457. super();
  458. expandedStack = new Stack();
  459. toggleClickCount = 2;
  460. expandedState = new Hashtable();
  461. setLayout(null);
  462. rowHeight = 16;
  463. visibleRowCount = 20;
  464. rootVisible = true;
  465. selectionModel = new DefaultTreeSelectionModel();
  466. cellRenderer = null;
  467. scrollsOnExpand = true;
  468. setOpaque(true);
  469. expandsSelectedPaths = true;
  470. updateUI();
  471. setModel(newModel);
  472. }
  473. /**
  474. * Returns the L&F object that renders this component.
  475. *
  476. * @return the TreeUI object that renders this component
  477. */
  478. public TreeUI getUI() {
  479. return (TreeUI)ui;
  480. }
  481. /**
  482. * Sets the L&F object that renders this component.
  483. *
  484. * @param ui the TreeUI L&F object
  485. * @see UIDefaults#getUI
  486. */
  487. public void setUI(TreeUI ui) {
  488. if ((TreeUI)this.ui != ui) {
  489. settingUI = true;
  490. uiTreeExpansionListener = null;
  491. try {
  492. super.setUI(ui);
  493. }
  494. finally {
  495. settingUI = false;
  496. }
  497. }
  498. }
  499. /**
  500. * Notification from the <code>UIManager</code> that the L&F has changed.
  501. * Replaces the current UI object with the latest version from the
  502. * <code>UIManager</code>.
  503. *
  504. * @see JComponent#updateUI
  505. */
  506. public void updateUI() {
  507. setUI((TreeUI)UIManager.getUI(this));
  508. invalidate();
  509. }
  510. /**
  511. * Returns the name of the L&F class that renders this component.
  512. *
  513. * @return the string "TreeUI"
  514. * @see JComponent#getUIClassID
  515. * @see UIDefaults#getUI
  516. */
  517. public String getUIClassID() {
  518. return uiClassID;
  519. }
  520. /**
  521. * Returns the current <code>TreeCellRenderer</code>
  522. * that is rendering each cell.
  523. *
  524. * @return the <code>TreeCellRenderer</code> that is rendering each cell
  525. */
  526. public TreeCellRenderer getCellRenderer() {
  527. return cellRenderer;
  528. }
  529. /**
  530. * Sets the <code>TreeCellRenderer</code> that will be used to
  531. * draw each cell.
  532. *
  533. * @param x the <code>TreeCellRenderer</code> that is to render each cell
  534. * @beaninfo
  535. * bound: true
  536. * description: The TreeCellRenderer that will be used to draw
  537. * each cell.
  538. */
  539. public void setCellRenderer(TreeCellRenderer x) {
  540. TreeCellRenderer oldValue = cellRenderer;
  541. cellRenderer = x;
  542. firePropertyChange(CELL_RENDERER_PROPERTY, oldValue, cellRenderer);
  543. invalidate();
  544. }
  545. /**
  546. * Determines whether the tree is editable. Fires a property
  547. * change event if the new setting is different from the existing
  548. * setting.
  549. *
  550. * @param flag a boolean value, true if the tree is editable
  551. * @beaninfo
  552. * bound: true
  553. * description: Whether the tree is editable.
  554. */
  555. public void setEditable(boolean flag) {
  556. boolean oldValue = this.editable;
  557. this.editable = flag;
  558. firePropertyChange(EDITABLE_PROPERTY, oldValue, flag);
  559. if (accessibleContext != null) {
  560. accessibleContext.firePropertyChange(
  561. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  562. (oldValue ? AccessibleState.EDITABLE : null),
  563. (flag ? AccessibleState.EDITABLE : null));
  564. }
  565. }
  566. /**
  567. * Returns true if the tree is editable.
  568. *
  569. * @return true if the tree is editable
  570. */
  571. public boolean isEditable() {
  572. return editable;
  573. }
  574. /**
  575. * Sets the cell editor. A <code>null</code> value implies that the
  576. * tree cannot be edited. If this represents a change in the
  577. * <code>cellEditor</code>, the <code>propertyChange</code>
  578. * method is invoked on all listeners.
  579. *
  580. * @param cellEditor the <code>TreeCellEditor</code> to use
  581. * @beaninfo
  582. * bound: true
  583. * description: The cell editor. A null value implies the tree
  584. * cannot be edited.
  585. */
  586. public void setCellEditor(TreeCellEditor cellEditor) {
  587. TreeCellEditor oldEditor = this.cellEditor;
  588. this.cellEditor = cellEditor;
  589. firePropertyChange(CELL_EDITOR_PROPERTY, oldEditor, cellEditor);
  590. invalidate();
  591. }
  592. /**
  593. * Returns the editor used to edit entries in the tree.
  594. *
  595. * @return the <code>TreeCellEditor</code> in use,
  596. * or <code>null</code> if the tree cannot be edited
  597. */
  598. public TreeCellEditor getCellEditor() {
  599. return cellEditor;
  600. }
  601. /**
  602. * Returns the <code>TreeModel</code> that is providing the data.
  603. *
  604. * @return the <code>TreeModel</code> that is providing the data
  605. */
  606. public TreeModel getModel() {
  607. return treeModel;
  608. }
  609. /**
  610. * Sets the <code>TreeModel</code> that will provide the data.
  611. *
  612. * @param newModel the <code>TreeModel</code> that is to provide the data
  613. * @beaninfo
  614. * bound: true
  615. * description: The TreeModel that will provide the data.
  616. */
  617. public void setModel(TreeModel newModel) {
  618. TreeModel oldModel = treeModel;
  619. if(treeModel != null && treeModelListener != null)
  620. treeModel.removeTreeModelListener(treeModelListener);
  621. if (accessibleContext != null) {
  622. if (treeModel != null) {
  623. treeModel.removeTreeModelListener((TreeModelListener)accessibleContext);
  624. }
  625. if (newModel != null) {
  626. newModel.addTreeModelListener((TreeModelListener)accessibleContext);
  627. }
  628. }
  629. treeModel = newModel;
  630. clearToggledPaths();
  631. if(treeModel != null) {
  632. if(treeModelListener == null)
  633. treeModelListener = createTreeModelListener();
  634. if(treeModelListener != null)
  635. treeModel.addTreeModelListener(treeModelListener);
  636. // Mark the root as expanded, if it isn't a leaf.
  637. if(!treeModel.isLeaf(treeModel.getRoot()))
  638. expandedState.put(new TreePath(treeModel.getRoot()),
  639. Boolean.TRUE);
  640. }
  641. firePropertyChange(TREE_MODEL_PROPERTY, oldModel, treeModel);
  642. invalidate();
  643. }
  644. /**
  645. * Returns true if the root node of the tree is displayed.
  646. *
  647. * @return true if the root node of the tree is displayed
  648. * @see #rootVisible
  649. */
  650. public boolean isRootVisible() {
  651. return rootVisible;
  652. }
  653. /**
  654. * Determines whether or not the root node from
  655. * the <code>TreeModel</code> is visible.
  656. *
  657. * @param rootVisible true if the root node of the tree is to be displayed
  658. * @see #rootVisible
  659. * @beaninfo
  660. * bound: true
  661. * description: Whether or not the root node
  662. * from the TreeModel is visible.
  663. */
  664. public void setRootVisible(boolean rootVisible) {
  665. boolean oldValue = this.rootVisible;
  666. this.rootVisible = rootVisible;
  667. firePropertyChange(ROOT_VISIBLE_PROPERTY, oldValue, this.rootVisible);
  668. if (accessibleContext != null) {
  669. ((AccessibleJTree)accessibleContext).fireVisibleDataPropertyChange();
  670. }
  671. }
  672. /**
  673. * Determines whether the node handles are to be displayed.
  674. *
  675. * @param newValue true if root handles are to be displayed
  676. * @see #showsRootHandles
  677. * @beaninfo
  678. * bound: true
  679. * description: Whether the node handles are to be
  680. * displayed.
  681. */
  682. public void setShowsRootHandles(boolean newValue) {
  683. boolean oldValue = showsRootHandles;
  684. TreeModel model = getModel();
  685. showsRootHandles = newValue;
  686. firePropertyChange(SHOWS_ROOT_HANDLES_PROPERTY, oldValue,
  687. showsRootHandles);
  688. if (accessibleContext != null) {
  689. ((AccessibleJTree)accessibleContext).fireVisibleDataPropertyChange();
  690. }
  691. // Make SURE the root is expanded
  692. if(model != null) {
  693. expandPath(new TreePath(model.getRoot()));
  694. }
  695. invalidate();
  696. }
  697. /**
  698. * Returns true if handles for the root nodes are displayed.
  699. *
  700. * @return true if root handles are displayed
  701. * @see #showsRootHandles
  702. */
  703. public boolean getShowsRootHandles()
  704. {
  705. return showsRootHandles;
  706. }
  707. /**
  708. * Sets the height of each cell, in pixels. If the specified value
  709. * is less than or equal to zero the current cell renderer is
  710. * queried for each row's height.
  711. *
  712. * @param rowHeight the height of each cell, in pixels
  713. * @beaninfo
  714. * bound: true
  715. * description: The height of each cell.
  716. */
  717. public void setRowHeight(int rowHeight)
  718. {
  719. int oldValue = this.rowHeight;
  720. this.rowHeight = rowHeight;
  721. firePropertyChange(ROW_HEIGHT_PROPERTY, oldValue, this.rowHeight);
  722. invalidate();
  723. }
  724. /**
  725. * Returns the height of each row. If the returned value is less than
  726. * or equal to 0 the height for each row is determined by the
  727. * renderer.
  728. *
  729. * @param the height of each cell, in pixels; zero or negative if the
  730. * height of each row is determined by the tree cell renderer
  731. */
  732. public int getRowHeight()
  733. {
  734. return rowHeight;
  735. }
  736. /**
  737. * Returns true if the height of each display row is a fixed size.
  738. *
  739. * @return true if the height of each row is a fixed size
  740. */
  741. public boolean isFixedRowHeight()
  742. {
  743. return (rowHeight > 0);
  744. }
  745. /**
  746. * Specifies whether the UI should use a large model.
  747. * (Not all UIs will implement this.) Fires a property change
  748. * for the LARGE_MODEL_PROPERTY.
  749. *
  750. * @param newValue true to suggest a large model to the UI
  751. * @see #largeModel
  752. * @beaninfo
  753. * bound: true
  754. * description: Whether the UI should use a
  755. * large model.
  756. */
  757. public void setLargeModel(boolean newValue) {
  758. boolean oldValue = largeModel;
  759. largeModel = newValue;
  760. firePropertyChange(LARGE_MODEL_PROPERTY, oldValue, newValue);
  761. }
  762. /**
  763. * Returns true if the tree is configured for a large model.
  764. *
  765. * @return true if a large model is suggested
  766. * @see #largeModel
  767. */
  768. public boolean isLargeModel() {
  769. return largeModel;
  770. }
  771. /**
  772. * Determines what happens when editing is interrupted by selecting
  773. * another node in the tree, a change in the tree's data, or by some
  774. * other means. Setting this property to <code>true</code> causes the
  775. * changes to be automatically saved when editing is interrupted.
  776. * <p>
  777. * Fires a property change for the INVOKES_STOP_CELL_EDITING_PROPERTY.
  778. *
  779. * @param newValue true means that <code>stopCellEditing</code> is invoked
  780. * when editing is interruped, and data is saved; false means that
  781. * <code>cancelCellEditing</code> is invoked, and changes are lost
  782. * @beaninfo
  783. * bound: true
  784. * description: Determines what happens when editing is interrupted,
  785. * selecting another node in the tree, a change in the
  786. * tree's data, or some other means.
  787. */
  788. public void setInvokesStopCellEditing(boolean newValue) {
  789. boolean oldValue = invokesStopCellEditing;
  790. invokesStopCellEditing = newValue;
  791. firePropertyChange(INVOKES_STOP_CELL_EDITING_PROPERTY, oldValue,
  792. newValue);
  793. }
  794. /**
  795. * Returns the indicator that tells what happens when editing is
  796. * interrupted.
  797. *
  798. * @return the indicator that tells what happens when editing is
  799. * interrupted
  800. * @see #setInvokesStopCellEditing
  801. */
  802. public boolean getInvokesStopCellEditing() {
  803. return invokesStopCellEditing;
  804. }
  805. /**
  806. * Determines whether or not when a node is expanded, as many of
  807. * the descendants are scrolled to be inside the viewport as
  808. * possible. The default is true.
  809. * @beaninfo
  810. * bound: true
  811. * description: Indicates if a node descendent should be scrolled when expanded.
  812. */
  813. public void setScrollsOnExpand(boolean newValue) {
  814. boolean oldValue = scrollsOnExpand;
  815. scrollsOnExpand = newValue;
  816. firePropertyChange(SCROLLS_ON_EXPAND_PROPERTY, oldValue,
  817. newValue);
  818. }
  819. /**
  820. * Returns true if the tree scrolls to show previously hidden children.
  821. *
  822. * @return true if when a node is expanded as many of the descendants
  823. * as possible are scrolled to be visible
  824. */
  825. public boolean getScrollsOnExpand() {
  826. return scrollsOnExpand;
  827. }
  828. /**
  829. * Sets the number of mouse clicks before a node will expand or close.
  830. * The default is two.
  831. *
  832. * @since 1.3
  833. * @beaninfo
  834. * bound: true
  835. * description: Number of clicks before a node will expand/collapse.
  836. */
  837. public void setToggleClickCount(int clickCount) {
  838. int oldCount = toggleClickCount;
  839. toggleClickCount = clickCount;
  840. firePropertyChange(TOGGLE_CLICK_COUNT_PROPERTY, oldCount,
  841. clickCount);
  842. }
  843. /**
  844. * Returns the number of mouse clicks needed to expand or close a node.
  845. *
  846. * @return number of mouse clicks before node is expanded
  847. * @since 1.3
  848. */
  849. public int getToggleClickCount() {
  850. return toggleClickCount;
  851. }
  852. /**
  853. * Configures the <code>expandsSelectedPaths</code> property. If
  854. * true, any time the selection is changed, either via the
  855. * <code>TreeSelectionModel</code>, or the cover methods provided by
  856. * <code>JTree</code>, the <code>TreePath</code>s parents will be
  857. * expanded to make them visible (visible meaning the parent path is
  858. * expanded, not necessarily in the visible rectangle of the
  859. * <code>JTree</code>). If false, when the selection
  860. * changes the nodes parent is not made visible (all its parents expanded).
  861. * This is useful if you wish to have your selection model maintain paths
  862. * that are not always visible (all parents expanded).
  863. *
  864. * @param newValue the new value for <code>expandsSelectedPaths</code>
  865. *
  866. * @since 1.3
  867. * @beaninfo
  868. * bound: true
  869. * description: Indicates whether changes to the selection should make
  870. * the parent of the path visible.
  871. */
  872. public void setExpandsSelectedPaths(boolean newValue) {
  873. boolean oldValue = expandsSelectedPaths;
  874. expandsSelectedPaths = newValue;
  875. firePropertyChange(EXPANDS_SELECTED_PATHS_PROPERTY, oldValue,
  876. newValue);
  877. }
  878. /**
  879. * Returns the <code>expandsSelectedPaths</code> property.
  880. * @return true if selection changes result in the parent path being
  881. * expanded
  882. * @since 1.3
  883. * @see #setExpandsSelectedPaths
  884. */
  885. public boolean getExpandsSelectedPaths() {
  886. return expandsSelectedPaths;
  887. }
  888. /**
  889. * Returns <code>isEditable</code>. This is invoked from the UI before
  890. * editing begins to insure that the given path can be edited. This
  891. * is provided as an entry point for subclassers to add filtered
  892. * editing without having to resort to creating a new editor.
  893. *
  894. * @return true if every parent node and the node itself is editabled
  895. * @see #isEditable
  896. */
  897. public boolean isPathEditable(TreePath path) {
  898. return isEditable();
  899. }
  900. /**
  901. * Overrides <code>JComponent</code>'s <code>getToolTipText</code>
  902. * method in order to allow
  903. * renderer's tips to be used if it has text set.
  904. * <p>
  905. * NOTE: For <code>JTree</code> to properly display tooltips of its
  906. * renderers, <code>JTree</code> must be a registered component with the
  907. * <code>ToolTipManager</code>. This can be done by invoking
  908. * <code>ToolTipManager.sharedInstance().registerComponent(tree)</code>.
  909. * This is not done automatically!
  910. *
  911. * @param event the <code>MouseEvent</code> that initiated the
  912. * <code>ToolTip</code> display
  913. * @return a string containing the tooltip or <code>null</code>
  914. * if <code>event</code> is null
  915. */
  916. public String getToolTipText(MouseEvent event) {
  917. if(event != null) {
  918. Point p = event.getPoint();
  919. int selRow = getRowForLocation(p.x, p.y);
  920. TreeCellRenderer r = getCellRenderer();
  921. if(selRow != -1 && r != null) {
  922. TreePath path = getPathForRow(selRow);
  923. Object lastPath = path.getLastPathComponent();
  924. Component rComponent = r.getTreeCellRendererComponent
  925. (this, lastPath, isRowSelected(selRow),
  926. isExpanded(selRow), getModel().isLeaf(lastPath), selRow,
  927. true);
  928. if(rComponent instanceof JComponent) {
  929. MouseEvent newEvent;
  930. Rectangle pathBounds = getPathBounds(path);
  931. p.translate(-pathBounds.x, -pathBounds.y);
  932. newEvent = new MouseEvent(rComponent, event.getID(),
  933. event.getWhen(),
  934. event.getModifiers(),
  935. p.x, p.y, event.getClickCount(),
  936. event.isPopupTrigger());
  937. return ((JComponent)rComponent).getToolTipText(newEvent);
  938. }
  939. }
  940. }
  941. return null;
  942. }
  943. /**
  944. * Called by the renderers to convert the specified value to
  945. * text. This implementation returns <code>value.toString</code>, ignoring
  946. * all other arguments. To control the conversion, subclass this
  947. * method and use any of the arguments you need.
  948. *
  949. * @param value the <code>Object</code> to convert to text
  950. * @param selected true if the node is selected
  951. * @param expanded true if the node is expanded
  952. * @param leaf true if the node is a leaf node
  953. * @param row an integer specifying the node's display row, where 0 is
  954. * the first row in the display
  955. * @param hasFocus true if the node has the focus
  956. * @return the <code>String</code> representation of the node's value
  957. */
  958. public String convertValueToText(Object value, boolean selected,
  959. boolean expanded, boolean leaf, int row,
  960. boolean hasFocus) {
  961. if(value != null)
  962. return value.toString();
  963. return "";
  964. }
  965. //
  966. // The following are convenience methods that get forwarded to the
  967. // current TreeUI.
  968. //
  969. /**
  970. * Returns the number of rows that are currently being displayed.
  971. *
  972. * @return the number of rows that are being displayed
  973. */
  974. public int getRowCount() {
  975. TreeUI tree = getUI();
  976. if(tree != null)
  977. return tree.getRowCount(this);
  978. return 0;
  979. }
  980. /**
  981. * Selects the node identified by the specified path. If any
  982. * component of the path is hidden (under a collapsed node), and
  983. * <code>getExpandsSelectedPaths</code> is true it is
  984. * exposed (made viewable).
  985. *
  986. * @param path the <code>TreePath</code> specifying the node to select
  987. */
  988. public void setSelectionPath(TreePath path) {
  989. getSelectionModel().setSelectionPath(path);
  990. }
  991. /**
  992. * Selects the nodes identified by the specified array of paths.
  993. * If any component in any of the paths is hidden (under a collapsed
  994. * node), and <code>getExpandsSelectedPaths</code> is true
  995. * it is exposed (made viewable).
  996. *
  997. * @param paths an array of <code>TreePath</code> objects that specifies
  998. * the nodes to select
  999. */
  1000. public void setSelectionPaths(TreePath[] paths) {
  1001. getSelectionModel().setSelectionPaths(paths);
  1002. }
  1003. /**
  1004. * Sets the path identifies as the lead. The lead may not be selected.
  1005. * The lead is not maintained by <code>JTree</code>,
  1006. * rather the UI will update it.
  1007. *
  1008. * @param newPath the new lead path
  1009. * @since 1.3
  1010. * @beaninfo
  1011. * bound: true
  1012. * description: Lead selection path
  1013. */
  1014. public void setLeadSelectionPath(TreePath newPath) {
  1015. TreePath oldValue = leadPath;
  1016. leadPath = newPath;
  1017. firePropertyChange(LEAD_SELECTION_PATH_PROPERTY, oldValue, newPath);
  1018. }
  1019. /**
  1020. * Sets the path identified as the anchor.
  1021. * The anchor is not maintained by <code>JTree</code>, rather the UI will
  1022. * update it.
  1023. *
  1024. * @param newPath the new anchor path
  1025. * @since 1.3
  1026. * @beaninfo
  1027. * bound: true
  1028. * description: Anchor selection path
  1029. */
  1030. public void setAnchorSelectionPath(TreePath newPath) {
  1031. TreePath oldValue = anchorPath;
  1032. anchorPath = newPath;
  1033. firePropertyChange(ANCHOR_SELECTION_PATH_PROPERTY, oldValue, newPath);
  1034. }
  1035. /**
  1036. * Selects the node at the specified row in the display.
  1037. *
  1038. * @param row the row to select, where 0 is the first row in
  1039. * the display
  1040. */
  1041. public void setSelectionRow(int row) {
  1042. int[] rows = { row };
  1043. setSelectionRows(rows);
  1044. }
  1045. /**
  1046. * Selects the nodes corresponding to each of the specified rows
  1047. * in the display. If a particular element of <code>rows</code> is
  1048. * < 0 or >= <code>getRowCount</code>, it will be ignored.
  1049. * If none of the elements
  1050. * in <code>rows</code> are valid rows, the selection will
  1051. * be cleared. That is it will be as if <code>clearSelection</code>
  1052. * was invoked.
  1053. *
  1054. * @param rows an array of ints specifying the rows to select,
  1055. * where 0 indicates the first row in the display
  1056. */
  1057. public void setSelectionRows(int[] rows) {
  1058. TreeUI ui = getUI();
  1059. if(ui != null && rows != null) {
  1060. int numRows = rows.length;
  1061. TreePath[] paths = new TreePath[numRows];
  1062. for(int counter = 0; counter < numRows; counter++) {
  1063. paths[counter] = ui.getPathForRow(this, rows[counter]);
  1064. }
  1065. setSelectionPaths(paths);
  1066. }
  1067. }
  1068. /**
  1069. * Adds the node identified by the specified <code>TreePath</code>
  1070. * to the current
  1071. * selection. If any component of the path isn't viewable, and
  1072. * <code>getExpandsSelectedPaths</code>is true it is
  1073. * made viewable.
  1074. *
  1075. * @param path the <code>TreePath</code> to add
  1076. */
  1077. public void addSelectionPath(TreePath path) {
  1078. getSelectionModel().addSelectionPath(path);
  1079. }
  1080. /**
  1081. * Adds each path in the array of paths to the current selection. If
  1082. * any component of any of the paths isn't viewable and
  1083. * <code>getExpandsSelectedPaths</code> is true, it is
  1084. * made viewable.
  1085. *
  1086. * @param paths an array of <code>TreePath</code> objects that specifies
  1087. * the nodes to add
  1088. */
  1089. public void addSelectionPaths(TreePath[] paths) {
  1090. getSelectionModel().addSelectionPaths(paths);
  1091. }
  1092. /**
  1093. * Adds the path at the specified row to the current selection.
  1094. *
  1095. * @param row an integer specifying the row of the node to add,
  1096. * where 0 is the first row in the display
  1097. */
  1098. public void addSelectionRow(int row) {
  1099. int[] rows = { row };
  1100. addSelectionRows(rows);
  1101. }
  1102. /**
  1103. * Adds the paths at each of the specified rows to the current selection.
  1104. *
  1105. * @param rows an array of ints specifying the rows to add,
  1106. * where 0 indicates the first row in the display
  1107. */
  1108. public void addSelectionRows(int[] rows) {
  1109. TreeUI ui = getUI();
  1110. if(ui != null && rows != null) {
  1111. int numRows = rows.length;
  1112. TreePath[] paths = new TreePath[numRows];
  1113. for(int counter = 0; counter < numRows; counter++)
  1114. paths[counter] = ui.getPathForRow(this, rows[counter]);
  1115. addSelectionPaths(paths);
  1116. }
  1117. }
  1118. /**
  1119. * Returns the last path component in the first node of the current
  1120. * selection.
  1121. *
  1122. * @return the last <code>Object</code> in the first selected node's
  1123. * <code>TreePath</code>,
  1124. * or <code>null</code> if nothing is selected
  1125. * @see TreePath#getLastPathComponent
  1126. */
  1127. public Object getLastSelectedPathComponent() {
  1128. TreePath selPath = getSelectionModel().getSelectionPath();
  1129. if(selPath != null)
  1130. return selPath.getLastPathComponent();
  1131. return null;
  1132. }
  1133. /**
  1134. * Returns the path identified as the lead.
  1135. * @return path identified as the lead
  1136. */
  1137. public TreePath getLeadSelectionPath() {
  1138. return leadPath;
  1139. }
  1140. /**
  1141. * Returns the path identified as the anchor.
  1142. * @return path identified as the anchor
  1143. * @since 1.3
  1144. */
  1145. public TreePath getAnchorSelectionPath() {
  1146. return anchorPath;
  1147. }
  1148. /**
  1149. * Returns the path to the first selected node.
  1150. *
  1151. * @return the <code>TreePath</code> for the first selected node,
  1152. * or <code>null</code> if nothing is currently selected
  1153. */
  1154. public TreePath getSelectionPath() {
  1155. return getSelectionModel().getSelectionPath();
  1156. }
  1157. /**
  1158. * Returns the paths of all selected values.
  1159. *
  1160. * @return an array of <code>TreePath</code> objects indicating the selected
  1161. * nodes, or <code>null</code> if nothing is currently selected
  1162. */
  1163. public TreePath[] getSelectionPaths() {
  1164. return getSelectionModel().getSelectionPaths();
  1165. }
  1166. /**
  1167. * Returns all of the currently selected rows. This method is simply
  1168. * forwarded to the <code>TreeSelectionModel</code>.
  1169. * If nothing is selected <code>null</code> or an empty array will
  1170. * be returned, based on the <code>TreeSelectionModel</code>
  1171. * implementation.
  1172. *
  1173. * @return an array of integers that identifies all currently selected rows
  1174. * where 0 is the first row in the display
  1175. */
  1176. public int[] getSelectionRows() {
  1177. return getSelectionModel().getSelectionRows();
  1178. }
  1179. /**
  1180. * Returns the number of nodes selected.
  1181. *
  1182. * @return the number of nodes selected
  1183. */
  1184. public int getSelectionCount() {
  1185. return selectionModel.getSelectionCount();
  1186. }
  1187. /**
  1188. * Gets the first selected row.
  1189. *
  1190. * @return an integer designating the first selected row, where 0 is the
  1191. * first row in the display
  1192. */
  1193. public int getMinSelectionRow() {
  1194. return getSelectionModel().getMinSelectionRow();
  1195. }
  1196. /**
  1197. * Returns the last selected row.
  1198. *
  1199. * @return an integer designating the last selected row, where 0 is the
  1200. * first row in the display
  1201. */
  1202. public int getMaxSelectionRow() {
  1203. return getSelectionModel().getMaxSelectionRow();
  1204. }
  1205. /**
  1206. * Returns the row index corresponding to the lead path.
  1207. *
  1208. * @return an integer giving the row index of the lead path,
  1209. * where 0 is the first row in the display; or -1
  1210. * if <code>leadPath</code> is <code>null</code>
  1211. */
  1212. public int getLeadSelectionRow() {
  1213. TreePath leadPath = getLeadSelectionPath();
  1214. if (leadPath != null) {
  1215. return getRowForPath(leadPath);
  1216. }
  1217. return -1;
  1218. }
  1219. /**
  1220. * Returns true if the item identified by the path is currently selected.
  1221. *
  1222. * @param path a <code>TreePath</code> identifying a node
  1223. * @return true if the node is selected
  1224. */
  1225. public boolean isPathSelected(TreePath path) {
  1226. return getSelectionModel().isPathSelected(path);
  1227. }
  1228. /**
  1229. * Returns true if the node identitifed by row is selected.
  1230. *
  1231. * @param row an integer specifying a display row, where 0 is the first
  1232. * row in the display
  1233. * @return true if the node is selected
  1234. */
  1235. public boolean isRowSelected(int row) {
  1236. return getSelectionModel().isRowSelected(row);
  1237. }
  1238. /**
  1239. * Returns an <code>Enumeration</code> of the descendants of the
  1240. * path <code>parent</code> that
  1241. * are currently expanded. If <code>parent</code> is not currently
  1242. * expanded, this will return <code>null</code>.
  1243. * If you expand/collapse nodes while
  1244. * iterating over the returned <code>Enumeration</code>
  1245. * this may not return all
  1246. * the expanded paths, or may return paths that are no longer expanded.
  1247. *
  1248. * @param parent the path which is to be examined
  1249. * @return an <code>Enumeration</code> of the descendents of
  1250. * <code>parent</code>, or <code>null</code> if
  1251. * <code>parent</code> is not currently expanded
  1252. */
  1253. public Enumeration getExpandedDescendants(TreePath parent) {
  1254. if(!isExpanded(parent))
  1255. return null;
  1256. Enumeration toggledPaths = expandedState.keys();
  1257. Vector elements = null;
  1258. TreePath path;
  1259. Object value;
  1260. if(toggledPaths != null) {
  1261. while(toggledPaths.hasMoreElements()) {
  1262. path = (TreePath)toggledPaths.nextElement();
  1263. value = expandedState.get(path);
  1264. // Add the path if it is expanded, a descendant of parent,
  1265. // and it is visible (all parents expanded). This is rather
  1266. // expensive!
  1267. if(path != parent && value != null &&
  1268. ((Boolean)value).booleanValue() &&
  1269. parent.isDescendant(path) && isVisible(path)) {
  1270. if (elements == null) {
  1271. elements = new Vector();
  1272. }
  1273. elements.addElement(path);
  1274. }
  1275. }
  1276. }
  1277. if (elements == null) {
  1278. return DefaultMutableTreeNode.EMPTY_ENUMERATION;
  1279. }
  1280. return elements.elements();
  1281. }
  1282. /**
  1283. * Returns true if the node identified by the path has ever been
  1284. * expanded.
  1285. * @return true if the <code>path</code> has ever been expanded
  1286. */
  1287. public boolean hasBeenExpanded(TreePath path) {
  1288. return (path != null && expandedState.get(path) != null);
  1289. }
  1290. /**
  1291. * Returns true if the node identified by the path is currently expanded,
  1292. *
  1293. * @param path the <code>TreePath</code> specifying the node to check
  1294. * @return false if any of the nodes in the node's path are collapsed,
  1295. * true if all nodes in the path are expanded
  1296. */
  1297. public boolean isExpanded(TreePath path) {
  1298. if(path == null)
  1299. return false;
  1300. // Is this node expanded?
  1301. Object value = expandedState.get(path);
  1302. if(value == null || !((Boolean)value).booleanValue())
  1303. return false;
  1304. // It is, make sure its parent is also expanded.
  1305. TreePath parentPath = path.getParentPath();
  1306. if(parentPath != null)
  1307. return isExpanded(parentPath);
  1308. return true;
  1309. }
  1310. /**
  1311. * Returns true if the node at the specified display row is currently
  1312. * expanded.
  1313. *
  1314. * @param row the row to check, where 0 is the first row in the
  1315. * display
  1316. * @return true if the node is currently expanded, otherwise false
  1317. */
  1318. public boolean isExpanded(int row) {
  1319. TreeUI tree = getUI();
  1320. if(tree != null) {
  1321. TreePath path = tree.getPathForRow(this, row);
  1322. if(path != null)
  1323. return isExpanded(path);
  1324. }
  1325. return false;
  1326. }
  1327. /**
  1328. * Returns true if the value identified by path is currently collapsed,
  1329. * this will return false if any of the values in path are currently
  1330. * not being displayed.
  1331. *
  1332. * @param path the <code>TreePath</code> to check
  1333. * @return true if any of the nodes in the node's path are collapsed,
  1334. * false if all nodes in the path are expanded
  1335. */
  1336. public boolean isCollapsed(TreePath path) {
  1337. return !isExpanded(path);
  1338. }
  1339. /**
  1340. * Returns true if the node at the specified display row is collapsed.
  1341. *
  1342. * @param row the row to check, where 0 is the first row in the
  1343. * display
  1344. * @return true if the node is currently collapsed, otherwise false
  1345. */
  1346. public boolean isCollapsed(int row) {
  1347. return !isExpanded(row);
  1348. }
  1349. /**
  1350. * Ensures that the node identified by path is currently viewable.
  1351. *
  1352. * @param path the <code>TreePath</code> to make visible
  1353. */
  1354. public void makeVisible(TreePath path) {
  1355. if(path != null) {
  1356. TreePath parentPath = path.getParentPath();
  1357. if(parentPath != null) {
  1358. expandPath(parentPath);
  1359. }
  1360. }
  1361. }
  1362. /**
  1363. * Returns true if the value identified by path is currently viewable,
  1364. * which means it is either the root or all of its parents are expanded.
  1365. * Otherwise, this method returns false.
  1366. *
  1367. * @return true if the node is viewable, otherwise false
  1368. */
  1369. public boolean isVisible(TreePath path) {
  1370. if(path != null) {
  1371. TreePath parentPath = path.getParentPath();
  1372. if(parentPath != null)
  1373. return isExpanded(parentPath);
  1374. // Root.
  1375. return true;
  1376. }
  1377. return false;
  1378. }
  1379. /**
  1380. * Returns the <code>Rectangle</code> that the specified node will be drawn
  1381. * into. Returns <code>null</code> if any component in the path is hidden
  1382. * (under a collapsed parent).
  1383. * <p>
  1384. * Note:<br>
  1385. * This method returns a valid rectangle, even if the specified
  1386. * node is not currently displayed.
  1387. *
  1388. * @param path the <code>TreePath</code> identifying the node
  1389. * @return the <code>Rectangle</code> the node is drawn in,
  1390. * or <code>null</code>
  1391. */
  1392. public Rectangle getPathBounds(TreePath path) {
  1393. TreeUI tree = getUI();
  1394. if(tree != null)
  1395. return tree.getPathBounds(this, path);
  1396. return null;
  1397. }
  1398. /**
  1399. * Returns the <code>Rectangle</code> that the node at the specified row is
  1400. * drawn in.
  1401. *
  1402. * @param row the row to be drawn, where 0 is the first row in the
  1403. * display
  1404. * @return the <code>Rectangle</code> the node is drawn in
  1405. */
  1406. public Rectangle getRowBounds(int row) {