- /*
- * @(#)JTree.java 1.165 03/01/23
- *
- * Copyright 2003 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.swing.text.Position;
- import javax.accessibility.*;
- /**
- * <a name="jtree_description">
- * A control that displays a set of hierarchical data as an outline.
- * You can find task-oriented documentation and examples of using trees in
- * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html">How to Use Trees</a>,
- * a section in <em>The Java Tutorial.</em>
- * <p>
- * A specific node in a tree can be identified either by a
- * <code>TreePath</code> (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.
- * An <i>expanded</i> node is a non-leaf node (as identified by
- * <code>TreeModel.isLeaf(node)</code> returning false) that will displays
- * its children when all its ancestors are <i>expanded</i>.
- * A <i>collapsed</i>
- * node is one which hides them. A <i>hidden</i> node is one which is
- * under a collapsed ancestor. All of a <i>viewable</i> nodes parents
- * are expanded, 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>
- * The following <code>JTree</code> 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>
- * The next group of <code>JTree</code> 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 <code>TreeSelectionListener</code> interface and add the instance
- * using the method <code>addTreeSelectionListener</code>.
- * <code>valueChanged</code> will be invoked when the
- * selection changes, that is if the user clicks twice on the same
- * node <code>valueChanged</code> will only be invoked once.
- * <p>
- * If you are interested in detecting either double-click events or when
- * a user clicks on a node, regardless of whether or not it was selected,
- * we recommend you do the following:
- * <pre>
- * final JTree tree = ...;
- *
- * MouseListener ml = new MouseAdapter() {
- * public void <b>mousePressed</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 <code>JTree</code> to display compound nodes
- * (for example, nodes containing both
- * a graphic icon and text), subclass {@link TreeCellRenderer} and use
- * {@link #setCellRenderer} to tell the tree to use it. To edit such nodes,
- * subclass {@link TreeCellEditor} and use {@link #setCellEditor}.
- * <p>
- * Like all <code>JComponent</code> classes, you can use {@link InputMap} and
- * {@link ActionMap}
- * to associate an {@link Action} object with a {@link KeyStroke}
- * and execute the action under specified conditions.
- * <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"><code>JTree</code> key assignments</a>.
- * <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. As of 1.4, support for long term storage
- * of all JavaBeans<sup><font size="-2">TM</font></sup>
- * has been added to the <code>java.beans</code> package.
- * Please see {@link java.beans.XMLEncoder}.
- *
- * @beaninfo
- * attribute: isContainer false
- * description: A component that displays a set of hierarchical data as an outline.
- *
- * @version 1.165, 01/23/03
- * @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 <code>null</code>, the UI uses a default
- * <code>cellRenderer</code>.
- */
- 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 <code>TreePath</code> to <code>Boolean</code>
- * 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
- * <code>selectionListeners</code>.
- */
- protected transient TreeSelectionRedirector selectionRedirector;
- /**
- * Editor for the entries. Default is <code>null</code>
- * (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 <code>Scrollable</code> 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 <code>stopCellEditing</code>
- * is invoked, and changes are saved. If false,
- * <code>cancelCellEditing</code> 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 <code>expandedState</code>.
- */
- transient protected TreeModelListener treeModelListener;
- /**
- * Used when <code>setExpandedState</code> is invoked,
- * will be a <code>Stack</code> of <code>Stack</code>s.
- */
- transient private Stack expandedStack;
- /**
- * Lead selection path, may not be <code>null</code>.
- */
- private TreePath leadPath;
- /**
- * Anchor path.
- */
- private TreePath anchorPath;
- /**
- * True if paths in the selection should be expanded.
- */
- private boolean expandsSelectedPaths;
- /**
- * This is set to true for the life of the <code>setUI</code> call.
- */
- private boolean settingUI;
- /** If true, mouse presses on selections initiate a drag operation. */
- private boolean dragEnabled;
- /**
- * When <code>addTreeExpansionListener</code> is invoked,
- * and <code>settingUI</code> is true, this ivar gets set to the passed in
- * <code>Listener</code>. This listener is then notified first in
- * <code>fireTreeCollapsed</code> and <code>fireTreeExpanded</code>.
- * <p>This is an ugly workaround for a way to have the UI listener
- * get notified before other listeners.
- */
- private transient TreeExpansionListener uiTreeExpansionListener;
- /**
- * Max number of stacks to keep around.
- */
- private static int TEMP_STACK_SIZE = 11;
- //
- // Bound property names
- //
- /** Bound property name for <code>cellRenderer</code>. */
- public final static String CELL_RENDERER_PROPERTY = "cellRenderer";
- /** Bound property name for <code>treeModel</code>. */
- public final static String TREE_MODEL_PROPERTY = "model";
- /** Bound property name for <code>rootVisible</code>. */
- public final static String ROOT_VISIBLE_PROPERTY = "rootVisible";
- /** Bound property name for <code>showsRootHandles</code>. */
- public final static String SHOWS_ROOT_HANDLES_PROPERTY = "showsRootHandles";
- /** Bound property name for <code>rowHeight</code>. */
- public final static String ROW_HEIGHT_PROPERTY = "rowHeight";
- /** Bound property name for <code>cellEditor</code>. */
- public final static String CELL_EDITOR_PROPERTY = "cellEditor";
- /** Bound property name for <code>editable</code>. */
- public final static String EDITABLE_PROPERTY = "editable";
- /** Bound property name for <code>largeModel</code>. */
- 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 <code>visibleRowCount</code>. */
- public final static String VISIBLE_ROW_COUNT_PROPERTY = "visibleRowCount";
- /** Bound property name for <code>messagesStopCellEditing</code>. */
- public final static String INVOKES_STOP_CELL_EDITING_PROPERTY = "invokesStopCellEditing";
- /** Bound property name for <code>scrollsOnExpand</code>. */
- public final static String SCROLLS_ON_EXPAND_PROPERTY = "scrollsOnExpand";
- /** Bound property name for <code>toggleClickCount</code>. */
- public final static String TOGGLE_CLICK_COUNT_PROPERTY = "toggleClickCount";
- /** Bound property name for <code>leadSelectionPath</code>.
- * @since 1.3 */
- public final static String LEAD_SELECTION_PATH_PROPERTY = "leadSelectionPath";
- /** Bound property name for anchor selection path.
- * @since 1.3 */
- public final static String ANCHOR_SELECTION_PATH_PROPERTY = "anchorSelectionPath";
- /** Bound property name for expands selected paths property
- * @since 1.3 */
- public final static String EXPANDS_SELECTED_PATHS_PROPERTY = "expandsSelectedPaths";
- /**
- * Creates and returns a sample <code>TreeModel</code>.
- * Used primarily for beanbuilders to show something interesting.
- *
- * @return the default <code>TreeModel</code>
- */
- 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 <code>TreeModel</code> wrapping the specified object.
- * If the object is:<ul>
- * <li>an array of <code>Object</code>s,
- * <li>a <code>Hashtable</code>, or
- * <li>a <code>Vector</code>
- * </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 <code>Object</code> used as the foundation for
- * the <code>TreeModel</code>
- * @return a <code>TreeModel</code> 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 <code>JTree</code> with a sample model.
- * The default model used by the tree defines a leaf node as any node
- * without children.
- *
- * @see DefaultTreeModel#asksAllowsChildren
- */
- public JTree() {
- this(getDefaultTreeModel());
- }
- /**
- * Returns a <code>JTree</code> 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 <code>Object</code>s
- * @see DefaultTreeModel#asksAllowsChildren
- */
- public JTree(Object[] value) {
- this(createTreeModel(value));
- this.setRootVisible(false);
- this.setShowsRootHandles(true);
- expandRoot();
- }
- /**
- * Returns a <code>JTree</code> with each element of the specified
- * <code>Vector</code> 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 <code>Vector</code>
- * @see DefaultTreeModel#asksAllowsChildren
- */
- public JTree(Vector value) {
- this(createTreeModel(value));
- this.setRootVisible(false);
- this.setShowsRootHandles(true);
- expandRoot();
- }
- /**
- * Returns a <code>JTree</code> created from a <code>Hashtable</code>
- * which does not display with root.
- * Each value-half of the key/value pairs in the <code>HashTable</code>
- * becomes a child of the new root node. By default, the tree defines
- * a leaf node as any node without children.
- *
- * @param value a <code>Hashtable</code>
- * @see DefaultTreeModel#asksAllowsChildren
- */
- public JTree(Hashtable value) {
- this(createTreeModel(value));
- this.setRootVisible(false);
- this.setShowsRootHandles(true);
- expandRoot();
- }
- /**
- * Returns a <code>JTree</code> with the specified
- * <code>TreeNode</code> as its root,
- * which displays the root node.
- * By default, the tree defines a leaf node as any node without children.
- *
- * @param root a <code>TreeNode</code> object
- * @see DefaultTreeModel#asksAllowsChildren
- */
- public JTree(TreeNode root) {
- this(root, false);
- }
- /**
- * Returns a <code>JTree</code> with the specified <code>TreeNode</code>
- * 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 <code>TreeNode</code> 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
- * @see DefaultTreeModel#asksAllowsChildren
- */
- public JTree(TreeNode root, boolean asksAllowsChildren) {
- this(new DefaultTreeModel(root, asksAllowsChildren));
- }
- /**
- * Returns an instance of <code>JTree</code> which displays the root node
- * -- the tree is created using the specified data model.
- *
- * @param newModel the <code>TreeModel</code> to use as the data model
- */
- 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);
- expandsSelectedPaths = true;
- updateUI();
- setModel(newModel);
- }
- /**
- * Returns the L&F object that renders this component.
- *
- * @return the <code>TreeUI</code> object that renders this component
- */
- public TreeUI getUI() {
- return (TreeUI)ui;
- }
- /**
- * Sets the L&F object that renders this component.
- *
- * @param ui the <code>TreeUI</code> L&F object
- * @see UIDefaults#getUI
- * @beaninfo
- * bound: true
- * hidden: true
- * attribute: visualUpdate true
- * description: The UI object that implements the Component's LookAndFeel.
- */
- public void setUI(TreeUI ui) {
- if ((TreeUI)this.ui != ui) {
- settingUI = true;
- uiTreeExpansionListener = null;
- try {
- super.setUI(ui);
- }
- finally {
- settingUI = false;
- }
- }
- }
- /**
- * Notification from the <code>UIManager</code> that the L&F has changed.
- * Replaces the current UI object with the latest version from the
- * <code>UIManager</code>.
- *
- * @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 the string "TreeUI"
- * @see JComponent#getUIClassID
- * @see UIDefaults#getUI
- */
- public String getUIClassID() {
- return uiClassID;
- }
- /**
- * Returns the current <code>TreeCellRenderer</code>
- * that is rendering each cell.
- *
- * @return the <code>TreeCellRenderer</code> that is rendering each cell
- */
- public TreeCellRenderer getCellRenderer() {
- return cellRenderer;
- }
- /**
- * Sets the <code>TreeCellRenderer</code> that will be used to
- * draw each cell.
- *
- * @param x the <code>TreeCellRenderer</code> 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 <code>null</code> value implies that the
- * tree cannot be edited. If this represents a change in the
- * <code>cellEditor</code>, the <code>propertyChange</code>
- * method is invoked on all listeners.
- *
- * @param cellEditor the <code>TreeCellEditor</code> 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 <code>TreeCellEditor</code> in use,
- * or <code>null</code> if the tree cannot be edited
- */
- public TreeCellEditor getCellEditor() {
- return cellEditor;
- }
- /**
- * Returns the <code>TreeModel</code> that is providing the data.
- *
- * @return the <code>TreeModel</code> that is providing the data
- */
- public TreeModel getModel() {
- return treeModel;
- }
- /**
- * Sets the <code>TreeModel</code> that will provide the data.
- *
- * @param newModel the <code>TreeModel</code> 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.getRoot() != null &&
- !treeModel.isLeaf(treeModel.getRoot())) {
- expandedState.put(new TreePath(treeModel.getRoot()),
- Boolean.TRUE);
- }
- }
- firePropertyChange(TREE_MODEL_PROPERTY, oldModel, treeModel);
- invalidate();
- clearSelection();
- }
- /**
- * 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 <code>TreeModel</code> 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();
- }
- }
- /**
- * Sets the value of the <code>showsRootHandles</code> property,
- * which specifies whether the node handles should be displayed.
- * The default value of this property depends on the constructor
- * used to create the <code>JTree</code>.
- * Some look and feels might not support handles;
- * they will ignore this property.
- *
- * @param newValue <code>true</code> if root handles should be displayed;
- * otherwise, <code>false</code>
- * @see #showsRootHandles
- * @see #getShowsRootHandles
- * @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();
- }
- invalidate();
- }
- /**
- * Returns the value of the <code>showsRootHandles</code> property.
- *
- * @return the value of the <code>showsRootHandles</code> property
- * @see #showsRootHandles
- */
- public boolean getShowsRootHandles()
- {
- return showsRootHandles;
- }
- /**
- * Sets the height of each cell, in pixels. 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.
- *
- */
- 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 <code>stopCellEditing</code> is invoked
- * when editing is interrupted, and data is saved; false means that
- * <code>cancelCellEditing</code> 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;
- }
- /**
- * Sets the <code>scrollsOnExpand</code> property,
- * which determines whether the
- * tree might scroll to show previously hidden children.
- * If this property is <code>true</code> (the default),
- * when a node expands
- * the tree can use scrolling to make
- * the maximum possible number of the node's descendants visible.
- * In some look and feels, trees might not need to scroll when expanded;
- * those look and feels will ignore this property.
- *
- * @param newValue <code>false</code> to disable scrolling on expansion;
- * <code>true</code> to enable it
- * @see #getScrollsOnExpand
- *
- * @beaninfo
- * bound: true
- * description: Indicates if a node descendant should be scrolled when expanded.
- */
- public void setScrollsOnExpand(boolean newValue) {
- boolean oldValue = scrollsOnExpand;
- scrollsOnExpand = newValue;
- firePropertyChange(SCROLLS_ON_EXPAND_PROPERTY, oldValue,
- newValue);
- }
- /**
- * Returns the value of the <code>scrollsOnExpand</code> property.
- *
- * @return the value of the <code>scrollsOnExpand</code> property
- */
- public boolean getScrollsOnExpand() {
- return scrollsOnExpand;
- }
- /**
- * Sets the number of mouse clicks before a node will expand or close.
- * The default is two.
- *
- * @since 1.3
- * @beaninfo
- * bound: true
- * description: Number of clicks before a node will expand/collapse.
- */
- 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
- * @since 1.3
- */
- public int getToggleClickCount() {
- return toggleClickCount;
- }
- /**
- * Configures the <code>expandsSelectedPaths</code> property. If
- * true, any time the selection is changed, either via the
- * <code>TreeSelectionModel</code>, or the cover methods provided by
- * <code>JTree</code>, the <code>TreePath</code>s parents will be
- * expanded to make them visible (visible meaning the parent path is
- * expanded, not necessarily in the visible rectangle of the
- * <code>JTree</code>). If false, when the selection
- * changes the nodes parent is not made visible (all its parents expanded).
- * This is useful if you wish to have your selection model maintain paths
- * that are not always visible (all parents expanded).
- *
- * @param newValue the new value for <code>expandsSelectedPaths</code>
- *
- * @since 1.3
- * @beaninfo
- * bound: true
- * description: Indicates whether changes to the selection should make
- * the parent of the path visible.
- */
- public void setExpandsSelectedPaths(boolean newValue) {
- boolean oldValue = expandsSelectedPaths;
- expandsSelectedPaths = newValue;
- firePropertyChange(EXPANDS_SELECTED_PATHS_PROPERTY, oldValue,
- newValue);
- }
- /**
- * Returns the <code>expandsSelectedPaths</code> property.
- * @return true if selection changes result in the parent path being
- * expanded
- * @since 1.3
- * @see #setExpandsSelectedPaths
- */
- public boolean getExpandsSelectedPaths() {
- return expandsSelectedPaths;
- }
- /**
- * Sets the <code>dragEnabled</code> property,
- * which must be <code>true</code> to enable
- * automatic drag handling (the first part of drag and drop)
- * on this component.
- * The <code>transferHandler</code> property needs to be set
- * to a non-<code>null</code> value for the drag to do
- * anything. The default value of the <code>dragEnabled</code>
- * property
- * is <code>false</code>.
- *
- * <p>
- *
- * When automatic drag handling is enabled,
- * most look and feels begin a drag-and-drop operation
- * whenever the user presses the mouse button over a selection
- * and then moves the mouse a few pixels.
- * Setting this property to <code>true</code>
- * can therefore have a subtle effect on
- * how selections behave.
- *
- * <p>
- *
- * Some look and feels might not support automatic drag and drop;
- * they will ignore this property. You can work around such
- * look and feels by modifying the component
- * to directly call the <code>exportAsDrag</code> method of a
- * <code>TransferHandler</code>.
- *
- * @param b the value to set the <code>dragEnabled</code> property to
- * @exception HeadlessException if
- * <code>b</code> is <code>true</code> and
- * <code>GraphicsEnvironment.isHeadless()</code>
- * returns <code>true</code>
- * @see java.awt.GraphicsEnvironment#isHeadless
- * @see #getDragEnabled
- * @see #setTransferHandler
- * @see TransferHandler
- * @since 1.4
- *
- * @beaninfo
- * description: determines whether automatic drag handling is enabled
- * bound: false
- */
- public void setDragEnabled(boolean b) {
- if (b && GraphicsEnvironment.isHeadless()) {
- throw new HeadlessException();
- }
- dragEnabled = b;
- }
- /**
- * Gets the value of the <code>dragEnabled</code> property.
- *
- * @return the value of the <code>dragEnabled</code> property
- * @see #setDragEnabled
- * @since 1.4
- */
- public boolean getDragEnabled() {
- return dragEnabled;
- }
- /**
- * 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 editable
- * @see #isEditable
- */
- public boolean isPathEditable(TreePath path) {
- return isEditable();
- }
- /**
- * Overrides <code>JComponent</code>'s <code>getToolTipText</code>
- * method in order to allow
- * renderer's tips to be used if it has text set.
- * <p>
- * NOTE: For <code>JTree</code> to properly display tooltips of its
- * renderers, <code>JTree</code> must be a registered component with the
- * <code>ToolTipManager</code>. This can be done by invoking
- * <code>ToolTipManager.sharedInstance().registerComponent(tree)</code>.
- * This is not done automatically!
- *
- * @param event the <code>MouseEvent</code> that initiated the
- * <code>ToolTip</code> display
- * @return a string containing the tooltip or <code>null</code>
- * if <code>event</code> is null
- */
- 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 <code>value.toString</code>, ignoring
- * all other arguments. To control the conversion, subclass this
- * method and use any of the arguments you need.
- *
- * @param value the <code>Object</code> 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 integer 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 <code>String</code> 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), and
- * <code>getExpandsSelectedPaths</code> is true it is
- * exposed (made viewable).
- *
- * @param path the <code>TreePath</code> specifying the node to select
- */
- public void setSelectionPath(TreePath 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), and <code>getExpandsSelectedPaths</code> is true
- * it is exposed (made viewable).
- *
- * @param paths an array of <code>TreePath</code> objects that specifies
- * the nodes to select
- */
- public void setSelectionPaths(TreePath[] paths) {
- getSelectionModel().setSelectionPaths(paths);
- }
- /**
- * Sets the path identifies as the lead. The lead may not be selected.
- * The lead is not maintained by <code>JTree</code>,
- * rather the UI will update it.
- *
- * @param newPath the new lead path
- * @since 1.3
- * @beaninfo
- * bound: true
- * description: Lead selection path
- */
- public void setLeadSelectionPath(TreePath newPath) {
- TreePath oldValue = leadPath;
- leadPath = newPath;
- firePropertyChange(LEAD_SELECTION_PATH_PROPERTY, oldValue, newPath);
- }
- /**
- * Sets the path identified as the anchor.
- * The anchor is not maintained by <code>JTree</code>, rather the UI will
- * update it.
- *
- * @param newPath the new anchor path
- * @since 1.3
- * @beaninfo
- * bound: true
- * description: Anchor selection path
- */
- public void setAnchorSelectionPath(TreePath newPath) {
- TreePath oldValue = anchorPath;
- anchorPath = newPath;
- firePropertyChange(ANCHOR_SELECTION_PATH_PROPERTY, oldValue, newPath);
- }
- /**
- * 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 >= <code>getRowCount</code>, 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 <code>TreePath</code>
- * to the current selection. If any component of the path isn't
- * viewable, and <code>getExpandsSelectedPaths</code> is true it is
- * made viewable.
- * <p>
- * Note that <code>JTree</code> does not allow duplicate nodes to
- * exist as children under the same parent -- each sibling must be
- * a unique object.
- *
- * @param path the <code>TreePath</code> to add
- */
- public void addSelectionPath(TreePath 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 and
- * <code>getExpandsSelectedPaths</code> is true, it is
- * made viewable.
- * <p>
- * Note that <code>JTree</code> does not allow duplicate nodes to
- * exist as children under the same parent -- each sibling must be
- * a unique object.
- *
- * @param paths an array of <code>TreePath</code> objects that specifies
- * the nodes to add
- */
- public void addSelectionPaths(TreePath[] paths) {
- getSelectionModel().addSelectionPaths(paths);
- }
- /**
- * Adds the path at the specified row to the current selection.
- *
- * @param row an integer 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 <code>Object</code> in the first selected node's
- * <code>TreePath</code>,
- * or <code>null</code> 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 identified as the lead.
- * @return path identified as the lead
- */
- public TreePath getLeadSelectionPath() {
- return leadPath;
- }
- /**
- * Returns the path identified as the anchor.
- * @return path identified as the anchor
- * @since 1.3
- */
- public TreePath getAnchorSelectionPath() {
- return anchorPath;
- }
- /**
- * Returns the path to the first selected node.
- *
- * @return the <code>TreePath</code> for the first selected node,
- * or <code>null</code> if nothing is currently selected
- */
- public TreePath getSelectionPath() {
- return getSelectionModel().getSelectionPath();
- }
- /**
- * Returns the paths of all selected values.
- *
- * @return an array of <code>TreePath</code> objects indicating the selected
- * nodes, or <code>null</code> 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 <code>TreeSelectionModel</code>.
- * If nothing is selected <code>null</code> or an empty array will
- * be returned, based on the <code>TreeSelectionModel</code>
- * implementation.
- *
- * @return an array of integers 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 integer designating the first selected row, where 0 is the
- * first row in the display
- */
- public int getMinSelectionRow() {
- return getSelectionModel().getMinSelectionRow();
- }
- /**
- * Returns the last selected row.
- *
- * @return an integer designating the last selected row, where 0 is the
- * first row in the display
- */
- public int getMaxSelectionRow() {
- return getSelectionModel().getMaxSelectionRow();
- }
- /**
- * Returns the row index corresponding to the lead path.
- *
- * @return an integer giving the row index of the lead path,
- * where 0 is the first row in the display; or -1
- * if <code>leadPath</code> is <code>null</code>
- */
- public int getLeadSelectionRow() {
- TreePath leadPath = getLeadSelectionPath();
- if (leadPath != null) {
- return getRowForPath(leadPath);
- }
- return -1;
- }
- /**
- * Returns true if the item identified by the path is currently selected.
- *
- * @param path a <code>TreePath</code> identifying a node
- * @return true if the node is selected
- */
- public boolean isPathSelected(TreePath path) {
- return getSelectionModel().isPathSelected(path);
- }
- /**
- * Returns true if the node identified by row is selected.
- *
- * @param row an integer 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 <code>Enumeration</code> of the descendants of the
- * path <code>parent</code> that
- * are currently expanded. If <code>parent</code> is not currently
- * expanded, this will return <code>null</code>.
- * If you expand/collapse nodes while
- * iterating over the returned <code>Enumeration</code>
- * this may not return all
- * the expanded paths, or may return paths that are no longer expanded.
- *
- * @param parent the path which is to be examined
- * @return an <code>Enumeration</code> of the descendents of
- * <code>parent</code>, or <code>null</code> if
- * <code>parent</code> is not currently expanded
- */
- public Enumeration getExpandedDescendants(TreePath parent) {
- if(!isExpanded(parent))
- return null;
- Enumeration toggledPaths = expandedState.keys();
- Vector elements = null;
- 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(path != parent && value != null &&
- ((Boolean)value).booleanValue() &&
- parent.isDescendant(path) && isVisible(path)) {
- if (elements == null) {
- elements = new Vector();
- }
- elements.addElement(path);
- }
- }
- }
- if (elements == null) {
- return DefaultMutableTreeNode.EMPTY_ENUMERATION;
- }
- return elements.elements();
- }
- /**
- * Returns true if the node identified by the path has ever been
- * expanded.
- * @return true if the <code>path</code> 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 <code>TreePath</code> 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) {
- Boolean value = (Boolean)expandedState.get(path);
- return (value != null && value.booleanValue());
- }
- }