0N/A/*
3261N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport java.beans.*;
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport javax.swing.event.*;
0N/Aimport javax.swing.plaf.*;
0N/Aimport javax.swing.tree.*;
0N/Aimport javax.swing.text.Position;
0N/Aimport javax.accessibility.*;
0N/Aimport sun.swing.SwingUtilities2;
0N/Aimport sun.swing.SwingUtilities2.Section;
0N/Aimport static sun.swing.SwingUtilities2.Section.*;
0N/A
0N/A
0N/A/**
0N/A * <a name="jtree_description">
0N/A * A control that displays a set of hierarchical data as an outline.
0N/A * You can find task-oriented documentation and examples of using trees in
0N/A * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html">How to Use Trees</a>,
0N/A * a section in <em>The Java Tutorial.</em>
0N/A * <p>
0N/A * A specific node in a tree can be identified either by a
0N/A * <code>TreePath</code> (an object
0N/A * that encapsulates a node and all of its ancestors), or by its
0N/A * display row, where each row in the display area displays one node.
0N/A * An <i>expanded</i> node is a non-leaf node (as identified by
0N/A * <code>TreeModel.isLeaf(node)</code> returning false) that will displays
0N/A * its children when all its ancestors are <i>expanded</i>.
0N/A * A <i>collapsed</i>
0N/A * node is one which hides them. A <i>hidden</i> node is one which is
0N/A * under a collapsed ancestor. All of a <i>viewable</i> nodes parents
0N/A * are expanded, but may or may not be displayed. A <i>displayed</i> node
0N/A * is both viewable and in the display area, where it can be seen.
0N/A * <p>
0N/A * The following <code>JTree</code> methods use "visible" to mean "displayed":
0N/A * <ul>
0N/A * <li><code>isRootVisible()</code>
0N/A * <li><code>setRootVisible()</code>
0N/A * <li><code>scrollPathToVisible()</code>
0N/A * <li><code>scrollRowToVisible()</code>
0N/A * <li><code>getVisibleRowCount()</code>
0N/A * <li><code>setVisibleRowCount()</code>
0N/A * </ul>
0N/A * <p>
0N/A * The next group of <code>JTree</code> methods use "visible" to mean
0N/A * "viewable" (under an expanded parent):
0N/A * <ul>
0N/A * <li><code>isVisible()</code>
0N/A * <li><code>makeVisible()</code>
0N/A * </ul>
0N/A * <p>
0N/A * If you are interested in knowing when the selection changes implement
0N/A * the <code>TreeSelectionListener</code> interface and add the instance
0N/A * using the method <code>addTreeSelectionListener</code>.
0N/A * <code>valueChanged</code> will be invoked when the
0N/A * selection changes, that is if the user clicks twice on the same
0N/A * node <code>valueChanged</code> will only be invoked once.
0N/A * <p>
0N/A * If you are interested in detecting either double-click events or when
0N/A * a user clicks on a node, regardless of whether or not it was selected,
0N/A * we recommend you do the following:
0N/A * <pre>
0N/A * final JTree tree = ...;
0N/A *
0N/A * MouseListener ml = new MouseAdapter() {
0N/A * public void <b>mousePressed</b>(MouseEvent e) {
0N/A * int selRow = tree.getRowForLocation(e.getX(), e.getY());
0N/A * TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
0N/A * if(selRow != -1) {
0N/A * if(e.getClickCount() == 1) {
0N/A * mySingleClick(selRow, selPath);
0N/A * }
0N/A * else if(e.getClickCount() == 2) {
0N/A * myDoubleClick(selRow, selPath);
0N/A * }
0N/A * }
0N/A * }
0N/A * };
0N/A * tree.addMouseListener(ml);
0N/A * </pre>
0N/A * NOTE: This example obtains both the path and row, but you only need to
0N/A * get the one you're interested in.
0N/A * <p>
0N/A * To use <code>JTree</code> to display compound nodes
0N/A * (for example, nodes containing both
0N/A * a graphic icon and text), subclass {@link TreeCellRenderer} and use
0N/A * {@link #setCellRenderer} to tell the tree to use it. To edit such nodes,
0N/A * subclass {@link TreeCellEditor} and use {@link #setCellEditor}.
0N/A * <p>
0N/A * Like all <code>JComponent</code> classes, you can use {@link InputMap} and
0N/A * {@link ActionMap}
0N/A * to associate an {@link Action} object with a {@link KeyStroke}
0N/A * and execute the action under specified conditions.
0N/A * <p>
0N/A * <strong>Warning:</strong> Swing is not thread safe. For more
0N/A * information see <a
0N/A * href="package-summary.html#threading">Swing's Threading
0N/A * Policy</a>.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * @beaninfo
0N/A * attribute: isContainer false
0N/A * description: A component that displays a set of hierarchical data as an outline.
0N/A *
0N/A * @author Rob Davis
0N/A * @author Ray Ryan
0N/A * @author Scott Violet
0N/A */
0N/Apublic class JTree extends JComponent implements Scrollable, Accessible
0N/A{
0N/A /**
0N/A * @see #getUIClassID
0N/A * @see #readObject
0N/A */
0N/A private static final String uiClassID = "TreeUI";
0N/A
0N/A /**
0N/A * The model that defines the tree displayed by this object.
0N/A */
0N/A transient protected TreeModel treeModel;
0N/A
0N/A /**
0N/A * Models the set of selected nodes in this tree.
0N/A */
0N/A transient protected TreeSelectionModel selectionModel;
0N/A
0N/A /**
0N/A * True if the root node is displayed, false if its children are
0N/A * the highest visible nodes.
0N/A */
0N/A protected boolean rootVisible;
0N/A
0N/A /**
0N/A * The cell used to draw nodes. If <code>null</code>, the UI uses a default
0N/A * <code>cellRenderer</code>.
0N/A */
0N/A transient protected TreeCellRenderer cellRenderer;
0N/A
0N/A /**
0N/A * Height to use for each display row. If this is <= 0 the renderer
0N/A * determines the height for each row.
0N/A */
0N/A protected int rowHeight;
0N/A private boolean rowHeightSet = false;
0N/A
0N/A /**
0N/A * Maps from <code>TreePath</code> to <code>Boolean</code>
0N/A * indicating whether or not the
0N/A * particular path is expanded. This ONLY indicates whether a
0N/A * given path is expanded, and NOT if it is visible or not. That
0N/A * information must be determined by visiting all the parent
0N/A * paths and seeing if they are visible.
0N/A */
625N/A transient private Hashtable<TreePath, Boolean> expandedState;
0N/A
0N/A
0N/A /**
0N/A * True if handles are displayed at the topmost level of the tree.
0N/A * <p>
0N/A * A handle is a small icon that displays adjacent to the node which
0N/A * allows the user to click once to expand or collapse the node. A
0N/A * common interface shows a plus sign (+) for a node which can be
0N/A * expanded and a minus sign (-) for a node which can be collapsed.
0N/A * Handles are always shown for nodes below the topmost level.
0N/A * <p>
0N/A * If the <code>rootVisible</code> setting specifies that the root
0N/A * node is to be displayed, then that is the only node at the topmost
0N/A * level. If the root node is not displayed, then all of its
0N/A * children are at the topmost level of the tree. Handles are
0N/A * always displayed for nodes other than the topmost.
0N/A * <p>
0N/A * If the root node isn't visible, it is generally a good to make
0N/A * this value true. Otherwise, the tree looks exactly like a list,
0N/A * and users may not know that the "list entries" are actually
0N/A * tree nodes.
0N/A *
0N/A * @see #rootVisible
0N/A */
0N/A protected boolean showsRootHandles;
0N/A private boolean showsRootHandlesSet = false;
0N/A
0N/A /**
0N/A * Creates a new event and passed it off the
0N/A * <code>selectionListeners</code>.
0N/A */
0N/A protected transient TreeSelectionRedirector selectionRedirector;
0N/A
0N/A /**
0N/A * Editor for the entries. Default is <code>null</code>
0N/A * (tree is not editable).
0N/A */
0N/A transient protected TreeCellEditor cellEditor;
0N/A
0N/A /**
0N/A * Is the tree editable? Default is false.
0N/A */
0N/A protected boolean editable;
0N/A
0N/A /**
0N/A * Is this tree a large model? This is a code-optimization setting.
0N/A * A large model can be used when the cell height is the same for all
0N/A * nodes. The UI will then cache very little information and instead
0N/A * continually message the model. Without a large model the UI caches
0N/A * most of the information, resulting in fewer method calls to the model.
0N/A * <p>
0N/A * This value is only a suggestion to the UI. Not all UIs will
0N/A * take advantage of it. Default value is false.
0N/A */
0N/A protected boolean largeModel;
0N/A
0N/A /**
0N/A * Number of rows to make visible at one time. This value is used for
0N/A * the <code>Scrollable</code> interface. It determines the preferred
0N/A * size of the display area.
0N/A */
0N/A protected int visibleRowCount;
0N/A
0N/A /**
0N/A * If true, when editing is to be stopped by way of selection changing,
0N/A * data in tree changing or other means <code>stopCellEditing</code>
0N/A * is invoked, and changes are saved. If false,
0N/A * <code>cancelCellEditing</code> is invoked, and changes
0N/A * are discarded. Default is false.
0N/A */
0N/A protected boolean invokesStopCellEditing;
0N/A
0N/A /**
0N/A * If true, when a node is expanded, as many of the descendants are
0N/A * scrolled to be visible.
0N/A */
0N/A protected boolean scrollsOnExpand;
0N/A private boolean scrollsOnExpandSet = false;
0N/A
0N/A /**
0N/A * Number of mouse clicks before a node is expanded.
0N/A */
0N/A protected int toggleClickCount;
0N/A
0N/A /**
0N/A * Updates the <code>expandedState</code>.
0N/A */
0N/A transient protected TreeModelListener treeModelListener;
0N/A
0N/A /**
0N/A * Used when <code>setExpandedState</code> is invoked,
0N/A * will be a <code>Stack</code> of <code>Stack</code>s.
0N/A */
625N/A transient private Stack<Stack<TreePath>> expandedStack;
0N/A
0N/A /**
0N/A * Lead selection path, may not be <code>null</code>.
0N/A */
0N/A private TreePath leadPath;
0N/A
0N/A /**
0N/A * Anchor path.
0N/A */
0N/A private TreePath anchorPath;
0N/A
0N/A /**
0N/A * True if paths in the selection should be expanded.
0N/A */
0N/A private boolean expandsSelectedPaths;
0N/A
0N/A /**
0N/A * This is set to true for the life of the <code>setUI</code> call.
0N/A */
0N/A private boolean settingUI;
0N/A
0N/A /** If true, mouse presses on selections initiate a drag operation. */
0N/A private boolean dragEnabled;
0N/A
0N/A /**
0N/A * The drop mode for this component.
0N/A */
0N/A private DropMode dropMode = DropMode.USE_SELECTION;
0N/A
0N/A /**
0N/A * The drop location.
0N/A */
0N/A private transient DropLocation dropLocation;
0N/A
0N/A /**
0N/A * A subclass of <code>TransferHandler.DropLocation</code> representing
0N/A * a drop location for a <code>JTree</code>.
0N/A *
0N/A * @see #getDropLocation
0N/A * @since 1.6
0N/A */
0N/A public static final class DropLocation extends TransferHandler.DropLocation {
0N/A private final TreePath path;
0N/A private final int index;
0N/A
0N/A private DropLocation(Point p, TreePath path, int index) {
0N/A super(p);
0N/A this.path = path;
0N/A this.index = index;
0N/A }
0N/A
0N/A /**
0N/A * Returns the index where the dropped data should be inserted
0N/A * with respect to the path returned by <code>getPath()</code>.
0N/A * <p>
0N/A * For drop modes <code>DropMode.USE_SELECTION</code> and
0N/A * <code>DropMode.ON</code>, this index is unimportant (and it will
0N/A * always be <code>-1</code>) as the only interesting data is the
0N/A * path over which the drop operation occurred.
0N/A * <p>
0N/A * For drop mode <code>DropMode.INSERT</code>, this index
0N/A * indicates the index at which the data should be inserted into
0N/A * the parent path represented by <code>getPath()</code>.
0N/A * <code>-1</code> indicates that the drop occurred over the
0N/A * parent itself, and in most cases should be treated as inserting
0N/A * into either the beginning or the end of the parent's list of
0N/A * children.
0N/A * <p>
0N/A * For <code>DropMode.ON_OR_INSERT</code>, this value will be
0N/A * an insert index, as described above, or <code>-1</code> if
0N/A * the drop occurred over the path itself.
0N/A *
0N/A * @return the child index
0N/A * @see #getPath
0N/A */
0N/A public int getChildIndex() {
0N/A return index;
0N/A }
0N/A
0N/A /**
0N/A * Returns the path where dropped data should be placed in the
0N/A * tree.
0N/A * <p>
0N/A * Interpretation of this value depends on the drop mode set on the
0N/A * component. If the drop mode is <code>DropMode.USE_SELECTION</code>
0N/A * or <code>DropMode.ON</code>, the return value is the path in the
0N/A * tree over which the data has been (or will be) dropped.
0N/A * <code>null</code> indicates that the drop is over empty space,
0N/A * not associated with a particular path.
0N/A * <p>
0N/A * If the drop mode is <code>DropMode.INSERT</code>, the return value
0N/A * refers to the path that should become the parent of the new data,
0N/A * in which case <code>getChildIndex()</code> indicates where the
0N/A * new item should be inserted into this parent path. A
0N/A * <code>null</code> path indicates that no parent path has been
0N/A * determined, which can happen for multiple reasons:
0N/A * <ul>
0N/A * <li>The tree has no model
0N/A * <li>There is no root in the tree
0N/A * <li>The root is collapsed
0N/A * <li>The root is a leaf node
0N/A * </ul>
0N/A * It is up to the developer to decide if and how they wish to handle
0N/A * the <code>null</code> case.
0N/A * <p>
0N/A * If the drop mode is <code>DropMode.ON_OR_INSERT</code>,
0N/A * <code>getChildIndex</code> can be used to determine whether the
0N/A * drop is on top of the path itself (<code>-1</code>) or the index
0N/A * at which it should be inserted into the path (values other than
0N/A * <code>-1</code>).
0N/A *
0N/A * @return the drop path
0N/A * @see #getChildIndex
0N/A */
0N/A public TreePath getPath() {
0N/A return path;
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of this drop location.
0N/A * This method is intended to be used for debugging purposes,
0N/A * and the content and format of the returned string may vary
0N/A * between implementations.
0N/A *
0N/A * @return a string representation of this drop location
0N/A */
0N/A public String toString() {
0N/A return getClass().getName()
0N/A + "[dropPoint=" + getDropPoint() + ","
0N/A + "path=" + path + ","
0N/A + "childIndex=" + index + "]";
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The row to expand during DnD.
0N/A */
0N/A private int expandRow = -1;
0N/A
0N/A private class TreeTimer extends Timer {
0N/A public TreeTimer() {
0N/A super(2000, null);
0N/A setRepeats(false);
0N/A }
0N/A
0N/A public void fireActionPerformed(ActionEvent ae) {
0N/A JTree.this.expandRow(expandRow);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * A timer to expand nodes during drop.
0N/A */
0N/A private TreeTimer dropTimer;
0N/A
0N/A /**
0N/A * When <code>addTreeExpansionListener</code> is invoked,
0N/A * and <code>settingUI</code> is true, this ivar gets set to the passed in
0N/A * <code>Listener</code>. This listener is then notified first in
0N/A * <code>fireTreeCollapsed</code> and <code>fireTreeExpanded</code>.
0N/A * <p>This is an ugly workaround for a way to have the UI listener
0N/A * get notified before other listeners.
0N/A */
0N/A private transient TreeExpansionListener uiTreeExpansionListener;
0N/A
0N/A /**
0N/A * Max number of stacks to keep around.
0N/A */
0N/A private static int TEMP_STACK_SIZE = 11;
0N/A
0N/A //
0N/A // Bound property names
0N/A //
0N/A /** Bound property name for <code>cellRenderer</code>. */
0N/A public final static String CELL_RENDERER_PROPERTY = "cellRenderer";
0N/A /** Bound property name for <code>treeModel</code>. */
0N/A public final static String TREE_MODEL_PROPERTY = "model";
0N/A /** Bound property name for <code>rootVisible</code>. */
0N/A public final static String ROOT_VISIBLE_PROPERTY = "rootVisible";
0N/A /** Bound property name for <code>showsRootHandles</code>. */
0N/A public final static String SHOWS_ROOT_HANDLES_PROPERTY = "showsRootHandles";
0N/A /** Bound property name for <code>rowHeight</code>. */
0N/A public final static String ROW_HEIGHT_PROPERTY = "rowHeight";
0N/A /** Bound property name for <code>cellEditor</code>. */
0N/A public final static String CELL_EDITOR_PROPERTY = "cellEditor";
0N/A /** Bound property name for <code>editable</code>. */
0N/A public final static String EDITABLE_PROPERTY = "editable";
0N/A /** Bound property name for <code>largeModel</code>. */
0N/A public final static String LARGE_MODEL_PROPERTY = "largeModel";
0N/A /** Bound property name for selectionModel. */
0N/A public final static String SELECTION_MODEL_PROPERTY = "selectionModel";
0N/A /** Bound property name for <code>visibleRowCount</code>. */
0N/A public final static String VISIBLE_ROW_COUNT_PROPERTY = "visibleRowCount";
0N/A /** Bound property name for <code>messagesStopCellEditing</code>. */
0N/A public final static String INVOKES_STOP_CELL_EDITING_PROPERTY = "invokesStopCellEditing";
0N/A /** Bound property name for <code>scrollsOnExpand</code>. */
0N/A public final static String SCROLLS_ON_EXPAND_PROPERTY = "scrollsOnExpand";
0N/A /** Bound property name for <code>toggleClickCount</code>. */
0N/A public final static String TOGGLE_CLICK_COUNT_PROPERTY = "toggleClickCount";
0N/A /** Bound property name for <code>leadSelectionPath</code>.
0N/A * @since 1.3 */
0N/A public final static String LEAD_SELECTION_PATH_PROPERTY = "leadSelectionPath";
0N/A /** Bound property name for anchor selection path.
0N/A * @since 1.3 */
0N/A public final static String ANCHOR_SELECTION_PATH_PROPERTY = "anchorSelectionPath";
0N/A /** Bound property name for expands selected paths property
0N/A * @since 1.3 */
0N/A public final static String EXPANDS_SELECTED_PATHS_PROPERTY = "expandsSelectedPaths";
0N/A
0N/A
0N/A /**
0N/A * Creates and returns a sample <code>TreeModel</code>.
0N/A * Used primarily for beanbuilders to show something interesting.
0N/A *
0N/A * @return the default <code>TreeModel</code>
0N/A */
0N/A protected static TreeModel getDefaultTreeModel() {
0N/A DefaultMutableTreeNode root = new DefaultMutableTreeNode("JTree");
0N/A DefaultMutableTreeNode parent;
0N/A
0N/A parent = new DefaultMutableTreeNode("colors");
0N/A root.add(parent);
0N/A parent.add(new DefaultMutableTreeNode("blue"));
0N/A parent.add(new DefaultMutableTreeNode("violet"));
0N/A parent.add(new DefaultMutableTreeNode("red"));
0N/A parent.add(new DefaultMutableTreeNode("yellow"));
0N/A
0N/A parent = new DefaultMutableTreeNode("sports");
0N/A root.add(parent);
0N/A parent.add(new DefaultMutableTreeNode("basketball"));
0N/A parent.add(new DefaultMutableTreeNode("soccer"));
0N/A parent.add(new DefaultMutableTreeNode("football"));
0N/A parent.add(new DefaultMutableTreeNode("hockey"));
0N/A
0N/A parent = new DefaultMutableTreeNode("food");
0N/A root.add(parent);
0N/A parent.add(new DefaultMutableTreeNode("hot dogs"));
0N/A parent.add(new DefaultMutableTreeNode("pizza"));
0N/A parent.add(new DefaultMutableTreeNode("ravioli"));
0N/A parent.add(new DefaultMutableTreeNode("bananas"));
0N/A return new DefaultTreeModel(root);
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>TreeModel</code> wrapping the specified object.
0N/A * If the object is:<ul>
0N/A * <li>an array of <code>Object</code>s,
0N/A * <li>a <code>Hashtable</code>, or
0N/A * <li>a <code>Vector</code>
0N/A * </ul>then a new root node is created with each of the incoming
0N/A * objects as children. Otherwise, a new root is created with
0N/A * a value of {@code "root"}.
0N/A *
0N/A * @param value the <code>Object</code> used as the foundation for
0N/A * the <code>TreeModel</code>
0N/A * @return a <code>TreeModel</code> wrapping the specified object
0N/A */
0N/A protected static TreeModel createTreeModel(Object value) {
0N/A DefaultMutableTreeNode root;
0N/A
0N/A if((value instanceof Object[]) || (value instanceof Hashtable) ||
0N/A (value instanceof Vector)) {
0N/A root = new DefaultMutableTreeNode("root");
0N/A DynamicUtilTreeNode.createChildren(root, value);
0N/A }
0N/A else {
0N/A root = new DynamicUtilTreeNode("root", value);
0N/A }
0N/A return new DefaultTreeModel(root, false);
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>JTree</code> with a sample model.
0N/A * The default model used by the tree defines a leaf node as any node
0N/A * without children.
0N/A *
0N/A * @see DefaultTreeModel#asksAllowsChildren
0N/A */
0N/A public JTree() {
0N/A this(getDefaultTreeModel());
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>JTree</code> with each element of the
0N/A * specified array as the
0N/A * child of a new root node which is not displayed.
0N/A * By default, the tree defines a leaf node as any node without
0N/A * children.
0N/A *
0N/A * @param value an array of <code>Object</code>s
0N/A * @see DefaultTreeModel#asksAllowsChildren
0N/A */
0N/A public JTree(Object[] value) {
0N/A this(createTreeModel(value));
0N/A this.setRootVisible(false);
0N/A this.setShowsRootHandles(true);
0N/A expandRoot();
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>JTree</code> with each element of the specified
0N/A * <code>Vector</code> as the
0N/A * child of a new root node which is not displayed. By default, the
0N/A * tree defines a leaf node as any node without children.
0N/A *
0N/A * @param value a <code>Vector</code>
0N/A * @see DefaultTreeModel#asksAllowsChildren
0N/A */
0N/A public JTree(Vector<?> value) {
0N/A this(createTreeModel(value));
0N/A this.setRootVisible(false);
0N/A this.setShowsRootHandles(true);
0N/A expandRoot();
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>JTree</code> created from a <code>Hashtable</code>
0N/A * which does not display with root.
0N/A * Each value-half of the key/value pairs in the <code>HashTable</code>
0N/A * becomes a child of the new root node. By default, the tree defines
0N/A * a leaf node as any node without children.
0N/A *
0N/A * @param value a <code>Hashtable</code>
0N/A * @see DefaultTreeModel#asksAllowsChildren
0N/A */
0N/A public JTree(Hashtable<?,?> value) {
0N/A this(createTreeModel(value));
0N/A this.setRootVisible(false);
0N/A this.setShowsRootHandles(true);
0N/A expandRoot();
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>JTree</code> with the specified
0N/A * <code>TreeNode</code> as its root,
0N/A * which displays the root node.
0N/A * By default, the tree defines a leaf node as any node without children.
0N/A *
0N/A * @param root a <code>TreeNode</code> object
0N/A * @see DefaultTreeModel#asksAllowsChildren
0N/A */
0N/A public JTree(TreeNode root) {
0N/A this(root, false);
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>JTree</code> with the specified <code>TreeNode</code>
0N/A * as its root, which
0N/A * displays the root node and which decides whether a node is a
0N/A * leaf node in the specified manner.
0N/A *
0N/A * @param root a <code>TreeNode</code> object
0N/A * @param asksAllowsChildren if false, any node without children is a
0N/A * leaf node; if true, only nodes that do not allow
0N/A * children are leaf nodes
0N/A * @see DefaultTreeModel#asksAllowsChildren
0N/A */
0N/A public JTree(TreeNode root, boolean asksAllowsChildren) {
0N/A this(new DefaultTreeModel(root, asksAllowsChildren));
0N/A }
0N/A
0N/A /**
0N/A * Returns an instance of <code>JTree</code> which displays the root node
0N/A * -- the tree is created using the specified data model.
0N/A *
0N/A * @param newModel the <code>TreeModel</code> to use as the data model
0N/A */
0N/A @ConstructorProperties({"model"})
0N/A public JTree(TreeModel newModel) {
0N/A super();
625N/A expandedStack = new Stack<Stack<TreePath>>();
0N/A toggleClickCount = 2;
625N/A expandedState = new Hashtable<TreePath, Boolean>();
0N/A setLayout(null);
0N/A rowHeight = 16;
0N/A visibleRowCount = 20;
0N/A rootVisible = true;
0N/A selectionModel = new DefaultTreeSelectionModel();
0N/A cellRenderer = null;
0N/A scrollsOnExpand = true;
0N/A setOpaque(true);
0N/A expandsSelectedPaths = true;
0N/A updateUI();
0N/A setModel(newModel);
0N/A }
0N/A
0N/A /**
0N/A * Returns the L&F object that renders this component.
0N/A *
0N/A * @return the <code>TreeUI</code> object that renders this component
0N/A */
0N/A public TreeUI getUI() {
0N/A return (TreeUI)ui;
0N/A }
0N/A
0N/A /**
0N/A * Sets the L&F object that renders this component.
0N/A * <p>
0N/A * This is a bound property.
0N/A *
0N/A * @param ui the <code>TreeUI</code> L&F object
0N/A * @see UIDefaults#getUI
0N/A * @beaninfo
0N/A * bound: true
0N/A * hidden: true
0N/A * attribute: visualUpdate true
0N/A * description: The UI object that implements the Component's LookAndFeel.
0N/A */
0N/A public void setUI(TreeUI ui) {
625N/A if (this.ui != ui) {
0N/A settingUI = true;
0N/A uiTreeExpansionListener = null;
0N/A try {
0N/A super.setUI(ui);
0N/A }
0N/A finally {
0N/A settingUI = false;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notification from the <code>UIManager</code> that the L&F has changed.
0N/A * Replaces the current UI object with the latest version from the
0N/A * <code>UIManager</code>.
0N/A *
0N/A * @see JComponent#updateUI
0N/A */
0N/A public void updateUI() {
0N/A setUI((TreeUI)UIManager.getUI(this));
0N/A
0N/A SwingUtilities.updateRendererOrEditorUI(getCellRenderer());
0N/A SwingUtilities.updateRendererOrEditorUI(getCellEditor());
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the name of the L&F class that renders this component.
0N/A *
0N/A * @return the string "TreeUI"
0N/A * @see JComponent#getUIClassID
0N/A * @see UIDefaults#getUI
0N/A */
0N/A public String getUIClassID() {
0N/A return uiClassID;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the current <code>TreeCellRenderer</code>
0N/A * that is rendering each cell.
0N/A *
0N/A * @return the <code>TreeCellRenderer</code> that is rendering each cell
0N/A */
0N/A public TreeCellRenderer getCellRenderer() {
0N/A return cellRenderer;
0N/A }
0N/A
0N/A /**
0N/A * Sets the <code>TreeCellRenderer</code> that will be used to
0N/A * draw each cell.
0N/A * <p>
0N/A * This is a bound property.
0N/A *
0N/A * @param x the <code>TreeCellRenderer</code> that is to render each cell
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: The TreeCellRenderer that will be used to draw
0N/A * each cell.
0N/A */
0N/A public void setCellRenderer(TreeCellRenderer x) {
0N/A TreeCellRenderer oldValue = cellRenderer;
0N/A
0N/A cellRenderer = x;
0N/A firePropertyChange(CELL_RENDERER_PROPERTY, oldValue, cellRenderer);
0N/A invalidate();
0N/A }
0N/A
0N/A /**
0N/A * Determines whether the tree is editable. Fires a property
0N/A * change event if the new setting is different from the existing
0N/A * setting.
0N/A * <p>
0N/A * This is a bound property.
0N/A *
0N/A * @param flag a boolean value, true if the tree is editable
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: Whether the tree is editable.
0N/A */
0N/A public void setEditable(boolean flag) {
0N/A boolean oldValue = this.editable;
0N/A
0N/A this.editable = flag;
0N/A firePropertyChange(EDITABLE_PROPERTY, oldValue, flag);
0N/A if (accessibleContext != null) {
0N/A accessibleContext.firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A (oldValue ? AccessibleState.EDITABLE : null),
0N/A (flag ? AccessibleState.EDITABLE : null));
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the tree is editable.
0N/A *
0N/A * @return true if the tree is editable
0N/A */
0N/A public boolean isEditable() {
0N/A return editable;
0N/A }
0N/A
0N/A /**
0N/A * Sets the cell editor. A <code>null</code> value implies that the
0N/A * tree cannot be edited. If this represents a change in the
0N/A * <code>cellEditor</code>, the <code>propertyChange</code>
0N/A * method is invoked on all listeners.
0N/A * <p>
0N/A * This is a bound property.
0N/A *
0N/A * @param cellEditor the <code>TreeCellEditor</code> to use
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: The cell editor. A null value implies the tree
0N/A * cannot be edited.
0N/A */
0N/A public void setCellEditor(TreeCellEditor cellEditor) {
0N/A TreeCellEditor oldEditor = this.cellEditor;
0N/A
0N/A this.cellEditor = cellEditor;
0N/A firePropertyChange(CELL_EDITOR_PROPERTY, oldEditor, cellEditor);
0N/A invalidate();
0N/A }
0N/A
0N/A /**
0N/A * Returns the editor used to edit entries in the tree.
0N/A *
0N/A * @return the <code>TreeCellEditor</code> in use,
0N/A * or <code>null</code> if the tree cannot be edited
0N/A */
0N/A public TreeCellEditor getCellEditor() {
0N/A return cellEditor;
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>TreeModel</code> that is providing the data.
0N/A *
0N/A * @return the <code>TreeModel</code> that is providing the data
0N/A */
0N/A public TreeModel getModel() {
0N/A return treeModel;
0N/A }
0N/A
0N/A /**
0N/A * Sets the <code>TreeModel</code> that will provide the data.
0N/A * <p>
0N/A * This is a bound property.
0N/A *
0N/A * @param newModel the <code>TreeModel</code> that is to provide the data
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: The TreeModel that will provide the data.
0N/A */
0N/A public void setModel(TreeModel newModel) {
0N/A clearSelection();
0N/A
0N/A TreeModel oldModel = treeModel;
0N/A
0N/A if(treeModel != null && treeModelListener != null)
0N/A treeModel.removeTreeModelListener(treeModelListener);
0N/A
0N/A if (accessibleContext != null) {
0N/A if (treeModel != null) {
0N/A treeModel.removeTreeModelListener((TreeModelListener)accessibleContext);
0N/A }
0N/A if (newModel != null) {
0N/A newModel.addTreeModelListener((TreeModelListener)accessibleContext);
0N/A }
0N/A }
0N/A
0N/A treeModel = newModel;
0N/A clearToggledPaths();
0N/A if(treeModel != null) {
0N/A if(treeModelListener == null)
0N/A treeModelListener = createTreeModelListener();
0N/A if(treeModelListener != null)
0N/A treeModel.addTreeModelListener(treeModelListener);
0N/A // Mark the root as expanded, if it isn't a leaf.
0N/A if(treeModel.getRoot() != null &&
0N/A !treeModel.isLeaf(treeModel.getRoot())) {
0N/A expandedState.put(new TreePath(treeModel.getRoot()),
0N/A Boolean.TRUE);
0N/A }
0N/A }
0N/A firePropertyChange(TREE_MODEL_PROPERTY, oldModel, treeModel);
0N/A invalidate();
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the root node of the tree is displayed.
0N/A *
0N/A * @return true if the root node of the tree is displayed
0N/A * @see #rootVisible
0N/A */
0N/A public boolean isRootVisible() {
0N/A return rootVisible;
0N/A }
0N/A
0N/A /**
0N/A * Determines whether or not the root node from
0N/A * the <code>TreeModel</code> is visible.
0N/A * <p>
0N/A * This is a bound property.
0N/A *
0N/A * @param rootVisible true if the root node of the tree is to be displayed
0N/A * @see #rootVisible
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: Whether or not the root node
0N/A * from the TreeModel is visible.
0N/A */
0N/A public void setRootVisible(boolean rootVisible) {
0N/A boolean oldValue = this.rootVisible;
0N/A
0N/A this.rootVisible = rootVisible;
0N/A firePropertyChange(ROOT_VISIBLE_PROPERTY, oldValue, this.rootVisible);
0N/A if (accessibleContext != null) {
0N/A ((AccessibleJTree)accessibleContext).fireVisibleDataPropertyChange();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sets the value of the <code>showsRootHandles</code> property,
0N/A * which specifies whether the node handles should be displayed.
0N/A * The default value of this property depends on the constructor
0N/A * used to create the <code>JTree</code>.
0N/A * Some look and feels might not support handles;
0N/A * they will ignore this property.
0N/A * <p>
0N/A * This is a bound property.
0N/A *
0N/A * @param newValue <code>true</code> if root handles should be displayed;
0N/A * otherwise, <code>false</code>
0N/A * @see #showsRootHandles
0N/A * @see #getShowsRootHandles
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: Whether the node handles are to be
0N/A * displayed.
0N/A */
0N/A public void setShowsRootHandles(boolean newValue) {
0N/A boolean oldValue = showsRootHandles;
0N/A TreeModel model = getModel();
0N/A
0N/A showsRootHandles = newValue;
0N/A showsRootHandlesSet = true;
0N/A firePropertyChange(SHOWS_ROOT_HANDLES_PROPERTY, oldValue,
0N/A showsRootHandles);
0N/A if (accessibleContext != null) {
0N/A ((AccessibleJTree)accessibleContext).fireVisibleDataPropertyChange();
0N/A }
0N/A invalidate();
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of the <code>showsRootHandles</code> property.
0N/A *
0N/A * @return the value of the <code>showsRootHandles</code> property
0N/A * @see #showsRootHandles
0N/A */
0N/A public boolean getShowsRootHandles()
0N/A {
0N/A return showsRootHandles;
0N/A }
0N/A
0N/A /**
0N/A * Sets the height of each cell, in pixels. If the specified value
0N/A * is less than or equal to zero the current cell renderer is
0N/A * queried for each row's height.
0N/A * <p>
0N/A * This is a bound property.
0N/A *
0N/A * @param rowHeight the height of each cell, in pixels
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: The height of each cell.
0N/A */
0N/A public void setRowHeight(int rowHeight)
0N/A {
0N/A int oldValue = this.rowHeight;
0N/A
0N/A this.rowHeight = rowHeight;
0N/A rowHeightSet = true;
0N/A firePropertyChange(ROW_HEIGHT_PROPERTY, oldValue, this.rowHeight);
0N/A invalidate();
0N/A }
0N/A
0N/A /**
0N/A * Returns the height of each row. If the returned value is less than
0N/A * or equal to 0 the height for each row is determined by the
0N/A * renderer.
0N/A *
0N/A */
0N/A public int getRowHeight()
0N/A {
0N/A return rowHeight;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the height of each display row is a fixed size.
0N/A *
0N/A * @return true if the height of each row is a fixed size
0N/A */
0N/A public boolean isFixedRowHeight()
0N/A {
0N/A return (rowHeight > 0);
0N/A }
0N/A
0N/A /**
0N/A * Specifies whether the UI should use a large model.
0N/A * (Not all UIs will implement this.) Fires a property change
0N/A * for the LARGE_MODEL_PROPERTY.
0N/A * <p>
0N/A * This is a bound property.
0N/A *
0N/A * @param newValue true to suggest a large model to the UI
0N/A * @see #largeModel
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: Whether the UI should use a
0N/A * large model.
0N/A */
0N/A public void setLargeModel(boolean newValue) {
0N/A boolean oldValue = largeModel;
0N/A
0N/A largeModel = newValue;
0N/A firePropertyChange(LARGE_MODEL_PROPERTY, oldValue, newValue);
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the tree is configured for a large model.
0N/A *
0N/A * @return true if a large model is suggested
0N/A * @see #largeModel
0N/A */
0N/A public boolean isLargeModel() {
0N/A return largeModel;
0N/A }
0N/A
0N/A /**
0N/A * Determines what happens when editing is interrupted by selecting
0N/A * another node in the tree, a change in the tree's data, or by some
0N/A * other means. Setting this property to <code>true</code> causes the
0N/A * changes to be automatically saved when editing is interrupted.
0N/A * <p>
0N/A * Fires a property change for the INVOKES_STOP_CELL_EDITING_PROPERTY.
0N/A *
0N/A * @param newValue true means that <code>stopCellEditing</code> is invoked
0N/A * when editing is interrupted, and data is saved; false means that
0N/A * <code>cancelCellEditing</code> is invoked, and changes are lost
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: Determines what happens when editing is interrupted,
0N/A * selecting another node in the tree, a change in the
0N/A * tree's data, or some other means.
0N/A */
0N/A public void setInvokesStopCellEditing(boolean newValue) {
0N/A boolean oldValue = invokesStopCellEditing;
0N/A
0N/A invokesStopCellEditing = newValue;
0N/A firePropertyChange(INVOKES_STOP_CELL_EDITING_PROPERTY, oldValue,
0N/A newValue);
0N/A }
0N/A
0N/A /**
0N/A * Returns the indicator that tells what happens when editing is
0N/A * interrupted.
0N/A *
0N/A * @return the indicator that tells what happens when editing is
0N/A * interrupted
0N/A * @see #setInvokesStopCellEditing
0N/A */
0N/A public boolean getInvokesStopCellEditing() {
0N/A return invokesStopCellEditing;
0N/A }
0N/A
0N/A /**
0N/A * Sets the <code>scrollsOnExpand</code> property,
0N/A * which determines whether the
0N/A * tree might scroll to show previously hidden children.
0N/A * If this property is <code>true</code> (the default),
0N/A * when a node expands
0N/A * the tree can use scrolling to make
0N/A * the maximum possible number of the node's descendants visible.
0N/A * In some look and feels, trees might not need to scroll when expanded;
0N/A * those look and feels will ignore this property.
0N/A * <p>
0N/A * This is a bound property.
0N/A *
0N/A * @param newValue <code>false</code> to disable scrolling on expansion;
0N/A * <code>true</code> to enable it
0N/A * @see #getScrollsOnExpand
0N/A *
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: Indicates if a node descendant should be scrolled when expanded.
0N/A */
0N/A public void setScrollsOnExpand(boolean newValue) {
0N/A boolean oldValue = scrollsOnExpand;
0N/A
0N/A scrollsOnExpand = newValue;
0N/A scrollsOnExpandSet = true;
0N/A firePropertyChange(SCROLLS_ON_EXPAND_PROPERTY, oldValue,
0N/A newValue);
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of the <code>scrollsOnExpand</code> property.
0N/A *
0N/A * @return the value of the <code>scrollsOnExpand</code> property
0N/A */
0N/A public boolean getScrollsOnExpand() {
0N/A return scrollsOnExpand;
0N/A }
0N/A
0N/A /**
0N/A * Sets the number of mouse clicks before a node will expand or close.
0N/A * The default is two.
0N/A * <p>
0N/A * This is a bound property.
0N/A *
0N/A * @since 1.3
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: Number of clicks before a node will expand/collapse.
0N/A */
0N/A public void setToggleClickCount(int clickCount) {
0N/A int oldCount = toggleClickCount;
0N/A
0N/A toggleClickCount = clickCount;
0N/A firePropertyChange(TOGGLE_CLICK_COUNT_PROPERTY, oldCount,
0N/A clickCount);
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of mouse clicks needed to expand or close a node.
0N/A *
0N/A * @return number of mouse clicks before node is expanded
0N/A * @since 1.3
0N/A */
0N/A public int getToggleClickCount() {
0N/A return toggleClickCount;
0N/A }
0N/A
0N/A /**
0N/A * Configures the <code>expandsSelectedPaths</code> property. If
0N/A * true, any time the selection is changed, either via the
0N/A * <code>TreeSelectionModel</code>, or the cover methods provided by
0N/A * <code>JTree</code>, the <code>TreePath</code>s parents will be
0N/A * expanded to make them visible (visible meaning the parent path is
0N/A * expanded, not necessarily in the visible rectangle of the
0N/A * <code>JTree</code>). If false, when the selection
0N/A * changes the nodes parent is not made visible (all its parents expanded).
0N/A * This is useful if you wish to have your selection model maintain paths
0N/A * that are not always visible (all parents expanded).
0N/A * <p>
0N/A * This is a bound property.
0N/A *
0N/A * @param newValue the new value for <code>expandsSelectedPaths</code>
0N/A *
0N/A * @since 1.3
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: Indicates whether changes to the selection should make
0N/A * the parent of the path visible.
0N/A */
0N/A public void setExpandsSelectedPaths(boolean newValue) {
0N/A boolean oldValue = expandsSelectedPaths;
0N/A
0N/A expandsSelectedPaths = newValue;
0N/A firePropertyChange(EXPANDS_SELECTED_PATHS_PROPERTY, oldValue,
0N/A newValue);
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>expandsSelectedPaths</code> property.
0N/A * @return true if selection changes result in the parent path being
0N/A * expanded
0N/A * @since 1.3
0N/A * @see #setExpandsSelectedPaths
0N/A */
0N/A public boolean getExpandsSelectedPaths() {
0N/A return expandsSelectedPaths;
0N/A }
0N/A
0N/A /**
0N/A * Turns on or off automatic drag handling. In order to enable automatic
0N/A * drag handling, this property should be set to {@code true}, and the
0N/A * tree's {@code TransferHandler} needs to be {@code non-null}.
0N/A * The default value of the {@code dragEnabled} property is {@code false}.
0N/A * <p>
0N/A * The job of honoring this property, and recognizing a user drag gesture,
0N/A * lies with the look and feel implementation, and in particular, the tree's
0N/A * {@code TreeUI}. When automatic drag handling is enabled, most look and
0N/A * feels (including those that subclass {@code BasicLookAndFeel}) begin a
0N/A * drag and drop operation whenever the user presses the mouse button over
0N/A * an item and then moves the mouse a few pixels. Setting this property to
0N/A * {@code true} can therefore have a subtle effect on how selections behave.
0N/A * <p>
0N/A * If a look and feel is used that ignores this property, you can still
0N/A * begin a drag and drop operation by calling {@code exportAsDrag} on the
0N/A * tree's {@code TransferHandler}.
0N/A *
0N/A * @param b whether or not to enable automatic drag handling
0N/A * @exception HeadlessException if
0N/A * <code>b</code> is <code>true</code> and
0N/A * <code>GraphicsEnvironment.isHeadless()</code>
0N/A * returns <code>true</code>
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A * @see #getDragEnabled
0N/A * @see #setTransferHandler
0N/A * @see TransferHandler
0N/A * @since 1.4
0N/A *
0N/A * @beaninfo
0N/A * description: determines whether automatic drag handling is enabled
0N/A * bound: false
0N/A */
0N/A public void setDragEnabled(boolean b) {
0N/A if (b && GraphicsEnvironment.isHeadless()) {
0N/A throw new HeadlessException();
0N/A }
0N/A dragEnabled = b;
0N/A }
0N/A
0N/A /**
0N/A * Returns whether or not automatic drag handling is enabled.
0N/A *
0N/A * @return the value of the {@code dragEnabled} property
0N/A * @see #setDragEnabled
0N/A * @since 1.4
0N/A */
0N/A public boolean getDragEnabled() {
0N/A return dragEnabled;
0N/A }
0N/A
0N/A /**
0N/A * Sets the drop mode for this component. For backward compatibility,
0N/A * the default for this property is <code>DropMode.USE_SELECTION</code>.
0N/A * Usage of one of the other modes is recommended, however, for an
0N/A * improved user experience. <code>DropMode.ON</code>, for instance,
0N/A * offers similar behavior of showing items as selected, but does so without
0N/A * affecting the actual selection in the tree.
0N/A * <p>
0N/A * <code>JTree</code> supports the following drop modes:
0N/A * <ul>
0N/A * <li><code>DropMode.USE_SELECTION</code></li>
0N/A * <li><code>DropMode.ON</code></li>
0N/A * <li><code>DropMode.INSERT</code></li>
0N/A * <li><code>DropMode.ON_OR_INSERT</code></li>
0N/A * </ul>
0N/A * <p>
0N/A * The drop mode is only meaningful if this component has a
0N/A * <code>TransferHandler</code> that accepts drops.
0N/A *
0N/A * @param dropMode the drop mode to use
0N/A * @throws IllegalArgumentException if the drop mode is unsupported
0N/A * or <code>null</code>
0N/A * @see #getDropMode
0N/A * @see #getDropLocation
0N/A * @see #setTransferHandler
0N/A * @see TransferHandler
0N/A * @since 1.6
0N/A */
0N/A public final void setDropMode(DropMode dropMode) {
0N/A if (dropMode != null) {
0N/A switch (dropMode) {
0N/A case USE_SELECTION:
0N/A case ON:
0N/A case INSERT:
0N/A case ON_OR_INSERT:
0N/A this.dropMode = dropMode;
0N/A return;
0N/A }
0N/A }
0N/A
0N/A throw new IllegalArgumentException(dropMode + ": Unsupported drop mode for tree");
0N/A }
0N/A
0N/A /**
0N/A * Returns the drop mode for this component.
0N/A *
0N/A * @return the drop mode for this component
0N/A * @see #setDropMode
0N/A * @since 1.6
0N/A */
0N/A public final DropMode getDropMode() {
0N/A return dropMode;
0N/A }
0N/A
0N/A /**
0N/A * Calculates a drop location in this component, representing where a
0N/A * drop at the given point should insert data.
0N/A *
0N/A * @param p the point to calculate a drop location for
0N/A * @return the drop location, or <code>null</code>
0N/A */
0N/A DropLocation dropLocationForPoint(Point p) {
0N/A DropLocation location = null;
0N/A
0N/A int row = getClosestRowForLocation(p.x, p.y);
0N/A Rectangle bounds = getRowBounds(row);
0N/A TreeModel model = getModel();
0N/A Object root = (model == null) ? null : model.getRoot();
0N/A TreePath rootPath = (root == null) ? null : new TreePath(root);
0N/A
625N/A TreePath child;
625N/A TreePath parent;
0N/A boolean outside = row == -1
0N/A || p.y < bounds.y
0N/A || p.y >= bounds.y + bounds.height;
0N/A
0N/A switch(dropMode) {
0N/A case USE_SELECTION:
0N/A case ON:
0N/A if (outside) {
0N/A location = new DropLocation(p, null, -1);
0N/A } else {
0N/A location = new DropLocation(p, getPathForRow(row), -1);
0N/A }
0N/A
0N/A break;
0N/A case INSERT:
0N/A case ON_OR_INSERT:
0N/A if (row == -1) {
0N/A if (root != null && !model.isLeaf(root) && isExpanded(rootPath)) {
0N/A location = new DropLocation(p, rootPath, 0);
0N/A } else {
0N/A location = new DropLocation(p, null, -1);
0N/A }
0N/A
0N/A break;
0N/A }
0N/A
0N/A boolean checkOn = dropMode == DropMode.ON_OR_INSERT
0N/A || !model.isLeaf(getPathForRow(row).getLastPathComponent());
0N/A
0N/A Section section = SwingUtilities2.liesInVertical(bounds, p, checkOn);
0N/A if(section == LEADING) {
0N/A child = getPathForRow(row);
0N/A parent = child.getParentPath();
0N/A } else if (section == TRAILING) {
0N/A int index = row + 1;
0N/A if (index >= getRowCount()) {
0N/A if (model.isLeaf(root) || !isExpanded(rootPath)) {
0N/A location = new DropLocation(p, null, -1);
0N/A } else {
0N/A parent = rootPath;
0N/A index = model.getChildCount(root);
0N/A location = new DropLocation(p, parent, index);
0N/A }
0N/A
0N/A break;
0N/A }
0N/A
0N/A child = getPathForRow(index);
0N/A parent = child.getParentPath();
0N/A } else {
0N/A assert checkOn;
0N/A location = new DropLocation(p, getPathForRow(row), -1);
0N/A break;
0N/A }
0N/A
0N/A if (parent != null) {
0N/A location = new DropLocation(p, parent,
0N/A model.getIndexOfChild(parent.getLastPathComponent(),
0N/A child.getLastPathComponent()));
0N/A } else if (checkOn || !model.isLeaf(root)) {
0N/A location = new DropLocation(p, rootPath, -1);
0N/A } else {
0N/A location = new DropLocation(p, null, -1);
0N/A }
0N/A
0N/A break;
0N/A default:
0N/A assert false : "Unexpected drop mode";
0N/A }
0N/A
0N/A if (outside || row != expandRow) {
0N/A cancelDropTimer();
0N/A }
0N/A
0N/A if (!outside && row != expandRow) {
0N/A if (isCollapsed(row)) {
0N/A expandRow = row;
0N/A startDropTimer();
0N/A }
0N/A }
0N/A
0N/A return location;
0N/A }
0N/A
0N/A /**
0N/A * Called to set or clear the drop location during a DnD operation.
0N/A * In some cases, the component may need to use it's internal selection
0N/A * temporarily to indicate the drop location. To help facilitate this,
0N/A * this method returns and accepts as a parameter a state object.
0N/A * This state object can be used to store, and later restore, the selection
0N/A * state. Whatever this method returns will be passed back to it in
0N/A * future calls, as the state parameter. If it wants the DnD system to
0N/A * continue storing the same state, it must pass it back every time.
0N/A * Here's how this is used:
0N/A * <p>
0N/A * Let's say that on the first call to this method the component decides
0N/A * to save some state (because it is about to use the selection to show
0N/A * a drop index). It can return a state object to the caller encapsulating
0N/A * any saved selection state. On a second call, let's say the drop location
0N/A * is being changed to something else. The component doesn't need to
0N/A * restore anything yet, so it simply passes back the same state object
0N/A * to have the DnD system continue storing it. Finally, let's say this
0N/A * method is messaged with <code>null</code>. This means DnD
0N/A * is finished with this component for now, meaning it should restore
0N/A * state. At this point, it can use the state parameter to restore
0N/A * said state, and of course return <code>null</code> since there's
0N/A * no longer anything to store.
0N/A *
0N/A * @param location the drop location (as calculated by
0N/A * <code>dropLocationForPoint</code>) or <code>null</code>
0N/A * if there's no longer a valid drop location
0N/A * @param state the state object saved earlier for this component,
0N/A * or <code>null</code>
0N/A * @param forDrop whether or not the method is being called because an
0N/A * actual drop occurred
0N/A * @return any saved state for this component, or <code>null</code> if none
0N/A */
0N/A Object setDropLocation(TransferHandler.DropLocation location,
0N/A Object state,
0N/A boolean forDrop) {
0N/A
0N/A Object retVal = null;
0N/A DropLocation treeLocation = (DropLocation)location;
0N/A
0N/A if (dropMode == DropMode.USE_SELECTION) {
0N/A if (treeLocation == null) {
0N/A if (!forDrop && state != null) {
0N/A setSelectionPaths(((TreePath[][])state)[0]);
0N/A setAnchorSelectionPath(((TreePath[][])state)[1][0]);
0N/A setLeadSelectionPath(((TreePath[][])state)[1][1]);
0N/A }
0N/A } else {
0N/A if (dropLocation == null) {
0N/A TreePath[] paths = getSelectionPaths();
0N/A if (paths == null) {
0N/A paths = new TreePath[0];
0N/A }
0N/A
0N/A retVal = new TreePath[][] {paths,
0N/A {getAnchorSelectionPath(), getLeadSelectionPath()}};
0N/A } else {
0N/A retVal = state;
0N/A }
0N/A
0N/A setSelectionPath(treeLocation.getPath());
0N/A }
0N/A }
0N/A
0N/A DropLocation old = dropLocation;
0N/A dropLocation = treeLocation;
0N/A firePropertyChange("dropLocation", old, dropLocation);
0N/A
0N/A return retVal;
0N/A }
0N/A
0N/A /**
0N/A * Called to indicate to this component that DnD is done.
0N/A * Allows for us to cancel the expand timer.
0N/A */
0N/A void dndDone() {
0N/A cancelDropTimer();
0N/A dropTimer = null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the location that this component should visually indicate
0N/A * as the drop location during a DnD operation over the component,
0N/A * or {@code null} if no location is to currently be shown.
0N/A * <p>
0N/A * This method is not meant for querying the drop location
0N/A * from a {@code TransferHandler}, as the drop location is only
0N/A * set after the {@code TransferHandler}'s <code>canImport</code>
0N/A * has returned and has allowed for the location to be shown.
0N/A * <p>
0N/A * When this property changes, a property change event with
0N/A * name "dropLocation" is fired by the component.
0N/A *
0N/A * @return the drop location
0N/A * @see #setDropMode
0N/A * @see TransferHandler#canImport(TransferHandler.TransferSupport)
0N/A * @since 1.6
0N/A */
0N/A public final DropLocation getDropLocation() {
0N/A return dropLocation;
0N/A }
0N/A
0N/A private void startDropTimer() {
0N/A if (dropTimer == null) {
0N/A dropTimer = new TreeTimer();
0N/A }
0N/A dropTimer.start();
0N/A }
0N/A
0N/A private void cancelDropTimer() {
0N/A if (dropTimer != null && dropTimer.isRunning()) {
0N/A expandRow = -1;
0N/A dropTimer.stop();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns <code>isEditable</code>. This is invoked from the UI before
0N/A * editing begins to insure that the given path can be edited. This
0N/A * is provided as an entry point for subclassers to add filtered
0N/A * editing without having to resort to creating a new editor.
0N/A *
0N/A * @return true if every parent node and the node itself is editable
0N/A * @see #isEditable
0N/A */
0N/A public boolean isPathEditable(TreePath path) {
0N/A return isEditable();
0N/A }
0N/A
0N/A /**
0N/A * Overrides <code>JComponent</code>'s <code>getToolTipText</code>
0N/A * method in order to allow
0N/A * renderer's tips to be used if it has text set.
0N/A * <p>
0N/A * NOTE: For <code>JTree</code> to properly display tooltips of its
0N/A * renderers, <code>JTree</code> must be a registered component with the
0N/A * <code>ToolTipManager</code>. This can be done by invoking
0N/A * <code>ToolTipManager.sharedInstance().registerComponent(tree)</code>.
0N/A * This is not done automatically!
0N/A *
0N/A * @param event the <code>MouseEvent</code> that initiated the
0N/A * <code>ToolTip</code> display
0N/A * @return a string containing the tooltip or <code>null</code>
0N/A * if <code>event</code> is null
0N/A */
0N/A public String getToolTipText(MouseEvent event) {
0N/A String tip = null;
0N/A
0N/A if(event != null) {
0N/A Point p = event.getPoint();
0N/A int selRow = getRowForLocation(p.x, p.y);
0N/A TreeCellRenderer r = getCellRenderer();
0N/A
0N/A if(selRow != -1 && r != null) {
0N/A TreePath path = getPathForRow(selRow);
0N/A Object lastPath = path.getLastPathComponent();
0N/A Component rComponent = r.getTreeCellRendererComponent
0N/A (this, lastPath, isRowSelected(selRow),
0N/A isExpanded(selRow), getModel().isLeaf(lastPath), selRow,
0N/A true);
0N/A
0N/A if(rComponent instanceof JComponent) {
0N/A MouseEvent newEvent;
0N/A Rectangle pathBounds = getPathBounds(path);
0N/A
0N/A p.translate(-pathBounds.x, -pathBounds.y);
0N/A newEvent = new MouseEvent(rComponent, event.getID(),
0N/A event.getWhen(),
0N/A event.getModifiers(),
0N/A p.x, p.y,
0N/A event.getXOnScreen(),
0N/A event.getYOnScreen(),
0N/A event.getClickCount(),
0N/A event.isPopupTrigger(),
0N/A MouseEvent.NOBUTTON);
0N/A
0N/A tip = ((JComponent)rComponent).getToolTipText(newEvent);
0N/A }
0N/A }
0N/A }
0N/A // No tip from the renderer get our own tip
0N/A if (tip == null) {
0N/A tip = getToolTipText();
0N/A }
0N/A return tip;
0N/A }
0N/A
0N/A /**
0N/A * Called by the renderers to convert the specified value to
0N/A * text. This implementation returns <code>value.toString</code>, ignoring
0N/A * all other arguments. To control the conversion, subclass this
0N/A * method and use any of the arguments you need.
0N/A *
0N/A * @param value the <code>Object</code> to convert to text
0N/A * @param selected true if the node is selected
0N/A * @param expanded true if the node is expanded
0N/A * @param leaf true if the node is a leaf node
0N/A * @param row an integer specifying the node's display row, where 0 is
0N/A * the first row in the display
0N/A * @param hasFocus true if the node has the focus
0N/A * @return the <code>String</code> representation of the node's value
0N/A */
0N/A public String convertValueToText(Object value, boolean selected,
0N/A boolean expanded, boolean leaf, int row,
0N/A boolean hasFocus) {
0N/A if(value != null) {
0N/A String sValue = value.toString();
0N/A if (sValue != null) {
0N/A return sValue;
0N/A }
0N/A }
0N/A return "";
0N/A }
0N/A
0N/A //
0N/A // The following are convenience methods that get forwarded to the
0N/A // current TreeUI.
0N/A //
0N/A
0N/A /**
0N/A * Returns the number of viewable nodes. A node is viewable if all of its
0N/A * parents are expanded. The root is only included in this count if
0N/A * {@code isRootVisible()} is {@code true}. This returns {@code 0} if
0N/A * the UI has not been set.
0N/A *
0N/A * @return the number of viewable nodes
0N/A */
0N/A public int getRowCount() {
0N/A TreeUI tree = getUI();
0N/A
0N/A if(tree != null)
0N/A return tree.getRowCount(this);
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Selects the node identified by the specified path. If any
0N/A * component of the path is hidden (under a collapsed node), and
0N/A * <code>getExpandsSelectedPaths</code> is true it is
0N/A * exposed (made viewable).
0N/A *
0N/A * @param path the <code>TreePath</code> specifying the node to select
0N/A */
0N/A public void setSelectionPath(TreePath path) {
0N/A getSelectionModel().setSelectionPath(path);
0N/A }
0N/A
0N/A /**
0N/A * Selects the nodes identified by the specified array of paths.
0N/A * If any component in any of the paths is hidden (under a collapsed
0N/A * node), and <code>getExpandsSelectedPaths</code> is true
0N/A * it is exposed (made viewable).
0N/A *
0N/A * @param paths an array of <code>TreePath</code> objects that specifies
0N/A * the nodes to select
0N/A */
0N/A public void setSelectionPaths(TreePath[] paths) {
0N/A getSelectionModel().setSelectionPaths(paths);
0N/A }
0N/A
0N/A /**
0N/A * Sets the path identifies as the lead. The lead may not be selected.
0N/A * The lead is not maintained by <code>JTree</code>,
0N/A * rather the UI will update it.
0N/A * <p>
0N/A * This is a bound property.
0N/A *
0N/A * @param newPath the new lead path
0N/A * @since 1.3
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: Lead selection path
0N/A */
0N/A public void setLeadSelectionPath(TreePath newPath) {
0N/A TreePath oldValue = leadPath;
0N/A
0N/A leadPath = newPath;
0N/A firePropertyChange(LEAD_SELECTION_PATH_PROPERTY, oldValue, newPath);
0N/A }
0N/A
0N/A /**
0N/A * Sets the path identified as the anchor.
0N/A * The anchor is not maintained by <code>JTree</code>, rather the UI will
0N/A * update it.
0N/A * <p>
0N/A * This is a bound property.
0N/A *
0N/A * @param newPath the new anchor path
0N/A * @since 1.3
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: Anchor selection path
0N/A */
0N/A public void setAnchorSelectionPath(TreePath newPath) {
0N/A TreePath oldValue = anchorPath;
0N/A
0N/A anchorPath = newPath;
0N/A firePropertyChange(ANCHOR_SELECTION_PATH_PROPERTY, oldValue, newPath);
0N/A }
0N/A
0N/A /**
0N/A * Selects the node at the specified row in the display.
0N/A *
0N/A * @param row the row to select, where 0 is the first row in
0N/A * the display
0N/A */
0N/A public void setSelectionRow(int row) {
0N/A int[] rows = { row };
0N/A
0N/A setSelectionRows(rows);
0N/A }
0N/A
0N/A /**
0N/A * Selects the nodes corresponding to each of the specified rows
0N/A * in the display. If a particular element of <code>rows</code> is
0N/A * < 0 or >= <code>getRowCount</code>, it will be ignored.
0N/A * If none of the elements
0N/A * in <code>rows</code> are valid rows, the selection will
0N/A * be cleared. That is it will be as if <code>clearSelection</code>
0N/A * was invoked.
0N/A *
0N/A * @param rows an array of ints specifying the rows to select,
0N/A * where 0 indicates the first row in the display
0N/A */
0N/A public void setSelectionRows(int[] rows) {
0N/A TreeUI ui = getUI();
0N/A
0N/A if(ui != null && rows != null) {
0N/A int numRows = rows.length;
0N/A TreePath[] paths = new TreePath[numRows];
0N/A
0N/A for(int counter = 0; counter < numRows; counter++) {
0N/A paths[counter] = ui.getPathForRow(this, rows[counter]);
0N/A }
0N/A setSelectionPaths(paths);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Adds the node identified by the specified <code>TreePath</code>
0N/A * to the current selection. If any component of the path isn't
0N/A * viewable, and <code>getExpandsSelectedPaths</code> is true it is
0N/A * made viewable.
0N/A * <p>
0N/A * Note that <code>JTree</code> does not allow duplicate nodes to
0N/A * exist as children under the same parent -- each sibling must be
0N/A * a unique object.
0N/A *
0N/A * @param path the <code>TreePath</code> to add
0N/A */
0N/A public void addSelectionPath(TreePath path) {
0N/A getSelectionModel().addSelectionPath(path);
0N/A }
0N/A
0N/A /**
0N/A * Adds each path in the array of paths to the current selection. If
0N/A * any component of any of the paths isn't viewable and
0N/A * <code>getExpandsSelectedPaths</code> is true, it is
0N/A * made viewable.
0N/A * <p>
0N/A * Note that <code>JTree</code> does not allow duplicate nodes to
0N/A * exist as children under the same parent -- each sibling must be
0N/A * a unique object.
0N/A *
0N/A * @param paths an array of <code>TreePath</code> objects that specifies
0N/A * the nodes to add
0N/A */
0N/A public void addSelectionPaths(TreePath[] paths) {
0N/A getSelectionModel().addSelectionPaths(paths);
0N/A }
0N/A
0N/A /**
0N/A * Adds the path at the specified row to the current selection.
0N/A *
0N/A * @param row an integer specifying the row of the node to add,
0N/A * where 0 is the first row in the display
0N/A */
0N/A public void addSelectionRow(int row) {
0N/A int[] rows = { row };
0N/A
0N/A addSelectionRows(rows);
0N/A }
0N/A
0N/A /**
0N/A * Adds the paths at each of the specified rows to the current selection.
0N/A *
0N/A * @param rows an array of ints specifying the rows to add,
0N/A * where 0 indicates the first row in the display
0N/A */
0N/A public void addSelectionRows(int[] rows) {
0N/A TreeUI ui = getUI();
0N/A
0N/A if(ui != null && rows != null) {
0N/A int numRows = rows.length;
0N/A TreePath[] paths = new TreePath[numRows];
0N/A
0N/A for(int counter = 0; counter < numRows; counter++)
0N/A paths[counter] = ui.getPathForRow(this, rows[counter]);
0N/A addSelectionPaths(paths);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the last path component of the selected path. This is
0N/A * a convenience method for
0N/A * {@code getSelectionModel().getSelectionPath().getLastPathComponent()}.
0N/A * This is typically only useful if the selection has one path.
0N/A *
0N/A * @return the last path component of the selected path, or
0N/A * <code>null</code> if nothing is selected
0N/A * @see TreePath#getLastPathComponent
0N/A */
0N/A public Object getLastSelectedPathComponent() {
0N/A TreePath selPath = getSelectionModel().getSelectionPath();
0N/A
0N/A if(selPath != null)
0N/A return selPath.getLastPathComponent();
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the path identified as the lead.
0N/A * @return path identified as the lead
0N/A */
0N/A public TreePath getLeadSelectionPath() {
0N/A return leadPath;
0N/A }
0N/A
0N/A /**
0N/A * Returns the path identified as the anchor.
0N/A * @return path identified as the anchor
0N/A * @since 1.3
0N/A */
0N/A public TreePath getAnchorSelectionPath() {
0N/A return anchorPath;
0N/A }
0N/A
0N/A /**
0N/A * Returns the path to the first selected node.
0N/A *
0N/A * @return the <code>TreePath</code> for the first selected node,
0N/A * or <code>null</code> if nothing is currently selected
0N/A */
0N/A public TreePath getSelectionPath() {
0N/A return getSelectionModel().getSelectionPath();
0N/A }
0N/A
0N/A /**
0N/A * Returns the paths of all selected values.
0N/A *
0N/A * @return an array of <code>TreePath</code> objects indicating the selected
0N/A * nodes, or <code>null</code> if nothing is currently selected
0N/A */
0N/A public TreePath[] getSelectionPaths() {
4510N/A TreePath[] selectionPaths = getSelectionModel().getSelectionPaths();
4510N/A
4510N/A return (selectionPaths != null && selectionPaths.length > 0) ? selectionPaths : null;
0N/A }
0N/A
0N/A /**
0N/A * Returns all of the currently selected rows. This method is simply
0N/A * forwarded to the <code>TreeSelectionModel</code>.
0N/A * If nothing is selected <code>null</code> or an empty array will
0N/A * be returned, based on the <code>TreeSelectionModel</code>
0N/A * implementation.
0N/A *
0N/A * @return an array of integers that identifies all currently selected rows
0N/A * where 0 is the first row in the display
0N/A */
0N/A public int[] getSelectionRows() {
0N/A return getSelectionModel().getSelectionRows();
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of nodes selected.
0N/A *
0N/A * @return the number of nodes selected
0N/A */
0N/A public int getSelectionCount() {
0N/A return selectionModel.getSelectionCount();
0N/A }
0N/A
0N/A /**
0N/A * Returns the smallest selected row. If the selection is empty, or
0N/A * none of the selected paths are viewable, {@code -1} is returned.
0N/A *
0N/A * @return the smallest selected row
0N/A */
0N/A public int getMinSelectionRow() {
0N/A return getSelectionModel().getMinSelectionRow();
0N/A }
0N/A
0N/A /**
0N/A * Returns the largest selected row. If the selection is empty, or
0N/A * none of the selected paths are viewable, {@code -1} is returned.
0N/A *
0N/A * @return the largest selected row
0N/A */
0N/A public int getMaxSelectionRow() {
0N/A return getSelectionModel().getMaxSelectionRow();
0N/A }
0N/A
0N/A /**
0N/A * Returns the row index corresponding to the lead path.
0N/A *
0N/A * @return an integer giving the row index of the lead path,
0N/A * where 0 is the first row in the display; or -1
0N/A * if <code>leadPath</code> is <code>null</code>
0N/A */
0N/A public int getLeadSelectionRow() {
0N/A TreePath leadPath = getLeadSelectionPath();
0N/A
0N/A if (leadPath != null) {
0N/A return getRowForPath(leadPath);
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the item identified by the path is currently selected.
0N/A *
0N/A * @param path a <code>TreePath</code> identifying a node
0N/A * @return true if the node is selected
0N/A */
0N/A public boolean isPathSelected(TreePath path) {
0N/A return getSelectionModel().isPathSelected(path);
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the node identified by row is selected.
0N/A *
0N/A * @param row an integer specifying a display row, where 0 is the first
0N/A * row in the display
0N/A * @return true if the node is selected
0N/A */
0N/A public boolean isRowSelected(int row) {
0N/A return getSelectionModel().isRowSelected(row);
0N/A }
0N/A
0N/A /**
0N/A * Returns an <code>Enumeration</code> of the descendants of the
0N/A * path <code>parent</code> that
0N/A * are currently expanded. If <code>parent</code> is not currently
0N/A * expanded, this will return <code>null</code>.
0N/A * If you expand/collapse nodes while
0N/A * iterating over the returned <code>Enumeration</code>
0N/A * this may not return all
0N/A * the expanded paths, or may return paths that are no longer expanded.
0N/A *
0N/A * @param parent the path which is to be examined
0N/A * @return an <code>Enumeration</code> of the descendents of
0N/A * <code>parent</code>, or <code>null</code> if
0N/A * <code>parent</code> is not currently expanded
0N/A */
0N/A public Enumeration<TreePath> getExpandedDescendants(TreePath parent) {
0N/A if(!isExpanded(parent))
0N/A return null;
0N/A
625N/A Enumeration<TreePath> toggledPaths = expandedState.keys();
625N/A Vector<TreePath> elements = null;
0N/A TreePath path;
0N/A Object value;
0N/A
0N/A if(toggledPaths != null) {
0N/A while(toggledPaths.hasMoreElements()) {
625N/A path = toggledPaths.nextElement();
0N/A value = expandedState.get(path);
0N/A // Add the path if it is expanded, a descendant of parent,
0N/A // and it is visible (all parents expanded). This is rather
0N/A // expensive!
0N/A if(path != parent && value != null &&
0N/A ((Boolean)value).booleanValue() &&
0N/A parent.isDescendant(path) && isVisible(path)) {
0N/A if (elements == null) {
625N/A elements = new Vector<TreePath>();
0N/A }
0N/A elements.addElement(path);
0N/A }
0N/A }
0N/A }
0N/A if (elements == null) {
0N/A Set<TreePath> empty = Collections.emptySet();
0N/A return Collections.enumeration(empty);
0N/A }
0N/A return elements.elements();
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the node identified by the path has ever been
0N/A * expanded.
0N/A * @return true if the <code>path</code> has ever been expanded
0N/A */
0N/A public boolean hasBeenExpanded(TreePath path) {
0N/A return (path != null && expandedState.get(path) != null);
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the node identified by the path is currently expanded,
0N/A *
0N/A * @param path the <code>TreePath</code> specifying the node to check
0N/A * @return false if any of the nodes in the node's path are collapsed,
0N/A * true if all nodes in the path are expanded
0N/A */
0N/A public boolean isExpanded(TreePath path) {
2526N/A
0N/A if(path == null)
0N/A return false;
2526N/A Object value;
2526N/A
2526N/A do{
2526N/A value = expandedState.get(path);
2526N/A if(value == null || !((Boolean)value).booleanValue())
2526N/A return false;
2526N/A } while( (path=path.getParentPath())!=null );
2526N/A
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the node at the specified display row is currently
0N/A * expanded.
0N/A *
0N/A * @param row the row to check, where 0 is the first row in the
0N/A * display
0N/A * @return true if the node is currently expanded, otherwise false
0N/A */
0N/A public boolean isExpanded(int row) {
0N/A TreeUI tree = getUI();
0N/A
0N/A if(tree != null) {
0N/A TreePath path = tree.getPathForRow(this, row);
0N/A
0N/A if(path != null) {
625N/A Boolean value = expandedState.get(path);
0N/A
0N/A return (value != null && value.booleanValue());
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the value identified by path is currently collapsed,
0N/A * this will return false if any of the values in path are currently
0N/A * not being displayed.
0N/A *
0N/A * @param path the <code>TreePath</code> to check
0N/A * @return true if any of the nodes in the node's path are collapsed,
0N/A * false if all nodes in the path are expanded
0N/A */
0N/A public boolean isCollapsed(TreePath path) {
0N/A return !isExpanded(path);
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the node at the specified display row is collapsed.
0N/A *
0N/A * @param row the row to check, where 0 is the first row in the
0N/A * display
0N/A * @return true if the node is currently collapsed, otherwise false
0N/A */
0N/A public boolean isCollapsed(int row) {
0N/A return !isExpanded(row);
0N/A }
0N/A
0N/A /**
0N/A * Ensures that the node identified by path is currently viewable.
0N/A *
0N/A * @param path the <code>TreePath</code> to make visible
0N/A */
0N/A public void makeVisible(TreePath path) {
0N/A if(path != null) {
0N/A TreePath parentPath = path.getParentPath();
0N/A
0N/A if(parentPath != null) {
0N/A expandPath(parentPath);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the value identified by path is currently viewable,
0N/A * which means it is either the root or all of its parents are expanded.
0N/A * Otherwise, this method returns false.
0N/A *
0N/A * @return true if the node is viewable, otherwise false
0N/A */
0N/A public boolean isVisible(TreePath path) {
0N/A if(path != null) {
0N/A TreePath parentPath = path.getParentPath();
0N/A
0N/A if(parentPath != null)
0N/A return isExpanded(parentPath);
0N/A // Root.
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>Rectangle</code> that the specified node will be drawn
0N/A * into. Returns <code>null</code> if any component in the path is hidden
0N/A * (under a collapsed parent).
0N/A * <p>
0N/A * Note:<br>
0N/A * This method returns a valid rectangle, even if the specified
0N/A * node is not currently displayed.
0N/A *
0N/A * @param path the <code>TreePath</code> identifying the node
0N/A * @return the <code>Rectangle</code> the node is drawn in,
0N/A * or <code>null</code>
0N/A */
0N/A public Rectangle getPathBounds(TreePath path) {
0N/A TreeUI tree = getUI();
0N/A
0N/A if(tree != null)
0N/A return tree.getPathBounds(this, path);
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>Rectangle</code> that the node at the specified row is
0N/A * drawn in.
0N/A *
0N/A * @param row the row to be drawn, where 0 is the first row in the
0N/A * display
0N/A * @return the <code>Rectangle</code> the node is drawn in
0N/A */
0N/A public Rectangle getRowBounds(int row) {
0N/A return getPathBounds(getPathForRow(row));
0N/A }
0N/A
0N/A /**
0N/A * Makes sure all the path components in path are expanded (except
0N/A * for the last path component) and scrolls so that the
0N/A * node identified by the path is displayed. Only works when this
0N/A * <code>JTree</code> is contained in a <code>JScrollPane</code>.
0N/A *
0N/A * @param path the <code>TreePath</code> identifying the node to
0N/A * bring into view
0N/A */
0N/A public void scrollPathToVisible(TreePath path) {
0N/A if(path != null) {
0N/A makeVisible(path);
0N/A
0N/A Rectangle bounds = getPathBounds(path);
0N/A
0N/A if(bounds != null) {
0N/A scrollRectToVisible(bounds);
0N/A if (accessibleContext != null) {
0N/A ((AccessibleJTree)accessibleContext).fireVisibleDataPropertyChange();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Scrolls the item identified by row until it is displayed. The minimum
0N/A * of amount of scrolling necessary to bring the row into view
0N/A * is performed. Only works when this <code>JTree</code> is contained in a
0N/A * <code>JScrollPane</code>.
0N/A *
0N/A * @param row an integer specifying the row to scroll, where 0 is the
0N/A * first row in the display
0N/A */
0N/A public void scrollRowToVisible(int row) {
0N/A scrollPathToVisible(getPathForRow(row));
0N/A }
0N/A
0N/A /**
0N/A * Returns the path for the specified row. If <code>row</code> is
0N/A * not visible, or a {@code TreeUI} has not been set, <code>null</code>
0N/A * is returned.
0N/A *
0N/A * @param row an integer specifying a row
0N/A * @return the <code>TreePath</code> to the specified node,
0N/A * <code>null</code> if <code>row < 0</code>
0N/A * or <code>row >= getRowCount()</code>
0N/A */
0N/A public TreePath getPathForRow(int row) {
0N/A TreeUI tree = getUI();
0N/A
0N/A if(tree != null)
0N/A return tree.getPathForRow(this, row);
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the row that displays the node identified by the specified
0N/A * path.
0N/A *
0N/A * @param path the <code>TreePath</code> identifying a node
0N/A * @return an integer specifying the display row, where 0 is the first
0N/A * row in the display, or -1 if any of the elements in path
0N/A * are hidden under a collapsed parent.
0N/A */
0N/A public int getRowForPath(TreePath path) {
0N/A TreeUI tree = getUI();
0N/A
0N/A if(tree != null)
0N/A return tree.getRowForPath(this, path);
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Ensures that the node identified by the specified path is
0N/A * expanded and viewable. If the last item in the path is a
0N/A * leaf, this will have no effect.
0N/A *
0N/A * @param path the <code>TreePath</code> identifying a node
0N/A */
0N/A public void expandPath(TreePath path) {
0N/A // Only expand if not leaf!
0N/A TreeModel model = getModel();
0N/A
0N/A if(path != null && model != null &&
0N/A !model.isLeaf(path.getLastPathComponent())) {
0N/A setExpandedState(path, true);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Ensures that the node in the specified row is expanded and
0N/A * viewable.
0N/A * <p>
0N/A * If <code>row</code> is < 0 or >= <code>getRowCount</code> this
0N/A * will have no effect.
0N/A *
0N/A * @param row an integer specifying a display row, where 0 is the
0N/A * first row in the display
0N/A */
0N/A public void expandRow(int row) {
0N/A expandPath(getPathForRow(row));
0N/A }
0N/A
0N/A /**
0N/A * Ensures that the node identified by the specified path is
0N/A * collapsed and viewable.
0N/A *
0N/A * @param path the <code>TreePath</code> identifying a node
0N/A */
0N/A public void collapsePath(TreePath path) {
0N/A setExpandedState(path, false);
0N/A }
0N/A
0N/A /**
0N/A * Ensures that the node in the specified row is collapsed.
0N/A * <p>
0N/A * If <code>row</code> is < 0 or >= <code>getRowCount</code> this
0N/A * will have no effect.
0N/A *
0N/A * @param row an integer specifying a display row, where 0 is the
0N/A * first row in the display
0N/A */
0N/A public void collapseRow(int row) {
0N/A collapsePath(getPathForRow(row));
0N/A }
0N/A
0N/A /**
0N/A * Returns the path for the node at the specified location.
0N/A *
0N/A * @param x an integer giving the number of pixels horizontally from
0N/A * the left edge of the display area, minus any left margin
0N/A * @param y an integer giving the number of pixels vertically from
0N/A * the top of the display area, minus any top margin
0N/A * @return the <code>TreePath</code> for the node at that location
0N/A */
0N/A public TreePath getPathForLocation(int x, int y) {
0N/A TreePath closestPath = getClosestPathForLocation(x, y);
0N/A
0N/A if(closestPath != null) {
0N/A Rectangle pathBounds = getPathBounds(closestPath);
0N/A
0N/A if(pathBounds != null &&
0N/A x >= pathBounds.x && x < (pathBounds.x + pathBounds.width) &&
0N/A y >= pathBounds.y && y < (pathBounds.y + pathBounds.height))
0N/A return closestPath;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the row for the specified location.
0N/A *
0N/A * @param x an integer giving the number of pixels horizontally from
0N/A * the left edge of the display area, minus any left margin
0N/A * @param y an integer giving the number of pixels vertically from
0N/A * the top of the display area, minus any top margin
0N/A * @return the row corresponding to the location, or -1 if the
0N/A * location is not within the bounds of a displayed cell
0N/A * @see #getClosestRowForLocation
0N/A */
0N/A public int getRowForLocation(int x, int y) {
0N/A return getRowForPath(getPathForLocation(x, y));
0N/A }
0N/A
0N/A /**
0N/A * Returns the path to the node that is closest to x,y. If
0N/A * no nodes are currently viewable, or there is no model, returns
0N/A * <code>null</code>, otherwise it always returns a valid path. To test if
0N/A * the node is exactly at x, y, get the node's bounds and
0N/A * test x, y against that.
0N/A *
0N/A * @param x an integer giving the number of pixels horizontally from
0N/A * the left edge of the display area, minus any left margin
0N/A * @param y an integer giving the number of pixels vertically from
0N/A * the top of the display area, minus any top margin
0N/A * @return the <code>TreePath</code> for the node closest to that location,
0N/A * <code>null</code> if nothing is viewable or there is no model
0N/A *
0N/A * @see #getPathForLocation
0N/A * @see #getPathBounds
0N/A */
0N/A public TreePath getClosestPathForLocation(int x, int y) {
0N/A TreeUI tree = getUI();
0N/A
0N/A if(tree != null)
0N/A return tree.getClosestPathForLocation(this, x, y);
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the row to the node that is closest to x,y. If no nodes
0N/A * are viewable or there is no model, returns -1. Otherwise,
0N/A * it always returns a valid row. To test if the returned object is
0N/A * exactly at x, y, get the bounds for the node at the returned
0N/A * row and test x, y against that.
0N/A *
0N/A * @param x an integer giving the number of pixels horizontally from
0N/A * the left edge of the display area, minus any left margin
0N/A * @param y an integer giving the number of pixels vertically from
0N/A * the top of the display area, minus any top margin
0N/A * @return the row closest to the location, -1 if nothing is
0N/A * viewable or there is no model
0N/A *
0N/A * @see #getRowForLocation
0N/A * @see #getRowBounds
0N/A */
0N/A public int getClosestRowForLocation(int x, int y) {
0N/A return getRowForPath(getClosestPathForLocation(x, y));
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the tree is being edited. The item that is being
0N/A * edited can be obtained using <code>getSelectionPath</code>.
0N/A *
0N/A * @return true if the user is currently editing a node
0N/A * @see #getSelectionPath
0N/A */
0N/A public boolean isEditing() {
0N/A TreeUI tree = getUI();
0N/A
0N/A if(tree != null)
0N/A return tree.isEditing(this);
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Ends the current editing session.
0N/A * (The <code>DefaultTreeCellEditor</code>
0N/A * object saves any edits that are currently in progress on a cell.
0N/A * Other implementations may operate differently.)
0N/A * Has no effect if the tree isn't being edited.
0N/A * <blockquote>
0N/A * <b>Note:</b><br>
0N/A * To make edit-saves automatic whenever the user changes
0N/A * their position in the tree, use {@link #setInvokesStopCellEditing}.
0N/A * </blockquote>
0N/A *
0N/A * @return true if editing was in progress and is now stopped,
0N/A * false if editing was not in progress
0N/A */
0N/A public boolean stopEditing() {
0N/A TreeUI tree = getUI();
0N/A
0N/A if(tree != null)
0N/A return tree.stopEditing(this);
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Cancels the current editing session. Has no effect if the
0N/A * tree isn't being edited.
0N/A */
0N/A public void cancelEditing() {
0N/A TreeUI tree = getUI();
0N/A
0N/A if(tree != null)
0N/A tree.cancelEditing(this);
0N/A }
0N/A
0N/A /**
0N/A * Selects the node identified by the specified path and initiates
0N/A * editing. The edit-attempt fails if the <code>CellEditor</code>
0N/A * does not allow
0N/A * editing for the specified item.
0N/A *
0N/A * @param path the <code>TreePath</code> identifying a node
0N/A */
0N/A public void startEditingAtPath(TreePath path) {
0N/A TreeUI tree = getUI();
0N/A
0N/A if(tree != null)
0N/A tree.startEditingAtPath(this, path);
0N/A }
0N/A
0N/A /**
0N/A * Returns the path to the element that is currently being edited.
0N/A *
0N/A * @return the <code>TreePath</code> for the node being edited
0N/A */
0N/A public TreePath getEditingPath() {
0N/A TreeUI tree = getUI();
0N/A
0N/A if(tree != null)
0N/A return tree.getEditingPath(this);
0N/A return null;
0N/A }
0N/A
0N/A //
0N/A // Following are primarily convenience methods for mapping from
0N/A // row based selections to path selections. Sometimes it is
0N/A // easier to deal with these than paths (mouse downs, key downs
0N/A // usually just deal with index based selections).
0N/A // Since row based selections require a UI many of these won't work
0N/A // without one.
0N/A //
0N/A
0N/A /**
0N/A * Sets the tree's selection model. When a <code>null</code> value is
0N/A * specified an empty
0N/A * <code>selectionModel</code> is used, which does not allow selections.
0N/A * <p>
0N/A * This is a bound property.
0N/A *
0N/A * @param selectionModel the <code>TreeSelectionModel</code> to use,
0N/A * or <code>null</code> to disable selections
0N/A * @see TreeSelectionModel
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: The tree's selection model.
0N/A */
0N/A public void setSelectionModel(TreeSelectionModel selectionModel) {
0N/A if(selectionModel == null)
0N/A selectionModel = EmptySelectionModel.sharedInstance();
0N/A
0N/A TreeSelectionModel oldValue = this.selectionModel;
0N/A
0N/A if (this.selectionModel != null && selectionRedirector != null) {
0N/A this.selectionModel.removeTreeSelectionListener
0N/A (selectionRedirector);
0N/A }
0N/A if (accessibleContext != null) {
0N/A this.selectionModel.removeTreeSelectionListener((TreeSelectionListener)accessibleContext);
0N/A selectionModel.addTreeSelectionListener((TreeSelectionListener)accessibleContext);
0N/A }
0N/A
0N/A this.selectionModel = selectionModel;
0N/A if (selectionRedirector != null) {
0N/A this.selectionModel.addTreeSelectionListener(selectionRedirector);
0N/A }
0N/A firePropertyChange(SELECTION_MODEL_PROPERTY, oldValue,
0N/A this.selectionModel);
0N/A
0N/A if (accessibleContext != null) {
0N/A accessibleContext.firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY,
0N/A Boolean.valueOf(false), Boolean.valueOf(true));
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the model for selections. This should always return a
0N/A * non-<code>null</code> value. If you don't want to allow anything
0N/A * to be selected
0N/A * set the selection model to <code>null</code>, which forces an empty
0N/A * selection model to be used.
0N/A *
0N/A * @see #setSelectionModel
0N/A */
0N/A public TreeSelectionModel getSelectionModel() {
0N/A return selectionModel;
0N/A }
0N/A
0N/A /**
0N/A * Returns the paths (inclusive) between the specified rows. If
0N/A * the specified indices are within the viewable set of rows, or
0N/A * bound the viewable set of rows, then the indices are
0N/A * constrained by the viewable set of rows. If the specified
0N/A * indices are not within the viewable set of rows, or do not
0N/A * bound the viewable set of rows, then an empty array is
0N/A * returned. For example, if the row count is {@code 10}, and this
0N/A * method is invoked with {@code -1, 20}, then the specified
0N/A * indices are constrained to the viewable set of rows, and this is
0N/A * treated as if invoked with {@code 0, 9}. On the other hand, if
0N/A * this were invoked with {@code -10, -1}, then the specified
0N/A * indices do not bound the viewable set of rows, and an empty
0N/A * array is returned.
0N/A * <p>
0N/A * The parameters are not order dependent. That is, {@code
0N/A * getPathBetweenRows(x, y)} is equivalent to
0N/A * {@code getPathBetweenRows(y, x)}.
0N/A * <p>
0N/A * An empty array is returned if the row count is {@code 0}, or
0N/A * the specified indices do not bound the viewable set of rows.
0N/A *
0N/A * @param index0 the first index in the range
0N/A * @param index1 the last index in the range
0N/A * @return the paths (inclusive) between the specified row indices
0N/A */
0N/A protected TreePath[] getPathBetweenRows(int index0, int index1) {
0N/A TreeUI tree = getUI();
0N/A if (tree != null) {
0N/A int rowCount = getRowCount();
0N/A if (rowCount > 0 && !((index0 < 0 && index1 < 0) ||
0N/A (index0 >= rowCount && index1 >= rowCount))){
0N/A index0 = Math.min(rowCount - 1, Math.max(index0, 0));
0N/A index1 = Math.min(rowCount - 1, Math.max(index1, 0));
0N/A int minIndex = Math.min(index0, index1);
0N/A int maxIndex = Math.max(index0, index1);
0N/A TreePath[] selection = new TreePath[
0N/A maxIndex - minIndex + 1];
0N/A for(int counter = minIndex; counter <= maxIndex; counter++) {
0N/A selection[counter - minIndex] =
0N/A tree.getPathForRow(this, counter);
0N/A }
0N/A return selection;
0N/A }
0N/A }
0N/A return new TreePath[0];
0N/A }
0N/A
0N/A /**
0N/A * Selects the rows in the specified interval (inclusive). If
0N/A * the specified indices are within the viewable set of rows, or bound
0N/A * the viewable set of rows, then the specified rows are constrained by
0N/A * the viewable set of rows. If the specified indices are not within the
0N/A * viewable set of rows, or do not bound the viewable set of rows, then
0N/A * the selection is cleared. For example, if the row count is {@code
0N/A * 10}, and this method is invoked with {@code -1, 20}, then the
0N/A * specified indices bounds the viewable range, and this is treated as
0N/A * if invoked with {@code 0, 9}. On the other hand, if this were
0N/A * invoked with {@code -10, -1}, then the specified indices do not
0N/A * bound the viewable set of rows, and the selection is cleared.
0N/A * <p>
0N/A * The parameters are not order dependent. That is, {@code
0N/A * setSelectionInterval(x, y)} is equivalent to
0N/A * {@code setSelectionInterval(y, x)}.
0N/A *
0N/A * @param index0 the first index in the range to select
0N/A * @param index1 the last index in the range to select
0N/A */
0N/A public void setSelectionInterval(int index0, int index1) {
0N/A TreePath[] paths = getPathBetweenRows(index0, index1);
0N/A
0N/A this.getSelectionModel().setSelectionPaths(paths);
0N/A }
0N/A
0N/A /**
0N/A * Adds the specified rows (inclusive) to the selection. If the
0N/A * specified indices are within the viewable set of rows, or bound
0N/A * the viewable set of rows, then the specified indices are
0N/A * constrained by the viewable set of rows. If the indices are not
0N/A * within the viewable set of rows, or do not bound the viewable
0N/A * set of rows, then the selection is unchanged. For example, if
0N/A * the row count is {@code 10}, and this method is invoked with
0N/A * {@code -1, 20}, then the specified indices bounds the viewable
0N/A * range, and this is treated as if invoked with {@code 0, 9}. On
0N/A * the other hand, if this were invoked with {@code -10, -1}, then
0N/A * the specified indices do not bound the viewable set of rows,
0N/A * and the selection is unchanged.
0N/A * <p>
0N/A * The parameters are not order dependent. That is, {@code
0N/A * addSelectionInterval(x, y)} is equivalent to
0N/A * {@code addSelectionInterval(y, x)}.
0N/A *
0N/A * @param index0 the first index in the range to add to the selection
0N/A * @param index1 the last index in the range to add to the selection
0N/A */
0N/A public void addSelectionInterval(int index0, int index1) {
0N/A TreePath[] paths = getPathBetweenRows(index0, index1);
0N/A
0N/A if (paths != null && paths.length > 0) {
0N/A this.getSelectionModel().addSelectionPaths(paths);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Removes the specified rows (inclusive) from the selection. If
0N/A * the specified indices are within the viewable set of rows, or bound
0N/A * the viewable set of rows, then the specified indices are constrained by
0N/A * the viewable set of rows. If the specified indices are not within the
0N/A * viewable set of rows, or do not bound the viewable set of rows, then
0N/A * the selection is unchanged. For example, if the row count is {@code
0N/A * 10}, and this method is invoked with {@code -1, 20}, then the
0N/A * specified range bounds the viewable range, and this is treated as
0N/A * if invoked with {@code 0, 9}. On the other hand, if this were
0N/A * invoked with {@code -10, -1}, then the specified range does not
0N/A * bound the viewable set of rows, and the selection is unchanged.
0N/A * <p>
0N/A * The parameters are not order dependent. That is, {@code
0N/A * removeSelectionInterval(x, y)} is equivalent to
0N/A * {@code removeSelectionInterval(y, x)}.
0N/A *
0N/A * @param index0 the first row to remove from the selection
0N/A * @param index1 the last row to remove from the selection
0N/A */
0N/A public void removeSelectionInterval(int index0, int index1) {
0N/A TreePath[] paths = getPathBetweenRows(index0, index1);
0N/A
0N/A if (paths != null && paths.length > 0) {
0N/A this.getSelectionModel().removeSelectionPaths(paths);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Removes the node identified by the specified path from the current
0N/A * selection.
0N/A *
0N/A * @param path the <code>TreePath</code> identifying a node
0N/A */
0N/A public void removeSelectionPath(TreePath path) {
0N/A this.getSelectionModel().removeSelectionPath(path);
0N/A }
0N/A
0N/A /**
0N/A * Removes the nodes identified by the specified paths from the
0N/A * current selection.
0N/A *
0N/A * @param paths an array of <code>TreePath</code> objects that
0N/A * specifies the nodes to remove
0N/A */
0N/A public void removeSelectionPaths(TreePath[] paths) {
0N/A this.getSelectionModel().removeSelectionPaths(paths);
0N/A }
0N/A
0N/A /**
0N/A * Removes the row at the index <code>row</code> from the current
0N/A * selection.
0N/A *
0N/A * @param row the row to remove
0N/A */
0N/A public void removeSelectionRow(int row) {
0N/A int[] rows = { row };
0N/A
0N/A removeSelectionRows(rows);
0N/A }
0N/A
0N/A /**
0N/A * Removes the rows that are selected at each of the specified
0N/A * rows.
0N/A *
0N/A * @param rows an array of ints specifying display rows, where 0 is
0N/A * the first row in the display
0N/A */
0N/A public void removeSelectionRows(int[] rows) {
0N/A TreeUI ui = getUI();
0N/A
0N/A if(ui != null && rows != null) {
0N/A int numRows = rows.length;
0N/A TreePath[] paths = new TreePath[numRows];
0N/A
0N/A for(int counter = 0; counter < numRows; counter++)
0N/A paths[counter] = ui.getPathForRow(this, rows[counter]);
0N/A removeSelectionPaths(paths);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Clears the selection.
0N/A */
0N/A public void clearSelection() {
0N/A getSelectionModel().clearSelection();
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the selection is currently empty.
0N/A *
0N/A * @return true if the selection is currently empty
0N/A */
0N/A public boolean isSelectionEmpty() {
0N/A return getSelectionModel().isSelectionEmpty();
0N/A }
0N/A
0N/A /**
0N/A * Adds a listener for <code>TreeExpansion</code> events.
0N/A *
0N/A * @param tel a TreeExpansionListener that will be notified when
0N/A * a tree node is expanded or collapsed (a "negative
0N/A * expansion")
0N/A */
0N/A public void addTreeExpansionListener(TreeExpansionListener tel) {
0N/A if (settingUI) {
0N/A uiTreeExpansionListener = tel;
0N/A }
0N/A listenerList.add(TreeExpansionListener.class, tel);
0N/A }
0N/A
0N/A /**
0N/A * Removes a listener for <code>TreeExpansion</code> events.
0N/A *
0N/A * @param tel the <code>TreeExpansionListener</code> to remove
0N/A */
0N/A public void removeTreeExpansionListener(TreeExpansionListener tel) {
0N/A listenerList.remove(TreeExpansionListener.class, tel);
0N/A if (uiTreeExpansionListener == tel) {
0N/A uiTreeExpansionListener = null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the <code>TreeExpansionListener</code>s added
0N/A * to this JTree with addTreeExpansionListener().
0N/A *
0N/A * @return all of the <code>TreeExpansionListener</code>s added or an empty
0N/A * array if no listeners have been added
0N/A * @since 1.4
0N/A */
0N/A public TreeExpansionListener[] getTreeExpansionListeners() {
625N/A return listenerList.getListeners(TreeExpansionListener.class);
0N/A }
0N/A
0N/A /**
0N/A * Adds a listener for <code>TreeWillExpand</code> events.
0N/A *
0N/A * @param tel a <code>TreeWillExpandListener</code> that will be notified
0N/A * when a tree node will be expanded or collapsed (a "negative
0N/A * expansion")
0N/A */
0N/A public void addTreeWillExpandListener(TreeWillExpandListener tel) {
0N/A listenerList.add(TreeWillExpandListener.class, tel);
0N/A }
0N/A
0N/A /**
0N/A * Removes a listener for <code>TreeWillExpand</code> events.
0N/A *
0N/A * @param tel the <code>TreeWillExpandListener</code> to remove
0N/A */
0N/A public void removeTreeWillExpandListener(TreeWillExpandListener tel) {
0N/A listenerList.remove(TreeWillExpandListener.class, tel);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the <code>TreeWillExpandListener</code>s added
0N/A * to this JTree with addTreeWillExpandListener().
0N/A *
0N/A * @return all of the <code>TreeWillExpandListener</code>s added or an empty
0N/A * array if no listeners have been added
0N/A * @since 1.4
0N/A */
0N/A public TreeWillExpandListener[] getTreeWillExpandListeners() {
625N/A return listenerList.getListeners(TreeWillExpandListener.class);
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that have registered interest for
0N/A * notification on this event type. The event instance
0N/A * is lazily created using the <code>path</code> parameter.
0N/A *
0N/A * @param path the <code>TreePath</code> indicating the node that was
0N/A * expanded
0N/A * @see EventListenerList
0N/A */
0N/A public void fireTreeExpanded(TreePath path) {
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A TreeExpansionEvent e = null;
0N/A if (uiTreeExpansionListener != null) {
0N/A e = new TreeExpansionEvent(this, path);
0N/A uiTreeExpansionListener.treeExpanded(e);
0N/A }
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==TreeExpansionListener.class &&
0N/A listeners[i + 1] != uiTreeExpansionListener) {
0N/A // Lazily create the event:
0N/A if (e == null)
0N/A e = new TreeExpansionEvent(this, path);
0N/A ((TreeExpansionListener)listeners[i+1]).
0N/A treeExpanded(e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that have registered interest for
0N/A * notification on this event type. The event instance
0N/A * is lazily created using the <code>path</code> parameter.
0N/A *
0N/A * @param path the <code>TreePath</code> indicating the node that was
0N/A * collapsed
0N/A * @see EventListenerList
0N/A */
0N/A public void fireTreeCollapsed(TreePath path) {
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A TreeExpansionEvent e = null;
0N/A if (uiTreeExpansionListener != null) {
0N/A e = new TreeExpansionEvent(this, path);
0N/A uiTreeExpansionListener.treeCollapsed(e);
0N/A }
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==TreeExpansionListener.class &&
0N/A listeners[i + 1] != uiTreeExpansionListener) {
0N/A // Lazily create the event:
0N/A if (e == null)
0N/A e = new TreeExpansionEvent(this, path);
0N/A ((TreeExpansionListener)listeners[i+1]).
0N/A treeCollapsed(e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that have registered interest for
0N/A * notification on this event type. The event instance
0N/A * is lazily created using the <code>path</code> parameter.
0N/A *
0N/A * @param path the <code>TreePath</code> indicating the node that was
0N/A * expanded
0N/A * @see EventListenerList
0N/A */
0N/A public void fireTreeWillExpand(TreePath path) throws ExpandVetoException {
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A TreeExpansionEvent e = null;
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==TreeWillExpandListener.class) {
0N/A // Lazily create the event:
0N/A if (e == null)
0N/A e = new TreeExpansionEvent(this, path);
0N/A ((TreeWillExpandListener)listeners[i+1]).
0N/A treeWillExpand(e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that have registered interest for
0N/A * notification on this event type. The event instance
0N/A * is lazily created using the <code>path</code> parameter.
0N/A *
0N/A * @param path the <code>TreePath</code> indicating the node that was
0N/A * expanded
0N/A * @see EventListenerList
0N/A */
0N/A public void fireTreeWillCollapse(TreePath path) throws ExpandVetoException {
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A TreeExpansionEvent e = null;
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==TreeWillExpandListener.class) {
0N/A // Lazily create the event:
0N/A if (e == null)
0N/A e = new TreeExpansionEvent(this, path);
0N/A ((TreeWillExpandListener)listeners[i+1]).
0N/A treeWillCollapse(e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Adds a listener for <code>TreeSelection</code> events.
0N/A *
0N/A * @param tsl the <code>TreeSelectionListener</code> that will be notified
0N/A * when a node is selected or deselected (a "negative
0N/A * selection")
0N/A */
0N/A public void addTreeSelectionListener(TreeSelectionListener tsl) {
0N/A listenerList.add(TreeSelectionListener.class,tsl);
0N/A if(listenerList.getListenerCount(TreeSelectionListener.class) != 0
0N/A && selectionRedirector == null) {
0N/A selectionRedirector = new TreeSelectionRedirector();
0N/A selectionModel.addTreeSelectionListener(selectionRedirector);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Removes a <code>TreeSelection</code> listener.
0N/A *
0N/A * @param tsl the <code>TreeSelectionListener</code> to remove
0N/A */
0N/A public void removeTreeSelectionListener(TreeSelectionListener tsl) {
0N/A listenerList.remove(TreeSelectionListener.class,tsl);
0N/A if(listenerList.getListenerCount(TreeSelectionListener.class) == 0
0N/A && selectionRedirector != null) {
0N/A selectionModel.removeTreeSelectionListener
0N/A (selectionRedirector);
0N/A selectionRedirector = null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the <code>TreeSelectionListener</code>s added
0N/A * to this JTree with addTreeSelectionListener().
0N/A *
0N/A * @return all of the <code>TreeSelectionListener</code>s added or an empty
0N/A * array if no listeners have been added
0N/A * @since 1.4
0N/A */
0N/A public TreeSelectionListener[] getTreeSelectionListeners() {
625N/A return listenerList.getListeners(TreeSelectionListener.class);
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that have registered interest for
0N/A * notification on this event type.
0N/A *
0N/A * @param e the <code>TreeSelectionEvent</code> to be fired;
0N/A * generated by the
0N/A * <code>TreeSelectionModel</code>
0N/A * when a node is selected or deselected
0N/A * @see EventListenerList
0N/A */
0N/A protected void fireValueChanged(TreeSelectionEvent e) {
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A // TreeSelectionEvent e = null;
0N/A if (listeners[i]==TreeSelectionListener.class) {
0N/A // Lazily create the event:
0N/A // if (e == null)
0N/A // e = new ListSelectionEvent(this, firstIndex, lastIndex);
0N/A ((TreeSelectionListener)listeners[i+1]).valueChanged(e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sent when the tree has changed enough that we need to resize
0N/A * the bounds, but not enough that we need to remove the
0N/A * expanded node set (e.g nodes were expanded or collapsed, or
0N/A * nodes were inserted into the tree). You should never have to
0N/A * invoke this, the UI will invoke this as it needs to.
0N/A */
0N/A public void treeDidChange() {
0N/A revalidate();
0N/A repaint();
0N/A }
0N/A
0N/A /**
0N/A * Sets the number of rows that are to be displayed.
0N/A * This will only work if the tree is contained in a
0N/A * <code>JScrollPane</code>,
0N/A * and will adjust the preferred size and size of that scrollpane.
0N/A * <p>
0N/A * This is a bound property.
0N/A *
0N/A * @param newCount the number of rows to display
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: The number of rows that are to be displayed.
0N/A */
0N/A public void setVisibleRowCount(int newCount) {
0N/A int oldCount = visibleRowCount;
0N/A
0N/A visibleRowCount = newCount;
0N/A firePropertyChange(VISIBLE_ROW_COUNT_PROPERTY, oldCount,
0N/A visibleRowCount);
0N/A invalidate();
0N/A if (accessibleContext != null) {
0N/A ((AccessibleJTree)accessibleContext).fireVisibleDataPropertyChange();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of rows that are displayed in the display area.
0N/A *
0N/A * @return the number of rows displayed
0N/A */
0N/A public int getVisibleRowCount() {
0N/A return visibleRowCount;
0N/A }
0N/A
0N/A /**
0N/A * Expands the root path, assuming the current TreeModel has been set.
0N/A */
0N/A private void expandRoot() {
0N/A TreeModel model = getModel();
0N/A
0N/A if(model != null && model.getRoot() != null) {
0N/A expandPath(new TreePath(model.getRoot()));
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the TreePath to the next tree element that
0N/A * begins with a prefix. To handle the conversion of a
0N/A * <code>TreePath</code> into a String, <code>convertValueToText</code>
0N/A * is used.
0N/A *
0N/A * @param prefix the string to test for a match
0N/A * @param startingRow the row for starting the search
0N/A * @param bias the search direction, either
0N/A * Position.Bias.Forward or Position.Bias.Backward.
0N/A * @return the TreePath of the next tree element that
0N/A * starts with the prefix; otherwise null
0N/A * @exception IllegalArgumentException if prefix is null
0N/A * or startingRow is out of bounds
0N/A * @since 1.4
0N/A */
0N/A public TreePath getNextMatch(String prefix, int startingRow,
0N/A Position.Bias bias) {
0N/A
0N/A int max = getRowCount();
0N/A if (prefix == null) {
0N/A throw new IllegalArgumentException();
0N/A }
0N/A if (startingRow < 0 || startingRow >= max) {
0N/A throw new IllegalArgumentException();
0N/A }
0N/A prefix = prefix.toUpperCase();
0N/A
0N/A // start search from the next/previous element froom the
0N/A // selected element
0N/A int increment = (bias == Position.Bias.Forward) ? 1 : -1;
0N/A int row = startingRow;
0N/A do {
0N/A TreePath path = getPathForRow(row);
0N/A String text = convertValueToText(
0N/A path.getLastPathComponent(), isRowSelected(row),
0N/A isExpanded(row), true, row, false);
0N/A
0N/A if (text.toUpperCase().startsWith(prefix)) {
0N/A return path;
0N/A }
0N/A row = (row + increment + max) % max;
0N/A } while (row != startingRow);
0N/A return null;
0N/A }
0N/A
0N/A // Serialization support.
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
625N/A Vector<Object> values = new Vector<Object>();
0N/A
0N/A s.defaultWriteObject();
0N/A // Save the cellRenderer, if its Serializable.
0N/A if(cellRenderer != null && cellRenderer instanceof Serializable) {
0N/A values.addElement("cellRenderer");
0N/A values.addElement(cellRenderer);
0N/A }
0N/A // Save the cellEditor, if its Serializable.
0N/A if(cellEditor != null && cellEditor instanceof Serializable) {
0N/A values.addElement("cellEditor");
0N/A values.addElement(cellEditor);
0N/A }
0N/A // Save the treeModel, if its Serializable.
0N/A if(treeModel != null && treeModel instanceof Serializable) {
0N/A values.addElement("treeModel");
0N/A values.addElement(treeModel);
0N/A }
0N/A // Save the selectionModel, if its Serializable.
0N/A if(selectionModel != null && selectionModel instanceof Serializable) {
0N/A values.addElement("selectionModel");
0N/A values.addElement(selectionModel);
0N/A }
0N/A
0N/A Object expandedData = getArchivableExpandedState();
0N/A
0N/A if(expandedData != null) {
0N/A values.addElement("expandedState");
0N/A values.addElement(expandedData);
0N/A }
0N/A
0N/A s.writeObject(values);
0N/A if (getUIClassID().equals(uiClassID)) {
0N/A byte count = JComponent.getWriteObjCounter(this);
0N/A JComponent.setWriteObjCounter(this, --count);
0N/A if (count == 0 && ui != null) {
0N/A ui.installUI(this);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void readObject(ObjectInputStream s)
0N/A throws IOException, ClassNotFoundException {
0N/A s.defaultReadObject();
0N/A
0N/A // Create an instance of expanded state.
0N/A
625N/A expandedState = new Hashtable<TreePath, Boolean>();
625N/A
625N/A expandedStack = new Stack<Stack<TreePath>>();
0N/A
0N/A Vector values = (Vector)s.readObject();
0N/A int indexCounter = 0;
0N/A int maxCounter = values.size();
0N/A
0N/A if(indexCounter < maxCounter && values.elementAt(indexCounter).
0N/A equals("cellRenderer")) {
0N/A cellRenderer = (TreeCellRenderer)values.elementAt(++indexCounter);
0N/A indexCounter++;
0N/A }
0N/A if(indexCounter < maxCounter && values.elementAt(indexCounter).
0N/A equals("cellEditor")) {
0N/A cellEditor = (TreeCellEditor)values.elementAt(++indexCounter);
0N/A indexCounter++;
0N/A }
0N/A if(indexCounter < maxCounter && values.elementAt(indexCounter).
0N/A equals("treeModel")) {
0N/A treeModel = (TreeModel)values.elementAt(++indexCounter);
0N/A indexCounter++;
0N/A }
0N/A if(indexCounter < maxCounter && values.elementAt(indexCounter).
0N/A equals("selectionModel")) {
0N/A selectionModel = (TreeSelectionModel)values.elementAt(++indexCounter);
0N/A indexCounter++;
0N/A }
0N/A if(indexCounter < maxCounter && values.elementAt(indexCounter).
0N/A equals("expandedState")) {
0N/A unarchiveExpandedState(values.elementAt(++indexCounter));
0N/A indexCounter++;
0N/A }
0N/A // Reinstall the redirector.
0N/A if(listenerList.getListenerCount(TreeSelectionListener.class) != 0) {
0N/A selectionRedirector = new TreeSelectionRedirector();
0N/A selectionModel.addTreeSelectionListener(selectionRedirector);
0N/A }
0N/A // Listener to TreeModel.
0N/A if(treeModel != null) {
0N/A treeModelListener = createTreeModelListener();
0N/A if(treeModelListener != null)
0N/A treeModel.addTreeModelListener(treeModelListener);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns an object that can be archived indicating what nodes are
0N/A * expanded and what aren't. The objects from the model are NOT
0N/A * written out.
0N/A */
0N/A private Object getArchivableExpandedState() {
0N/A TreeModel model = getModel();
0N/A
0N/A if(model != null) {
625N/A Enumeration<TreePath> paths = expandedState.keys();
0N/A
0N/A if(paths != null) {
625N/A Vector<Object> state = new Vector<Object>();
0N/A
0N/A while(paths.hasMoreElements()) {
625N/A TreePath path = paths.nextElement();
0N/A Object archivePath;
0N/A
0N/A try {
0N/A archivePath = getModelIndexsForPath(path);
0N/A } catch (Error error) {
0N/A archivePath = null;
0N/A }
0N/A if(archivePath != null) {
0N/A state.addElement(archivePath);
0N/A state.addElement(expandedState.get(path));
0N/A }
0N/A }
0N/A return state;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Updates the expanded state of nodes in the tree based on the
0N/A * previously archived state <code>state</code>.
0N/A */
0N/A private void unarchiveExpandedState(Object state) {
0N/A if(state instanceof Vector) {
0N/A Vector paths = (Vector)state;
0N/A
0N/A for(int counter = paths.size() - 1; counter >= 0; counter--) {
0N/A Boolean eState = (Boolean)paths.elementAt(counter--);
0N/A TreePath path;
0N/A
0N/A try {
0N/A path = getPathForIndexs((int[])paths.elementAt(counter));
0N/A if(path != null)
0N/A expandedState.put(path, eState);
0N/A } catch (Error error) {}
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of integers specifying the indexs of the
0N/A * components in the <code>path</code>. If <code>path</code> is
0N/A * the root, this will return an empty array. If <code>path</code>
0N/A * is <code>null</code>, <code>null</code> will be returned.
0N/A */
0N/A private int[] getModelIndexsForPath(TreePath path) {
0N/A if(path != null) {
0N/A TreeModel model = getModel();
0N/A int count = path.getPathCount();
0N/A int[] indexs = new int[count - 1];
0N/A Object parent = model.getRoot();
0N/A
0N/A for(int counter = 1; counter < count; counter++) {
0N/A indexs[counter - 1] = model.getIndexOfChild
0N/A (parent, path.getPathComponent(counter));
0N/A parent = path.getPathComponent(counter);
0N/A if(indexs[counter - 1] < 0)
0N/A return null;
0N/A }
0N/A return indexs;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>TreePath</code> created by obtaining the children
0N/A * for each of the indices in <code>indexs</code>. If <code>indexs</code>
0N/A * or the <code>TreeModel</code> is <code>null</code>, it will return
0N/A * <code>null</code>.
0N/A */
0N/A private TreePath getPathForIndexs(int[] indexs) {
0N/A if(indexs == null)
0N/A return null;
0N/A
0N/A TreeModel model = getModel();
0N/A
0N/A if(model == null)
0N/A return null;
0N/A
0N/A int count = indexs.length;
0N/A Object parent = model.getRoot();
0N/A TreePath parentPath = new TreePath(parent);
0N/A
0N/A for(int counter = 0; counter < count; counter++) {
0N/A parent = model.getChild(parent, indexs[counter]);
0N/A if(parent == null)
0N/A return null;
0N/A parentPath = parentPath.pathByAddingChild(parent);
0N/A }
0N/A return parentPath;
0N/A }
0N/A
0N/A /**
0N/A * <code>EmptySelectionModel</code> is a <code>TreeSelectionModel</code>
0N/A * that does not allow anything to be selected.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A */
0N/A protected static class EmptySelectionModel extends
0N/A DefaultTreeSelectionModel
0N/A {
0N/A /**
0N/A * The single instance of {@code EmptySelectionModel}.
0N/A */
0N/A protected static final EmptySelectionModel sharedInstance =
0N/A new EmptySelectionModel();
0N/A
0N/A /**
0N/A * Returns the single instance of {@code EmptySelectionModel}.
0N/A *
0N/A * @return single instance of {@code EmptySelectionModel}
0N/A */
0N/A static public EmptySelectionModel sharedInstance() {
0N/A return sharedInstance;
0N/A }
0N/A
0N/A /**
0N/A * This is overriden to do nothing; {@code EmptySelectionModel}
0N/A * does not allow a selection.
0N/A *
0N/A * @param paths the paths to select; this is ignored
0N/A */
0N/A public void setSelectionPaths(TreePath[] paths) {}
0N/A
0N/A /**
0N/A * This is overriden to do nothing; {@code EmptySelectionModel}
0N/A * does not allow a selection.
0N/A *
0N/A * @param paths the paths to add to the selection; this is ignored
0N/A */
0N/A public void addSelectionPaths(TreePath[] paths) {}
0N/A
0N/A /**
0N/A * This is overriden to do nothing; {@code EmptySelectionModel}
0N/A * does not allow a selection.
0N/A *
0N/A * @param paths the paths to remove; this is ignored
0N/A */
0N/A public void removeSelectionPaths(TreePath[] paths) {}
0N/A
0N/A /**
0N/A * This is overriden to do nothing; {@code EmptySelectionModel}
0N/A * does not allow a selection.
0N/A *
0N/A * @param mode the selection mode; this is ignored
0N/A * @since 1.7
0N/A */
0N/A public void setSelectionMode(int mode) {
0N/A }
0N/A
0N/A /**
0N/A * This is overriden to do nothing; {@code EmptySelectionModel}
0N/A * does not allow a selection.
0N/A *
0N/A * @param mapper the {@code RowMapper} instance; this is ignored
0N/A * @since 1.7
0N/A */
0N/A public void setRowMapper(RowMapper mapper) {
0N/A }
0N/A
0N/A /**
0N/A * This is overriden to do nothing; {@code EmptySelectionModel}
0N/A * does not allow a selection.
0N/A *
0N/A * @param listener the listener to add; this is ignored
0N/A * @since 1.7
0N/A */
0N/A public void addTreeSelectionListener(TreeSelectionListener listener) {
0N/A }
0N/A
0N/A /**
0N/A * This is overriden to do nothing; {@code EmptySelectionModel}
0N/A * does not allow a selection.
0N/A *
0N/A * @param listener the listener to remove; this is ignored
0N/A * @since 1.7
0N/A */
0N/A public void removeTreeSelectionListener(
0N/A TreeSelectionListener listener) {
0N/A }
0N/A
0N/A /**
0N/A * This is overriden to do nothing; {@code EmptySelectionModel}
0N/A * does not allow a selection.
0N/A *
0N/A * @param listener the listener to add; this is ignored
0N/A * @since 1.7
0N/A */
0N/A public void addPropertyChangeListener(
0N/A PropertyChangeListener listener) {
0N/A }
0N/A
0N/A /**
0N/A * This is overriden to do nothing; {@code EmptySelectionModel}
0N/A * does not allow a selection.
0N/A *
0N/A * @param listener the listener to remove; this is ignored
0N/A * @since 1.7
0N/A */
0N/A public void removePropertyChangeListener(
0N/A PropertyChangeListener listener) {
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Handles creating a new <code>TreeSelectionEvent</code> with the
0N/A * <code>JTree</code> as the
0N/A * source and passing it off to all the listeners.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A */
0N/A protected class TreeSelectionRedirector implements Serializable,
0N/A TreeSelectionListener
0N/A {
0N/A /**
0N/A * Invoked by the <code>TreeSelectionModel</code> when the
0N/A * selection changes.
0N/A *
0N/A * @param e the <code>TreeSelectionEvent</code> generated by the
0N/A * <code>TreeSelectionModel</code>
0N/A */
0N/A public void valueChanged(TreeSelectionEvent e) {
0N/A TreeSelectionEvent newE;
0N/A
0N/A newE = (TreeSelectionEvent)e.cloneWithSource(JTree.this);
0N/A fireValueChanged(newE);
0N/A }
0N/A } // End of class JTree.TreeSelectionRedirector
0N/A
0N/A //
0N/A // Scrollable interface
0N/A //
0N/A
0N/A /**
0N/A * Returns the preferred display size of a <code>JTree</code>. The height is
0N/A * determined from <code>getVisibleRowCount</code> and the width
0N/A * is the current preferred width.
0N/A *
0N/A * @return a <code>Dimension</code> object containing the preferred size
0N/A */
0N/A public Dimension getPreferredScrollableViewportSize() {
0N/A int width = getPreferredSize().width;
0N/A int visRows = getVisibleRowCount();
0N/A int height = -1;
0N/A
0N/A if(isFixedRowHeight())
0N/A height = visRows * getRowHeight();
0N/A else {
0N/A TreeUI ui = getUI();
0N/A
0N/A if (ui != null && visRows > 0) {
0N/A int rc = ui.getRowCount(this);
0N/A
0N/A if (rc >= visRows) {
0N/A Rectangle bounds = getRowBounds(visRows - 1);
0N/A if (bounds != null) {
0N/A height = bounds.y + bounds.height;
0N/A }
0N/A }
0N/A else if (rc > 0) {
0N/A Rectangle bounds = getRowBounds(0);
0N/A if (bounds != null) {
0N/A height = bounds.height * visRows;
0N/A }
0N/A }
0N/A }
0N/A if (height == -1) {
0N/A height = 16 * visRows;
0N/A }
0N/A }
0N/A return new Dimension(width, height);
0N/A }
0N/A
0N/A /**
0N/A * Returns the amount to increment when scrolling. The amount is
0N/A * the height of the first displayed row that isn't completely in view
0N/A * or, if it is totally displayed, the height of the next row in the
0N/A * scrolling direction.
0N/A *
0N/A * @param visibleRect the view area visible within the viewport
0N/A * @param orientation either <code>SwingConstants.VERTICAL</code>
0N/A * or <code>SwingConstants.HORIZONTAL</code>
0N/A * @param direction less than zero to scroll up/left,
0N/A * greater than zero for down/right
0N/A * @return the "unit" increment for scrolling in the specified direction
0N/A * @see JScrollBar#setUnitIncrement(int)
0N/A */
0N/A public int getScrollableUnitIncrement(Rectangle visibleRect,
0N/A int orientation, int direction) {
0N/A if(orientation == SwingConstants.VERTICAL) {
0N/A Rectangle rowBounds;
0N/A int firstIndex = getClosestRowForLocation
0N/A (0, visibleRect.y);
0N/A
0N/A if(firstIndex != -1) {
0N/A rowBounds = getRowBounds(firstIndex);
0N/A if(rowBounds.y != visibleRect.y) {
0N/A if(direction < 0) {
0N/A // UP
0N/A return Math.max(0, (visibleRect.y - rowBounds.y));
0N/A }
0N/A return (rowBounds.y + rowBounds.height - visibleRect.y);
0N/A }
0N/A if(direction < 0) { // UP
0N/A if(firstIndex != 0) {
0N/A rowBounds = getRowBounds(firstIndex - 1);
0N/A return rowBounds.height;
0N/A }
0N/A }
0N/A else {
0N/A return rowBounds.height;
0N/A }
0N/A }
0N/A return 0;
0N/A }
0N/A return 4;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the amount for a block increment, which is the height or
0N/A * width of <code>visibleRect</code>, based on <code>orientation</code>.
0N/A *
0N/A * @param visibleRect the view area visible within the viewport
0N/A * @param orientation either <code>SwingConstants.VERTICAL</code>
0N/A * or <code>SwingConstants.HORIZONTAL</code>
0N/A * @param direction less than zero to scroll up/left,
0N/A * greater than zero for down/right.
0N/A * @return the "block" increment for scrolling in the specified direction
0N/A * @see JScrollBar#setBlockIncrement(int)
0N/A */
0N/A public int getScrollableBlockIncrement(Rectangle visibleRect,
0N/A int orientation, int direction) {
0N/A return (orientation == SwingConstants.VERTICAL) ? visibleRect.height :
0N/A visibleRect.width;
0N/A }
0N/A
0N/A /**
0N/A * Returns false to indicate that the width of the viewport does not
0N/A * determine the width of the table, unless the preferred width of
0N/A * the tree is smaller than the viewports width. In other words:
0N/A * ensure that the tree is never smaller than its viewport.
0N/A *
0N/A * @return whether the tree should track the width of the viewport
0N/A * @see Scrollable#getScrollableTracksViewportWidth
0N/A */
0N/A public boolean getScrollableTracksViewportWidth() {
2333N/A Container parent = SwingUtilities.getUnwrappedParent(this);
2333N/A if (parent instanceof JViewport) {
2333N/A return parent.getWidth() > getPreferredSize().width;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns false to indicate that the height of the viewport does not
0N/A * determine the height of the table, unless the preferred height
0N/A * of the tree is smaller than the viewports height. In other words:
0N/A * ensure that the tree is never smaller than its viewport.
0N/A *
0N/A * @return whether the tree should track the height of the viewport
0N/A * @see Scrollable#getScrollableTracksViewportHeight
0N/A */
0N/A public boolean getScrollableTracksViewportHeight() {
2333N/A Container parent = SwingUtilities.getUnwrappedParent(this);
2333N/A if (parent instanceof JViewport) {
2333N/A return parent.getHeight() > getPreferredSize().height;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Sets the expanded state of this <code>JTree</code>.
0N/A * If <code>state</code> is
0N/A * true, all parents of <code>path</code> and path are marked as
0N/A * expanded. If <code>state</code> is false, all parents of
0N/A * <code>path</code> are marked EXPANDED, but <code>path</code> itself
0N/A * is marked collapsed.<p>
0N/A * This will fail if a <code>TreeWillExpandListener</code> vetos it.
0N/A */
0N/A protected void setExpandedState(TreePath path, boolean state) {
0N/A if(path != null) {
0N/A // Make sure all parents of path are expanded.
625N/A Stack<TreePath> stack;
625N/A TreePath parentPath = path.getParentPath();
0N/A
0N/A if (expandedStack.size() == 0) {
625N/A stack = new Stack<TreePath>();
0N/A }
0N/A else {
625N/A stack = expandedStack.pop();
0N/A }
0N/A
0N/A try {
0N/A while(parentPath != null) {
0N/A if(isExpanded(parentPath)) {
0N/A parentPath = null;
0N/A }
0N/A else {
0N/A stack.push(parentPath);
0N/A parentPath = parentPath.getParentPath();
0N/A }
0N/A }
0N/A for(int counter = stack.size() - 1; counter >= 0; counter--) {
625N/A parentPath = stack.pop();
0N/A if(!isExpanded(parentPath)) {
0N/A try {
0N/A fireTreeWillExpand(parentPath);
0N/A } catch (ExpandVetoException eve) {
0N/A // Expand vetoed!
0N/A return;
0N/A }
0N/A expandedState.put(parentPath, Boolean.TRUE);
0N/A fireTreeExpanded(parentPath);
0N/A if (accessibleContext != null) {
0N/A ((AccessibleJTree)accessibleContext).
0N/A fireVisibleDataPropertyChange();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A finally {
0N/A if (expandedStack.size() < TEMP_STACK_SIZE) {
0N/A stack.removeAllElements();
0N/A expandedStack.push(stack);
0N/A }
0N/A }
0N/A if(!state) {
0N/A // collapse last path.
0N/A Object cValue = expandedState.get(path);
0N/A
0N/A if(cValue != null && ((Boolean)cValue).booleanValue()) {
0N/A try {
0N/A fireTreeWillCollapse(path);
0N/A }
0N/A catch (ExpandVetoException eve) {
0N/A return;
0N/A }
0N/A expandedState.put(path, Boolean.FALSE);
0N/A fireTreeCollapsed(path);
0N/A if (removeDescendantSelectedPaths(path, false) &&
0N/A !isPathSelected(path)) {
0N/A // A descendant was selected, select the parent.
0N/A addSelectionPath(path);
0N/A }
0N/A if (accessibleContext != null) {
0N/A ((AccessibleJTree)accessibleContext).
0N/A fireVisibleDataPropertyChange();
0N/A }
0N/A }
0N/A }
0N/A else {
0N/A // Expand last path.
0N/A Object cValue = expandedState.get(path);
0N/A
0N/A if(cValue == null || !((Boolean)cValue).booleanValue()) {
0N/A try {
0N/A fireTreeWillExpand(path);
0N/A }
0N/A catch (ExpandVetoException eve) {
0N/A return;
0N/A }
0N/A expandedState.put(path, Boolean.TRUE);
0N/A fireTreeExpanded(path);
0N/A if (accessibleContext != null) {
0N/A ((AccessibleJTree)accessibleContext).
0N/A fireVisibleDataPropertyChange();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns an <code>Enumeration</code> of <code>TreePaths</code>
0N/A * that have been expanded that
0N/A * are descendants of <code>parent</code>.
0N/A */
0N/A protected Enumeration<TreePath>
0N/A getDescendantToggledPaths(TreePath parent)
0N/A {
0N/A if(parent == null)
0N/A return null;
0N/A
625N/A Vector<TreePath> descendants = new Vector<TreePath>();
625N/A Enumeration<TreePath> nodes = expandedState.keys();
0N/A
0N/A while(nodes.hasMoreElements()) {
625N/A TreePath path = nodes.nextElement();
0N/A if(parent.isDescendant(path))
0N/A descendants.addElement(path);
0N/A }
0N/A return descendants.elements();
0N/A }
0N/A
0N/A /**
0N/A * Removes any descendants of the <code>TreePaths</code> in
0N/A * <code>toRemove</code>
0N/A * that have been expanded.
0N/A *
0N/A * @param toRemove an enumeration of the paths to remove; a value of
0N/A * {@code null} is ignored
0N/A * @throws ClassCastException if {@code toRemove} contains an
0N/A * element that is not a {@code TreePath}; {@code null}
0N/A * values are ignored
0N/A */
0N/A protected void
0N/A removeDescendantToggledPaths(Enumeration<TreePath> toRemove)
0N/A {
0N/A if(toRemove != null) {
0N/A while(toRemove.hasMoreElements()) {
625N/A Enumeration descendants = getDescendantToggledPaths
625N/A (toRemove.nextElement());
0N/A
0N/A if(descendants != null) {
0N/A while(descendants.hasMoreElements()) {
0N/A expandedState.remove(descendants.nextElement());
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Clears the cache of toggled tree paths. This does NOT send out
0N/A * any <code>TreeExpansionListener</code> events.
0N/A */
0N/A protected void clearToggledPaths() {
0N/A expandedState.clear();
0N/A }
0N/A
0N/A /**
0N/A * Creates and returns an instance of <code>TreeModelHandler</code>.
0N/A * The returned
0N/A * object is responsible for updating the expanded state when the
0N/A * <code>TreeModel</code> changes.
0N/A * <p>
0N/A * For more information on what expanded state means, see the
0N/A * <a href=#jtree_description>JTree description</a> above.
0N/A */
0N/A protected TreeModelListener createTreeModelListener() {
0N/A return new TreeModelHandler();
0N/A }
0N/A
0N/A /**
0N/A * Removes any paths in the selection that are descendants of
0N/A * <code>path</code>. If <code>includePath</code> is true and
0N/A * <code>path</code> is selected, it will be removed from the selection.
0N/A *
0N/A * @return true if a descendant was selected
0N/A * @since 1.3
0N/A */
0N/A protected boolean removeDescendantSelectedPaths(TreePath path,
0N/A boolean includePath) {
0N/A TreePath[] toRemove = getDescendantSelectedPaths(path, includePath);
0N/A
0N/A if (toRemove != null) {
0N/A getSelectionModel().removeSelectionPaths(toRemove);
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of paths in the selection that are descendants of
0N/A * <code>path</code>. The returned array may contain <code>null</code>s.
0N/A */
0N/A private TreePath[] getDescendantSelectedPaths(TreePath path,
0N/A boolean includePath) {
0N/A TreeSelectionModel sm = getSelectionModel();
0N/A TreePath[] selPaths = (sm != null) ? sm.getSelectionPaths() :
0N/A null;
0N/A
0N/A if(selPaths != null) {
0N/A boolean shouldRemove = false;
0N/A
0N/A for(int counter = selPaths.length - 1; counter >= 0; counter--) {
0N/A if(selPaths[counter] != null &&
0N/A path.isDescendant(selPaths[counter]) &&
0N/A (!path.equals(selPaths[counter]) || includePath))
0N/A shouldRemove = true;
0N/A else
0N/A selPaths[counter] = null;
0N/A }
0N/A if(!shouldRemove) {
0N/A selPaths = null;
0N/A }
0N/A return selPaths;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Removes any paths from the selection model that are descendants of
0N/A * the nodes identified by in <code>e</code>.
0N/A */
0N/A void removeDescendantSelectedPaths(TreeModelEvent e) {
0N/A TreePath pPath = e.getTreePath();
0N/A Object[] oldChildren = e.getChildren();
0N/A TreeSelectionModel sm = getSelectionModel();
0N/A
0N/A if (sm != null && pPath != null && oldChildren != null &&
0N/A oldChildren.length > 0) {
0N/A for (int counter = oldChildren.length - 1; counter >= 0;
0N/A counter--) {
0N/A // Might be better to call getDescendantSelectedPaths
0N/A // numerous times, then push to the model.
0N/A removeDescendantSelectedPaths(pPath.pathByAddingChild
0N/A (oldChildren[counter]), true);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Listens to the model and updates the <code>expandedState</code>
0N/A * accordingly when nodes are removed, or changed.
0N/A */
0N/A protected class TreeModelHandler implements TreeModelListener {
0N/A public void treeNodesChanged(TreeModelEvent e) { }
0N/A
0N/A public void treeNodesInserted(TreeModelEvent e) { }
0N/A
0N/A public void treeStructureChanged(TreeModelEvent e) {
0N/A if(e == null)
0N/A return;
0N/A
0N/A // NOTE: If I change this to NOT remove the descendants
0N/A // and update BasicTreeUIs treeStructureChanged method
0N/A // to update descendants in response to a treeStructureChanged
0N/A // event, all the children of the event won't collapse!
0N/A TreePath parent = e.getTreePath();
0N/A
0N/A if(parent == null)
0N/A return;
0N/A
0N/A if (parent.getPathCount() == 1) {
0N/A // New root, remove everything!
0N/A clearToggledPaths();
0N/A if(treeModel.getRoot() != null &&
0N/A !treeModel.isLeaf(treeModel.getRoot())) {
0N/A // Mark the root as expanded, if it isn't a leaf.
0N/A expandedState.put(parent, Boolean.TRUE);
0N/A }
0N/A }
0N/A else if(expandedState.get(parent) != null) {
0N/A Vector<TreePath> toRemove = new Vector<TreePath>(1);
0N/A boolean isExpanded = isExpanded(parent);
0N/A
0N/A toRemove.addElement(parent);
0N/A removeDescendantToggledPaths(toRemove.elements());
0N/A if(isExpanded) {
0N/A TreeModel model = getModel();
0N/A
0N/A if(model == null || model.isLeaf
0N/A (parent.getLastPathComponent()))
0N/A collapsePath(parent);
0N/A else
0N/A expandedState.put(parent, Boolean.TRUE);
0N/A }
0N/A }
0N/A removeDescendantSelectedPaths(parent, false);
0N/A }
0N/A
0N/A public void treeNodesRemoved(TreeModelEvent e) {
0N/A if(e == null)
0N/A return;
0N/A
0N/A TreePath parent = e.getTreePath();
0N/A Object[] children = e.getChildren();
0N/A
0N/A if(children == null)
0N/A return;
0N/A
0N/A TreePath rPath;
0N/A Vector<TreePath> toRemove
0N/A = new Vector<TreePath>(Math.max(1, children.length));
0N/A
0N/A for(int counter = children.length - 1; counter >= 0; counter--) {
0N/A rPath = parent.pathByAddingChild(children[counter]);
0N/A if(expandedState.get(rPath) != null)
0N/A toRemove.addElement(rPath);
0N/A }
0N/A if(toRemove.size() > 0)
0N/A removeDescendantToggledPaths(toRemove.elements());
0N/A
0N/A TreeModel model = getModel();
0N/A
0N/A if(model == null || model.isLeaf(parent.getLastPathComponent()))
0N/A expandedState.remove(parent);
0N/A
0N/A removeDescendantSelectedPaths(e);
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * <code>DynamicUtilTreeNode</code> can wrap
0N/A * vectors/hashtables/arrays/strings and
0N/A * create the appropriate children tree nodes as necessary. It is
0N/A * dynamic in that it will only create the children as necessary.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A */
0N/A public static class DynamicUtilTreeNode extends DefaultMutableTreeNode {
0N/A /**
0N/A * Does the this <code>JTree</code> have children?
0N/A * This property is currently not implemented.
0N/A */
0N/A protected boolean hasChildren;
0N/A /** Value to create children with. */
0N/A protected Object childValue;
0N/A /** Have the children been loaded yet? */
0N/A protected boolean loadedChildren;
0N/A
0N/A /**
0N/A * Adds to parent all the children in <code>children</code>.
0N/A * If <code>children</code> is an array or vector all of its
0N/A * elements are added is children, otherwise if <code>children</code>
0N/A * is a hashtable all the key/value pairs are added in the order
0N/A * <code>Enumeration</code> returns them.
0N/A */
0N/A public static void createChildren(DefaultMutableTreeNode parent,
0N/A Object children) {
0N/A if(children instanceof Vector) {
0N/A Vector childVector = (Vector)children;
0N/A
0N/A for(int counter = 0, maxCounter = childVector.size();
0N/A counter < maxCounter; counter++)
0N/A parent.add(new DynamicUtilTreeNode
0N/A (childVector.elementAt(counter),
0N/A childVector.elementAt(counter)));
0N/A }
0N/A else if(children instanceof Hashtable) {
0N/A Hashtable childHT = (Hashtable)children;
0N/A Enumeration keys = childHT.keys();
0N/A Object aKey;
0N/A
0N/A while(keys.hasMoreElements()) {
0N/A aKey = keys.nextElement();
0N/A parent.add(new DynamicUtilTreeNode(aKey,
0N/A childHT.get(aKey)));
0N/A }
0N/A }
0N/A else if(children instanceof Object[]) {
0N/A Object[] childArray = (Object[])children;
0N/A
0N/A for(int counter = 0, maxCounter = childArray.length;
0N/A counter < maxCounter; counter++)
0N/A parent.add(new DynamicUtilTreeNode(childArray[counter],
0N/A childArray[counter]));
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates a node with the specified object as its value and
0N/A * with the specified children. For the node to allow children,
0N/A * the children-object must be an array of objects, a
0N/A * <code>Vector</code>, or a <code>Hashtable</code> -- even
0N/A * if empty. Otherwise, the node is not
0N/A * allowed to have children.
0N/A *
0N/A * @param value the <code>Object</code> that is the value for the
0N/A * new node
0N/A * @param children an array of <code>Object</code>s, a
0N/A * <code>Vector</code>, or a <code>Hashtable</code>
0N/A * used to create the child nodes; if any other
0N/A * object is specified, or if the value is
0N/A * <code>null</code>,
0N/A * then the node is not allowed to have children
0N/A */
0N/A public DynamicUtilTreeNode(Object value, Object children) {
0N/A super(value);
0N/A loadedChildren = false;
0N/A childValue = children;
0N/A if(children != null) {
0N/A if(children instanceof Vector)
0N/A setAllowsChildren(true);
0N/A else if(children instanceof Hashtable)
0N/A setAllowsChildren(true);
0N/A else if(children instanceof Object[])
0N/A setAllowsChildren(true);
0N/A else
0N/A setAllowsChildren(false);
0N/A }
0N/A else
0N/A setAllowsChildren(false);
0N/A }
0N/A
0N/A /**
0N/A * Returns true if this node allows children. Whether the node
0N/A * allows children depends on how it was created.
0N/A *
0N/A * @return true if this node allows children, false otherwise
0N/A * @see #JTree.DynamicUtilTreeNode
0N/A */
0N/A public boolean isLeaf() {
0N/A return !getAllowsChildren();
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of child nodes.
0N/A *
0N/A * @return the number of child nodes
0N/A */
0N/A public int getChildCount() {
0N/A if(!loadedChildren)
0N/A loadChildren();
0N/A return super.getChildCount();
0N/A }
0N/A
0N/A /**
0N/A * Loads the children based on <code>childValue</code>.
0N/A * If <code>childValue</code> is a <code>Vector</code>
0N/A * or array each element is added as a child,
0N/A * if <code>childValue</code> is a <code>Hashtable</code>
0N/A * each key/value pair is added in the order that
0N/A * <code>Enumeration</code> returns the keys.
0N/A */
0N/A protected void loadChildren() {
0N/A loadedChildren = true;
0N/A createChildren(this, childValue);
0N/A }
0N/A
0N/A /**
0N/A * Subclassed to load the children, if necessary.
0N/A */
0N/A public TreeNode getChildAt(int index) {
0N/A if(!loadedChildren)
0N/A loadChildren();
0N/A return super.getChildAt(index);
0N/A }
0N/A
0N/A /**
0N/A * Subclassed to load the children, if necessary.
0N/A */
0N/A public Enumeration children() {
0N/A if(!loadedChildren)
0N/A loadChildren();
0N/A return super.children();
0N/A }
0N/A }
0N/A
0N/A void setUIProperty(String propertyName, Object value) {
0N/A if (propertyName == "rowHeight") {
0N/A if (!rowHeightSet) {
0N/A setRowHeight(((Number)value).intValue());
0N/A rowHeightSet = false;
0N/A }
0N/A } else if (propertyName == "scrollsOnExpand") {
0N/A if (!scrollsOnExpandSet) {
0N/A setScrollsOnExpand(((Boolean)value).booleanValue());
0N/A scrollsOnExpandSet = false;
0N/A }
0N/A } else if (propertyName == "showsRootHandles") {
0N/A if (!showsRootHandlesSet) {
0N/A setShowsRootHandles(((Boolean)value).booleanValue());
0N/A showsRootHandlesSet = false;
0N/A }
0N/A } else {
0N/A super.setUIProperty(propertyName, value);
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a string representation of this <code>JTree</code>.
0N/A * This method
0N/A * is intended to be used only for debugging purposes, and the
0N/A * content and format of the returned string may vary between
0N/A * implementations. The returned string may be empty but may not
0N/A * be <code>null</code>.
0N/A *
0N/A * @return a string representation of this <code>JTree</code>.
0N/A */
0N/A protected String paramString() {
0N/A String rootVisibleString = (rootVisible ?
0N/A "true" : "false");
0N/A String showsRootHandlesString = (showsRootHandles ?
0N/A "true" : "false");
0N/A String editableString = (editable ?
0N/A "true" : "false");
0N/A String largeModelString = (largeModel ?
0N/A "true" : "false");
0N/A String invokesStopCellEditingString = (invokesStopCellEditing ?
0N/A "true" : "false");
0N/A String scrollsOnExpandString = (scrollsOnExpand ?
0N/A "true" : "false");
0N/A
0N/A return super.paramString() +
0N/A ",editable=" + editableString +
0N/A ",invokesStopCellEditing=" + invokesStopCellEditingString +
0N/A ",largeModel=" + largeModelString +
0N/A ",rootVisible=" + rootVisibleString +
0N/A ",rowHeight=" + rowHeight +
0N/A ",scrollsOnExpand=" + scrollsOnExpandString +
0N/A ",showsRootHandles=" + showsRootHandlesString +
0N/A ",toggleClickCount=" + toggleClickCount +
0N/A ",visibleRowCount=" + visibleRowCount;
0N/A }
0N/A
0N/A/////////////////
0N/A// Accessibility support
0N/A////////////////
0N/A
0N/A /**
0N/A * Gets the AccessibleContext associated with this JTree.
0N/A * For JTrees, the AccessibleContext takes the form of an
0N/A * AccessibleJTree.
0N/A * A new AccessibleJTree instance is created if necessary.
0N/A *
0N/A * @return an AccessibleJTree that serves as the
0N/A * AccessibleContext of this JTree
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (accessibleContext == null) {
0N/A accessibleContext = new AccessibleJTree();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A /**
0N/A * This class implements accessibility support for the
0N/A * <code>JTree</code> class. It provides an implementation of the
0N/A * Java Accessibility API appropriate to tree user-interface elements.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A */
0N/A protected class AccessibleJTree extends AccessibleJComponent
0N/A implements AccessibleSelection, TreeSelectionListener,
0N/A TreeModelListener, TreeExpansionListener {
0N/A
0N/A TreePath leadSelectionPath;
0N/A Accessible leadSelectionAccessible;
0N/A
0N/A public AccessibleJTree() {
0N/A // Add a tree model listener for JTree
0N/A TreeModel model = JTree.this.getModel();
0N/A if (model != null) {
0N/A model.addTreeModelListener(this);
0N/A }
0N/A JTree.this.addTreeExpansionListener(this);
0N/A JTree.this.addTreeSelectionListener(this);
0N/A leadSelectionPath = JTree.this.getLeadSelectionPath();
0N/A leadSelectionAccessible = (leadSelectionPath != null)
0N/A ? new AccessibleJTreeNode(JTree.this,
0N/A leadSelectionPath,
0N/A JTree.this)
0N/A : null;
0N/A }
0N/A
0N/A /**
0N/A * Tree Selection Listener value change method. Used to fire the
0N/A * property change
0N/A *
0N/A * @param e ListSelectionEvent
0N/A *
0N/A */
0N/A public void valueChanged(TreeSelectionEvent e) {
0N/A // Fixes 4546503 - JTree is sending incorrect active
0N/A // descendant events
0N/A TreePath oldLeadSelectionPath = e.getOldLeadSelectionPath();
0N/A leadSelectionPath = e.getNewLeadSelectionPath();
0N/A
0N/A if (oldLeadSelectionPath != leadSelectionPath) {
0N/A // Set parent to null so AccessibleJTreeNode computes
0N/A // its parent.
0N/A Accessible oldLSA = leadSelectionAccessible;
0N/A leadSelectionAccessible = (leadSelectionPath != null)
0N/A ? new AccessibleJTreeNode(JTree.this,
0N/A leadSelectionPath,
0N/A null) // parent
0N/A : null;
0N/A firePropertyChange(AccessibleContext.ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY,
0N/A oldLSA, leadSelectionAccessible);
0N/A }
0N/A firePropertyChange(AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY,
0N/A Boolean.valueOf(false), Boolean.valueOf(true));
0N/A }
0N/A
0N/A /**
0N/A * Fire a visible data property change notification.
0N/A * A 'visible' data property is one that represents
0N/A * something about the way the component appears on the
0N/A * display, where that appearance isn't bound to any other
0N/A * property. It notifies screen readers that the visual
0N/A * appearance of the component has changed, so they can
0N/A * notify the user.
0N/A */
0N/A public void fireVisibleDataPropertyChange() {
0N/A firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
0N/A Boolean.valueOf(false), Boolean.valueOf(true));
0N/A }
0N/A
0N/A // Fire the visible data changes for the model changes.
0N/A
0N/A /**
0N/A * Tree Model Node change notification.
0N/A *
0N/A * @param e a Tree Model event
0N/A */
0N/A public void treeNodesChanged(TreeModelEvent e) {
0N/A fireVisibleDataPropertyChange();
0N/A }
0N/A
0N/A /**
0N/A * Tree Model Node change notification.
0N/A *
0N/A * @param e a Tree node insertion event
0N/A */
0N/A public void treeNodesInserted(TreeModelEvent e) {
0N/A fireVisibleDataPropertyChange();
0N/A }
0N/A
0N/A /**
0N/A * Tree Model Node change notification.
0N/A *
0N/A * @param e a Tree node(s) removal event
0N/A */
0N/A public void treeNodesRemoved(TreeModelEvent e) {
0N/A fireVisibleDataPropertyChange();
0N/A }
0N/A
0N/A /**
0N/A * Tree Model structure change change notification.
0N/A *
0N/A * @param e a Tree Model event
0N/A */
0N/A public void treeStructureChanged(TreeModelEvent e) {
0N/A fireVisibleDataPropertyChange();
0N/A }
0N/A
0N/A /**
0N/A * Tree Collapsed notification.
0N/A *
0N/A * @param e a TreeExpansionEvent
0N/A */
0N/A public void treeCollapsed(TreeExpansionEvent e) {
0N/A fireVisibleDataPropertyChange();
0N/A TreePath path = e.getPath();
0N/A if (path != null) {
0N/A // Set parent to null so AccessibleJTreeNode computes
0N/A // its parent.
0N/A AccessibleJTreeNode node = new AccessibleJTreeNode(JTree.this,
0N/A path,
0N/A null);
0N/A PropertyChangeEvent pce = new PropertyChangeEvent(node,
0N/A AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A AccessibleState.EXPANDED,
0N/A AccessibleState.COLLAPSED);
0N/A firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A null, pce);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Tree Model Expansion notification.
0N/A *
0N/A * @param e a Tree node insertion event
0N/A */
0N/A public void treeExpanded(TreeExpansionEvent e) {
0N/A fireVisibleDataPropertyChange();
0N/A TreePath path = e.getPath();
0N/A if (path != null) {
0N/A // TIGER - 4839971
0N/A // Set parent to null so AccessibleJTreeNode computes
0N/A // its parent.
0N/A AccessibleJTreeNode node = new AccessibleJTreeNode(JTree.this,
0N/A path,
0N/A null);
0N/A PropertyChangeEvent pce = new PropertyChangeEvent(node,
0N/A AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A AccessibleState.COLLAPSED,
0N/A AccessibleState.EXPANDED);
0N/A firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A null, pce);
0N/A }
0N/A }
0N/A
0N/A
0N/A private AccessibleContext getCurrentAccessibleContext() {
0N/A Component c = getCurrentComponent();
0N/A if (c instanceof Accessible) {
625N/A return c.getAccessibleContext();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A private Component getCurrentComponent() {
0N/A // is the object visible?
0N/A // if so, get row, selected, focus & leaf state,
0N/A // and then get the renderer component and return it
0N/A TreeModel model = JTree.this.getModel();
0N/A if (model == null) {
0N/A return null;
0N/A }
0N/A TreePath path = new TreePath(model.getRoot());
0N/A if (JTree.this.isVisible(path)) {
0N/A TreeCellRenderer r = JTree.this.getCellRenderer();
0N/A TreeUI ui = JTree.this.getUI();
0N/A if (ui != null) {
0N/A int row = ui.getRowForPath(JTree.this, path);
0N/A int lsr = JTree.this.getLeadSelectionRow();
0N/A boolean hasFocus = JTree.this.isFocusOwner()
0N/A && (lsr == row);
0N/A boolean selected = JTree.this.isPathSelected(path);
0N/A boolean expanded = JTree.this.isExpanded(path);
0N/A
0N/A return r.getTreeCellRendererComponent(JTree.this,
0N/A model.getRoot(), selected, expanded,
0N/A model.isLeaf(model.getRoot()), row, hasFocus);
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A // Overridden methods from AccessibleJComponent
0N/A
0N/A /**
0N/A * Get the role of this object.
0N/A *
0N/A * @return an instance of AccessibleRole describing the role of the
0N/A * object
0N/A * @see AccessibleRole
0N/A */
0N/A public AccessibleRole getAccessibleRole() {
0N/A return AccessibleRole.TREE;
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>Accessible</code> child, if one exists,
0N/A * contained at the local coordinate <code>Point</code>.
0N/A * Otherwise returns <code>null</code>.
0N/A *
0N/A * @param p point in local coordinates of this <code>Accessible</code>
0N/A * @return the <code>Accessible</code>, if it exists,
0N/A * at the specified location; else <code>null</code>
0N/A */
0N/A public Accessible getAccessibleAt(Point p) {
0N/A TreePath path = getClosestPathForLocation(p.x, p.y);
0N/A if (path != null) {
0N/A // JTree.this is NOT the parent; parent will get computed later
0N/A return new AccessibleJTreeNode(JTree.this, path, null);
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of top-level children nodes of this
0N/A * JTree. Each of these nodes may in turn have children nodes.
0N/A *
0N/A * @return the number of accessible children nodes in the tree.
0N/A */
0N/A public int getAccessibleChildrenCount() {
0N/A TreeModel model = JTree.this.getModel();
0N/A if (model == null) {
0N/A return 0;
0N/A }
0N/A if (isRootVisible()) {
0N/A return 1; // the root node
0N/A }
0N/A
0N/A // return the root's first set of children count
0N/A return model.getChildCount(model.getRoot());
0N/A }
0N/A
0N/A /**
0N/A * Return the nth Accessible child of the object.
0N/A *
0N/A * @param i zero-based index of child
0N/A * @return the nth Accessible child of the object
0N/A */
0N/A public Accessible getAccessibleChild(int i) {
0N/A TreeModel model = JTree.this.getModel();
0N/A if (model == null) {
0N/A return null;
0N/A }
0N/A if (isRootVisible()) {
0N/A if (i == 0) { // return the root node Accessible
0N/A Object[] objPath = { model.getRoot() };
0N/A TreePath path = new TreePath(objPath);
0N/A return new AccessibleJTreeNode(JTree.this, path, JTree.this);
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A // return Accessible for one of root's child nodes
0N/A int count = model.getChildCount(model.getRoot());
0N/A if (i < 0 || i >= count) {
0N/A return null;
0N/A }
0N/A Object obj = model.getChild(model.getRoot(), i);
0N/A Object[] objPath = { model.getRoot(), obj };
0N/A TreePath path = new TreePath(objPath);
0N/A return new AccessibleJTreeNode(JTree.this, path, JTree.this);
0N/A }
0N/A
0N/A /**
0N/A * Get the index of this object in its accessible parent.
0N/A *
0N/A * @return the index of this object in its parent. Since a JTree
0N/A * top-level object does not have an accessible parent.
0N/A * @see #getAccessibleParent
0N/A */
0N/A public int getAccessibleIndexInParent() {
0N/A // didn't ever need to override this...
0N/A return super.getAccessibleIndexInParent();
0N/A }
0N/A
0N/A // AccessibleSelection methods
0N/A /**
0N/A * Get the AccessibleSelection associated with this object. In the
0N/A * implementation of the Java Accessibility API for this class,
0N/A * return this object, which is responsible for implementing the
0N/A * AccessibleSelection interface on behalf of itself.
0N/A *
0N/A * @return this object
0N/A */
0N/A public AccessibleSelection getAccessibleSelection() {
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of items currently selected.
0N/A * If no items are selected, the return value will be 0.
0N/A *
0N/A * @return the number of items currently selected.
0N/A */
0N/A public int getAccessibleSelectionCount() {
0N/A Object[] rootPath = new Object[1];
0N/A rootPath[0] = treeModel.getRoot();
0N/A TreePath childPath = new TreePath(rootPath);
0N/A if (JTree.this.isPathSelected(childPath)) {
0N/A return 1;
0N/A } else {
0N/A return 0;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns an Accessible representing the specified selected item
0N/A * in the object. If there isn't a selection, or there are
0N/A * fewer items selected than the integer passed in, the return
0N/A * value will be null.
0N/A *
0N/A * @param i the zero-based index of selected items
0N/A * @return an Accessible containing the selected item
0N/A */
0N/A public Accessible getAccessibleSelection(int i) {
0N/A // The JTree can have only one accessible child, the root.
0N/A if (i == 0) {
0N/A Object[] rootPath = new Object[1];
0N/A rootPath[0] = treeModel.getRoot();
0N/A TreePath childPath = new TreePath(rootPath);
0N/A if (JTree.this.isPathSelected(childPath)) {
0N/A return new AccessibleJTreeNode(JTree.this, childPath, JTree.this);
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the current child of this object is selected.
0N/A *
0N/A * @param i the zero-based index of the child in this Accessible object.
0N/A * @see AccessibleContext#getAccessibleChild
0N/A */
0N/A public boolean isAccessibleChildSelected(int i) {
0N/A // The JTree can have only one accessible child, the root.
0N/A if (i == 0) {
0N/A Object[] rootPath = new Object[1];
0N/A rootPath[0] = treeModel.getRoot();
0N/A TreePath childPath = new TreePath(rootPath);
0N/A return JTree.this.isPathSelected(childPath);
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Adds the specified selected item in the object to the object's
0N/A * selection. If the object supports multiple selections,
0N/A * the specified item is added to any existing selection, otherwise
0N/A * it replaces any existing selection in the object. If the
0N/A * specified item is already selected, this method has no effect.
0N/A *
0N/A * @param i the zero-based index of selectable items
0N/A */
0N/A public void addAccessibleSelection(int i) {
0N/A TreeModel model = JTree.this.getModel();
0N/A if (model != null) {
0N/A if (i == 0) {
0N/A Object[] objPath = {model.getRoot()};
0N/A TreePath path = new TreePath(objPath);
0N/A JTree.this.addSelectionPath(path);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Removes the specified selected item in the object from the object's
0N/A * selection. If the specified item isn't currently selected, this
0N/A * method has no effect.
0N/A *
0N/A * @param i the zero-based index of selectable items
0N/A */
0N/A public void removeAccessibleSelection(int i) {
0N/A TreeModel model = JTree.this.getModel();
0N/A if (model != null) {
0N/A if (i == 0) {
0N/A Object[] objPath = {model.getRoot()};
0N/A TreePath path = new TreePath(objPath);
0N/A JTree.this.removeSelectionPath(path);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Clears the selection in the object, so that nothing in the
0N/A * object is selected.
0N/A */
0N/A public void clearAccessibleSelection() {
0N/A int childCount = getAccessibleChildrenCount();
0N/A for (int i = 0; i < childCount; i++) {
0N/A removeAccessibleSelection(i);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Causes every selected item in the object to be selected
0N/A * if the object supports multiple selections.
0N/A */
0N/A public void selectAllAccessibleSelection() {
0N/A TreeModel model = JTree.this.getModel();
0N/A if (model != null) {
0N/A Object[] objPath = {model.getRoot()};
0N/A TreePath path = new TreePath(objPath);
0N/A JTree.this.addSelectionPath(path);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This class implements accessibility support for the
0N/A * <code>JTree</code> child. It provides an implementation of the
0N/A * Java Accessibility API appropriate to tree nodes.
0N/A */
0N/A protected class AccessibleJTreeNode extends AccessibleContext
0N/A implements Accessible, AccessibleComponent, AccessibleSelection,
0N/A AccessibleAction {
0N/A
0N/A private JTree tree = null;
0N/A private TreeModel treeModel = null;
0N/A private Object obj = null;
0N/A private TreePath path = null;
0N/A private Accessible accessibleParent = null;
0N/A private int index = 0;
0N/A private boolean isLeaf = false;
0N/A
0N/A /**
0N/A * Constructs an AccessibleJTreeNode
0N/A * @since 1.4
0N/A */
0N/A public AccessibleJTreeNode(JTree t, TreePath p, Accessible ap) {
0N/A tree = t;
0N/A path = p;
0N/A accessibleParent = ap;
0N/A treeModel = t.getModel();
0N/A obj = p.getLastPathComponent();
0N/A if (treeModel != null) {
0N/A isLeaf = treeModel.isLeaf(obj);
0N/A }
0N/A }
0N/A
0N/A private TreePath getChildTreePath(int i) {
0N/A // Tree nodes can't be so complex that they have
0N/A // two sets of children -> we're ignoring that case
0N/A if (i < 0 || i >= getAccessibleChildrenCount()) {
0N/A return null;
0N/A } else {
0N/A Object childObj = treeModel.getChild(obj, i);
0N/A Object[] objPath = path.getPath();
0N/A Object[] objChildPath = new Object[objPath.length+1];
0N/A java.lang.System.arraycopy(objPath, 0, objChildPath, 0, objPath.length);
0N/A objChildPath[objChildPath.length-1] = childObj;
0N/A return new TreePath(objChildPath);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the AccessibleContext associated with this tree node.
0N/A * In the implementation of the Java Accessibility API for
0N/A * this class, return this object, which is its own
0N/A * AccessibleContext.
0N/A *
0N/A * @return this object
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A return this;
0N/A }
0N/A
0N/A private AccessibleContext getCurrentAccessibleContext() {
0N/A Component c = getCurrentComponent();
0N/A if (c instanceof Accessible) {
625N/A return c.getAccessibleContext();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A private Component getCurrentComponent() {
0N/A // is the object visible?
0N/A // if so, get row, selected, focus & leaf state,
0N/A // and then get the renderer component and return it
0N/A if (tree.isVisible(path)) {
0N/A TreeCellRenderer r = tree.getCellRenderer();
0N/A if (r == null) {
0N/A return null;
0N/A }
0N/A TreeUI ui = tree.getUI();
0N/A if (ui != null) {
0N/A int row = ui.getRowForPath(JTree.this, path);
0N/A boolean selected = tree.isPathSelected(path);
0N/A boolean expanded = tree.isExpanded(path);
0N/A boolean hasFocus = false; // how to tell?? -PK
0N/A return r.getTreeCellRendererComponent(tree, obj,
0N/A selected, expanded, isLeaf, row, hasFocus);
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A // AccessibleContext methods
0N/A
0N/A /**
0N/A * Get the accessible name of this object.
0N/A *
0N/A * @return the localized name of the object; null if this
0N/A * object does not have a name
0N/A */
0N/A public String getAccessibleName() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A String name = ac.getAccessibleName();
0N/A if ((name != null) && (name != "")) {
0N/A return ac.getAccessibleName();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A if ((accessibleName != null) && (accessibleName != "")) {
0N/A return accessibleName;
0N/A } else {
0N/A // fall back to the client property
0N/A return (String)getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Set the localized accessible name of this object.
0N/A *
0N/A * @param s the new localized name of the object.
0N/A */
0N/A public void setAccessibleName(String s) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A ac.setAccessibleName(s);
0N/A } else {
0N/A super.setAccessibleName(s);
0N/A }
0N/A }
0N/A
0N/A //
0N/A // *** should check tooltip text for desc. (needs MouseEvent)
0N/A //
0N/A /**
0N/A * Get the accessible description of this object.
0N/A *
0N/A * @return the localized description of the object; null if
0N/A * this object does not have a description
0N/A */
0N/A public String getAccessibleDescription() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A return ac.getAccessibleDescription();
0N/A } else {
0N/A return super.getAccessibleDescription();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Set the accessible description of this object.
0N/A *
0N/A * @param s the new localized description of the object
0N/A */
0N/A public void setAccessibleDescription(String s) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A ac.setAccessibleDescription(s);
0N/A } else {
0N/A super.setAccessibleDescription(s);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the role of this object.
0N/A *
0N/A * @return an instance of AccessibleRole describing the role of the object
0N/A * @see AccessibleRole
0N/A */
0N/A public AccessibleRole getAccessibleRole() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A return ac.getAccessibleRole();
0N/A } else {
0N/A return AccessibleRole.UNKNOWN;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the state set of this object.
0N/A *
0N/A * @return an instance of AccessibleStateSet containing the
0N/A * current state set of the object
0N/A * @see AccessibleState
0N/A */
0N/A public AccessibleStateSet getAccessibleStateSet() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A AccessibleStateSet states;
0N/A if (ac != null) {
0N/A states = ac.getAccessibleStateSet();
0N/A } else {
0N/A states = new AccessibleStateSet();
0N/A }
0N/A // need to test here, 'cause the underlying component
0N/A // is a cellRenderer, which is never showing...
0N/A if (isShowing()) {
0N/A states.add(AccessibleState.SHOWING);
0N/A } else if (states.contains(AccessibleState.SHOWING)) {
0N/A states.remove(AccessibleState.SHOWING);
0N/A }
0N/A if (isVisible()) {
0N/A states.add(AccessibleState.VISIBLE);
0N/A } else if (states.contains(AccessibleState.VISIBLE)) {
0N/A states.remove(AccessibleState.VISIBLE);
0N/A }
0N/A if (tree.isPathSelected(path)){
0N/A states.add(AccessibleState.SELECTED);
0N/A }
0N/A if (path == getLeadSelectionPath()) {
0N/A states.add(AccessibleState.ACTIVE);
0N/A }
0N/A if (!isLeaf) {
0N/A states.add(AccessibleState.EXPANDABLE);
0N/A }
0N/A if (tree.isExpanded(path)) {
0N/A states.add(AccessibleState.EXPANDED);
0N/A } else {
0N/A states.add(AccessibleState.COLLAPSED);
0N/A }
0N/A if (tree.isEditable()) {
0N/A states.add(AccessibleState.EDITABLE);
0N/A }
0N/A return states;
0N/A }
0N/A
0N/A /**
0N/A * Get the Accessible parent of this object.
0N/A *
0N/A * @return the Accessible parent of this object; null if this
0N/A * object does not have an Accessible parent
0N/A */
0N/A public Accessible getAccessibleParent() {
0N/A // someone wants to know, so we need to create our parent
0N/A // if we don't have one (hey, we're a talented kid!)
0N/A if (accessibleParent == null) {
0N/A Object[] objPath = path.getPath();
0N/A if (objPath.length > 1) {
0N/A Object objParent = objPath[objPath.length-2];
0N/A if (treeModel != null) {
0N/A index = treeModel.getIndexOfChild(objParent, obj);
0N/A }
0N/A Object[] objParentPath = new Object[objPath.length-1];
0N/A java.lang.System.arraycopy(objPath, 0, objParentPath,
0N/A 0, objPath.length-1);
0N/A TreePath parentPath = new TreePath(objParentPath);
0N/A accessibleParent = new AccessibleJTreeNode(tree,
0N/A parentPath,
0N/A null);
0N/A this.setAccessibleParent(accessibleParent);
0N/A } else if (treeModel != null) {
0N/A accessibleParent = tree; // we're the top!
0N/A index = 0; // we're an only child!
0N/A this.setAccessibleParent(accessibleParent);
0N/A }
0N/A }
0N/A return accessibleParent;
0N/A }
0N/A
0N/A /**
0N/A * Get the index of this object in its accessible parent.
0N/A *
0N/A * @return the index of this object in its parent; -1 if this
0N/A * object does not have an accessible parent.
0N/A * @see #getAccessibleParent
0N/A */
0N/A public int getAccessibleIndexInParent() {
0N/A // index is invalid 'till we have an accessibleParent...
0N/A if (accessibleParent == null) {
0N/A getAccessibleParent();
0N/A }
0N/A Object[] objPath = path.getPath();
0N/A if (objPath.length > 1) {
0N/A Object objParent = objPath[objPath.length-2];
0N/A if (treeModel != null) {
0N/A index = treeModel.getIndexOfChild(objParent, obj);
0N/A }
0N/A }
0N/A return index;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of accessible children in the object.
0N/A *
0N/A * @return the number of accessible children in the object.
0N/A */
0N/A public int getAccessibleChildrenCount() {
0N/A // Tree nodes can't be so complex that they have
0N/A // two sets of children -> we're ignoring that case
0N/A return treeModel.getChildCount(obj);
0N/A }
0N/A
0N/A /**
0N/A * Return the specified Accessible child of the object.
0N/A *
0N/A * @param i zero-based index of child
0N/A * @return the Accessible child of the object
0N/A */
0N/A public Accessible getAccessibleChild(int i) {
0N/A // Tree nodes can't be so complex that they have
0N/A // two sets of children -> we're ignoring that case
0N/A if (i < 0 || i >= getAccessibleChildrenCount()) {
0N/A return null;
0N/A } else {
0N/A Object childObj = treeModel.getChild(obj, i);
0N/A Object[] objPath = path.getPath();
0N/A Object[] objChildPath = new Object[objPath.length+1];
0N/A java.lang.System.arraycopy(objPath, 0, objChildPath, 0, objPath.length);
0N/A objChildPath[objChildPath.length-1] = childObj;
0N/A TreePath childPath = new TreePath(objChildPath);
0N/A return new AccessibleJTreeNode(JTree.this, childPath, this);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets the locale of the component. If the component does not have
0N/A * a locale, then the locale of its parent is returned.
0N/A *
0N/A * @return This component's locale. If this component does not have
0N/A * a locale, the locale of its parent is returned.
0N/A * @exception IllegalComponentStateException
0N/A * If the Component does not have its own locale and has not yet
0N/A * been added to a containment hierarchy such that the locale can be
0N/A * determined from the containing parent.
0N/A * @see #setLocale
0N/A */
0N/A public Locale getLocale() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A return ac.getLocale();
0N/A } else {
0N/A return tree.getLocale();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Add a PropertyChangeListener to the listener list.
0N/A * The listener is registered for all properties.
0N/A *
0N/A * @param l The PropertyChangeListener to be added
0N/A */
0N/A public void addPropertyChangeListener(PropertyChangeListener l) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A ac.addPropertyChangeListener(l);
0N/A } else {
0N/A super.addPropertyChangeListener(l);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Remove a PropertyChangeListener from the listener list.
0N/A * This removes a PropertyChangeListener that was registered
0N/A * for all properties.
0N/A *
0N/A * @param l The PropertyChangeListener to be removed
0N/A */
0N/A public void removePropertyChangeListener(PropertyChangeListener l) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A ac.removePropertyChangeListener(l);
0N/A } else {
0N/A super.removePropertyChangeListener(l);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the AccessibleAction associated with this object. In the
0N/A * implementation of the Java Accessibility API for this class,
0N/A * return this object, which is responsible for implementing the
0N/A * AccessibleAction interface on behalf of itself.
0N/A *
0N/A * @return this object
0N/A */
0N/A public AccessibleAction getAccessibleAction() {
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Get the AccessibleComponent associated with this object. In the
0N/A * implementation of the Java Accessibility API for this class,
0N/A * return this object, which is responsible for implementing the
0N/A * AccessibleComponent interface on behalf of itself.
0N/A *
0N/A * @return this object
0N/A */
0N/A public AccessibleComponent getAccessibleComponent() {
0N/A return this; // to override getBounds()
0N/A }
0N/A
0N/A /**
0N/A * Get the AccessibleSelection associated with this object if one
0N/A * exists. Otherwise return null.
0N/A *
0N/A * @return the AccessibleSelection, or null
0N/A */
0N/A public AccessibleSelection getAccessibleSelection() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null && isLeaf) {
0N/A return getCurrentAccessibleContext().getAccessibleSelection();
0N/A } else {
0N/A return this;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the AccessibleText associated with this object if one
0N/A * exists. Otherwise return null.
0N/A *
0N/A * @return the AccessibleText, or null
0N/A */
0N/A public AccessibleText getAccessibleText() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A return getCurrentAccessibleContext().getAccessibleText();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the AccessibleValue associated with this object if one
0N/A * exists. Otherwise return null.
0N/A *
0N/A * @return the AccessibleValue, or null
0N/A */
0N/A public AccessibleValue getAccessibleValue() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A return getCurrentAccessibleContext().getAccessibleValue();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A
0N/A // AccessibleComponent methods
0N/A
0N/A /**
0N/A * Get the background color of this object.
0N/A *
0N/A * @return the background color, if supported, of the object;
0N/A * otherwise, null
0N/A */
0N/A public Color getBackground() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ac).getBackground();
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A return c.getBackground();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Set the background color of this object.
0N/A *
0N/A * @param c the new Color for the background
0N/A */
0N/A public void setBackground(Color c) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).setBackground(c);
0N/A } else {
0N/A Component cp = getCurrentComponent();
0N/A if (cp != null) {
0N/A cp.setBackground(c);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Get the foreground color of this object.
0N/A *
0N/A * @return the foreground color, if supported, of the object;
0N/A * otherwise, null
0N/A */
0N/A public Color getForeground() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ac).getForeground();
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A return c.getForeground();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void setForeground(Color c) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).setForeground(c);
0N/A } else {
0N/A Component cp = getCurrentComponent();
0N/A if (cp != null) {
0N/A cp.setForeground(c);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public Cursor getCursor() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ac).getCursor();
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A return c.getCursor();
0N/A } else {
0N/A Accessible ap = getAccessibleParent();
0N/A if (ap instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ap).getCursor();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void setCursor(Cursor c) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).setCursor(c);
0N/A } else {
0N/A Component cp = getCurrentComponent();
0N/A if (cp != null) {
0N/A cp.setCursor(c);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public Font getFont() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ac).getFont();
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A return c.getFont();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void setFont(Font f) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).setFont(f);
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A c.setFont(f);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public FontMetrics getFontMetrics(Font f) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ac).getFontMetrics(f);
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A return c.getFontMetrics(f);
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public boolean isEnabled() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ac).isEnabled();
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A return c.isEnabled();
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void setEnabled(boolean b) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).setEnabled(b);
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A c.setEnabled(b);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public boolean isVisible() {
0N/A Rectangle pathBounds = tree.getPathBounds(path);
0N/A Rectangle parentBounds = tree.getVisibleRect();
625N/A return pathBounds != null && parentBounds != null &&
625N/A parentBounds.intersects(pathBounds);
0N/A }
0N/A
0N/A public void setVisible(boolean b) {
0N/A }
0N/A
0N/A public boolean isShowing() {
0N/A return (tree.isShowing() && isVisible());
0N/A }
0N/A
0N/A public boolean contains(Point p) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A Rectangle r = ((AccessibleComponent) ac).getBounds();
0N/A return r.contains(p);
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A Rectangle r = c.getBounds();
0N/A return r.contains(p);
0N/A } else {
0N/A return getBounds().contains(p);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public Point getLocationOnScreen() {
0N/A if (tree != null) {
0N/A Point treeLocation = tree.getLocationOnScreen();
0N/A Rectangle pathBounds = tree.getPathBounds(path);
0N/A if (treeLocation != null && pathBounds != null) {
0N/A Point nodeLocation = new Point(pathBounds.x,
0N/A pathBounds.y);
0N/A nodeLocation.translate(treeLocation.x, treeLocation.y);
0N/A return nodeLocation;
0N/A } else {
0N/A return null;
0N/A }
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A protected Point getLocationInJTree() {
0N/A Rectangle r = tree.getPathBounds(path);
0N/A if (r != null) {
0N/A return r.getLocation();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public Point getLocation() {
0N/A Rectangle r = getBounds();
0N/A if (r != null) {
0N/A return r.getLocation();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public void setLocation(Point p) {
0N/A }
0N/A
0N/A public Rectangle getBounds() {
0N/A Rectangle r = tree.getPathBounds(path);
0N/A Accessible parent = getAccessibleParent();
0N/A if (parent != null) {
0N/A if (parent instanceof AccessibleJTreeNode) {
0N/A Point parentLoc = ((AccessibleJTreeNode) parent).getLocationInJTree();
0N/A if (parentLoc != null && r != null) {
0N/A r.translate(-parentLoc.x, -parentLoc.y);
0N/A } else {
0N/A return null; // not visible!
0N/A }
0N/A }
0N/A }
0N/A return r;
0N/A }
0N/A
0N/A public void setBounds(Rectangle r) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).setBounds(r);
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A c.setBounds(r);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public Dimension getSize() {
0N/A return getBounds().getSize();
0N/A }
0N/A
0N/A public void setSize (Dimension d) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).setSize(d);
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A c.setSize(d);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>Accessible</code> child, if one exists,
0N/A * contained at the local coordinate <code>Point</code>.
0N/A * Otherwise returns <code>null</code>.
0N/A *
0N/A * @param p point in local coordinates of this
0N/A * <code>Accessible</code>
0N/A * @return the <code>Accessible</code>, if it exists,
0N/A * at the specified location; else <code>null</code>
0N/A */
0N/A public Accessible getAccessibleAt(Point p) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ac).getAccessibleAt(p);
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public boolean isFocusTraversable() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ac).isFocusTraversable();
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A return c.isFocusTraversable();
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void requestFocus() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).requestFocus();
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A c.requestFocus();
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void addFocusListener(FocusListener l) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).addFocusListener(l);
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A c.addFocusListener(l);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void removeFocusListener(FocusListener l) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).removeFocusListener(l);
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A c.removeFocusListener(l);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // AccessibleSelection methods
0N/A
0N/A /**
0N/A * Returns the number of items currently selected.
0N/A * If no items are selected, the return value will be 0.
0N/A *
0N/A * @return the number of items currently selected.
0N/A */
0N/A public int getAccessibleSelectionCount() {
0N/A int count = 0;
0N/A int childCount = getAccessibleChildrenCount();
0N/A for (int i = 0; i < childCount; i++) {
0N/A TreePath childPath = getChildTreePath(i);
0N/A if (tree.isPathSelected(childPath)) {
0N/A count++;
0N/A }
0N/A }
0N/A return count;
0N/A }
0N/A
0N/A /**
0N/A * Returns an Accessible representing the specified selected item
0N/A * in the object. If there isn't a selection, or there are
0N/A * fewer items selected than the integer passed in, the return
0N/A * value will be null.
0N/A *
0N/A * @param i the zero-based index of selected items
0N/A * @return an Accessible containing the selected item
0N/A */
0N/A public Accessible getAccessibleSelection(int i) {
0N/A int childCount = getAccessibleChildrenCount();
0N/A if (i < 0 || i >= childCount) {
0N/A return null; // out of range
0N/A }
0N/A int count = 0;
0N/A for (int j = 0; j < childCount && i >= count; j++) {
0N/A TreePath childPath = getChildTreePath(j);
0N/A if (tree.isPathSelected(childPath)) {
0N/A if (count == i) {
0N/A return new AccessibleJTreeNode(tree, childPath, this);
0N/A } else {
0N/A count++;
0N/A }
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the current child of this object is selected.
0N/A *
0N/A * @param i the zero-based index of the child in this Accessible
0N/A * object.
0N/A * @see AccessibleContext#getAccessibleChild
0N/A */
0N/A public boolean isAccessibleChildSelected(int i) {
0N/A int childCount = getAccessibleChildrenCount();
0N/A if (i < 0 || i >= childCount) {
0N/A return false; // out of range
0N/A } else {
0N/A TreePath childPath = getChildTreePath(i);
0N/A return tree.isPathSelected(childPath);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Adds the specified selected item in the object to the object's
0N/A * selection. If the object supports multiple selections,
0N/A * the specified item is added to any existing selection, otherwise
0N/A * it replaces any existing selection in the object. If the
0N/A * specified item is already selected, this method has no effect.
0N/A *
0N/A * @param i the zero-based index of selectable items
0N/A */
0N/A public void addAccessibleSelection(int i) {
0N/A TreeModel model = JTree.this.getModel();
0N/A if (model != null) {
0N/A if (i >= 0 && i < getAccessibleChildrenCount()) {
0N/A TreePath path = getChildTreePath(i);
0N/A JTree.this.addSelectionPath(path);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Removes the specified selected item in the object from the
0N/A * object's
0N/A * selection. If the specified item isn't currently selected, this
0N/A * method has no effect.
0N/A *
0N/A * @param i the zero-based index of selectable items
0N/A */
0N/A public void removeAccessibleSelection(int i) {
0N/A TreeModel model = JTree.this.getModel();
0N/A if (model != null) {
0N/A if (i >= 0 && i < getAccessibleChildrenCount()) {
0N/A TreePath path = getChildTreePath(i);
0N/A JTree.this.removeSelectionPath(path);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Clears the selection in the object, so that nothing in the
0N/A * object is selected.
0N/A */
0N/A public void clearAccessibleSelection() {
0N/A int childCount = getAccessibleChildrenCount();
0N/A for (int i = 0; i < childCount; i++) {
0N/A removeAccessibleSelection(i);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Causes every selected item in the object to be selected
0N/A * if the object supports multiple selections.
0N/A */
0N/A public void selectAllAccessibleSelection() {
0N/A TreeModel model = JTree.this.getModel();
0N/A if (model != null) {
0N/A int childCount = getAccessibleChildrenCount();
0N/A TreePath path;
0N/A for (int i = 0; i < childCount; i++) {
0N/A path = getChildTreePath(i);
0N/A JTree.this.addSelectionPath(path);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // AccessibleAction methods
0N/A
0N/A /**
0N/A * Returns the number of accessible actions available in this
0N/A * tree node. If this node is not a leaf, there is at least
0N/A * one action (toggle expand), in addition to any available
0N/A * on the object behind the TreeCellRenderer.
0N/A *
0N/A * @return the number of Actions in this object
0N/A */
0N/A public int getAccessibleActionCount() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A AccessibleAction aa = ac.getAccessibleAction();
0N/A if (aa != null) {
0N/A return (aa.getAccessibleActionCount() + (isLeaf ? 0 : 1));
0N/A }
0N/A }
0N/A return isLeaf ? 0 : 1;
0N/A }
0N/A
0N/A /**
0N/A * Return a description of the specified action of the tree node.
0N/A * If this node is not a leaf, there is at least one action
0N/A * description (toggle expand), in addition to any available
0N/A * on the object behind the TreeCellRenderer.
0N/A *
0N/A * @param i zero-based index of the actions
0N/A * @return a description of the action
0N/A */
0N/A public String getAccessibleActionDescription(int i) {
0N/A if (i < 0 || i >= getAccessibleActionCount()) {
0N/A return null;
0N/A }
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (i == 0) {
0N/A // TIGER - 4766636
0N/A return AccessibleAction.TOGGLE_EXPAND;
0N/A } else if (ac != null) {
0N/A AccessibleAction aa = ac.getAccessibleAction();
0N/A if (aa != null) {
0N/A return aa.getAccessibleActionDescription(i - 1);
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Perform the specified Action on the tree node. If this node
0N/A * is not a leaf, there is at least one action which can be
0N/A * done (toggle expand), in addition to any available on the
0N/A * object behind the TreeCellRenderer.
0N/A *
0N/A * @param i zero-based index of actions
0N/A * @return true if the the action was performed; else false.
0N/A */
0N/A public boolean doAccessibleAction(int i) {
0N/A if (i < 0 || i >= getAccessibleActionCount()) {
0N/A return false;
0N/A }
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (i == 0) {
0N/A if (JTree.this.isExpanded(path)) {
0N/A JTree.this.collapsePath(path);
0N/A } else {
0N/A JTree.this.expandPath(path);
0N/A }
0N/A return true;
0N/A } else if (ac != null) {
0N/A AccessibleAction aa = ac.getAccessibleAction();
0N/A if (aa != null) {
0N/A return aa.doAccessibleAction(i - 1);
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A } // inner class AccessibleJTreeNode
0N/A
0N/A } // inner class AccessibleJTree
0N/A
0N/A} // End of class JTree