This example Java source code file (JTree.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
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.*;
import javax.swing.tree.*;
import javax.swing.text.Position;
import javax.accessibility.*;
import sun.swing.SwingUtilities2;
import sun.swing.SwingUtilities2.Section;
import static sun.swing.SwingUtilities2.Section.*;
/**
* <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://docs.oracle.com/javase/tutorial/uiswing/components/tree.html">How to Use Trees,
* a section in <em>The Java Tutorial.
* <p>
* A specific node in a tree can be identified either by a
* <code>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.
* An <i>expanded node is a non-leaf node (as identified by
* <code>TreeModel.isLeaf(node) returning false) that will displays
* its children when all its ancestors are <i>expanded.
* A <i>collapsed
* node is one which hides them. A <i>hidden node is one which is
* under a collapsed ancestor. All of a <i>viewable nodes parents
* are expanded, but may or may not be displayed. A <i>displayed node
* is both viewable and in the display area, where it can be seen.
* </p>
* The following <code>JTree methods use "visible" to mean "displayed":
* <ul>
* <li>isRootVisible()
* <li>setRootVisible()
* <li>scrollPathToVisible()
* <li>scrollRowToVisible()
* <li>getVisibleRowCount()
* <li>setVisibleRowCount()
* </ul>
* The next group of <code>JTree methods use "visible" to mean
* "viewable" (under an expanded parent):
* <ul>
* <li>isVisible()
* <li>makeVisible()
* </ul>
* If you are interested in knowing when the selection changes implement
* the <code>TreeSelectionListener interface and add the instance
* using the method <code>addTreeSelectionListener.
* <code>valueChanged will be invoked when the
* selection changes, that is if the user clicks twice on the same
* node <code>valueChanged 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:
* </p>
* <pre>
* final JTree tree = ...;
*
* MouseListener ml = new MouseAdapter() {
* public void <b>mousePressed(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 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>
* <p>
* Like all <code>JComponent 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>
* <strong>Warning: Swing is not thread safe. For more
* information see <a
* href="package-summary.html#threading">Swing's Threading
* Policy</a>.
* <p>
* <strong>Warning:
* 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™
* has been added to the <code>java.beans package.
* Please see {@link java.beans.XMLEncoder}.
*</p>
* @beaninfo
* attribute: isContainer false
* description: A component that displays a set of hierarchical data as an outline.
*
* @author Rob Davis
* @author Ray Ryan
* @author Scott Violet
*/
@SuppressWarnings("serial")
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, the UI uses a default
* <code>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;
private boolean rowHeightSet = false;
/**
* Maps from <code>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<TreePath, Boolean> 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 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;
private boolean showsRootHandlesSet = false;
/**
* Creates a new event and passed it off the
* <code>selectionListeners.
*/
protected transient TreeSelectionRedirector selectionRedirector;
/**
* Editor for the entries. Default is <code>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 <code>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 <code>stopCellEditing
* is invoked, and changes are saved. If false,
* <code>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;
private boolean scrollsOnExpandSet = false;
/**
* Number of mouse clicks before a node is expanded.
*/
protected int toggleClickCount;
/**
* Updates the <code>expandedState.
*/
transient protected TreeModelListener treeModelListener;
/**
* Used when <code>setExpandedState is invoked,
* will be a <code>Stack of Stacks.
*/
transient private Stack<Stack expandedStack;
/**
* Lead selection path, may not be <code>null.
*/
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 call.
*/
private boolean settingUI;
/** If true, mouse presses on selections initiate a drag operation. */
private boolean dragEnabled;
/**
* The drop mode for this component.
*/
private DropMode dropMode = DropMode.USE_SELECTION;
/**
* The drop location.
*/
private transient DropLocation dropLocation;
/**
* A subclass of <code>TransferHandler.DropLocation representing
* a drop location for a <code>JTree.
*
* @see #getDropLocation
* @since 1.6
*/
public static final class DropLocation extends TransferHandler.DropLocation {
private final TreePath path;
private final int index;
private DropLocation(Point p, TreePath path, int index) {
super(p);
this.path = path;
this.index = index;
}
/**
* Returns the index where the dropped data should be inserted
* with respect to the path returned by <code>getPath().
* <p>
* For drop modes <code>DropMode.USE_SELECTION and
* <code>DropMode.ON, this index is unimportant (and it will
* always be <code>-1) as the only interesting data is the
* path over which the drop operation occurred.
* <p>
* For drop mode <code>DropMode.INSERT, this index
* indicates the index at which the data should be inserted into
* the parent path represented by <code>getPath().
* <code>-1 indicates that the drop occurred over the
* parent itself, and in most cases should be treated as inserting
* into either the beginning or the end of the parent's list of
* children.
* <p>
* For <code>DropMode.ON_OR_INSERT, this value will be
* an insert index, as described above, or <code>-1 if
* the drop occurred over the path itself.
*
* @return the child index
* @see #getPath
*/
public int getChildIndex() {
return index;
}
/**
* Returns the path where dropped data should be placed in the
* tree.
* <p>
* Interpretation of this value depends on the drop mode set on the
* component. If the drop mode is <code>DropMode.USE_SELECTION
* or <code>DropMode.ON, the return value is the path in the
* tree over which the data has been (or will be) dropped.
* <code>null indicates that the drop is over empty space,
* not associated with a particular path.
* <p>
* If the drop mode is <code>DropMode.INSERT, the return value
* refers to the path that should become the parent of the new data,
* in which case <code>getChildIndex() indicates where the
* new item should be inserted into this parent path. A
* <code>null path indicates that no parent path has been
* determined, which can happen for multiple reasons:
* <ul>
* <li>The tree has no model
* <li>There is no root in the tree
* <li>The root is collapsed
* <li>The root is a leaf node
* </ul>
* It is up to the developer to decide if and how they wish to handle
* the <code>null case.
* <p>
* If the drop mode is <code>DropMode.ON_OR_INSERT,
* <code>getChildIndex can be used to determine whether the
* drop is on top of the path itself (<code>-1) or the index
* at which it should be inserted into the path (values other than
* <code>-1).
*
* @return the drop path
* @see #getChildIndex
*/
public TreePath getPath() {
return path;
}
/**
* Returns a string representation of this drop location.
* This method is intended to be used for debugging purposes,
* and the content and format of the returned string may vary
* between implementations.
*
* @return a string representation of this drop location
*/
public String toString() {
return getClass().getName()
+ "[dropPoint=" + getDropPoint() + ","
+ "path=" + path + ","
+ "childIndex=" + index + "]";
}
}
/**
* The row to expand during DnD.
*/
private int expandRow = -1;
@SuppressWarnings("serial")
private class TreeTimer extends Timer {
public TreeTimer() {
super(2000, null);
setRepeats(false);
}
public void fireActionPerformed(ActionEvent ae) {
JTree.this.expandRow(expandRow);
}
}
/**
* A timer to expand nodes during drop.
*/
private TreeTimer dropTimer;
/**
* When <code>addTreeExpansionListener is invoked,
* and <code>settingUI is true, this ivar gets set to the passed in
* <code>Listener. This listener is then notified first in
* <code>fireTreeCollapsed and fireTreeExpanded.
* <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. */
public final static String CELL_RENDERER_PROPERTY = "cellRenderer";
/** Bound property name for <code>treeModel. */
public final static String TREE_MODEL_PROPERTY = "model";
/** Bound property name for <code>rootVisible. */
public final static String ROOT_VISIBLE_PROPERTY = "rootVisible";
/** Bound property name for <code>showsRootHandles. */
public final static String SHOWS_ROOT_HANDLES_PROPERTY = "showsRootHandles";
/** Bound property name for <code>rowHeight. */
public final static String ROW_HEIGHT_PROPERTY = "rowHeight";
/** Bound property name for <code>cellEditor. */
public final static String CELL_EDITOR_PROPERTY = "cellEditor";
/** Bound property name for <code>editable. */
public final static String EDITABLE_PROPERTY = "editable";
/** Bound property name for <code>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 <code>visibleRowCount. */
public final static String VISIBLE_ROW_COUNT_PROPERTY = "visibleRowCount";
/** Bound property name for <code>messagesStopCellEditing. */
public final static String INVOKES_STOP_CELL_EDITING_PROPERTY = "invokesStopCellEditing";
/** Bound property name for <code>scrollsOnExpand. */
public final static String SCROLLS_ON_EXPAND_PROPERTY = "scrollsOnExpand";
/** Bound property name for <code>toggleClickCount. */
public final static String TOGGLE_CLICK_COUNT_PROPERTY = "toggleClickCount";
/** Bound property name for <code>leadSelectionPath.
* @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.
* Used primarily for beanbuilders to show something interesting.
*
* @return the default <code>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 <code>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
* a value of {@code "root"}.
*
* @param value the <code>Object used as the foundation for
* the <code>TreeModel
* @return a <code>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 <code>JTree 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 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>Objects
* @see DefaultTreeModel#asksAllowsChildren
*/
public JTree(Object[] value) {
this(createTreeModel(value));
this.setRootVisible(false);
this.setShowsRootHandles(true);
expandRoot();
}
/**
* Returns a <code>JTree with each element of the specified
* <code>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 <code>Vector
* @see DefaultTreeModel#asksAllowsChildren
*/
public JTree(Vector<?> value) {
this(createTreeModel(value));
this.setRootVisible(false);
this.setShowsRootHandles(true);
expandRoot();
}
/**
* Returns a <code>JTree created from a Hashtable
* which does not display with root.
* Each value-half of the key/value pairs in the <code>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 <code>Hashtable
* @see DefaultTreeModel#asksAllowsChildren
*/
public JTree(Hashtable<?,?> value) {
this(createTreeModel(value));
this.setRootVisible(false);
this.setShowsRootHandles(true);
expandRoot();
}
/**
* Returns a <code>JTree with the specified
* <code>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 <code>TreeNode object
* @see DefaultTreeModel#asksAllowsChildren
*/
public JTree(TreeNode root) {
this(root, false);
}
/**
* Returns a <code>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 <code>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
* @see DefaultTreeModel#asksAllowsChildren
*/
public JTree(TreeNode root, boolean asksAllowsChildren) {
this(new DefaultTreeModel(root, asksAllowsChildren));
}
/**
* Returns an instance of <code>JTree which displays the root node
* -- the tree is created using the specified data model.
*
* @param newModel the <code>TreeModel to use as the data model
*/
@ConstructorProperties({"model"})
public JTree(TreeModel newModel) {
super();
expandedStack = new Stack<Stack();
toggleClickCount = 2;
expandedState = new Hashtable<TreePath, Boolean>();
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 object that renders this component
*/
public TreeUI getUI() {
return (TreeUI)ui;
}
/**
* Sets the L&F object that renders this component.
* <p>
* This is a bound property.
*
* @param ui the <code>TreeUI 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 (this.ui != ui) {
settingUI = true;
uiTreeExpansionListener = null;
try {
super.setUI(ui);
}
finally {
settingUI = false;
}
}
}
/**
* Notification from the <code>UIManager that the L&F has changed.
* Replaces the current UI object with the latest version from the
* <code>UIManager.
*
* @see JComponent#updateUI
*/
public void updateUI() {
setUI((TreeUI)UIManager.getUI(this));
SwingUtilities.updateRendererOrEditorUI(getCellRenderer());
SwingUtilities.updateRendererOrEditorUI(getCellEditor());
}
/**
* 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
* that is rendering each cell.
*
* @return the <code>TreeCellRenderer that is rendering each cell
*/
public TreeCellRenderer getCellRenderer() {
return cellRenderer;
}
/**
* Sets the <code>TreeCellRenderer that will be used to
* draw each cell.
* <p>
* This is a bound property.
*
* @param x the <code>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.
* <p>
* This is a bound property.
*
* @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 value implies that the
* tree cannot be edited. If this represents a change in the
* <code>cellEditor, the propertyChange
* method is invoked on all listeners.
* <p>
* This is a bound property.
*
* @param cellEditor the <code>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 <code>TreeCellEditor in use,
* or <code>null if the tree cannot be edited
*/
public TreeCellEditor getCellEditor() {
return cellEditor;
}
/**
* Returns the <code>TreeModel that is providing the data.
*
* @return the <code>TreeModel that is providing the data
*/
public TreeModel getModel() {
return treeModel;
}
/**
* Sets the <code>TreeModel that will provide the data.
* <p>
* This is a bound property.
*
* @param newModel the <code>TreeModel that is to provide the data
* @beaninfo
* bound: true
* description: The TreeModel that will provide the data.
*/
public void setModel(TreeModel newModel) {
clearSelection();
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();
}
/**
* 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 is visible.
* <p>
* This is a bound property.
*
* @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 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.
* Some look and feels might not support handles;
* they will ignore this property.
* <p>
* This is a bound property.
*
* @param newValue <code>true if root handles should be displayed;
* otherwise, <code>false
* @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;
showsRootHandlesSet = true;
firePropertyChange(SHOWS_ROOT_HANDLES_PROPERTY, oldValue,
showsRootHandles);
if (accessibleContext != null) {
((AccessibleJTree)accessibleContext).fireVisibleDataPropertyChange();
}
invalidate();
}
/**
* Returns the value of the <code>showsRootHandles property.
*
* @return the value of the <code>showsRootHandles 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.
* <p>
* This is a bound property.
*
* @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;
rowHeightSet = true;
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.
* <p>
* This is a bound 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 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 is invoked
* when editing is interrupted, and data is saved; false means that
* <code>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;
}
/**
* Sets the <code>scrollsOnExpand property,
* which determines whether the
* tree might scroll to show previously hidden children.
* If this property is <code>true (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.
* <p>
* This is a bound property.
*
* @param newValue <code>false to disable scrolling on expansion;
* <code>true 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;
scrollsOnExpandSet = true;
firePropertyChange(SCROLLS_ON_EXPAND_PROPERTY, oldValue,
newValue);
}
/**
* Returns the value of the <code>scrollsOnExpand property.
*
* @return the value of the <code>scrollsOnExpand property
*/
public boolean getScrollsOnExpand() {
return scrollsOnExpand;
}
/**
* Sets the number of mouse clicks before a node will expand or close.
* The default is two.
* <p>
* This is a bound property.
*
* @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 property. If
* true, any time the selection is changed, either via the
* <code>TreeSelectionModel, or the cover methods provided by
* <code>JTree, the TreePaths 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). 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).
* <p>
* This is a bound property.
*
* @param newValue the new value for <code>expandsSelectedPaths
*
* @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 property.
* @return true if selection changes result in the parent path being
* expanded
* @since 1.3
* @see #setExpandsSelectedPaths
*/
public boolean getExpandsSelectedPaths() {
return expandsSelectedPaths;
}
/**
* Turns on or off automatic drag handling. In order to enable automatic
* drag handling, this property should be set to {@code true}, and the
* tree's {@code TransferHandler} needs to be {@code non-null}.
* The default value of the {@code dragEnabled} property is {@code false}.
* <p>
* The job of honoring this property, and recognizing a user drag gesture,
* lies with the look and feel implementation, and in particular, the tree's
* {@code TreeUI}. When automatic drag handling is enabled, most look and
* feels (including those that subclass {@code BasicLookAndFeel}) begin a
* drag and drop operation whenever the user presses the mouse button over
* an item and then moves the mouse a few pixels. Setting this property to
* {@code true} can therefore have a subtle effect on how selections behave.
* <p>
* If a look and feel is used that ignores this property, you can still
* begin a drag and drop operation by calling {@code exportAsDrag} on the
* tree's {@code TransferHandler}.
*
* @param b whether or not to enable automatic drag handling
* @exception HeadlessException if
* <code>b is true and
* <code>GraphicsEnvironment.isHeadless()
* returns <code>true
* @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;
}
/**
* Returns whether or not automatic drag handling is enabled.
*
* @return the value of the {@code dragEnabled} property
* @see #setDragEnabled
* @since 1.4
*/
public boolean getDragEnabled() {
return dragEnabled;
}
/**
* Sets the drop mode for this component. For backward compatibility,
* the default for this property is <code>DropMode.USE_SELECTION.
* Usage of one of the other modes is recommended, however, for an
* improved user experience. <code>DropMode.ON, for instance,
* offers similar behavior of showing items as selected, but does so without
* affecting the actual selection in the tree.
* <p>
* <code>JTree supports the following drop modes:
* <ul>
* <li>DropMode.USE_SELECTION
* <li>DropMode.ON
* <li>DropMode.INSERT
* <li>DropMode.ON_OR_INSERT
* </ul>
* <p>
* The drop mode is only meaningful if this component has a
* <code>TransferHandler that accepts drops.
*
* @param dropMode the drop mode to use
* @throws IllegalArgumentException if the drop mode is unsupported
* or <code>null
* @see #getDropMode
* @see #getDropLocation
* @see #setTransferHandler
* @see TransferHandler
* @since 1.6
*/
public final void setDropMode(DropMode dropMode) {
if (dropMode != null) {
switch (dropMode) {
case USE_SELECTION:
case ON:
case INSERT:
case ON_OR_INSERT:
this.dropMode = dropMode;
return;
}
}
throw new IllegalArgumentException(dropMode + ": Unsupported drop mode for tree");
}
/**
* Returns the drop mode for this component.
*
* @return the drop mode for this component
* @see #setDropMode
* @since 1.6
*/
public final DropMode getDropMode() {
return dropMode;
}
/**
* Calculates a drop location in this component, representing where a
* drop at the given point should insert data.
*
* @param p the point to calculate a drop location for
* @return the drop location, or <code>null
*/
DropLocation dropLocationForPoint(Point p) {
DropLocation location = null;
int row = getClosestRowForLocation(p.x, p.y);
Rectangle bounds = getRowBounds(row);
TreeModel model = getModel();
Object root = (model == null) ? null : model.getRoot();
TreePath rootPath = (root == null) ? null : new TreePath(root);
TreePath child;
TreePath parent;
boolean outside = row == -1
|| p.y < bounds.y
|| p.y >= bounds.y + bounds.height;
switch(dropMode) {
case USE_SELECTION:
case ON:
if (outside) {
location = new DropLocation(p, null, -1);
} else {
location = new DropLocation(p, getPathForRow(row), -1);
}
break;
case INSERT:
case ON_OR_INSERT:
if (row == -1) {
if (root != null && !model.isLeaf(root) && isExpanded(rootPath)) {
location = new DropLocation(p, rootPath, 0);
} else {
location = new DropLocation(p, null, -1);
}
break;
}
boolean checkOn = dropMode == DropMode.ON_OR_INSERT
|| !model.isLeaf(getPathForRow(row).getLastPathComponent());
Section section = SwingUtilities2.liesInVertical(bounds, p, checkOn);
if(section == LEADING) {
child = getPathForRow(row);
parent = child.getParentPath();
} else if (section == TRAILING) {
int index = row + 1;
if (index >= getRowCount()) {
if (model.isLeaf(root) || !isExpanded(rootPath)) {
location = new DropLocation(p, null, -1);
} else {
parent = rootPath;
index = model.getChildCount(root);
location = new DropLocation(p, parent, index);
}
break;
}
child = getPathForRow(index);
parent = child.getParentPath();
} else {
assert checkOn;
location = new DropLocation(p, getPathForRow(row), -1);
break;
}
if (parent != null) {
location = new DropLocation(p, parent,
model.getIndexOfChild(parent.getLastPathComponent(),
child.getLastPathComponent()));
} else if (checkOn || !model.isLeaf(root)) {
location = new DropLocation(p, rootPath, -1);
} else {
location = new DropLocation(p, null, -1);
}
break;
default:
assert false : "Unexpected drop mode";
}
if (outside || row != expandRow) {
cancelDropTimer();
}
if (!outside && row != expandRow) {
if (isCollapsed(row)) {
expandRow = row;
startDropTimer();
}
}
return location;
}
/**
* Called to set or clear the drop location during a DnD operation.
* In some cases, the component may need to use it's internal selection
* temporarily to indicate the drop location. To help facilitate this,
* this method returns and accepts as a parameter a state object.
* This state object can be used to store, and later restore, the selection
* state. Whatever this method returns will be passed back to it in
* future calls, as the state parameter. If it wants the DnD system to
* continue storing the same state, it must pass it back every time.
* Here's how this is used:
* <p>
* Let's say that on the first call to this method the component decides
* to save some state (because it is about to use the selection to show
* a drop index). It can return a state object to the caller encapsulating
* any saved selection state. On a second call, let's say the drop location
* is being changed to something else. The component doesn't need to
* restore anything yet, so it simply passes back the same state object
* to have the DnD system continue storing it. Finally, let's say this
* method is messaged with <code>null. This means DnD
* is finished with this component for now, meaning it should restore
* state. At this point, it can use the state parameter to restore
* said state, and of course return <code>null since there's
* no longer anything to store.
*
* @param location the drop location (as calculated by
* <code>dropLocationForPoint) or null
* if there's no longer a valid drop location
* @param state the state object saved earlier for this component,
* or <code>null
* @param forDrop whether or not the method is being called because an
* actual drop occurred
* @return any saved state for this component, or <code>null if none
*/
Object setDropLocation(TransferHandler.DropLocation location,
Object state,
boolean forDrop) {
Object retVal = null;
DropLocation treeLocation = (DropLocation)location;
if (dropMode == DropMode.USE_SELECTION) {
if (treeLocation == null) {
if (!forDrop && state != null) {
setSelectionPaths(((TreePath[][])state)[0]);
setAnchorSelectionPath(((TreePath[][])state)[1][0]);
setLeadSelectionPath(((TreePath[][])state)[1][1]);
}
} else {
if (dropLocation == null) {
TreePath[] paths = getSelectionPaths();
if (paths == null) {
paths = new TreePath[0];
}
retVal = new TreePath[][] {paths,
{getAnchorSelectionPath(), getLeadSelectionPath()}};
} else {
retVal = state;
}
setSelectionPath(treeLocation.getPath());
}
}
DropLocation old = dropLocation;
dropLocation = treeLocation;
firePropertyChange("dropLocation", old, dropLocation);
return retVal;
}
/**
* Called to indicate to this component that DnD is done.
* Allows for us to cancel the expand timer.
*/
void dndDone() {
cancelDropTimer();
dropTimer = null;
}
/**
* Returns the location that this component should visually indicate
* as the drop location during a DnD operation over the component,
* or {@code null} if no location is to currently be shown.
* <p>
* This method is not meant for querying the drop location
* from a {@code TransferHandler}, as the drop location is only
* set after the {@code TransferHandler}'s <code>canImport
* has returned and has allowed for the location to be shown.
* <p>
* When this property changes, a property change event with
* name "dropLocation" is fired by the component.
*
* @return the drop location
* @see #setDropMode
* @see TransferHandler#canImport(TransferHandler.TransferSupport)
* @since 1.6
*/
public final DropLocation getDropLocation() {
return dropLocation;
}
private void startDropTimer() {
if (dropTimer == null) {
dropTimer = new TreeTimer();
}
dropTimer.start();
}
private void cancelDropTimer() {
if (dropTimer != null && dropTimer.isRunning()) {
expandRow = -1;
dropTimer.stop();
}
}
/**
* Returns <code>isEditable. 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's getToolTipText
* method in order to allow
* renderer's tips to be used if it has text set.
* <p>
* NOTE: For <code>JTree to properly display tooltips of its
* renderers, <code>JTree must be a registered component with the
* <code>ToolTipManager. This can be done by invoking
* <code>ToolTipManager.sharedInstance().registerComponent(tree).
* This is not done automatically!
*
* @param event the <code>MouseEvent that initiated the
* <code>ToolTip display
* @return a string containing the tooltip or <code>null
* if <code>event is null
*/
public String getToolTipText(MouseEvent event) {
String tip = null;
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.getXOnScreen(),
event.getYOnScreen(),
event.getClickCount(),
event.isPopupTrigger(),
MouseEvent.NOBUTTON);
tip = ((JComponent)rComponent).getToolTipText(newEvent);
}
}
}
// No tip from the renderer get our own tip
if (tip == null) {
tip = getToolTipText();
}
return tip;
}
/**
* Called by the renderers to convert the specified value to
* text. This implementation returns <code>value.toString, ignoring
* all other arguments. To control the conversion, subclass this
* method and use any of the arguments you need.
*
* @param value the <code>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 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 representation of the node's value
*/
public String convertValueToText(Object value, boolean selected,
boolean expanded, boolean leaf, int row,
boolean hasFocus) {
if(value != null) {
String sValue = value.toString();
if (sValue != null) {
return sValue;
}
}
return "";
}
//
// The following are convenience methods that get forwarded to the
// current TreeUI.
//
/**
* Returns the number of viewable nodes. A node is viewable if all of its
* parents are expanded. The root is only included in this count if
* {@code isRootVisible()} is {@code true}. This returns {@code 0} if
* the UI has not been set.
*
* @return the number of viewable nodes
*/
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 is true it is
* exposed (made viewable).
*
* @param path the <code>TreePath 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 is true
* it is exposed (made viewable).
*
* @param paths an array of <code>TreePath 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,
* rather the UI will update it.
* <p>
* This is a bound property.
*
* @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, rather the UI will
* update it.
* <p>
* This is a bound property.
*
* @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 is
* < 0 or >= <code>getRowCount, it will be ignored.
* If none of the elements
* in <code>rows are valid rows, the selection will
* be cleared. That is it will be as if <code>clearSelection
* 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
* to the current selection. If any component of the path isn't
* viewable, and <code>getExpandsSelectedPaths is true it is
* made viewable.
* <p>
* Note that <code>JTree 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 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 is true, it is
* made viewable.
* <p>
* Note that <code>JTree 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 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 of the selected path. This is
* a convenience method for
* {@code getSelectionModel().getSelectionPath().getLastPathComponent()}.
* This is typically only useful if the selection has one path.
*
* @return the last path component of the selected path, or
* <code>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 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 for the first selected node,
* or <code>null if nothing is currently selected
*/
public TreePath getSelectionPath() {
return getSelectionModel().getSelectionPath();
}
/**
* Returns the paths of all selected values.
*
* @return an array of <code>TreePath objects indicating the selected
* nodes, or <code>null if nothing is currently selected
*/
public TreePath[] getSelectionPaths() {
TreePath[] selectionPaths = getSelectionModel().getSelectionPaths();
return (selectionPaths != null && selectionPaths.length > 0) ? selectionPaths : null;
}
/**
* Returns all of the currently selected rows. This method is simply
* forwarded to the <code>TreeSelectionModel.
* If nothing is selected <code>null or an empty array will
* be returned, based on the <code>TreeSelectionModel
* 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();
}
/**
* Returns the smallest selected row. If the selection is empty, or
* none of the selected paths are viewable, {@code -1} is returned.
*
* @return the smallest selected row
*/
public int getMinSelectionRow() {
return getSelectionModel().getMinSelectionRow();
}
/**
* Returns the largest selected row. If the selection is empty, or
* none of the selected paths are viewable, {@code -1} is returned.
*
* @return the largest selected row
*/
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 is null
*/
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 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 of the descendants of the
* path <code>parent that
* are currently expanded. If <code>parent is not currently
* expanded, this will return <code>null.
* If you expand/collapse nodes while
* iterating over the returned <code>Enumeration
* 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 of the descendents of
* <code>parent, or null if
* <code>parent is not currently expanded
*/
public Enumeration<TreePath> getExpandedDescendants(TreePath parent) {
if(!isExpanded(parent))
return null;
Enumeration<TreePath> toggledPaths = expandedState.keys();
Vector<TreePath> elements = null;
TreePath path;
Object value;
if(toggledPaths != null) {
while(toggledPaths.hasMoreElements()) {
path = 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<TreePath>();
}
elements.addElement(path);
}
}
}
if (elements == null) {
Set<TreePath> empty = Collections.emptySet();
return Collections.enumeration(empty);
}
return elements.elements();
}
/**
* Returns true if the node identified by the path has ever been
* expanded.
* @return true if the <code>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 <code>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;
Object value;
do{
value = expandedState.get(path);
if(value == null || !((Boolean)value).booleanValue())
return false;
} while( (path=path.getParentPath())!=null );
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 = expandedState.get(path);
return (value != null && value.booleanValue());
}
}
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 <code>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 <code>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 expanded.
* 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 <code>Rectangle that the specified node will be drawn
* into. Returns <code>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 <code>TreePath identifying the node
* @return the <code>Rectangle the node is drawn in,
* or <code>null
*/
public Rectangle getPathBounds(TreePath path) {
TreeUI tree = getUI();
if(tree != null)
return tree.getPathBounds(this, path);
return null;
}
/**
* Returns the <code>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 <code>Rectangle the node is drawn in
*/
public Rectangle getRowBounds(int 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
* <code>JTree is contained in a JScrollPane.
*
* @param path the <code>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 <code>JTree is contained in a
* <code>JScrollPane.
*
* @param row an integer 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 <code>row is
* not visible, or a {@code TreeUI} has not been set, <code>null
* is returned.
*
* @param row an integer specifying a row
* @return the <code>TreePath to the specified node,
* <code>null if row < 0
* or <code>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 <code>TreePath identifying a node
* @return an integer 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. If the last item in the path is a
* leaf, this will have no effect.
*
* @param path the <code>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 is < 0 or >= getRowCount this
* will have no effect.
*
* @param row an integer 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 <code>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 is < 0 or >= getRowCount this
* will have no effect.
*
* @param row an integer 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 integer giving the number of pixels horizontally from
* the left edge of the display area, minus any left margin
* @param y an integer giving the number of pixels vertically from
* the top of the display area, minus any top margin
* @return the <code>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(pathBounds != null &&
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 integer giving the number of pixels horizontally from
* the left edge of the display area, minus any left margin
* @param y an integer 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
* <code>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 integer giving the number of pixels horizontally from
* the left edge of the display area, minus any left margin
* @param y an integer giving the number of pixels vertically from
* the top of the display area, minus any top margin
* @return the <code>TreePath for the node closest to that location,
* <code>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 integer giving the number of pixels horizontally from
* the left edge of the display area, minus any left margin
* @param y an integer 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.
*
* @return true if the user is currently editing a node
* @see #getSelectionPath
*/
public boolean isEditing() {
TreeUI tree = getUI();
if(tree != null)
return tree.isEditing(this);
return false;
}
/**
* Ends the current editing session.
* (The <code>DefaultTreeCellEditor
* object saves any edits that are currently in progress on a cell.
* Other implementations may operate differently.)
* Has no effect if the tree isn't being edited.
* <blockquote>
* <b>Note:
* To make edit-saves automatic whenever the user changes
* their position in the tree, use {@link #setInvokesStopCellEditing}.
* </blockquote>
*
* @return true if editing was in progress and is now stopped,
* false if editing was not in progress
*/
public boolean stopEditing() {
TreeUI tree = getUI();
if(tree != null)
return tree.stopEditing(this);
return false;
}
/**
* Cancels the current editing session. Has no effect if the
* tree isn't being edited.
*/
public void cancelEditing() {
TreeUI tree = getUI();
if(tree != null)
tree.cancelEditing(this);
}
/**
* Selects the node identified by the specified path and initiates
* editing. The edit-attempt fails if the <code>CellEditor
* does not allow
* editing for the specified item.
*
* @param path the <code>TreePath identifying a node
*/
public void startEditingAtPath(TreePath path) {
TreeUI tree = getUI();
if(tree != null)
tree.startEditingAtPath(this, path);
}
/**
* Returns the path to the element that is currently being edited.
*
* @return the <code>TreePath for the node being edited
*/
public TreePath getEditingPath() {
TreeUI tree = getUI();
if(tree != null)
return tree.getEditingPath(this);
return null;
}
//
// Following are primarily convenience methods for mapping from
// row based selections to path selections. Sometimes it is
// easier to deal with these than paths (mouse downs, key downs
// usually just deal with index based selections).
// Since row based selections require a UI many of these won't work
// without one.
//
/**
* Sets the tree's selection model. When a <code>null value is
* specified an empty
* <code>selectionModel is used, which does not allow selections.
* <p>
* This is a bound property.
*
* @param selectionModel the <code>TreeSelectionModel to use,
* or <code>null to disable selections
* @see TreeSelectionModel
* @beaninfo
* bound: true
* description: The tree's selection model.
*/
public void setSelectionModel(TreeSelectionModel selectionModel) {
if(selectionModel == null)
selectionModel = EmptySelectionModel.sharedInstance();
TreeSelectionModel oldValue = this.selectionModel;
if (this.selectionModel != null && selectionRedirector != null) {
this.selectionModel.removeTreeSelectionListener
(selectionRedirector);
}
if (accessibleContext != null) {
this.selectionModel.removeTreeSelectionListener((TreeSelectionListener)accessibleContext);
selectionModel.addTreeSelectionListener((TreeSelectionListener)accessibleContext);
}
this.selectionModel = selectionModel;
if (selectionRedirector != null) {
this.selectionModel.addTreeSelectionListener(selectionRedirector);
}
firePropertyChange(SELECTION_MODEL_PROPERTY, oldValue,
this.selectionModel);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY,
Boolean.valueOf(false), Boolean.valueOf(true));
}
}
/**
* Returns the model for selections. This should always return a
* non-<code>null value. If you don't want to allow anything
* to be selected
* set the selection model to <code>null, which forces an empty
* selection model to be used.
*
* @see #setSelectionModel
*/
public TreeSelectionModel getSelectionModel() {
return selectionModel;
}
/**
* Returns the paths (inclusive) between the specified rows. If
* the specified indices are within the viewable set of rows, or
* bound the viewable set of rows, then the indices are
* constrained by the viewable set of rows. If the specified
* indices are not within the viewable set of rows, or do not
* bound the viewable set of rows, then an empty array is
* returned. For example, if the row count is {@code 10}, and this
* method is invoked with {@code -1, 20}, then the specified
* indices are constrained to the viewable set of rows, and this is
* treated as if invoked with {@code 0, 9}. On the other hand, if
* this were invoked with {@code -10, -1}, then the specified
* indices do not bound the viewable set of rows, and an empty
* array is returned.
* <p>
* The parameters are not order dependent. That is, {@code
* getPathBetweenRows(x, y)} is equivalent to
* {@code getPathBetweenRows(y, x)}.
* <p>
* An empty array is returned if the row count is {@code 0}, or
* the specified indices do not bound the viewable set of rows.
*
* @param index0 the first index in the range
* @param index1 the last index in the range
* @return the paths (inclusive) between the specified row indices
*/
protected TreePath[] getPathBetweenRows(int index0, int index1) {
TreeUI tree = getUI();
if (tree != null) {
int rowCount = getRowCount();
if (rowCount > 0 && !((index0 < 0 && index1 < 0) ||
(index0 >= rowCount && index1 >= rowCount))){
index0 = Math.min(rowCount - 1, Math.max(index0, 0));
index1 = Math.min(rowCount - 1, Math.max(index1, 0));
int minIndex = Math.min(index0, index1);
int maxIndex = Math.max(index0, index1);
TreePath[] selection = new TreePath[
maxIndex - minIndex + 1];
for(int counter = minIndex; counter <= maxIndex; counter++) {
selection[counter - minIndex] =
tree.getPathForRow(this, counter);
}
return selection;
}
}
return new TreePath[0];
}
/**
* Selects the rows in the specified interval (inclusive). If
* the specified indices are within the viewable set of rows, or bound
* the viewable set of rows, then the specified rows are constrained by
* the viewable set of rows. If the specified indices are not within the
* viewable set of rows, or do not bound the viewable set of rows, then
* the selection is cleared. For example, if the row count is {@code
* 10}, and this method is invoked with {@code -1, 20}, then the
* specified indices bounds the viewable range, and this is treated as
* if invoked with {@code 0, 9}. On the other hand, if this were
* invoked with {@code -10, -1}, then the specified indices do not
* bound the viewable set of rows, and the selection is cleared.
* <p>
* The parameters are not order dependent. That is, {@code
* setSelectionInterval(x, y)} is equivalent to
* {@code setSelectionInterval(y, x)}.
*
* @param index0 the first index in the range to select
* @param index1 the last index in the range to select
*/
public void setSelectionInterval(int index0, int index1) {
TreePath[] paths = getPathBetweenRows(index0, index1);
this.getSelectionModel().setSelectionPaths(paths);
}
/**
* Adds the specified rows (inclusive) to the selection. If the
* specified indices are within the viewable set of rows, or bound
* the viewable set of rows, then the specified indices are
* constrained by the viewable set of rows. If the indices are not
* within the viewable set of rows, or do not bound the viewable
* set of rows, then the selection is unchanged. For example, if
* the row count is {@code 10}, and this method is invoked with
* {@code -1, 20}, then the specified indices bounds the viewable
* range, and this is treated as if invoked with {@code 0, 9}. On
* the other hand, if this were invoked with {@code -10, -1}, then
* the specified indices do not bound the viewable set of rows,
* and the selection is unchanged.
* <p>
* The parameters are not order dependent. That is, {@code
* addSelectionInterval(x, y)} is equivalent to
* {@code addSelectionInterval(y, x)}.
*
* @param index0 the first index in the range to add to the selection
* @param index1 the last index in the range to add to the selection
*/
public void addSelectionInterval(int index0, int index1) {
TreePath[] paths = getPathBetweenRows(index0, index1);
if (paths != null && paths.length > 0) {
this.getSelectionModel().addSelectionPaths(paths);
}
}
/**
* Removes the specified rows (inclusive) from the selection. If
* the specified indices are within the viewable set of rows, or bound
* the viewable set of rows, then the specified indices are constrained by
* the viewable set of rows. If the specified indices are not within the
* viewable set of rows, or do not bound the viewable set of rows, then
* the selection is unchanged. For example, if the row count is {@code
* 10}, and this method is invoked with {@code -1, 20}, then the
* specified range bounds the viewable range, and this is treated as
* if invoked with {@code 0, 9}. On the other hand, if this were
* invoked with {@code -10, -1}, then the specified range does not
* bound the viewable set of rows, and the selection is unchanged.
* <p>
* The parameters are not order dependent. That is, {@code
* removeSelectionInterval(x, y)} is equivalent to
* {@code removeSelectionInterval(y, x)}.
*
* @param index0 the first row to remove from the selection
* @param index1 the last row to remove from the selection
*/
public void removeSelectionInterval(int index0, int index1) {
TreePath[] paths = getPathBetweenRows(index0, index1);
if (paths != null && paths.length > 0) {
this.getSelectionModel().removeSelectionPaths(paths);
}
}
/**
* Removes the node identified by the specified path from the current
* selection.
*
* @param path the <code>TreePath identifying a node
*/
public void removeSelectionPath(TreePath path) {
this.getSelectionModel().removeSelectionPath(path);
}
/**
* Removes the nodes identified by the specified paths from the
* current selection.
*
* @param paths an array of <code>TreePath objects that
* specifies the nodes to remove
*/
public void removeSelectionPaths(TreePath[] paths) {
this.getSelectionModel().removeSelectionPaths(paths);
}
/**
* Removes the row at the index <code>row from the current
* selection.
*
* @param row the row to remove
*/
public void removeSelectionRow(int row) {
int[] rows = { row };
removeSelectionRows(rows);
}
/**
* Removes the rows that are selected at each of the specified
* rows.
*
* @param rows an array of ints specifying display rows, where 0 is
* the first row in the display
*/
public void removeSelectionRows(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]);
removeSelectionPaths(paths);
}
}
/**
* Clears the selection.
*/
public void clearSelection() {
getSelectionModel().clearSelection();
}
/**
* Returns true if the selection is currently empty.
*
* @return true if the selection is currently empty
*/
public boolean isSelectionEmpty() {
return getSelectionModel().isSelectionEmpty();
}
/**
* Adds a listener for <code>TreeExpansion events.
*
* @param tel a TreeExpansionListener that will be notified when
* a tree node is expanded or collapsed (a "negative
* expansion")
*/
public void addTreeExpansionListener(TreeExpansionListener tel) {
if (settingUI) {
uiTreeExpansionListener = tel;
}
listenerList.add(TreeExpansionListener.class, tel);
}
/**
* Removes a listener for <code>TreeExpansion events.
*
* @param tel the <code>TreeExpansionListener to remove
*/
public void removeTreeExpansionListener(TreeExpansionListener tel) {
listenerList.remove(TreeExpansionListener.class, tel);
if (uiTreeExpansionListener == tel) {
uiTreeExpansionListener = null;
}
}
/**
* Returns an array of all the <code>TreeExpansionListeners added
* to this JTree with addTreeExpansionListener().
*
* @return all of the <code>TreeExpansionListeners added or an empty
* array if no listeners have been added
* @since 1.4
*/
public TreeExpansionListener[] getTreeExpansionListeners() {
return listenerList.getListeners(TreeExpansionListener.class);
}
/**
* Adds a listener for <code>TreeWillExpand events.
*
* @param tel a <code>TreeWillExpandListener that will be notified
* when a tree node will be expanded or collapsed (a "negative
* expansion")
*/
public void addTreeWillExpandListener(TreeWillExpandListener tel) {
listenerList.add(TreeWillExpandListener.class, tel);
}
/**
* Removes a listener for <code>TreeWillExpand events.
*
* @param tel the <code>TreeWillExpandListener to remove
*/
public void removeTreeWillExpandListener(TreeWillExpandListener tel) {
listenerList.remove(TreeWillExpandListener.class, tel);
}
/**
* Returns an array of all the <code>TreeWillExpandListeners added
* to this JTree with addTreeWillExpandListener().
*
* @return all of the <code>TreeWillExpandListeners added or an empty
* array if no listeners have been added
* @since 1.4
*/
public TreeWillExpandListener[] getTreeWillExpandListeners() {
return listenerList.getListeners(TreeWillExpandListener.class);
}
/**
* Notifies all listeners that have registered interest for
* notification on this event type. The event instance
* is lazily created using the <code>path parameter.
*
* @param path the <code>TreePath indicating the node that was
* expanded
* @see EventListenerList
*/
public void fireTreeExpanded(TreePath path) {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
TreeExpansionEvent e = null;
if (uiTreeExpansionListener != null) {
e = new TreeExpansionEvent(this, path);
uiTreeExpansionListener.treeExpanded(e);
}
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==TreeExpansionListener.class &&
listeners[i + 1] != uiTreeExpansionListener) {
// Lazily create the event:
if (e == null)
e = new TreeExpansionEvent(this, path);
((TreeExpansionListener)listeners[i+1]).
treeExpanded(e);
}
}
}
/**
* Notifies all listeners that have registered interest for
* notification on this event type. The event instance
* is lazily created using the <code>path parameter.
*
* @param path the <code>TreePath indicating the node that was
* collapsed
* @see EventListenerList
*/
public void fireTreeCollapsed(TreePath path) {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
TreeExpansionEvent e = null;
if (uiTreeExpansionListener != null) {
e = new TreeExpansionEvent(this, path);
uiTreeExpansionListener.treeCollapsed(e);
}
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==TreeExpansionListener.class &&
listeners[i + 1] != uiTreeExpansionListener) {
// Lazily create the event:
if (e == null)
e = new TreeExpansionEvent(this, path);
((TreeExpansionListener)listeners[i+1]).
treeCollapsed(e);
}
}
}
/**
* Notifies all listeners that have registered interest for
* notification on this event type. The event instance
* is lazily created using the <code>path parameter.
*
* @param path the <code>TreePath indicating the node that was
* expanded
* @see EventListenerList
*/
public void fireTreeWillExpand(TreePath path) throws ExpandVetoException {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
TreeExpansionEvent e = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==TreeWillExpandListener.class) {
// Lazily create the event:
if (e == null)
e = new TreeExpansionEvent(this, path);
((TreeWillExpandListener)listeners[i+1]).
treeWillExpand(e);
}
}
}
/**
* Notifies all listeners that have registered interest for
* notification on this event type. The event instance
* is lazily created using the <code>path parameter.
*
* @param path the <code>TreePath indicating the node that was
* expanded
* @see EventListenerList
*/
public void fireTreeWillCollapse(TreePath path) throws ExpandVetoException {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
TreeExpansionEvent e = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==TreeWillExpandListener.class) {
// Lazily create the event:
if (e == null)
e = new TreeExpansionEvent(this, path);
((TreeWillExpandListener)listeners[i+1]).
treeWillCollapse(e);
}
}
}
/**
* Adds a listener for <code>TreeSelection events.
*
* @param tsl the <code>TreeSelectionListener that will be notified
* when a node is selected or deselected (a "negative
* selection")
*/
public void addTreeSelectionListener(TreeSelectionListener tsl) {
listenerList.add(TreeSelectionListener.class,tsl);
if(listenerList.getListenerCount(TreeSelectionListener.class) != 0
&& selectionRedirector == null) {
selectionRedirector = new TreeSelectionRedirector();
selectionModel.addTreeSelectionListener(selectionRedirector);
}
}
/**
* Removes a <code>TreeSelection listener.
*
* @param tsl the <code>TreeSelectionListener to remove
*/
public void removeTreeSelectionListener(TreeSelectionListener tsl) {
listenerList.remove(TreeSelectionListener.class,tsl);
if(listenerList.getListenerCount(TreeSelectionListener.class) == 0
&& selectionRedirector != null) {
selectionModel.removeTreeSelectionListener
(selectionRedirector);
selectionRedirector = null;
}
}
/**
* Returns an array of all the <code>TreeSelectionListeners added
* to this JTree with addTreeSelectionListener().
*
* @return all of the <code>TreeSelectionListeners added or an empty
* array if no listeners have been added
* @since 1.4
*/
public TreeSelectionListener[] getTreeSelectionListeners() {
return listenerList.getListeners(TreeSelectionListener.class);
}
/**
* Notifies all listeners that have registered interest for
* notification on this event type.
*
* @param e the <code>TreeSelectionEvent to be fired;
* generated by the
* <code>TreeSelectionModel
* when a node is selected or deselected
* @see EventListenerList
*/
protected void fireValueChanged(TreeSelectionEvent e) {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
// TreeSelectionEvent e = null;
if (listeners[i]==TreeSelectionListener.class) {
// Lazily create the event:
// if (e == null)
// e = new ListSelectionEvent(this, firstIndex, lastIndex);
((TreeSelectionListener)listeners[i+1]).valueChanged(e);
}
}
}
/**
* Sent when the tree has changed enough that we need to resize
* the bounds, but not enough that we need to remove the
* expanded node set (e.g nodes were expanded or collapsed, or
* nodes were inserted into the tree). You should never have to
* invoke this, the UI will invoke this as it needs to.
*/
public void treeDidChange() {
revalidate();
repaint();
}
/**
* Sets the number of rows that are to be displayed.
* This will only work if the tree is contained in a
* <code>JScrollPane,
* and will adjust the preferred size and size of that scrollpane.
* <p>
* This is a bound property.
*
* @param newCount the number of rows to display
* @beaninfo
* bound: true
* description: The number of rows that are to be displayed.
*/
public void setVisibleRowCount(int newCount) {
int oldCount = visibleRowCount;
visibleRowCount = newCount;
firePropertyChange(VISIBLE_ROW_COUNT_PROPERTY, oldCount,
visibleRowCount);
invalidate();
if (accessibleContext != null) {
((AccessibleJTree)accessibleContext).fireVisibleDataPropertyChange();
}
}
/**
* Returns the number of rows that are displayed in the display area.
*
* @return the number of rows displayed
*/
public int getVisibleRowCount() {
return visibleRowCount;
}
/**
* Expands the root path, assuming the current TreeModel has been set.
*/
private void expandRoot() {
TreeModel model = getModel();
if(model != null && model.getRoot() != null) {
expandPath(new TreePath(model.getRoot()));
}
}
/**
* Returns the TreePath to the next tree element that
* begins with a prefix. To handle the conversion of a
* <code>TreePath into a String, convertValueToText
* is used.
*
* @param prefix the string to test for a match
* @param startingRow the row for starting the search
* @param bias the search direction, either
* Position.Bias.Forward or Position.Bias.Backward.
* @return the TreePath of the next tree element that
* starts with the prefix; otherwise null
* @exception IllegalArgumentException if prefix is null
* or startingRow is out of bounds
* @since 1.4
*/
public TreePath getNextMatch(String prefix, int startingRow,
Position.Bias bias) {
int max = getRowCount();
if (prefix == null) {
throw new IllegalArgumentException();
}
if (startingRow < 0 || startingRow >= max) {
throw new IllegalArgumentException();
}
prefix = prefix.toUpperCase();
// start search from the next/previous element froom the
// selected element
int increment = (bias == Position.Bias.Forward) ? 1 : -1;
int row = startingRow;
do {
TreePath path = getPathForRow(row);
String text = convertValueToText(
path.getLastPathComponent(), isRowSelected(row),
isExpanded(row), true, row, false);
if (text.toUpperCase().startsWith(prefix)) {
return path;
}
row = (row + increment + max) % max;
} while (row != startingRow);
return null;
}
// Serialization support.
private void writeObject(ObjectOutputStream s) throws IOException {
Vector<Object> values = new Vector
Other Java examples (source code examples)
Here is a short list of links related to this Java JTree.java source code file: