- /*
- * @(#)JTree.java 1.111 01/11/29
- *
- * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
- * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
- */
- package javax.swing;
- import java.awt.*;
- import java.awt.event.*;
- import java.beans.*;
- import java.io.*;
- import java.util.*;
- import javax.swing.event.*;
- import javax.swing.plaf.TreeUI;
- import javax.swing.tree.*;
- import javax.accessibility.*;
- /**
- * A control that displays a set of hierarchical data as an outline.
- * A specific node can be identified either by a TreePath (an object
- * that encapsulates a node and all of its ancestors), or by its
- * display row, where each row in the display area displays one node.
- * <p>
- * An <i>expanded</i> node is one displays its children. A <i>collapsed</i>
- * node is one which hides them. A <i>hidden</i> node is one which is
- * under a collapsed parent. A <i>viewable</i> node is under a collapsed
- * parent, but may or may not be displayed. A <i>displayed</i> node
- * is both viewable and in the display area, where it can be seen.
- * <p>
- * These JTree methods use "visible" to mean "displayed":<ul>
- * <li><code>isRootVisible()</code>
- * <li><code>setRootVisible()</code>
- * <li><code>scrollPathToVisible()</code>
- * <li><code>scrollRowToVisible()</code>
- * <li><code>getVisibleRowCount()</code>
- * <li><code>setVisibleRowCount()</code>
- * </ul>
- * <p>
- * These JTree methods use "visible" to mean "viewable" (under an
- * expanded parent):<ul>
- * <li><code>isVisible()</code>
- * <li><code>makeVisible()</code>
- * </ul>
- * <p>
- * If you are interested in knowing when the selection changes implement
- * the TreeSelectionListener interface and add the instance using the
- * method addTreeSelectionListener. valueChanged will be invoked when the
- * selection changes, that is if the user clicks twice on the same
- * node valueChanged will only be invoked once.
- * <p>
- * If you are interested in knowing either double clicks events or when
- * a user clicks on a node, regardless of whether or not it was selected
- * it is recommended you do the following:
- * <pre>
- * final JTree tree = ...;
- *
- * MouseListener ml = new MouseAdapter() {
- * public void <b>mouseClicked</b>(MouseEvent e) {
- * int selRow = tree.getRowForLocation(e.getX(), e.getY());
- * TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
- * if(selRow != -1) {
- * if(e.getClickCount() == 1) {
- * mySingleClick(selRow, selPath);
- * }
- * else if(e.getClickCount() == 2) {
- * myDoubleClick(selRow, selPath);
- * }
- * }
- * }
- * };
- * tree.addMouseListener(ml);
- * </pre>
- * NOTE: This example obtains both the path and row, but you only need to
- * get the one you're interested in.
- * <p>
- * To use JTree to display compound nodes (for example, nodes containing both
- * a graphic icon and text), subclass {@link TreeCellRenderer} and use
- * {@link #setTreeCellRenderer} to tell the tree to use it. To edit such nodes,
- * subclass {@link TreeCellEditor} and use {@link #setTreeCellEditor}.
- * <p>
- * Like all JComponent classes, you can use {@link JComponent#registerKeyboardAction}
- * to associate an {@link Action} object with a {@link KeyStroke} and execute the
- * action under specified conditions.
- * <p>
- * See <a href="http://java.sun.com/docs/books/tutorial/ui/swing/tree.html">How to Use Trees</a>
- * in <a href="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
- * for further documentation.
- * <p>
- * For the keyboard keys used by this component in the standard Look and
- * Feel (L&F) renditions, see the
- * <a href="doc-files/Key-Index.html#JTree">JTree</a> key assignments.
- * <p>
- * <strong>Warning:</strong>
- * Serialized objects of this class will not be compatible with
- * future Swing releases. The current serialization support is appropriate
- * for short term storage or RMI between applications running the same
- * version of Swing. A future release of Swing will provide support for
- * long term persistence.
- *
- * @beaninfo
- * attribute: isContainer false
- *
- * @version 1.78 05/11/98
- * @author Rob Davis
- * @author Ray Ryan
- * @author Scott Violet
- */
- public class JTree extends JComponent implements Scrollable, Accessible
- {
- /**
- * @see #getUIClassID
- * @see #readObject
- */
- private static final String uiClassID = "TreeUI";
- /**
- * The model that defines the tree displayed by this object.
- */
- transient protected TreeModel treeModel;
- /**
- * Models the set of selected nodes in this tree.
- */
- transient protected TreeSelectionModel selectionModel;
- /**
- * True if the root node is displayed, false if its children are
- * the highest visible nodes.
- */
- protected boolean rootVisible;
- /**
- * The cell used to draw nodes. If null, the UI uses a default
- * cellRenderer.
- */
- transient protected TreeCellRenderer cellRenderer;
- /**
- * Height to use for each display row. If this is <= 0 the renderer
- * determines the height for each row.
- */
- protected int rowHeight;
- /**
- * Maps from TreePath to Boolean indicating whether or not the
- * particular path is expanded. This ONLY indicates whether a
- * given path is expanded, and NOT if it is visible or not. That
- * information must be determined by visiting all the parent
- * paths and seeing if they are visible.
- */
- transient private Hashtable expandedState;
- /**
- * True if handles are displayed at the topmost level of the tree.
- * <p>
- * A handle is a small icon that displays adjacent to the node which
- * allows the user to click once to expand or collapse the node. A
- * common interface shows a plus sign (+) for a node which can be
- * expanded and a minus sign (-) for a node which can be collapsed.
- * Handles are always shown for nodes below the topmost level.
- * <p>
- * If the <code>rootVisible</code> setting specifies that the root
- * node is to be displayed, then that is the only node at the topmost
- * level. If the root node is not displayed, then all of its
- * children are at the topmost level of the tree. Handles are
- * always displayed for nodes other than the topmost.
- * <p>
- * If the root node isn't visible, it is generally a good to make
- * this value true. Otherwise, the tree looks exactly like a list,
- * and users may not know that the "list entries" are actually
- * tree nodes.
- *
- * @see #rootVisible
- */
- protected boolean showsRootHandles;
- /**
- * Creates a new event and passed it off the selectionListeners.
- */
- protected transient TreeSelectionRedirector selectionRedirector;
- /**
- * Editor for the entries. Default is null (tree is not editable).
- */
- transient protected TreeCellEditor cellEditor;
- /**
- * Is the tree editable? Default is false.
- */
- protected boolean editable;
- /**
- * Is this tree a large model? This is a code-optimization setting.
- * A large model can be used when the cell height is the same for all
- * nodes. The UI will then cache very little information and instead
- * continually message the model. Without a large model the UI caches
- * most of the information, resulting in fewer method calls to the model.
- * <p>
- * This value is only a suggestion to the UI. Not all UIs will
- * take advantage of it. Default value is false.
- */
- protected boolean largeModel;
- /**
- * Number of rows to make visible at one time. This value is used for
- * the Scrollable interface. It determines the preferred size of the
- * display area.
- */
- protected int visibleRowCount;
- /**
- * If true, when editing is to be stopped by way of selection changing,
- * data in tree changing or other means stopCellEditing is invoked, and
- * changes are saved. If false, cancelCellEditing is invoked, and changes
- * are discarded. Default is false.
- */
- protected boolean invokesStopCellEditing;
- /**
- * If true, when a node is expanded, as many of the descendants are
- * scrolled to be visible.
- */
- protected boolean scrollsOnExpand;
- /**
- * Number of mouse clicks before a node is expanded.
- */
- protected int toggleClickCount;
- /**
- * Updates the expandedState.
- */
- transient protected TreeModelListener treeModelListener;
- /**
- * Used when setExpandedState is invoked, will be a Stack of Stacks.
- */
- transient private Stack expandedStack;
- /**
- * Max number of stacks to keep around.
- */
- private static int TEMP_STACK_SIZE = 11;
- //
- // Bound propery names
- //
- /** Bound property name for cellRenderer. */
- public final static String CELL_RENDERER_PROPERTY = "cellRenderer";
- /** Bound property name for treeModel. */
- public final static String TREE_MODEL_PROPERTY = "treeModel";
- /** Bound property name for rootVisible. */
- public final static String ROOT_VISIBLE_PROPERTY = "rootVisible";
- /** Bound property name for showsRootHandles. */
- public final static String SHOWS_ROOT_HANDLES_PROPERTY = "showsRootHandles";
- /** Bound property name for rowHeight. */
- public final static String ROW_HEIGHT_PROPERTY = "rowHeight";
- /** Bound property name for cellEditor. */
- public final static String CELL_EDITOR_PROPERTY = "cellEditor";
- /** Bound property name for editable. */
- public final static String EDITABLE_PROPERTY = "editable";
- /** Bound property name for largeModel. */
- public final static String LARGE_MODEL_PROPERTY = "largeModel";
- /** Bound property name for selectionModel. */
- public final static String SELECTION_MODEL_PROPERTY = "selectionModel";
- /** Bound property name for visibleRowCount. */
- public final static String VISIBLE_ROW_COUNT_PROPERTY = "visibleRowCount";
- /** Bound property name for messagesStopCellEditing. */
- public final static String INVOKES_STOP_CELL_EDITING_PROPERTY = "messagesStopCellEditing";
- /** Bound property name for scrollsOnExpand. */
- public final static String SCROLLS_ON_EXPAND_PROPERTY = "scrollsOnExpand";
- /** Bound property name for toggleClickCount. */
- //public final static String TOGGLE_CLICK_COUNT_PROPERTY = "toggleClickCount";
- /**
- * Creates and returns a sample TreeModel. Used primarily for beanbuilders.
- * to show something interesting.
- *
- * @return the default TreeModel
- */
- protected static TreeModel getDefaultTreeModel() {
- DefaultMutableTreeNode root = new DefaultMutableTreeNode("JTree");
- DefaultMutableTreeNode parent;
- parent = new DefaultMutableTreeNode("colors");
- root.add(parent);
- parent.add(new DefaultMutableTreeNode("blue"));
- parent.add(new DefaultMutableTreeNode("violet"));
- parent.add(new DefaultMutableTreeNode("red"));
- parent.add(new DefaultMutableTreeNode("yellow"));
- parent = new DefaultMutableTreeNode("sports");
- root.add(parent);
- parent.add(new DefaultMutableTreeNode("basketball"));
- parent.add(new DefaultMutableTreeNode("soccer"));
- parent.add(new DefaultMutableTreeNode("football"));
- parent.add(new DefaultMutableTreeNode("hockey"));
- parent = new DefaultMutableTreeNode("food");
- root.add(parent);
- parent.add(new DefaultMutableTreeNode("hot dogs"));
- parent.add(new DefaultMutableTreeNode("pizza"));
- parent.add(new DefaultMutableTreeNode("ravioli"));
- parent.add(new DefaultMutableTreeNode("bananas"));
- return new DefaultTreeModel(root);
- }
- /**
- * Returns a TreeModel wrapping the specified object. If the object
- * is:<ul>
- * <li>an array of Objects,
- * <li>a Hashtable, or
- * <li>a Vector
- * </ul>then a new root node is created with each of the incoming
- * objects as children. Otherwise, a new root is created with the
- * specified object as its value.
- *
- * @param value the Object used as the foundation for the TreeModel
- * @return a TreeModel wrapping the specified object
- */
- protected static TreeModel createTreeModel(Object value) {
- DefaultMutableTreeNode root;
- if((value instanceof Object[]) || (value instanceof Hashtable) ||
- (value instanceof Vector)) {
- root = new DefaultMutableTreeNode("root");
- DynamicUtilTreeNode.createChildren(root, value);
- }
- else {
- root = new DynamicUtilTreeNode("root", value);
- }
- return new DefaultTreeModel(root, false);
- }
- /**
- * Returns a JTree with a sample model.
- * The default model used by the tree defines a leaf node as any node without
- * children.
- *
- * @return a JTree with the default model, which defines a leaf node
- * as any node without children.
- * @see DefaultTreeModel#asksAllowsChildren
- */
- public JTree() {
- this(getDefaultTreeModel());
- }
- /**
- * Returns a JTree with each element of the specified array as the
- * child of a new root node which is not displayed.
- * By default, the tree defines a leaf node as any node without
- * children.
- *
- * @param value an array of Objects
- * @return a JTree with the contents of the array as children of
- * the root node
- * @see DefaultTreeModel#asksAllowsChildren
- */
- public JTree(Object[] value) {
- this(createTreeModel(value));
- this.setRootVisible(false);
- this.setShowsRootHandles(true);
- }
- /**
- * Returns a JTree with each element of the specified Vector as the
- * child of a new root node which is not displayed. By default, the
- * tree defines a leaf node as any node without children.
- *
- * @param value a Vector
- * @return a JTree with the contents of the Vector as children of
- * the root node
- * @see DefaultTreeModel#asksAllowsChildren
- */
- public JTree(Vector value) {
- this(createTreeModel(value));
- this.setRootVisible(false);
- this.setShowsRootHandles(true);
- }
- /**
- * Returns a JTree created from a Hashtable which does not display
- * the root. Each value-half of the key/value pairs in the HashTable
- * becomes a child of the new root node. By default, the tree defines
- * a leaf node as any node without children.
- *
- * @param value a Hashtable
- * @return a JTree with the contents of the Hashtable as children of
- * the root node
- * @see DefaultTreeModel#asksAllowsChildren
- */
- public JTree(Hashtable value) {
- this(createTreeModel(value));
- this.setRootVisible(false);
- this.setShowsRootHandles(true);
- }
- /**
- * Returns a JTree with the specified TreeNode as its root, which
- * displays the root node. By default, the tree defines a leaf node as any node
- * without children.
- *
- * @param root a TreeNode object
- * @return a JTree with the specified root node
- * @see DefaultTreeModel#asksAllowsChildren
- */
- public JTree(TreeNode root) {
- this(root, false);
- }
- /**
- * Returns a JTree with the specified TreeNode as its root, which
- * displays the root node and which decides whether a node is a
- * leaf node in the specified manner.
- *
- * @param root a TreeNode object
- * @param asksAllowsChildren if false, any node without children is a
- * leaf node. If true, only nodes that do not allow
- * children are leaf nodes.
- * @return a JTree with the specified root node
- * @see DefaultTreeModel#asksAllowsChildren
- */
- public JTree(TreeNode root, boolean asksAllowsChildren) {
- this(new DefaultTreeModel(root, asksAllowsChildren));
- }
- /**
- * Returns an instance of JTree which displays the root node
- * -- the tree is created using the specified data model.
- *
- * @param newModel the TreeModel to use as the data model
- * @return a JTree based on the TreeModel
- */
- public JTree(TreeModel newModel) {
- super();
- expandedStack = new Stack();
- toggleClickCount = 2;
- expandedState = new Hashtable();
- setLayout(null);
- rowHeight = 16;
- visibleRowCount = 20;
- rootVisible = true;
- selectionModel = new DefaultTreeSelectionModel();
- cellRenderer = null;
- scrollsOnExpand = true;
- setOpaque(true);
- updateUI();
- setModel(newModel);
- }
- /**
- * Returns the L&F object that renders this component.
- *
- * @return the TreeUI object that renders this component
- */
- public TreeUI getUI() {
- return (TreeUI)ui;
- }
- /**
- * Sets the L&F object that renders this component.
- *
- * @param ui the TreeUI L&F object
- * @see UIDefaults#getUI
- */
- public void setUI(TreeUI ui) {
- if ((TreeUI)this.ui != ui) {
- super.setUI(ui);
- repaint();
- }
- }
- /**
- * Notification from the UIManager that the L&F has changed.
- * Replaces the current UI object with the latest version from the
- * UIManager.
- *
- * @see JComponent#updateUI
- */
- public void updateUI() {
- setUI((TreeUI)UIManager.getUI(this));
- invalidate();
- }
- /**
- * Returns the name of the L&F class that renders this component.
- *
- * @return "TreeUI"
- * @see JComponent#getUIClassID
- * @see UIDefaults#getUI
- */
- public String getUIClassID() {
- return uiClassID;
- }
- /**
- * Returns the current TreeCellRenderer that is rendering each cell.
- *
- * @return the TreeCellRenderer that is rendering each cell
- */
- public TreeCellRenderer getCellRenderer() {
- return cellRenderer;
- }
- /**
- * Sets the TreeCellRenderer that will be used to draw each cell.
- *
- * @param x the TreeCellRenderer that is to render each cell
- * @beaninfo
- * bound: true
- * description: The TreeCellRenderer that will be used to draw
- * each cell.
- */
- public void setCellRenderer(TreeCellRenderer x) {
- TreeCellRenderer oldValue = cellRenderer;
- cellRenderer = x;
- firePropertyChange(CELL_RENDERER_PROPERTY, oldValue, cellRenderer);
- invalidate();
- }
- /**
- * Determines whether the tree is editable. Fires a property
- * change event if the new setting is different from the existing
- * setting.
- *
- * @param flag a boolean value, true if the tree is editable
- * @beaninfo
- * bound: true
- * description: Whether the tree is editable.
- */
- public void setEditable(boolean flag) {
- boolean oldValue = this.editable;
- this.editable = flag;
- firePropertyChange(EDITABLE_PROPERTY, oldValue, flag);
- if (accessibleContext != null) {
- accessibleContext.firePropertyChange(
- AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
- (oldValue ? AccessibleState.EDITABLE : null),
- (flag ? AccessibleState.EDITABLE : null));
- }
- }
- /**
- * Returns true if the tree is editable.
- *
- * @return true if the tree is editable.
- */
- public boolean isEditable() {
- return editable;
- }
- /**
- * Sets the cell editor. A null value implies that the
- * tree cannot be edited. If this represents a change in the
- * cellEditor, the propertyChange method is invoked on all
- * listeners.
- *
- * @param cellEditor the TreeCellEditor to use
- * @beaninfo
- * bound: true
- * description: The cell editor. A null value implies the tree
- * cannot be edited.
- */
- public void setCellEditor(TreeCellEditor cellEditor) {
- TreeCellEditor oldEditor = this.cellEditor;
- this.cellEditor = cellEditor;
- firePropertyChange(CELL_EDITOR_PROPERTY, oldEditor, cellEditor);
- invalidate();
- }
- /**
- * Returns the editor used to edit entries in the tree.
- *
- * @return the TreeCellEditor in use, or null if the tree cannot
- * be edited
- */
- public TreeCellEditor getCellEditor() {
- return cellEditor;
- }
- /**
- * Returns the TreeModel that is providing the data.
- *
- * @return the TreeModel that is providing the data
- */
- public TreeModel getModel() {
- return treeModel;
- }
- /**
- * Sets the TreeModel that will provide the data.
- *
- * @param newModel the TreeModel that is to provide the data
- * @beaninfo
- * bound: true
- * description: The TreeModel that will provide the data.
- */
- public void setModel(TreeModel newModel) {
- TreeModel oldModel = treeModel;
- if(treeModel != null && treeModelListener != null)
- treeModel.removeTreeModelListener(treeModelListener);
- if (accessibleContext != null) {
- if (treeModel != null) {
- treeModel.removeTreeModelListener((TreeModelListener)accessibleContext);
- }
- if (newModel != null) {
- newModel.addTreeModelListener((TreeModelListener)accessibleContext);
- }
- }
- treeModel = newModel;
- clearToggledPaths();
- if(treeModel != null) {
- if(treeModelListener == null)
- treeModelListener = createTreeModelListener();
- if(treeModelListener != null)
- treeModel.addTreeModelListener(treeModelListener);
- // Mark the root as expanded, if it isn't a leaf.
- if(!treeModel.isLeaf(treeModel.getRoot()))
- expandedState.put(new TreePath(treeModel.getRoot()),
- Boolean.TRUE);
- }
- firePropertyChange(TREE_MODEL_PROPERTY, oldModel, treeModel);
- invalidate();
- }
- /**
- * Returns true if the root node of the tree is displayed.
- *
- * @return true if the root node of the tree is displayed
- * @see #rootVisible
- */
- public boolean isRootVisible() {
- return rootVisible;
- }
- /**
- * Determines whether or not the root node from
- * the TreeModel is visible.
- *
- * @param rootVisible true if the root node of the tree is to be displayed
- * @see #rootVisible
- * @beaninfo
- * bound: true
- * description: Whether or not the root node
- * from the TreeModel is visible.
- */
- public void setRootVisible(boolean rootVisible) {
- boolean oldValue = this.rootVisible;
- this.rootVisible = rootVisible;
- firePropertyChange(ROOT_VISIBLE_PROPERTY, oldValue, this.rootVisible);
- if (accessibleContext != null) {
- ((AccessibleJTree)accessibleContext).fireVisibleDataPropertyChange();
- }
- }
- /**
- * Determines whether the node handles are to be displayed.
- *
- * @param newValue true if root handles are to be displayed
- * @see #showsRootHandles
- * @beaninfo
- * bound: true
- * description: Whether the node handles are to be
- * displayed.
- */
- public void setShowsRootHandles(boolean newValue) {
- boolean oldValue = showsRootHandles;
- TreeModel model = getModel();
- showsRootHandles = newValue;
- firePropertyChange(SHOWS_ROOT_HANDLES_PROPERTY, oldValue,
- showsRootHandles);
- if (accessibleContext != null) {
- ((AccessibleJTree)accessibleContext).fireVisibleDataPropertyChange();
- }
- // Make SURE the root is expanded
- if(model != null) {
- expandPath(new TreePath(model.getRoot()));
- }
- invalidate();
- }
- /**
- * Returns true if handles for the root nodes are displayed.
- *
- * @return true if root handles are displayed
- * @see #showsRootHandles
- */
- public boolean getShowsRootHandles()
- {
- return showsRootHandles;
- }
- /**
- * Sets the height of each cell. If the specified value
- * is less than or equal to zero the current cell renderer is
- * queried for each row's height.
- *
- * @param rowHeight the height of each cell, in pixels
- * @beaninfo
- * bound: true
- * description: The height of each cell.
- */
- public void setRowHeight(int rowHeight)
- {
- int oldValue = this.rowHeight;
- this.rowHeight = rowHeight;
- firePropertyChange(ROW_HEIGHT_PROPERTY, oldValue, this.rowHeight);
- invalidate();
- }
- /**
- * Returns the height of each row. If the returned value is less than
- * or equal to 0 the height for each row is determined by the
- * renderer.
- *
- * @param the height of each cell, in pixels. Zero or negative if the
- * height of each row is determined by the tree cell renderer
- */
- public int getRowHeight()
- {
- return rowHeight;
- }
- /**
- * Returns true if the height of each display row is a fixed size.
- *
- * @return true if the height of each row is a fixed size
- */
- public boolean isFixedRowHeight()
- {
- return (rowHeight > 0);
- }
- /**
- * Specifies whether the UI should use a large model.
- * (Not all UIs will implement this.) Fires a property change
- * for the LARGE_MODEL_PROPERTY.
- *
- * @param newValue true to suggest a large model to the UI
- * @see #largeModel
- * @beaninfo
- * bound: true
- * description: Whether the UI should use a
- * large model.
- */
- public void setLargeModel(boolean newValue) {
- boolean oldValue = largeModel;
- largeModel = newValue;
- firePropertyChange(LARGE_MODEL_PROPERTY, oldValue, newValue);
- }
- /**
- * Returns true if the tree is configured for a large model.
- *
- * @return true if a large model is suggested
- * @see #largeModel
- */
- public boolean isLargeModel() {
- return largeModel;
- }
- /**
- * Determines what happens when editing is interrupted by selecting
- * another node in the tree, a change in the tree's data, or by some
- * other means. Setting this property to <code>true</code> causes the
- * changes to be automatically saved when editing is interrupted.
- * <p>
- * Fires a property change for the INVOKES_STOP_CELL_EDITING_PROPERTY.
- *
- * @param newValue true means that stopCellEditing is invoked when
- * editing is interruped, and data is saved. False means that
- * cancelCellEditing is invoked, and changes are lost.
- * @beaninfo
- * bound: true
- * description: Determines what happens when editing is interrupted,
- * selecting another node in the tree, a change in the
- * tree's data, or some other means.
- */
- public void setInvokesStopCellEditing(boolean newValue) {
- boolean oldValue = invokesStopCellEditing;
- invokesStopCellEditing = newValue;
- firePropertyChange(INVOKES_STOP_CELL_EDITING_PROPERTY, oldValue,
- newValue);
- }
- /**
- * Returns the indicator that tells what happens when editing is
- * interrupted.
- *
- * @return the indicator that tells what happens when editing is
- * interrupted
- * @see #setInvokesStopCellEditing
- */
- public boolean getInvokesStopCellEditing() {
- return invokesStopCellEditing;
- }
- /**
- * Determines whether or not when a node is expanded, as many of
- * the descendants are scrolled to be inside the viewport as
- * possible. The default is true.
- */
- public void setScrollsOnExpand(boolean newValue) {
- boolean oldValue = scrollsOnExpand;
- scrollsOnExpand = newValue;
- firePropertyChange(SCROLLS_ON_EXPAND_PROPERTY, oldValue,
- newValue);
- }
- /**
- * Returns true if the tree scrolls to show previously hidden children.
- * @return true if when a node is expanded as many of the descendants
- * as possible are scrolled to be visible.
- */
- public boolean getScrollsOnExpand() {
- return scrollsOnExpand;
- }
- // NOTE: This property will be enabled in a future release.
- /**
- * Sets the number of mouse clicks before a node will expand or close.
- * The default is two.
- */
- /*
- public void setToggleClickCount(int clickCount) {
- int oldCount = toggleClickCount;
- toggleClickCount = clickCount;
- firePropertyChange(TOGGLE_CLICK_COUNT_PROPERTY, oldCount,
- clickCount);
- }
- */
- /**
- * Returns the number of mouse clicks needed to expand or close a node.
- * @return number of mouse clicks before node is expanded.
- */
- /*
- public int getToggleClickCount() {
- return toggleClickCount;
- }
- */
- /**
- * Returns <code>isEditable</code>. This is invoked from the UI before
- * editing begins to insure that the given path can be edited. This
- * is provided as an entry point for subclassers to add filtered
- * editing without having to resort to creating a new editor.
- *
- * @return true if every parent node and the node itself is editabled
- * @see #isEditable
- */
- public boolean isPathEditable(TreePath path) {
- return isEditable();
- }
- /**
- * Overrides JComponent's getToolTipText method in order to allow
- * renderer's tips to be used if it has text set.
- * <p>
- * NOTE: For JTree to properly display tooltips of its renderers
- * JTree must be a registered component with the ToolTipManager.
- * This can be done by invoking
- * <code>ToolTipManager.sharedInstance().registerComponent(tree)</code>.
- * This is not done automaticly!
- *
- * @param event the MouseEvent that initiated the ToolTip display
- */
- public String getToolTipText(MouseEvent event) {
- if(event != null) {
- Point p = event.getPoint();
- int selRow = getRowForLocation(p.x, p.y);
- TreeCellRenderer r = getCellRenderer();
- if(selRow != -1 && r != null) {
- TreePath path = getPathForRow(selRow);
- Object lastPath = path.getLastPathComponent();
- Component rComponent = r.getTreeCellRendererComponent
- (this, lastPath, isRowSelected(selRow),
- isExpanded(selRow), getModel().isLeaf(lastPath), selRow,
- true);
- if(rComponent instanceof JComponent) {
- MouseEvent newEvent;
- Rectangle pathBounds = getPathBounds(path);
- p.translate(-pathBounds.x, -pathBounds.y);
- newEvent = new MouseEvent(rComponent, event.getID(),
- event.getWhen(),
- event.getModifiers(),
- p.x, p.y, event.getClickCount(),
- event.isPopupTrigger());
- return ((JComponent)rComponent).getToolTipText(newEvent);
- }
- }
- }
- return null;
- }
- /**
- * Called by the renderers to convert the specified value to
- * text. This implementation returns value.toString(), ignoring
- * all other arguments. To control the conversion, subclass this
- * method and use any of the arguments you need.
- *
- * @param value the Object to convert to text
- * @param selected true if the node is selected
- * @param expanded true if the node is expanded
- * @param leaf true if the node is a leaf node
- * @param row an int specifying the node's display row, where 0 is
- * the first row in the display
- * @param hasFocus true if the node has the focus
- * @return the String representation of the node's value
- */
- public String convertValueToText(Object value, boolean selected,
- boolean expanded, boolean leaf, int row,
- boolean hasFocus) {
- if(value != null)
- return value.toString();
- return "";
- }
- //
- // The following are convenience methods that get forwarded to the
- // current TreeUI.
- //
- /**
- * Returns the number of rows that are currently being displayed.
- *
- * @return the number of rows that are being displayed
- */
- public int getRowCount() {
- TreeUI tree = getUI();
- if(tree != null)
- return tree.getRowCount(this);
- return 0;
- }
- /**
- * Selects the node identified by the specified path. If any
- * component of the path is hidden (under a collapsed node), it is
- * exposed (made viewable).
- *
- * @param path the TreePath specifying the node to select
- */
- public void setSelectionPath(TreePath path) {
- makeVisible(path);
- getSelectionModel().setSelectionPath(path);
- }
- /**
- * Selects the nodes identified by the specified array of paths.
- * If any component in any of the paths is hidden (under a collapsed
- * node), it is exposed (made viewable).
- *
- * @param paths an array of TreePath objects that specifies the nodes
- * to select
- */
- public void setSelectionPaths(TreePath[] paths) {
- if(paths != null) {
- for(int counter = paths.length - 1; counter >= 0; counter--)
- makeVisible(paths[counter]);
- }
- getSelectionModel().setSelectionPaths(paths);
- }
- /**
- * Selects the node at the specified row in the display.
- *
- * @param row the row to select, where 0 is the first row in
- * the display
- */
- public void setSelectionRow(int row) {
- int[] rows = { row };
- setSelectionRows(rows);
- }
- /**
- * Selects the nodes corresponding to each of the specified rows
- * in the display. If a particular element of <code>rows</code> is
- * < 0 or >= getRowCount, it will be ignored. If none of the elements
- * in <code>rows</code> are valid rows, the selection will
- * be cleared. That is it will be as if <code>clearSelection</code>
- * was invoked.
- *
- * @param rows an array of ints specifying the rows to select,
- * where 0 indicates the first row in the display
- */
- public void setSelectionRows(int[] rows) {
- TreeUI ui = getUI();
- if(ui != null && rows != null) {
- int numRows = rows.length;
- TreePath[] paths = new TreePath[numRows];
- for(int counter = 0; counter < numRows; counter++)
- paths[counter] = ui.getPathForRow(this, rows[counter]);
- setSelectionPaths(paths);
- }
- }
- /**
- * Adds the node identified by the specified TreePath to the current
- * selection. If any component of the path isn't viewable, it is
- * made viewable.
- *
- * @param path the TreePath to add
- */
- public void addSelectionPath(TreePath path) {
- makeVisible(path);
- getSelectionModel().addSelectionPath(path);
- }
- /**
- * Adds each path in the array of paths to the current selection. If
- * any component of any of the paths isn't viewable, it is
- * made viewable.
- *
- * @param paths an array of TreePath objects that specifies the nodes
- * to add
- */
- public void addSelectionPaths(TreePath[] paths) {
- if(paths != null) {
- for(int counter = paths.length - 1; counter >= 0; counter--)
- makeVisible(paths[counter]);
- }
- getSelectionModel().addSelectionPaths(paths);
- }
- /**
- * Adds the path at the specified row to the current selection.
- *
- * @param row an int specifying the row of the node to add,
- * where 0 is the first row in the display
- */
- public void addSelectionRow(int row) {
- int[] rows = { row };
- addSelectionRows(rows);
- }
- /**
- * Adds the paths at each of the specified rows to the current selection.
- *
- * @param rows an array of ints specifying the rows to add,
- * where 0 indicates the first row in the display
- */
- public void addSelectionRows(int[] rows) {
- TreeUI ui = getUI();
- if(ui != null && rows != null) {
- int numRows = rows.length;
- TreePath[] paths = new TreePath[numRows];
- for(int counter = 0; counter < numRows; counter++)
- paths[counter] = ui.getPathForRow(this, rows[counter]);
- addSelectionPaths(paths);
- }
- }
- /**
- * Returns the last path component in the first node of the current
- * selection.
- *
- * @return the last Object in the first selected node's TreePath,
- * or null if nothing is selected
- * @see TreePath#getLastPathComponent
- */
- public Object getLastSelectedPathComponent() {
- TreePath selPath = getSelectionModel().getSelectionPath();
- if(selPath != null)
- return selPath.getLastPathComponent();
- return null;
- }
- /**
- * Returns the path to the first selected node.
- *
- * @return the TreePath for the first selected node, or null if
- * nothing is currently selected
- */
- public TreePath getSelectionPath() {
- return getSelectionModel().getSelectionPath();
- }
- /**
- * Returns the paths of all selected values.
- *
- * @return an array of TreePath objects indicating the selected
- * nodes, or null if nothing is currently selected.
- */
- public TreePath[] getSelectionPaths() {
- return getSelectionModel().getSelectionPaths();
- }
- /**
- * Returns all of the currently selected rows. This method is simply
- * forwarded to the TreeSelectionModel. If nothing is selected null
- * or an empty array with be returned, based on the TreeSelectionModel
- * implementation.
- *
- * @return an array of ints that identifies all currently selected rows
- * where 0 is the first row in the display
- */
- public int[] getSelectionRows() {
- return getSelectionModel().getSelectionRows();
- }
- /**
- * Returns the number of nodes selected.
- *
- * @return the number of nodes selected
- */
- public int getSelectionCount() {
- return selectionModel.getSelectionCount();
- }
- /**
- * Gets the first selected row.
- *
- * @return an int designating the first selected row, where 0 is the
- * first row in the display
- */
- public int getMinSelectionRow() {
- return getSelectionModel().getMinSelectionRow();
- }
- /**
- * Gets the last selected row.
- *
- * @return an int designating the last selected row, where 0 is the
- * first row in the display
- */
- public int getMaxSelectionRow() {
- return getSelectionModel().getMaxSelectionRow();
- }
- /**
- * Returns the row index of the last node added to the selection.
- *
- * @return an int giving the row index of the last node added to the
- * selection, where 0 is the first row in the display
- */
- public int getLeadSelectionRow() {
- return getSelectionModel().getLeadSelectionRow();
- }
- /**
- * Returns the path of the last node added to the selection.
- *
- * @return the TreePath of the last node added to the selection.
- */
- public TreePath getLeadSelectionPath() {
- return getSelectionModel().getLeadSelectionPath();
- }
- /**
- * Returns true if the item identified by the path is currently selected.
- *
- * @param path a TreePath identifying a node
- * @return true if the node is selected
- */
- public boolean isPathSelected(TreePath path) {
- return getSelectionModel().isPathSelected(path);
- }
- /**
- * Returns true if the node identitifed by row is selected.
- *
- * @param row an int specifying a display row, where 0 is the first
- * row in the display
- * @return true if the node is selected
- */
- public boolean isRowSelected(int row) {
- return getSelectionModel().isRowSelected(row);
- }
- /**
- * Returns an Enumeration of the descendants of <code>path</code> that
- * are currently expanded. If <code>path</code> is not currently
- * expanded, this will return null. If you expand/collapse nodes while
- * iterating over the returned Enumeration this may not return all
- * the expanded paths, or may return paths that are no longer expanded.
- */
- public Enumeration getExpandedDescendants(TreePath parent) {
- if(!isExpanded(parent))
- return null;
- Enumeration toggledPaths = expandedState.keys();
- Vector elements = new Vector();
- TreePath path;
- Object value;
- if(toggledPaths != null) {
- while(toggledPaths.hasMoreElements()) {
- path = (TreePath)toggledPaths.nextElement();
- value = expandedState.get(path);
- // Add the path if it is expanded, a descendant of parent,
- // and it is visible (all parents expanded). This is rather
- // expensive!
- if(value != null && ((Boolean)value).booleanValue() &&
- parent.isDescendant(path) && isVisible(path)) {
- elements.addElement(path);
- }
- }
- }
- return elements.elements();
- }
- /**
- * Returns true if the node identified by the path has ever been
- * expanded.
- */
- public boolean hasBeenExpanded(TreePath path) {
- return (path != null && expandedState.get(path) != null);
- }
- /**
- * Returns true if the node identified by the path is currently expanded,
- *
- * @param path the TreePath specifying the node to check
- * @return false if any of the nodes in the node's path are collapsed,
- * true if all nodes in the path are expanded
- */
- public boolean isExpanded(TreePath path) {
- if(path == null)
- return false;
- // Is this node expanded?
- Object value = expandedState.get(path);
- if(value == null || !((Boolean)value).booleanValue())
- return false;
- // It is, make sure its parent is also expanded.
- TreePath parentPath = path.getParentPath();
- if(parentPath != null)
- return isExpanded(parentPath);
- return true;
- }
- /**
- * Returns true if the node at the specified display row is currently
- * expanded.
- *
- * @param row the row to check, where 0 is the first row in the
- * display
- * @return true if the node is currently expanded, otherwise false
- */
- public boolean isExpanded(int row) {
- TreeUI tree = getUI();
- if(tree != null) {
- TreePath path = tree.getPathForRow(this, row);
- if(path != null)
- return isExpanded(path);
- }
- return false;
- }
- /**
- * Returns true if the value identified by path is currently collapsed,
- * this will return false if any of the values in path are currently
- * not being displayed.
- *
- * @param path the TreePath to check
- * @return true if any of the nodes in the node's path are collapsed,
- * false if all nodes in the path are expanded
- */
- public boolean isCollapsed(TreePath path) {
- return !isExpanded(path);
- }
- /**
- * Returns true if the node at the specified display row is collapsed.
- *
- * @param row the row to check, where 0 is the first row in the
- * display
- * @return true if the node is currently collapsed, otherwise false
- */
- public boolean isCollapsed(int row) {
- return !isExpanded(row);
- }
- /**
- * Ensures that the node identified by path is currently viewable.
- *
- * @param path the TreePath to make visible
- */
- public void makeVisible(TreePath path) {
- if(path != null) {
- TreePath parentPath = path.getParentPath();
- if(parentPath != null) {
- expandPath(parentPath);
- }
- }
- }
- /**
- * Returns true if the value identified by path is currently viewable,
- * which means it is either the root or all of its parents are exapnded ,
- * Otherwise, this method returns false.
- *
- * @return true if the node is viewable, otherwise false
- */
- public boolean isVisible(TreePath path) {
- if(path != null) {
- TreePath parentPath = path.getParentPath();
- if(parentPath != null)
- return isExpanded(parentPath);
- // Root.
- return true;
- }
- return false;
- }
- /**
- * Returns the Rectangle that the specified node will be drawn
- * into. Returns null if any component in the path is hidden
- * (under a collapsed parent).
- * <p>
- * Note:<br>
- * This method returns a valid rectangle, even if the specified
- * node is not currently displayed.
- *
- * @param path the TreePath identifying the node
- * @return the Rectangle the node is drawn in, or null
- */
- public Rectangle getPathBounds(TreePath path) {
- TreeUI tree = getUI();
- if(tree != null)
- return tree.getPathBounds(this, path);
- return null;
- }
- /**
- * Returns the Rectangle that the node at the specified row is
- * drawn in.
- *
- * @param row the row to be drawn, where 0 is the first row in the
- * display
- * @return the Rectangle the node is drawn in
- */
- public Rectangle getRowBounds(int row) {
- TreePath path = getPathForRow(row);
- return getPathBounds(getPathForRow(row));
- }
- /**
- * Makes sure all the path components in path are expanded (except
- * for the last path component) and scrolls so that the
- * node identified by the path is displayed. Only works when this
- * JTree is contained in a JSrollPane.
- *
- * @param path the TreePath identifying the node to bring into view
- */
- public void scrollPathToVisible(TreePath path) {
- if(path != null) {
- makeVisible(path);
- Rectangle bounds = getPathBounds(path);
- if(bounds != null) {
- scrollRectToVisible(bounds);
- if (accessibleContext != null) {
- ((AccessibleJTree)accessibleContext).fireVisibleDataPropertyChange();
- }
- }
- }
- }
- /**
- * Scrolls the item identified by row until it is displayed. The minimum
- * of amount of scrolling necessary to bring the row into view
- * is performed. Only works when this JTree is contained in a
- * JSrollPane.
- *
- * @param row an int specifying the row to scroll, where 0 is the
- * first row in the display
- */
- public void scrollRowToVisible(int row) {
- scrollPathToVisible(getPathForRow(row));
- }
- /**
- * Returns the path for the specified row.
- * <!-->If row is not visible null is returned.<-->
- *
- * @param row an int specifying a row
- * @return the TreePath to the specified node, null if
- * row < 0 or row > getRowCount()
- */
- public TreePath getPathForRow(int row) {
- TreeUI tree = getUI();
- if(tree != null)
- return tree.getPathForRow(this, row);
- return null;
- }
- /**
- * Returns the row that displays the node identified by the specified
- * path.
- *
- * @param path the TreePath identifying a node
- * @return an int specifying the display row, where 0 is the first
- * row in the display, or -1 if any of the elements in path
- * are hidden under a collapsed parent.
- */
- public int getRowForPath(TreePath path) {
- TreeUI tree = getUI();
- if(tree != null)
- return tree.getRowForPath(this, path);
- return -1;
- }
- /**
- * Ensures that the node identified by the specified path is
- * expanded and viewable.
- *
- * @param path the TreePath identifying a node
- */
- public void expandPath(TreePath path) {
- // Only expand if not leaf!
- TreeModel model = getModel();
- if(path != null && model != null &&
- !model.isLeaf(path.getLastPathComponent())) {
- setExpandedState(path, true);
- }
- }
- /**
- * Ensures that the node in the specified row is expanded and
- * viewable. <p> If <code>row</code> is < 0 or >= getRowCount this
- * will have no effect.
- *
- * @param row an int specifying a display row, where 0 is the
- * first row in the display
- */
- public void expandRow(int row) {
- expandPath(getPathForRow(row));
- }
- /**
- * Ensures that the node identified by the specified path is
- * collapsed and viewable.
- *
- * @param path the TreePath identifying a node
- */
- public void collapsePath(TreePath path) {
- setExpandedState(path, false);
- }
- /**
- * Ensures that the node in the specified row is collapsed.
- * <p> If <code>row</code> is < 0 or >= getRowCount this
- * will have no effect.
- *
- * @param row an int specifying a display row, where 0 is the
- * first row in the display
- */
- public void collapseRow(int row) {
- collapsePath(getPathForRow(row));
- }
- /**
- * Returns the path for the node at the specified location.
- *
- * @param x an int giving the number of pixels horizontally from
- * the left edge of the display area, minus any left margin
- * @param y an int giving the number of pixels vertically from
- * the top of the display area, minus any top margin
- * @return the TreePath for the node at that location
- */
- public TreePath getPathForLocation(int x, int y) {
- TreePath closestPath = getClosestPathForLocation(x, y);
- if(closestPath != null) {
- Rectangle pathBounds = getPathBounds(closestPath);
- if(x >= pathBounds.x && x < (pathBounds.x + pathBounds.width) &&
- y >= pathBounds.y && y < (pathBounds.y + pathBounds.height))
- return closestPath;
- }
- return null;
- }
- /**
- * Returns the row for the specified location.
- *
- * @param x an int giving the number of pixels horizontally from
- * the left edge of the display area, minus any left margin
- * @param y an int giving the number of pixels vertically from
- * the top of the display area, minus any top margin
- * @return the row corresponding to the location, or -1 if the
- * location is not within the bounds of a displayed cell
- * @see #getClosestRowForLocation
- */
- public int getRowForLocation(int x, int y) {
- return getRowForPath(getPathForLocation(x, y));
- }
- /**
- * Returns the path to the node that is closest to x,y. If
- * no nodes are currently viewable, or there is no model, returns
- * null, otherwise it always returns a valid path. To test if
- * the node is exactly at x, y, get the node's bounds and
- * test x, y against that.
- *
- * @param x an int giving the number of pixels horizontally from
- * the left edge of the display area, minus any left margin
- * @param y an int giving the number of pixels vertically from
- * the top of the display area, minus any top margin
- * @return the TreePath for the node closest to that location,
- * null if nothing is viewable or there is no model
- *
- * @see #getPathForLocation
- * @see #getPathBounds
- */
- public TreePath getClosestPathForLocation(int x, int y) {
- TreeUI tree = getUI();
- if(tree != null)
- return tree.getClosestPathForLocation(this, x, y);
- return null;
- }
- /**
- * Returns the row to the node that is closest to x,y. If no nodes
- * are viewable or there is no model, returns -1. Otherwise,
- * it always returns a valid row. To test if the returned object is
- * exactly at x, y, get the bounds for the node at the returned
- * row and test x, y against that.
- *
- * @param x an int giving the number of pixels horizontally from
- * the left edge of the display area, minus any left margin
- * @param y an int giving the number of pixels vertically from
- * the top of the display area, minus any top margin
- * @return the row closest to the location, -1 if nothing is
- * viewable or there is no model
- *
- * @see #getRowForLocation
- * @see #getRowBounds
- */
- public int getClosestRowForLocation(int x, int y) {
- return getRowForPath(getClosestPathForLocation(x, y));
- }
- /**
- * Returns true if the tree is being edited. The item that is being
- * edited can be obtained using <code>getSelectionPath</code>.
- *
- * @return true if the user is currently editing