0N/A/*
3909N/A * Copyright (c) 1997, 2011, 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.plaf.basic;
0N/A
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.event.*;
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport java.awt.datatransfer.*;
0N/Aimport java.beans.*;
0N/Aimport java.util.Enumeration;
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Collections;
0N/Aimport java.util.Comparator;
0N/Aimport javax.swing.plaf.ComponentUI;
0N/Aimport javax.swing.plaf.UIResource;
0N/Aimport javax.swing.plaf.TreeUI;
0N/Aimport javax.swing.tree.*;
0N/Aimport javax.swing.text.Position;
0N/Aimport javax.swing.plaf.basic.DragRecognitionSupport.BeforeDrag;
0N/Aimport sun.swing.SwingUtilities2;
0N/A
0N/Aimport sun.swing.DefaultLookup;
0N/Aimport sun.swing.UIAction;
0N/A
0N/A/**
0N/A * The basic L&F for a hierarchical data structure.
0N/A * <p>
0N/A *
0N/A * @author Scott Violet
0N/A * @author Shannon Hickey (drag and drop)
0N/A */
0N/A
0N/Apublic class BasicTreeUI extends TreeUI
0N/A{
0N/A private static final StringBuilder BASELINE_COMPONENT_KEY =
0N/A new StringBuilder("Tree.baselineComponent");
0N/A
0N/A // Old actions forward to an instance of this.
0N/A static private final Actions SHARED_ACTION = new Actions();
0N/A
0N/A transient protected Icon collapsedIcon;
0N/A transient protected Icon expandedIcon;
0N/A
0N/A /**
0N/A * Color used to draw hash marks. If <code>null</code> no hash marks
0N/A * will be drawn.
0N/A */
0N/A private Color hashColor;
0N/A
0N/A /** Distance between left margin and where vertical dashes will be
0N/A * drawn. */
0N/A protected int leftChildIndent;
0N/A /** Distance to add to leftChildIndent to determine where cell
0N/A * contents will be drawn. */
0N/A protected int rightChildIndent;
0N/A /** Total distance that will be indented. The sum of leftChildIndent
0N/A * and rightChildIndent. */
0N/A protected int totalChildIndent;
0N/A
0N/A /** Minimum preferred size. */
0N/A protected Dimension preferredMinSize;
0N/A
0N/A /** Index of the row that was last selected. */
0N/A protected int lastSelectedRow;
0N/A
0N/A /** Component that we're going to be drawing into. */
0N/A protected JTree tree;
0N/A
0N/A /** Renderer that is being used to do the actual cell drawing. */
0N/A transient protected TreeCellRenderer currentCellRenderer;
0N/A
0N/A /** Set to true if the renderer that is currently in the tree was
0N/A * created by this instance. */
0N/A protected boolean createdRenderer;
0N/A
0N/A /** Editor for the tree. */
0N/A transient protected TreeCellEditor cellEditor;
0N/A
0N/A /** Set to true if editor that is currently in the tree was
0N/A * created by this instance. */
0N/A protected boolean createdCellEditor;
0N/A
0N/A /** Set to false when editing and shouldSelectCell() returns true meaning
0N/A * the node should be selected before editing, used in completeEditing. */
0N/A protected boolean stopEditingInCompleteEditing;
0N/A
0N/A /** Used to paint the TreeCellRenderer. */
0N/A protected CellRendererPane rendererPane;
0N/A
0N/A /** Size needed to completely display all the nodes. */
0N/A protected Dimension preferredSize;
0N/A
0N/A /** Is the preferredSize valid? */
0N/A protected boolean validCachedPreferredSize;
0N/A
0N/A /** Object responsible for handling sizing and expanded issues. */
0N/A // WARNING: Be careful with the bounds held by treeState. They are
0N/A // always in terms of left-to-right. They get mapped to right-to-left
0N/A // by the various methods of this class.
0N/A protected AbstractLayoutCache treeState;
0N/A
0N/A
0N/A /** Used for minimizing the drawing of vertical lines. */
0N/A protected Hashtable<TreePath,Boolean> drawingCache;
0N/A
0N/A /** True if doing optimizations for a largeModel. Subclasses that
0N/A * don't support this may wish to override createLayoutCache to not
0N/A * return a FixedHeightLayoutCache instance. */
0N/A protected boolean largeModel;
0N/A
0N/A /** Reponsible for telling the TreeState the size needed for a node. */
0N/A protected AbstractLayoutCache.NodeDimensions nodeDimensions;
0N/A
0N/A /** Used to determine what to display. */
0N/A protected TreeModel treeModel;
0N/A
0N/A /** Model maintaing the selection. */
0N/A protected TreeSelectionModel treeSelectionModel;
0N/A
0N/A /** How much the depth should be offset to properly calculate
0N/A * x locations. This is based on whether or not the root is visible,
0N/A * and if the root handles are visible. */
0N/A protected int depthOffset;
0N/A
0N/A // Following 4 ivars are only valid when editing.
0N/A
0N/A /** When editing, this will be the Component that is doing the actual
0N/A * editing. */
0N/A protected Component editingComponent;
0N/A
0N/A /** Path that is being edited. */
0N/A protected TreePath editingPath;
0N/A
0N/A /** Row that is being edited. Should only be referenced if
0N/A * editingComponent is not null. */
0N/A protected int editingRow;
0N/A
0N/A /** Set to true if the editor has a different size than the renderer. */
0N/A protected boolean editorHasDifferentSize;
0N/A
0N/A /** Row correspondin to lead path. */
0N/A private int leadRow;
0N/A /** If true, the property change event for LEAD_SELECTION_PATH_PROPERTY,
0N/A * or ANCHOR_SELECTION_PATH_PROPERTY will not generate a repaint. */
0N/A private boolean ignoreLAChange;
0N/A
0N/A /** Indicates the orientation. */
0N/A private boolean leftToRight;
0N/A
0N/A // Cached listeners
0N/A private PropertyChangeListener propertyChangeListener;
0N/A private PropertyChangeListener selectionModelPropertyChangeListener;
0N/A private MouseListener mouseListener;
0N/A private FocusListener focusListener;
0N/A private KeyListener keyListener;
0N/A /** Used for large models, listens for moved/resized events and
0N/A * updates the validCachedPreferredSize bit accordingly. */
0N/A private ComponentListener componentListener;
0N/A /** Listens for CellEditor events. */
0N/A private CellEditorListener cellEditorListener;
0N/A /** Updates the display when the selection changes. */
0N/A private TreeSelectionListener treeSelectionListener;
0N/A /** Is responsible for updating the display based on model events. */
0N/A private TreeModelListener treeModelListener;
0N/A /** Updates the treestate as the nodes expand. */
0N/A private TreeExpansionListener treeExpansionListener;
0N/A
0N/A /** UI property indicating whether to paint lines */
0N/A private boolean paintLines = true;
0N/A
0N/A /** UI property for painting dashed lines */
0N/A private boolean lineTypeDashed;
0N/A
0N/A /**
0N/A * The time factor to treate the series of typed alphanumeric key
0N/A * as prefix for first letter navigation.
0N/A */
0N/A private long timeFactor = 1000L;
0N/A
0N/A private Handler handler;
0N/A
0N/A /**
0N/A * A temporary variable for communication between startEditingOnRelease
0N/A * and startEditing.
0N/A */
0N/A private MouseEvent releaseEvent;
0N/A
0N/A public static ComponentUI createUI(JComponent x) {
0N/A return new BasicTreeUI();
0N/A }
0N/A
0N/A
0N/A static void loadActionMap(LazyActionMap map) {
0N/A map.put(new Actions(Actions.SELECT_PREVIOUS));
0N/A map.put(new Actions(Actions.SELECT_PREVIOUS_CHANGE_LEAD));
0N/A map.put(new Actions(Actions.SELECT_PREVIOUS_EXTEND_SELECTION));
0N/A
0N/A map.put(new Actions(Actions.SELECT_NEXT));
0N/A map.put(new Actions(Actions.SELECT_NEXT_CHANGE_LEAD));
0N/A map.put(new Actions(Actions.SELECT_NEXT_EXTEND_SELECTION));
0N/A
0N/A map.put(new Actions(Actions.SELECT_CHILD));
0N/A map.put(new Actions(Actions.SELECT_CHILD_CHANGE_LEAD));
0N/A
0N/A map.put(new Actions(Actions.SELECT_PARENT));
0N/A map.put(new Actions(Actions.SELECT_PARENT_CHANGE_LEAD));
0N/A
0N/A map.put(new Actions(Actions.SCROLL_UP_CHANGE_SELECTION));
0N/A map.put(new Actions(Actions.SCROLL_UP_CHANGE_LEAD));
0N/A map.put(new Actions(Actions.SCROLL_UP_EXTEND_SELECTION));
0N/A
0N/A map.put(new Actions(Actions.SCROLL_DOWN_CHANGE_SELECTION));
0N/A map.put(new Actions(Actions.SCROLL_DOWN_EXTEND_SELECTION));
0N/A map.put(new Actions(Actions.SCROLL_DOWN_CHANGE_LEAD));
0N/A
0N/A map.put(new Actions(Actions.SELECT_FIRST));
0N/A map.put(new Actions(Actions.SELECT_FIRST_CHANGE_LEAD));
0N/A map.put(new Actions(Actions.SELECT_FIRST_EXTEND_SELECTION));
0N/A
0N/A map.put(new Actions(Actions.SELECT_LAST));
0N/A map.put(new Actions(Actions.SELECT_LAST_CHANGE_LEAD));
0N/A map.put(new Actions(Actions.SELECT_LAST_EXTEND_SELECTION));
0N/A
0N/A map.put(new Actions(Actions.TOGGLE));
0N/A
0N/A map.put(new Actions(Actions.CANCEL_EDITING));
0N/A
0N/A map.put(new Actions(Actions.START_EDITING));
0N/A
0N/A map.put(new Actions(Actions.SELECT_ALL));
0N/A
0N/A map.put(new Actions(Actions.CLEAR_SELECTION));
0N/A
0N/A map.put(new Actions(Actions.SCROLL_LEFT));
0N/A map.put(new Actions(Actions.SCROLL_RIGHT));
0N/A
0N/A map.put(new Actions(Actions.SCROLL_LEFT_EXTEND_SELECTION));
0N/A map.put(new Actions(Actions.SCROLL_RIGHT_EXTEND_SELECTION));
0N/A
0N/A map.put(new Actions(Actions.SCROLL_RIGHT_CHANGE_LEAD));
0N/A map.put(new Actions(Actions.SCROLL_LEFT_CHANGE_LEAD));
0N/A
0N/A map.put(new Actions(Actions.EXPAND));
0N/A map.put(new Actions(Actions.COLLAPSE));
0N/A map.put(new Actions(Actions.MOVE_SELECTION_TO_PARENT));
0N/A
0N/A map.put(new Actions(Actions.ADD_TO_SELECTION));
0N/A map.put(new Actions(Actions.TOGGLE_AND_ANCHOR));
0N/A map.put(new Actions(Actions.EXTEND_TO));
0N/A map.put(new Actions(Actions.MOVE_SELECTION_TO));
0N/A
0N/A map.put(TransferHandler.getCutAction());
0N/A map.put(TransferHandler.getCopyAction());
0N/A map.put(TransferHandler.getPasteAction());
0N/A }
0N/A
0N/A
0N/A public BasicTreeUI() {
0N/A super();
0N/A }
0N/A
0N/A protected Color getHashColor() {
0N/A return hashColor;
0N/A }
0N/A
0N/A protected void setHashColor(Color color) {
0N/A hashColor = color;
0N/A }
0N/A
0N/A public void setLeftChildIndent(int newAmount) {
0N/A leftChildIndent = newAmount;
0N/A totalChildIndent = leftChildIndent + rightChildIndent;
0N/A if(treeState != null)
0N/A treeState.invalidateSizes();
0N/A updateSize();
0N/A }
0N/A
0N/A public int getLeftChildIndent() {
0N/A return leftChildIndent;
0N/A }
0N/A
0N/A public void setRightChildIndent(int newAmount) {
0N/A rightChildIndent = newAmount;
0N/A totalChildIndent = leftChildIndent + rightChildIndent;
0N/A if(treeState != null)
0N/A treeState.invalidateSizes();
0N/A updateSize();
0N/A }
0N/A
0N/A public int getRightChildIndent() {
0N/A return rightChildIndent;
0N/A }
0N/A
0N/A public void setExpandedIcon(Icon newG) {
0N/A expandedIcon = newG;
0N/A }
0N/A
0N/A public Icon getExpandedIcon() {
0N/A return expandedIcon;
0N/A }
0N/A
0N/A public void setCollapsedIcon(Icon newG) {
0N/A collapsedIcon = newG;
0N/A }
0N/A
0N/A public Icon getCollapsedIcon() {
0N/A return collapsedIcon;
0N/A }
0N/A
0N/A //
0N/A // Methods for configuring the behavior of the tree. None of them
0N/A // push the value to the JTree instance. You should really only
0N/A // call these methods on the JTree.
0N/A //
0N/A
0N/A /**
0N/A * Updates the componentListener, if necessary.
0N/A */
0N/A protected void setLargeModel(boolean largeModel) {
0N/A if(getRowHeight() < 1)
0N/A largeModel = false;
0N/A if(this.largeModel != largeModel) {
0N/A completeEditing();
0N/A this.largeModel = largeModel;
0N/A treeState = createLayoutCache();
0N/A configureLayoutCache();
0N/A updateLayoutCacheExpandedNodesIfNecessary();
0N/A updateSize();
0N/A }
0N/A }
0N/A
0N/A protected boolean isLargeModel() {
0N/A return largeModel;
0N/A }
0N/A
0N/A /**
0N/A * Sets the row height, this is forwarded to the treeState.
0N/A */
0N/A protected void setRowHeight(int rowHeight) {
0N/A completeEditing();
0N/A if(treeState != null) {
0N/A setLargeModel(tree.isLargeModel());
0N/A treeState.setRowHeight(rowHeight);
0N/A updateSize();
0N/A }
0N/A }
0N/A
0N/A protected int getRowHeight() {
0N/A return (tree == null) ? -1 : tree.getRowHeight();
0N/A }
0N/A
0N/A /**
0N/A * Sets the TreeCellRenderer to <code>tcr</code>. This invokes
0N/A * <code>updateRenderer</code>.
0N/A */
0N/A protected void setCellRenderer(TreeCellRenderer tcr) {
0N/A completeEditing();
0N/A updateRenderer();
0N/A if(treeState != null) {
0N/A treeState.invalidateSizes();
0N/A updateSize();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return currentCellRenderer, which will either be the trees
0N/A * renderer, or defaultCellRenderer, which ever wasn't null.
0N/A */
0N/A protected TreeCellRenderer getCellRenderer() {
0N/A return currentCellRenderer;
0N/A }
0N/A
0N/A /**
0N/A * Sets the TreeModel.
0N/A */
0N/A protected void setModel(TreeModel model) {
0N/A completeEditing();
0N/A if(treeModel != null && treeModelListener != null)
0N/A treeModel.removeTreeModelListener(treeModelListener);
0N/A treeModel = model;
0N/A if(treeModel != null) {
0N/A if(treeModelListener != null)
0N/A treeModel.addTreeModelListener(treeModelListener);
0N/A }
0N/A if(treeState != null) {
0N/A treeState.setModel(model);
0N/A updateLayoutCacheExpandedNodesIfNecessary();
0N/A updateSize();
0N/A }
0N/A }
0N/A
0N/A protected TreeModel getModel() {
0N/A return treeModel;
0N/A }
0N/A
0N/A /**
0N/A * Sets the root to being visible.
0N/A */
0N/A protected void setRootVisible(boolean newValue) {
0N/A completeEditing();
0N/A updateDepthOffset();
0N/A if(treeState != null) {
0N/A treeState.setRootVisible(newValue);
0N/A treeState.invalidateSizes();
0N/A updateSize();
0N/A }
0N/A }
0N/A
0N/A protected boolean isRootVisible() {
0N/A return (tree != null) ? tree.isRootVisible() : false;
0N/A }
0N/A
0N/A /**
0N/A * Determines whether the node handles are to be displayed.
0N/A */
0N/A protected void setShowsRootHandles(boolean newValue) {
0N/A completeEditing();
0N/A updateDepthOffset();
0N/A if(treeState != null) {
0N/A treeState.invalidateSizes();
0N/A updateSize();
0N/A }
0N/A }
0N/A
0N/A protected boolean getShowsRootHandles() {
0N/A return (tree != null) ? tree.getShowsRootHandles() : false;
0N/A }
0N/A
0N/A /**
0N/A * Sets the cell editor.
0N/A */
0N/A protected void setCellEditor(TreeCellEditor editor) {
0N/A updateCellEditor();
0N/A }
0N/A
0N/A protected TreeCellEditor getCellEditor() {
0N/A return (tree != null) ? tree.getCellEditor() : null;
0N/A }
0N/A
0N/A /**
0N/A * Configures the receiver to allow, or not allow, editing.
0N/A */
0N/A protected void setEditable(boolean newValue) {
0N/A updateCellEditor();
0N/A }
0N/A
0N/A protected boolean isEditable() {
0N/A return (tree != null) ? tree.isEditable() : false;
0N/A }
0N/A
0N/A /**
0N/A * Resets the selection model. The appropriate listener are installed
0N/A * on the model.
0N/A */
0N/A protected void setSelectionModel(TreeSelectionModel newLSM) {
0N/A completeEditing();
0N/A if(selectionModelPropertyChangeListener != null &&
0N/A treeSelectionModel != null)
0N/A treeSelectionModel.removePropertyChangeListener
0N/A (selectionModelPropertyChangeListener);
0N/A if(treeSelectionListener != null && treeSelectionModel != null)
0N/A treeSelectionModel.removeTreeSelectionListener
0N/A (treeSelectionListener);
0N/A treeSelectionModel = newLSM;
0N/A if(treeSelectionModel != null) {
0N/A if(selectionModelPropertyChangeListener != null)
0N/A treeSelectionModel.addPropertyChangeListener
0N/A (selectionModelPropertyChangeListener);
0N/A if(treeSelectionListener != null)
0N/A treeSelectionModel.addTreeSelectionListener
0N/A (treeSelectionListener);
0N/A if(treeState != null)
0N/A treeState.setSelectionModel(treeSelectionModel);
0N/A }
0N/A else if(treeState != null)
0N/A treeState.setSelectionModel(null);
0N/A if(tree != null)
0N/A tree.repaint();
0N/A }
0N/A
0N/A protected TreeSelectionModel getSelectionModel() {
0N/A return treeSelectionModel;
0N/A }
0N/A
0N/A //
0N/A // TreeUI methods
0N/A //
0N/A
0N/A /**
0N/A * Returns the Rectangle enclosing the label portion that the
0N/A * last item in path will be drawn into. Will return null if
0N/A * any component in path is currently valid.
0N/A */
0N/A public Rectangle getPathBounds(JTree tree, TreePath path) {
0N/A if(tree != null && treeState != null) {
0N/A return getPathBounds(path, tree.getInsets(), new Rectangle());
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A private Rectangle getPathBounds(TreePath path, Insets insets,
0N/A Rectangle bounds) {
0N/A bounds = treeState.getBounds(path, bounds);
0N/A if (bounds != null) {
0N/A if (leftToRight) {
0N/A bounds.x += insets.left;
0N/A } else {
0N/A bounds.x = tree.getWidth() - (bounds.x + bounds.width) -
0N/A insets.right;
0N/A }
0N/A bounds.y += insets.top;
0N/A }
0N/A return bounds;
0N/A }
0N/A
0N/A /**
0N/A * Returns the path for passed in row. If row is not visible
0N/A * null is returned.
0N/A */
0N/A public TreePath getPathForRow(JTree tree, int row) {
0N/A return (treeState != null) ? treeState.getPathForRow(row) : null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the row that the last item identified in path is visible
0N/A * at. Will return -1 if any of the elements in path are not
0N/A * currently visible.
0N/A */
0N/A public int getRowForPath(JTree tree, TreePath path) {
0N/A return (treeState != null) ? treeState.getRowForPath(path) : -1;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of rows that are being displayed.
0N/A */
0N/A public int getRowCount(JTree tree) {
0N/A return (treeState != null) ? treeState.getRowCount() : 0;
0N/A }
0N/A
0N/A /**
0N/A * Returns the path to the node that is closest to x,y. If
0N/A * there is nothing currently visible this will return null, otherwise
0N/A * it'll always return a valid path. If you need to test if the
0N/A * returned object is exactly at x, y you should get the bounds for
0N/A * the returned path and test x, y against that.
0N/A */
0N/A public TreePath getClosestPathForLocation(JTree tree, int x, int y) {
0N/A if(tree != null && treeState != null) {
0N/A // TreeState doesn't care about the x location, hence it isn't
0N/A // adjusted
0N/A y -= tree.getInsets().top;
0N/A return treeState.getPathClosestTo(x, y);
0N/A }
0N/A return null;
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 returned by getEditingPath().
0N/A */
0N/A public boolean isEditing(JTree tree) {
0N/A return (editingComponent != null);
0N/A }
0N/A
0N/A /**
0N/A * Stops the current editing session. This has no effect if the
0N/A * tree isn't being edited. Returns true if the editor allows the
0N/A * editing session to stop.
0N/A */
0N/A public boolean stopEditing(JTree tree) {
0N/A if(editingComponent != null && cellEditor.stopCellEditing()) {
0N/A completeEditing(false, false, true);
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Cancels the current editing session.
0N/A */
0N/A public void cancelEditing(JTree tree) {
0N/A if(editingComponent != null) {
0N/A completeEditing(false, true, false);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Selects the last item in path and tries to edit it. Editing will
0N/A * fail if the CellEditor won't allow it for the selected item.
0N/A */
0N/A public void startEditingAtPath(JTree tree, TreePath path) {
0N/A tree.scrollPathToVisible(path);
0N/A if(path != null && tree.isVisible(path))
0N/A startEditing(path, null);
0N/A }
0N/A
0N/A /**
0N/A * Returns the path to the element that is being edited.
0N/A */
0N/A public TreePath getEditingPath(JTree tree) {
0N/A return editingPath;
0N/A }
0N/A
0N/A //
0N/A // Install methods
0N/A //
0N/A
0N/A public void installUI(JComponent c) {
0N/A if ( c == null ) {
0N/A throw new NullPointerException( "null component passed to BasicTreeUI.installUI()" );
0N/A }
0N/A
0N/A tree = (JTree)c;
0N/A
0N/A prepareForUIInstall();
0N/A
0N/A // Boilerplate install block
0N/A installDefaults();
0N/A installKeyboardActions();
0N/A installComponents();
0N/A installListeners();
0N/A
0N/A completeUIInstall();
0N/A }
0N/A
0N/A /**
0N/A * Invoked after the <code>tree</code> instance variable has been
0N/A * set, but before any defaults/listeners have been installed.
0N/A */
0N/A protected void prepareForUIInstall() {
0N/A drawingCache = new Hashtable<TreePath,Boolean>(7);
0N/A
0N/A // Data member initializations
0N/A leftToRight = BasicGraphicsUtils.isLeftToRight(tree);
0N/A stopEditingInCompleteEditing = true;
0N/A lastSelectedRow = -1;
0N/A leadRow = -1;
0N/A preferredSize = new Dimension();
0N/A
0N/A largeModel = tree.isLargeModel();
0N/A if(getRowHeight() <= 0)
0N/A largeModel = false;
0N/A setModel(tree.getModel());
0N/A }
0N/A
0N/A /**
0N/A * Invoked from installUI after all the defaults/listeners have been
0N/A * installed.
0N/A */
0N/A protected void completeUIInstall() {
0N/A // Custom install code
0N/A
0N/A this.setShowsRootHandles(tree.getShowsRootHandles());
0N/A
0N/A updateRenderer();
0N/A
0N/A updateDepthOffset();
0N/A
0N/A setSelectionModel(tree.getSelectionModel());
0N/A
0N/A // Create, if necessary, the TreeState instance.
0N/A treeState = createLayoutCache();
0N/A configureLayoutCache();
0N/A
0N/A updateSize();
0N/A }
0N/A
0N/A protected void installDefaults() {
0N/A if(tree.getBackground() == null ||
0N/A tree.getBackground() instanceof UIResource) {
0N/A tree.setBackground(UIManager.getColor("Tree.background"));
0N/A }
0N/A if(getHashColor() == null || getHashColor() instanceof UIResource) {
0N/A setHashColor(UIManager.getColor("Tree.hash"));
0N/A }
0N/A if (tree.getFont() == null || tree.getFont() instanceof UIResource)
0N/A tree.setFont( UIManager.getFont("Tree.font") );
0N/A // JTree's original row height is 16. To correctly display the
0N/A // contents on Linux we should have set it to 18, Windows 19 and
0N/A // Solaris 20. As these values vary so much it's too hard to
0N/A // be backward compatable and try to update the row height, we're
0N/A // therefor NOT going to adjust the row height based on font. If the
0N/A // developer changes the font, it's there responsibility to update
0N/A // the row height.
0N/A
0N/A setExpandedIcon( (Icon)UIManager.get( "Tree.expandedIcon" ) );
0N/A setCollapsedIcon( (Icon)UIManager.get( "Tree.collapsedIcon" ) );
0N/A
0N/A setLeftChildIndent(((Integer)UIManager.get("Tree.leftChildIndent")).
0N/A intValue());
0N/A setRightChildIndent(((Integer)UIManager.get("Tree.rightChildIndent")).
0N/A intValue());
0N/A
0N/A LookAndFeel.installProperty(tree, "rowHeight",
0N/A UIManager.get("Tree.rowHeight"));
0N/A
0N/A largeModel = (tree.isLargeModel() && tree.getRowHeight() > 0);
0N/A
0N/A Object scrollsOnExpand = UIManager.get("Tree.scrollsOnExpand");
0N/A if (scrollsOnExpand != null) {
0N/A LookAndFeel.installProperty(tree, "scrollsOnExpand", scrollsOnExpand);
0N/A }
0N/A
0N/A paintLines = UIManager.getBoolean("Tree.paintLines");
0N/A lineTypeDashed = UIManager.getBoolean("Tree.lineTypeDashed");
0N/A
0N/A Long l = (Long)UIManager.get("Tree.timeFactor");
0N/A timeFactor = (l!=null) ? l.longValue() : 1000L;
0N/A
0N/A Object showsRootHandles = UIManager.get("Tree.showsRootHandles");
0N/A if (showsRootHandles != null) {
0N/A LookAndFeel.installProperty(tree,
0N/A JTree.SHOWS_ROOT_HANDLES_PROPERTY, showsRootHandles);
0N/A }
0N/A }
0N/A
0N/A protected void installListeners() {
0N/A if ( (propertyChangeListener = createPropertyChangeListener())
0N/A != null ) {
0N/A tree.addPropertyChangeListener(propertyChangeListener);
0N/A }
0N/A if ( (mouseListener = createMouseListener()) != null ) {
0N/A tree.addMouseListener(mouseListener);
0N/A if (mouseListener instanceof MouseMotionListener) {
0N/A tree.addMouseMotionListener((MouseMotionListener)mouseListener);
0N/A }
0N/A }
0N/A if ((focusListener = createFocusListener()) != null ) {
0N/A tree.addFocusListener(focusListener);
0N/A }
0N/A if ((keyListener = createKeyListener()) != null) {
0N/A tree.addKeyListener(keyListener);
0N/A }
0N/A if((treeExpansionListener = createTreeExpansionListener()) != null) {
0N/A tree.addTreeExpansionListener(treeExpansionListener);
0N/A }
0N/A if((treeModelListener = createTreeModelListener()) != null &&
0N/A treeModel != null) {
0N/A treeModel.addTreeModelListener(treeModelListener);
0N/A }
0N/A if((selectionModelPropertyChangeListener =
0N/A createSelectionModelPropertyChangeListener()) != null &&
0N/A treeSelectionModel != null) {
0N/A treeSelectionModel.addPropertyChangeListener
0N/A (selectionModelPropertyChangeListener);
0N/A }
0N/A if((treeSelectionListener = createTreeSelectionListener()) != null &&
0N/A treeSelectionModel != null) {
0N/A treeSelectionModel.addTreeSelectionListener(treeSelectionListener);
0N/A }
0N/A
0N/A TransferHandler th = tree.getTransferHandler();
0N/A if (th == null || th instanceof UIResource) {
0N/A tree.setTransferHandler(defaultTransferHandler);
0N/A // default TransferHandler doesn't support drop
0N/A // so we don't want drop handling
0N/A if (tree.getDropTarget() instanceof UIResource) {
0N/A tree.setDropTarget(null);
0N/A }
0N/A }
0N/A
0N/A LookAndFeel.installProperty(tree, "opaque", Boolean.TRUE);
0N/A }
0N/A
0N/A protected void installKeyboardActions() {
0N/A InputMap km = getInputMap(JComponent.
0N/A WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
0N/A
0N/A SwingUtilities.replaceUIInputMap(tree, JComponent.
0N/A WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
0N/A km);
0N/A km = getInputMap(JComponent.WHEN_FOCUSED);
0N/A SwingUtilities.replaceUIInputMap(tree, JComponent.WHEN_FOCUSED, km);
0N/A
0N/A LazyActionMap.installLazyActionMap(tree, BasicTreeUI.class,
0N/A "Tree.actionMap");
0N/A }
0N/A
0N/A InputMap getInputMap(int condition) {
0N/A if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
0N/A return (InputMap)DefaultLookup.get(tree, this,
0N/A "Tree.ancestorInputMap");
0N/A }
0N/A else if (condition == JComponent.WHEN_FOCUSED) {
0N/A InputMap keyMap = (InputMap)DefaultLookup.get(tree, this,
0N/A "Tree.focusInputMap");
0N/A InputMap rtlKeyMap;
0N/A
0N/A if (tree.getComponentOrientation().isLeftToRight() ||
0N/A ((rtlKeyMap = (InputMap)DefaultLookup.get(tree, this,
0N/A "Tree.focusInputMap.RightToLeft")) == null)) {
0N/A return keyMap;
0N/A } else {
0N/A rtlKeyMap.setParent(keyMap);
0N/A return rtlKeyMap;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Intalls the subcomponents of the tree, which is the renderer pane.
0N/A */
0N/A protected void installComponents() {
0N/A if ((rendererPane = createCellRendererPane()) != null) {
0N/A tree.add( rendererPane );
0N/A }
0N/A }
0N/A
0N/A //
0N/A // Create methods.
0N/A //
0N/A
0N/A /**
0N/A * Creates an instance of NodeDimensions that is able to determine
0N/A * the size of a given node in the tree.
0N/A */
0N/A protected AbstractLayoutCache.NodeDimensions createNodeDimensions() {
0N/A return new NodeDimensionsHandler();
0N/A }
0N/A
0N/A /**
0N/A * Creates a listener that is responsible that updates the UI based on
0N/A * how the tree changes.
0N/A */
0N/A protected PropertyChangeListener createPropertyChangeListener() {
0N/A return getHandler();
0N/A }
0N/A
0N/A private Handler getHandler() {
0N/A if (handler == null) {
0N/A handler = new Handler();
0N/A }
0N/A return handler;
0N/A }
0N/A
0N/A /**
0N/A * Creates the listener responsible for updating the selection based on
0N/A * mouse events.
0N/A */
0N/A protected MouseListener createMouseListener() {
0N/A return getHandler();
0N/A }
0N/A
0N/A /**
0N/A * Creates a listener that is responsible for updating the display
0N/A * when focus is lost/gained.
0N/A */
0N/A protected FocusListener createFocusListener() {
0N/A return getHandler();
0N/A }
0N/A
0N/A /**
0N/A * Creates the listener reponsible for getting key events from
0N/A * the tree.
0N/A */
0N/A protected KeyListener createKeyListener() {
0N/A return getHandler();
0N/A }
0N/A
0N/A /**
0N/A * Creates the listener responsible for getting property change
0N/A * events from the selection model.
0N/A */
0N/A protected PropertyChangeListener createSelectionModelPropertyChangeListener() {
0N/A return getHandler();
0N/A }
0N/A
0N/A /**
0N/A * Creates the listener that updates the display based on selection change
0N/A * methods.
0N/A */
0N/A protected TreeSelectionListener createTreeSelectionListener() {
0N/A return getHandler();
0N/A }
0N/A
0N/A /**
0N/A * Creates a listener to handle events from the current editor.
0N/A */
0N/A protected CellEditorListener createCellEditorListener() {
0N/A return getHandler();
0N/A }
0N/A
0N/A /**
0N/A * Creates and returns a new ComponentHandler. This is used for
0N/A * the large model to mark the validCachedPreferredSize as invalid
0N/A * when the component moves.
0N/A */
0N/A protected ComponentListener createComponentListener() {
0N/A return new ComponentHandler();
0N/A }
0N/A
0N/A /**
0N/A * Creates and returns the object responsible for updating the treestate
0N/A * when nodes expanded state changes.
0N/A */
0N/A protected TreeExpansionListener createTreeExpansionListener() {
0N/A return getHandler();
0N/A }
0N/A
0N/A /**
0N/A * Creates the object responsible for managing what is expanded, as
0N/A * well as the size of nodes.
0N/A */
0N/A protected AbstractLayoutCache createLayoutCache() {
0N/A if(isLargeModel() && getRowHeight() > 0) {
0N/A return new FixedHeightLayoutCache();
0N/A }
0N/A return new VariableHeightLayoutCache();
0N/A }
0N/A
0N/A /**
0N/A * Returns the renderer pane that renderer components are placed in.
0N/A */
0N/A protected CellRendererPane createCellRendererPane() {
0N/A return new CellRendererPane();
0N/A }
0N/A
0N/A /**
0N/A * Creates a default cell editor.
0N/A */
0N/A protected TreeCellEditor createDefaultCellEditor() {
0N/A if(currentCellRenderer != null &&
0N/A (currentCellRenderer instanceof DefaultTreeCellRenderer)) {
0N/A DefaultTreeCellEditor editor = new DefaultTreeCellEditor
0N/A (tree, (DefaultTreeCellRenderer)currentCellRenderer);
0N/A
0N/A return editor;
0N/A }
0N/A return new DefaultTreeCellEditor(tree, null);
0N/A }
0N/A
0N/A /**
0N/A * Returns the default cell renderer that is used to do the
0N/A * stamping of each node.
0N/A */
0N/A protected TreeCellRenderer createDefaultCellRenderer() {
0N/A return new DefaultTreeCellRenderer();
0N/A }
0N/A
0N/A /**
0N/A * Returns a listener that can update the tree when the model changes.
0N/A */
0N/A protected TreeModelListener createTreeModelListener() {
0N/A return getHandler();
0N/A }
0N/A
0N/A //
0N/A // Uninstall methods
0N/A //
0N/A
0N/A public void uninstallUI(JComponent c) {
0N/A completeEditing();
0N/A
0N/A prepareForUIUninstall();
0N/A
0N/A uninstallDefaults();
0N/A uninstallListeners();
0N/A uninstallKeyboardActions();
0N/A uninstallComponents();
0N/A
0N/A completeUIUninstall();
0N/A }
0N/A
0N/A protected void prepareForUIUninstall() {
0N/A }
0N/A
0N/A protected void completeUIUninstall() {
0N/A if(createdRenderer) {
0N/A tree.setCellRenderer(null);
0N/A }
0N/A if(createdCellEditor) {
0N/A tree.setCellEditor(null);
0N/A }
0N/A cellEditor = null;
0N/A currentCellRenderer = null;
0N/A rendererPane = null;
0N/A componentListener = null;
0N/A propertyChangeListener = null;
0N/A mouseListener = null;
0N/A focusListener = null;
0N/A keyListener = null;
0N/A setSelectionModel(null);
0N/A treeState = null;
0N/A drawingCache = null;
0N/A selectionModelPropertyChangeListener = null;
0N/A tree = null;
0N/A treeModel = null;
0N/A treeSelectionModel = null;
0N/A treeSelectionListener = null;
0N/A treeExpansionListener = null;
0N/A }
0N/A
0N/A protected void uninstallDefaults() {
0N/A if (tree.getTransferHandler() instanceof UIResource) {
0N/A tree.setTransferHandler(null);
0N/A }
0N/A }
0N/A
0N/A protected void uninstallListeners() {
0N/A if(componentListener != null) {
0N/A tree.removeComponentListener(componentListener);
0N/A }
0N/A if (propertyChangeListener != null) {
0N/A tree.removePropertyChangeListener(propertyChangeListener);
0N/A }
0N/A if (mouseListener != null) {
0N/A tree.removeMouseListener(mouseListener);
0N/A if (mouseListener instanceof MouseMotionListener) {
0N/A tree.removeMouseMotionListener((MouseMotionListener)mouseListener);
0N/A }
0N/A }
0N/A if (focusListener != null) {
0N/A tree.removeFocusListener(focusListener);
0N/A }
0N/A if (keyListener != null) {
0N/A tree.removeKeyListener(keyListener);
0N/A }
0N/A if(treeExpansionListener != null) {
0N/A tree.removeTreeExpansionListener(treeExpansionListener);
0N/A }
0N/A if(treeModel != null && treeModelListener != null) {
0N/A treeModel.removeTreeModelListener(treeModelListener);
0N/A }
0N/A if(selectionModelPropertyChangeListener != null &&
0N/A treeSelectionModel != null) {
0N/A treeSelectionModel.removePropertyChangeListener
0N/A (selectionModelPropertyChangeListener);
0N/A }
0N/A if(treeSelectionListener != null && treeSelectionModel != null) {
0N/A treeSelectionModel.removeTreeSelectionListener
0N/A (treeSelectionListener);
0N/A }
0N/A handler = null;
0N/A }
0N/A
0N/A protected void uninstallKeyboardActions() {
0N/A SwingUtilities.replaceUIActionMap(tree, null);
0N/A SwingUtilities.replaceUIInputMap(tree, JComponent.
0N/A WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
0N/A null);
0N/A SwingUtilities.replaceUIInputMap(tree, JComponent.WHEN_FOCUSED, null);
0N/A }
0N/A
0N/A /**
0N/A * Uninstalls the renderer pane.
0N/A */
0N/A protected void uninstallComponents() {
0N/A if(rendererPane != null) {
0N/A tree.remove(rendererPane);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Recomputes the right margin, and invalidates any tree states
0N/A */
0N/A private void redoTheLayout() {
0N/A if (treeState != null) {
0N/A treeState.invalidateSizes();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the baseline.
0N/A *
0N/A * @throws NullPointerException {@inheritDoc}
0N/A * @throws IllegalArgumentException {@inheritDoc}
0N/A * @see javax.swing.JComponent#getBaseline(int, int)
0N/A * @since 1.6
0N/A */
0N/A public int getBaseline(JComponent c, int width, int height) {
0N/A super.getBaseline(c, width, height);
0N/A UIDefaults lafDefaults = UIManager.getLookAndFeelDefaults();
0N/A Component renderer = (Component)lafDefaults.get(
0N/A BASELINE_COMPONENT_KEY);
0N/A if (renderer == null) {
0N/A TreeCellRenderer tcr = createDefaultCellRenderer();
0N/A renderer = tcr.getTreeCellRendererComponent(
0N/A tree, "a", false, false, false, -1, false);
0N/A lafDefaults.put(BASELINE_COMPONENT_KEY, renderer);
0N/A }
0N/A int rowHeight = tree.getRowHeight();
0N/A int baseline;
0N/A if (rowHeight > 0) {
0N/A baseline = renderer.getBaseline(Integer.MAX_VALUE, rowHeight);
0N/A }
0N/A else {
0N/A Dimension pref = renderer.getPreferredSize();
0N/A baseline = renderer.getBaseline(pref.width, pref.height);
0N/A }
0N/A return baseline + tree.getInsets().top;
0N/A }
0N/A
0N/A /**
0N/A * Returns an enum indicating how the baseline of the component
0N/A * changes as the size changes.
0N/A *
0N/A * @throws NullPointerException {@inheritDoc}
0N/A * @see javax.swing.JComponent#getBaseline(int, int)
0N/A * @since 1.6
0N/A */
0N/A public Component.BaselineResizeBehavior getBaselineResizeBehavior(
0N/A JComponent c) {
0N/A super.getBaselineResizeBehavior(c);
0N/A return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
0N/A }
0N/A
0N/A //
0N/A // Painting routines.
0N/A //
0N/A
0N/A public void paint(Graphics g, JComponent c) {
0N/A if (tree != c) {
0N/A throw new InternalError("incorrect component");
0N/A }
0N/A
0N/A // Should never happen if installed for a UI
0N/A if(treeState == null) {
0N/A return;
0N/A }
0N/A
0N/A Rectangle paintBounds = g.getClipBounds();
0N/A Insets insets = tree.getInsets();
0N/A TreePath initialPath = getClosestPathForLocation
0N/A (tree, 0, paintBounds.y);
0N/A Enumeration paintingEnumerator = treeState.getVisiblePathsFrom
0N/A (initialPath);
0N/A int row = treeState.getRowForPath(initialPath);
0N/A int endY = paintBounds.y + paintBounds.height;
0N/A
0N/A drawingCache.clear();
0N/A
0N/A if(initialPath != null && paintingEnumerator != null) {
0N/A TreePath parentPath = initialPath;
0N/A
0N/A // Draw the lines, knobs, and rows
0N/A
0N/A // Find each parent and have them draw a line to their last child
0N/A parentPath = parentPath.getParentPath();
0N/A while(parentPath != null) {
0N/A paintVerticalPartOfLeg(g, paintBounds, insets, parentPath);
0N/A drawingCache.put(parentPath, Boolean.TRUE);
0N/A parentPath = parentPath.getParentPath();
0N/A }
0N/A
0N/A boolean done = false;
0N/A // Information for the node being rendered.
0N/A boolean isExpanded;
0N/A boolean hasBeenExpanded;
0N/A boolean isLeaf;
0N/A Rectangle boundsBuffer = new Rectangle();
0N/A Rectangle bounds;
0N/A TreePath path;
0N/A boolean rootVisible = isRootVisible();
0N/A
0N/A while(!done && paintingEnumerator.hasMoreElements()) {
0N/A path = (TreePath)paintingEnumerator.nextElement();
0N/A if(path != null) {
0N/A isLeaf = treeModel.isLeaf(path.getLastPathComponent());
0N/A if(isLeaf)
0N/A isExpanded = hasBeenExpanded = false;
0N/A else {
0N/A isExpanded = treeState.getExpandedState(path);
0N/A hasBeenExpanded = tree.hasBeenExpanded(path);
0N/A }
0N/A bounds = getPathBounds(path, insets, boundsBuffer);
0N/A if(bounds == null)
0N/A // This will only happen if the model changes out
0N/A // from under us (usually in another thread).
0N/A // Swing isn't multithreaded, but I'll put this
0N/A // check in anyway.
0N/A return;
0N/A // See if the vertical line to the parent has been drawn.
0N/A parentPath = path.getParentPath();
0N/A if(parentPath != null) {
0N/A if(drawingCache.get(parentPath) == null) {
0N/A paintVerticalPartOfLeg(g, paintBounds,
0N/A insets, parentPath);
0N/A drawingCache.put(parentPath, Boolean.TRUE);
0N/A }
0N/A paintHorizontalPartOfLeg(g, paintBounds, insets,
0N/A bounds, path, row,
0N/A isExpanded,
0N/A hasBeenExpanded, isLeaf);
0N/A }
0N/A else if(rootVisible && row == 0) {
0N/A paintHorizontalPartOfLeg(g, paintBounds, insets,
0N/A bounds, path, row,
0N/A isExpanded,
0N/A hasBeenExpanded, isLeaf);
0N/A }
0N/A if(shouldPaintExpandControl(path, row, isExpanded,
0N/A hasBeenExpanded, isLeaf)) {
0N/A paintExpandControl(g, paintBounds, insets, bounds,
0N/A path, row, isExpanded,
0N/A hasBeenExpanded, isLeaf);
0N/A }
0N/A paintRow(g, paintBounds, insets, bounds, path,
0N/A row, isExpanded, hasBeenExpanded, isLeaf);
0N/A if((bounds.y + bounds.height) >= endY)
0N/A done = true;
0N/A }
0N/A else {
0N/A done = true;
0N/A }
0N/A row++;
0N/A }
0N/A }
0N/A
0N/A paintDropLine(g);
0N/A
0N/A // Empty out the renderer pane, allowing renderers to be gc'ed.
0N/A rendererPane.removeAll();
0N/A
0N/A drawingCache.clear();
0N/A }
0N/A
1999N/A /**
1999N/A * Tells if a {@code DropLocation} should be indicated by a line between
1999N/A * nodes. This is meant for {@code javax.swing.DropMode.INSERT} and
1999N/A * {@code javax.swing.DropMode.ON_OR_INSERT} drop modes.
1999N/A *
1999N/A * @param loc a {@code DropLocation}
1999N/A * @return {@code true} if the drop location should be shown as a line
1999N/A * @since 1.7
1999N/A */
1999N/A protected boolean isDropLine(JTree.DropLocation loc) {
0N/A return loc != null && loc.getPath() != null && loc.getChildIndex() != -1;
0N/A }
0N/A
1999N/A /**
1999N/A * Paints the drop line.
1999N/A *
1999N/A * @param g {@code Graphics} object to draw on
1999N/A * @since 1.7
1999N/A */
1999N/A protected void paintDropLine(Graphics g) {
0N/A JTree.DropLocation loc = tree.getDropLocation();
0N/A if (!isDropLine(loc)) {
0N/A return;
0N/A }
0N/A
0N/A Color c = UIManager.getColor("Tree.dropLineColor");
0N/A if (c != null) {
0N/A g.setColor(c);
0N/A Rectangle rect = getDropLineRect(loc);
0N/A g.fillRect(rect.x, rect.y, rect.width, rect.height);
0N/A }
0N/A }
0N/A
1999N/A /**
1999N/A * Returns a ubounding box for the drop line.
1999N/A *
1999N/A * @param loc a {@code DropLocation}
1999N/A * @return bounding box for the drop line
1999N/A * @since 1.7
1999N/A */
1999N/A protected Rectangle getDropLineRect(JTree.DropLocation loc) {
614N/A Rectangle rect;
0N/A TreePath path = loc.getPath();
0N/A int index = loc.getChildIndex();
0N/A boolean ltr = leftToRight;
0N/A
0N/A Insets insets = tree.getInsets();
0N/A
0N/A if (tree.getRowCount() == 0) {
0N/A rect = new Rectangle(insets.left,
0N/A insets.top,
0N/A tree.getWidth() - insets.left - insets.right,
0N/A 0);
0N/A } else {
0N/A TreeModel model = getModel();
0N/A Object root = model.getRoot();
0N/A
0N/A if (path.getLastPathComponent() == root
0N/A && index >= model.getChildCount(root)) {
0N/A
0N/A rect = tree.getRowBounds(tree.getRowCount() - 1);
0N/A rect.y = rect.y + rect.height;
0N/A Rectangle xRect;
0N/A
0N/A if (!tree.isRootVisible()) {
0N/A xRect = tree.getRowBounds(0);
0N/A } else if (model.getChildCount(root) == 0){
0N/A xRect = tree.getRowBounds(0);
0N/A xRect.x += totalChildIndent;
0N/A xRect.width -= totalChildIndent + totalChildIndent;
0N/A } else {
0N/A TreePath lastChildPath = path.pathByAddingChild(
0N/A model.getChild(root, model.getChildCount(root) - 1));
0N/A xRect = tree.getPathBounds(lastChildPath);
0N/A }
0N/A
0N/A rect.x = xRect.x;
0N/A rect.width = xRect.width;
0N/A } else {
0N/A rect = tree.getPathBounds(path.pathByAddingChild(
0N/A model.getChild(path.getLastPathComponent(), index)));
0N/A }
0N/A }
0N/A
0N/A if (rect.y != 0) {
0N/A rect.y--;
0N/A }
0N/A
0N/A if (!ltr) {
0N/A rect.x = rect.x + rect.width - 100;
0N/A }
0N/A
0N/A rect.width = 100;
0N/A rect.height = 2;
0N/A
0N/A return rect;
0N/A }
0N/A
0N/A /**
0N/A * Paints the horizontal part of the leg. The receiver should
0N/A * NOT modify <code>clipBounds</code>, or <code>insets</code>.<p>
0N/A * NOTE: <code>parentRow</code> can be -1 if the root is not visible.
0N/A */
0N/A protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds,
0N/A Insets insets, Rectangle bounds,
0N/A TreePath path, int row,
0N/A boolean isExpanded,
0N/A boolean hasBeenExpanded, boolean
0N/A isLeaf) {
0N/A if (!paintLines) {
0N/A return;
0N/A }
0N/A
0N/A // Don't paint the legs for the root'ish node if the
0N/A int depth = path.getPathCount() - 1;
0N/A if((depth == 0 || (depth == 1 && !isRootVisible())) &&
0N/A !getShowsRootHandles()) {
0N/A return;
0N/A }
0N/A
0N/A int clipLeft = clipBounds.x;
0N/A int clipRight = clipBounds.x + clipBounds.width;
0N/A int clipTop = clipBounds.y;
0N/A int clipBottom = clipBounds.y + clipBounds.height;
0N/A int lineY = bounds.y + bounds.height / 2;
0N/A
0N/A if (leftToRight) {
0N/A int leftX = bounds.x - getRightChildIndent();
0N/A int nodeX = bounds.x - getHorizontalLegBuffer();
0N/A
0N/A if(lineY >= clipTop
0N/A && lineY < clipBottom
0N/A && nodeX >= clipLeft
0N/A && leftX < clipRight
0N/A && leftX < nodeX) {
0N/A
0N/A g.setColor(getHashColor());
0N/A paintHorizontalLine(g, tree, lineY, leftX, nodeX - 1);
0N/A }
0N/A } else {
0N/A int nodeX = bounds.x + bounds.width + getHorizontalLegBuffer();
0N/A int rightX = bounds.x + bounds.width + getRightChildIndent();
0N/A
0N/A if(lineY >= clipTop
0N/A && lineY < clipBottom
0N/A && rightX >= clipLeft
0N/A && nodeX < clipRight
0N/A && nodeX < rightX) {
0N/A
0N/A g.setColor(getHashColor());
0N/A paintHorizontalLine(g, tree, lineY, nodeX, rightX - 1);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Paints the vertical part of the leg. The receiver should
0N/A * NOT modify <code>clipBounds</code>, <code>insets</code>.<p>
0N/A */
0N/A protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds,
0N/A Insets insets, TreePath path) {
0N/A if (!paintLines) {
0N/A return;
0N/A }
0N/A
0N/A int depth = path.getPathCount() - 1;
0N/A if (depth == 0 && !getShowsRootHandles() && !isRootVisible()) {
0N/A return;
0N/A }
0N/A int lineX = getRowX(-1, depth + 1);
0N/A if (leftToRight) {
0N/A lineX = lineX - getRightChildIndent() + insets.left;
0N/A }
0N/A else {
0N/A lineX = tree.getWidth() - lineX - insets.right +
0N/A getRightChildIndent() - 1;
0N/A }
0N/A int clipLeft = clipBounds.x;
0N/A int clipRight = clipBounds.x + (clipBounds.width - 1);
0N/A
0N/A if (lineX >= clipLeft && lineX <= clipRight) {
0N/A int clipTop = clipBounds.y;
0N/A int clipBottom = clipBounds.y + clipBounds.height;
0N/A Rectangle parentBounds = getPathBounds(tree, path);
0N/A Rectangle lastChildBounds = getPathBounds(tree,
0N/A getLastChildPath(path));
0N/A
0N/A if(lastChildBounds == null)
0N/A // This shouldn't happen, but if the model is modified
0N/A // in another thread it is possible for this to happen.
0N/A // Swing isn't multithreaded, but I'll add this check in
0N/A // anyway.
0N/A return;
0N/A
0N/A int top;
0N/A
0N/A if(parentBounds == null) {
0N/A top = Math.max(insets.top + getVerticalLegBuffer(),
0N/A clipTop);
0N/A }
0N/A else
0N/A top = Math.max(parentBounds.y + parentBounds.height +
0N/A getVerticalLegBuffer(), clipTop);
0N/A if(depth == 0 && !isRootVisible()) {
0N/A TreeModel model = getModel();
0N/A
0N/A if(model != null) {
0N/A Object root = model.getRoot();
0N/A
0N/A if(model.getChildCount(root) > 0) {
0N/A parentBounds = getPathBounds(tree, path.
0N/A pathByAddingChild(model.getChild(root, 0)));
0N/A if(parentBounds != null)
0N/A top = Math.max(insets.top + getVerticalLegBuffer(),
0N/A parentBounds.y +
0N/A parentBounds.height / 2);
0N/A }
0N/A }
0N/A }
0N/A
0N/A int bottom = Math.min(lastChildBounds.y +
0N/A (lastChildBounds.height / 2), clipBottom);
0N/A
0N/A if (top <= bottom) {
0N/A g.setColor(getHashColor());
0N/A paintVerticalLine(g, tree, lineX, top, bottom);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Paints the expand (toggle) part of a row. The receiver should
0N/A * NOT modify <code>clipBounds</code>, or <code>insets</code>.
0N/A */
0N/A protected void paintExpandControl(Graphics g,
0N/A Rectangle clipBounds, Insets insets,
0N/A Rectangle bounds, TreePath path,
0N/A int row, boolean isExpanded,
0N/A boolean hasBeenExpanded,
0N/A boolean isLeaf) {
0N/A Object value = path.getLastPathComponent();
0N/A
0N/A // Draw icons if not a leaf and either hasn't been loaded,
0N/A // or the model child count is > 0.
0N/A if (!isLeaf && (!hasBeenExpanded ||
0N/A treeModel.getChildCount(value) > 0)) {
0N/A int middleXOfKnob;
0N/A if (leftToRight) {
0N/A middleXOfKnob = bounds.x - getRightChildIndent() + 1;
0N/A } else {
0N/A middleXOfKnob = bounds.x + bounds.width + getRightChildIndent() - 1;
0N/A }
0N/A int middleYOfKnob = bounds.y + (bounds.height / 2);
0N/A
0N/A if (isExpanded) {
0N/A Icon expandedIcon = getExpandedIcon();
0N/A if(expandedIcon != null)
0N/A drawCentered(tree, g, expandedIcon, middleXOfKnob,
0N/A middleYOfKnob );
0N/A }
0N/A else {
0N/A Icon collapsedIcon = getCollapsedIcon();
0N/A if(collapsedIcon != null)
0N/A drawCentered(tree, g, collapsedIcon, middleXOfKnob,
0N/A middleYOfKnob);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Paints the renderer part of a row. The receiver should
0N/A * NOT modify <code>clipBounds</code>, or <code>insets</code>.
0N/A */
0N/A protected void paintRow(Graphics g, Rectangle clipBounds,
0N/A Insets insets, Rectangle bounds, TreePath path,
0N/A int row, boolean isExpanded,
0N/A boolean hasBeenExpanded, boolean isLeaf) {
0N/A // Don't paint the renderer if editing this row.
0N/A if(editingComponent != null && editingRow == row)
0N/A return;
0N/A
0N/A int leadIndex;
0N/A
0N/A if(tree.hasFocus()) {
0N/A leadIndex = getLeadSelectionRow();
0N/A }
0N/A else
0N/A leadIndex = -1;
0N/A
0N/A Component component;
0N/A
0N/A component = currentCellRenderer.getTreeCellRendererComponent
0N/A (tree, path.getLastPathComponent(),
0N/A tree.isRowSelected(row), isExpanded, isLeaf, row,
0N/A (leadIndex == row));
0N/A
0N/A rendererPane.paintComponent(g, component, tree, bounds.x, bounds.y,
0N/A bounds.width, bounds.height, true);
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the expand (toggle) control should be drawn for
0N/A * the specified row.
0N/A */
0N/A protected boolean shouldPaintExpandControl(TreePath path, int row,
0N/A boolean isExpanded,
0N/A boolean hasBeenExpanded,
0N/A boolean isLeaf) {
0N/A if(isLeaf)
0N/A return false;
0N/A
0N/A int depth = path.getPathCount() - 1;
0N/A
0N/A if((depth == 0 || (depth == 1 && !isRootVisible())) &&
0N/A !getShowsRootHandles())
0N/A return false;
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Paints a vertical line.
0N/A */
0N/A protected void paintVerticalLine(Graphics g, JComponent c, int x, int top,
0N/A int bottom) {
0N/A if (lineTypeDashed) {
0N/A drawDashedVerticalLine(g, x, top, bottom);
0N/A } else {
0N/A g.drawLine(x, top, x, bottom);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Paints a horizontal line.
0N/A */
0N/A protected void paintHorizontalLine(Graphics g, JComponent c, int y,
0N/A int left, int right) {
0N/A if (lineTypeDashed) {
0N/A drawDashedHorizontalLine(g, y, left, right);
0N/A } else {
0N/A g.drawLine(left, y, right, y);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The vertical element of legs between nodes starts at the bottom of the
0N/A * parent node by default. This method makes the leg start below that.
0N/A */
0N/A protected int getVerticalLegBuffer() {
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * The horizontal element of legs between nodes starts at the
0N/A * right of the left-hand side of the child node by default. This
0N/A * method makes the leg end before that.
0N/A */
0N/A protected int getHorizontalLegBuffer() {
0N/A return 0;
0N/A }
0N/A
0N/A private int findCenteredX(int x, int iconWidth) {
0N/A return leftToRight
0N/A ? x - (int)Math.ceil(iconWidth / 2.0)
0N/A : x - (int)Math.floor(iconWidth / 2.0);
0N/A }
0N/A
0N/A //
0N/A // Generic painting methods
0N/A //
0N/A
0N/A // Draws the icon centered at (x,y)
0N/A protected void drawCentered(Component c, Graphics graphics, Icon icon,
0N/A int x, int y) {
0N/A icon.paintIcon(c, graphics,
0N/A findCenteredX(x, icon.getIconWidth()),
0N/A y - icon.getIconHeight() / 2);
0N/A }
0N/A
0N/A // This method is slow -- revisit when Java2D is ready.
0N/A // assumes x1 <= x2
0N/A protected void drawDashedHorizontalLine(Graphics g, int y, int x1, int x2){
0N/A // Drawing only even coordinates helps join line segments so they
0N/A // appear as one line. This can be defeated by translating the
0N/A // Graphics by an odd amount.
0N/A x1 += (x1 % 2);
0N/A
0N/A for (int x = x1; x <= x2; x+=2) {
0N/A g.drawLine(x, y, x, y);
0N/A }
0N/A }
0N/A
0N/A // This method is slow -- revisit when Java2D is ready.
0N/A // assumes y1 <= y2
0N/A protected void drawDashedVerticalLine(Graphics g, int x, int y1, int y2) {
0N/A // Drawing only even coordinates helps join line segments so they
0N/A // appear as one line. This can be defeated by translating the
0N/A // Graphics by an odd amount.
0N/A y1 += (y1 % 2);
0N/A
0N/A for (int y = y1; y <= y2; y+=2) {
0N/A g.drawLine(x, y, x, y);
0N/A }
0N/A }
0N/A
0N/A //
0N/A // Various local methods
0N/A //
0N/A
0N/A /**
0N/A * Returns the location, along the x-axis, to render a particular row
0N/A * at. The return value does not include any Insets specified on the JTree.
0N/A * This does not check for the validity of the row or depth, it is assumed
0N/A * to be correct and will not throw an Exception if the row or depth
0N/A * doesn't match that of the tree.
0N/A *
0N/A * @param row Row to return x location for
0N/A * @param depth Depth of the row
0N/A * @return amount to indent the given row.
0N/A * @since 1.5
0N/A */
0N/A protected int getRowX(int row, int depth) {
0N/A return totalChildIndent * (depth + depthOffset);
0N/A }
0N/A
0N/A /**
0N/A * Makes all the nodes that are expanded in JTree expanded in LayoutCache.
0N/A * This invokes updateExpandedDescendants with the root path.
0N/A */
0N/A protected void updateLayoutCacheExpandedNodes() {
0N/A if(treeModel != null && treeModel.getRoot() != null)
0N/A updateExpandedDescendants(new TreePath(treeModel.getRoot()));
0N/A }
0N/A
0N/A private void updateLayoutCacheExpandedNodesIfNecessary() {
0N/A if (treeModel != null && treeModel.getRoot() != null) {
0N/A TreePath rootPath = new TreePath(treeModel.getRoot());
0N/A if (tree.isExpanded(rootPath)) {
0N/A updateLayoutCacheExpandedNodes();
0N/A } else {
0N/A treeState.setExpandedState(rootPath, false);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Updates the expanded state of all the descendants of <code>path</code>
0N/A * by getting the expanded descendants from the tree and forwarding
0N/A * to the tree state.
0N/A */
0N/A protected void updateExpandedDescendants(TreePath path) {
0N/A completeEditing();
0N/A if(treeState != null) {
0N/A treeState.setExpandedState(path, true);
0N/A
0N/A Enumeration descendants = tree.getExpandedDescendants(path);
0N/A
0N/A if(descendants != null) {
0N/A while(descendants.hasMoreElements()) {
0N/A path = (TreePath)descendants.nextElement();
0N/A treeState.setExpandedState(path, true);
0N/A }
0N/A }
1999N/A updateLeadSelectionRow();
0N/A updateSize();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a path to the last child of <code>parent</code>.
0N/A */
0N/A protected TreePath getLastChildPath(TreePath parent) {
0N/A if(treeModel != null) {
0N/A int childCount = treeModel.getChildCount
0N/A (parent.getLastPathComponent());
0N/A
0N/A if(childCount > 0)
0N/A return parent.pathByAddingChild(treeModel.getChild
0N/A (parent.getLastPathComponent(), childCount - 1));
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Updates how much each depth should be offset by.
0N/A */
0N/A protected void updateDepthOffset() {
0N/A if(isRootVisible()) {
0N/A if(getShowsRootHandles())
0N/A depthOffset = 1;
0N/A else
0N/A depthOffset = 0;
0N/A }
0N/A else if(!getShowsRootHandles())
0N/A depthOffset = -1;
0N/A else
0N/A depthOffset = 0;
0N/A }
0N/A
0N/A /**
0N/A * Updates the cellEditor based on the editability of the JTree that
0N/A * we're contained in. If the tree is editable but doesn't have a
0N/A * cellEditor, a basic one will be used.
0N/A */
0N/A protected void updateCellEditor() {
0N/A TreeCellEditor newEditor;
0N/A
0N/A completeEditing();
0N/A if(tree == null)
0N/A newEditor = null;
0N/A else {
0N/A if(tree.isEditable()) {
0N/A newEditor = tree.getCellEditor();
0N/A if(newEditor == null) {
0N/A newEditor = createDefaultCellEditor();
0N/A if(newEditor != null) {
0N/A tree.setCellEditor(newEditor);
0N/A createdCellEditor = true;
0N/A }
0N/A }
0N/A }
0N/A else
0N/A newEditor = null;
0N/A }
0N/A if(newEditor != cellEditor) {
0N/A if(cellEditor != null && cellEditorListener != null)
0N/A cellEditor.removeCellEditorListener(cellEditorListener);
0N/A cellEditor = newEditor;
0N/A if(cellEditorListener == null)
0N/A cellEditorListener = createCellEditorListener();
0N/A if(newEditor != null && cellEditorListener != null)
0N/A newEditor.addCellEditorListener(cellEditorListener);
0N/A createdCellEditor = false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Messaged from the tree we're in when the renderer has changed.
0N/A */
0N/A protected void updateRenderer() {
0N/A if(tree != null) {
0N/A TreeCellRenderer newCellRenderer;
0N/A
0N/A newCellRenderer = tree.getCellRenderer();
0N/A if(newCellRenderer == null) {
0N/A tree.setCellRenderer(createDefaultCellRenderer());
0N/A createdRenderer = true;
0N/A }
0N/A else {
0N/A createdRenderer = false;
0N/A currentCellRenderer = newCellRenderer;
0N/A if(createdCellEditor) {
0N/A tree.setCellEditor(null);
0N/A }
0N/A }
0N/A }
0N/A else {
0N/A createdRenderer = false;
0N/A currentCellRenderer = null;
0N/A }
0N/A updateCellEditor();
0N/A }
0N/A
0N/A /**
0N/A * Resets the TreeState instance based on the tree we're providing the
0N/A * look and feel for.
0N/A */
0N/A protected void configureLayoutCache() {
0N/A if(treeState != null && tree != null) {
0N/A if(nodeDimensions == null)
0N/A nodeDimensions = createNodeDimensions();
0N/A treeState.setNodeDimensions(nodeDimensions);
0N/A treeState.setRootVisible(tree.isRootVisible());
0N/A treeState.setRowHeight(tree.getRowHeight());
0N/A treeState.setSelectionModel(getSelectionModel());
0N/A // Only do this if necessary, may loss state if call with
0N/A // same model as it currently has.
0N/A if(treeState.getModel() != tree.getModel())
0N/A treeState.setModel(tree.getModel());
0N/A updateLayoutCacheExpandedNodesIfNecessary();
0N/A // Create a listener to update preferred size when bounds
0N/A // changes, if necessary.
0N/A if(isLargeModel()) {
0N/A if(componentListener == null) {
0N/A componentListener = createComponentListener();
0N/A if(componentListener != null)
0N/A tree.addComponentListener(componentListener);
0N/A }
0N/A }
0N/A else if(componentListener != null) {
0N/A tree.removeComponentListener(componentListener);
0N/A componentListener = null;
0N/A }
0N/A }
0N/A else if(componentListener != null) {
0N/A tree.removeComponentListener(componentListener);
0N/A componentListener = null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Marks the cached size as being invalid, and messages the
0N/A * tree with <code>treeDidChange</code>.
0N/A */
0N/A protected void updateSize() {
0N/A validCachedPreferredSize = false;
0N/A tree.treeDidChange();
0N/A }
0N/A
0N/A private void updateSize0() {
0N/A validCachedPreferredSize = false;
0N/A tree.revalidate();
0N/A }
0N/A
0N/A /**
0N/A * Updates the <code>preferredSize</code> instance variable,
0N/A * which is returned from <code>getPreferredSize()</code>.<p>
0N/A * For left to right orientations, the size is determined from the
0N/A * current AbstractLayoutCache. For RTL orientations, the preferred size
0N/A * becomes the width minus the minimum x position.
0N/A */
0N/A protected void updateCachedPreferredSize() {
0N/A if(treeState != null) {
0N/A Insets i = tree.getInsets();
0N/A
0N/A if(isLargeModel()) {
0N/A Rectangle visRect = tree.getVisibleRect();
0N/A
0N/A if (visRect.x == 0 && visRect.y == 0 &&
0N/A visRect.width == 0 && visRect.height == 0 &&
0N/A tree.getVisibleRowCount() > 0) {
0N/A // The tree doesn't have a valid bounds yet. Calculate
0N/A // based on visible row count.
0N/A visRect.width = 1;
0N/A visRect.height = tree.getRowHeight() *
0N/A tree.getVisibleRowCount();
0N/A } else {
0N/A visRect.x -= i.left;
0N/A visRect.y -= i.top;
0N/A }
5659N/A // we should consider a non-visible area above
5659N/A Component component = SwingUtilities.getUnwrappedParent(tree);
5659N/A if (component instanceof JViewport) {
5659N/A component = component.getParent();
5659N/A if (component instanceof JScrollPane) {
5659N/A JScrollPane pane = (JScrollPane) component;
5659N/A JScrollBar bar = pane.getHorizontalScrollBar();
5659N/A if ((bar != null) && bar.isVisible()) {
5659N/A int height = bar.getHeight();
5659N/A visRect.y -= height;
5659N/A visRect.height += height;
5659N/A }
5659N/A }
5659N/A }
0N/A preferredSize.width = treeState.getPreferredWidth(visRect);
0N/A }
0N/A else {
0N/A preferredSize.width = treeState.getPreferredWidth(null);
0N/A }
0N/A preferredSize.height = treeState.getPreferredHeight();
0N/A preferredSize.width += i.left + i.right;
0N/A preferredSize.height += i.top + i.bottom;
0N/A }
0N/A validCachedPreferredSize = true;
0N/A }
0N/A
0N/A /**
0N/A * Messaged from the VisibleTreeNode after it has been expanded.
0N/A */
0N/A protected void pathWasExpanded(TreePath path) {
0N/A if(tree != null) {
0N/A tree.fireTreeExpanded(path);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Messaged from the VisibleTreeNode after it has collapsed.
0N/A */
0N/A protected void pathWasCollapsed(TreePath path) {
0N/A if(tree != null) {
0N/A tree.fireTreeCollapsed(path);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Ensures that the rows identified by beginRow through endRow are
0N/A * visible.
0N/A */
0N/A protected void ensureRowsAreVisible(int beginRow, int endRow) {
0N/A if(tree != null && beginRow >= 0 && endRow < getRowCount(tree)) {
0N/A boolean scrollVert = DefaultLookup.getBoolean(tree, this,
0N/A "Tree.scrollsHorizontallyAndVertically", false);
0N/A if(beginRow == endRow) {
0N/A Rectangle scrollBounds = getPathBounds(tree, getPathForRow
0N/A (tree, beginRow));
0N/A
0N/A if(scrollBounds != null) {
0N/A if (!scrollVert) {
0N/A scrollBounds.x = tree.getVisibleRect().x;
0N/A scrollBounds.width = 1;
0N/A }
0N/A tree.scrollRectToVisible(scrollBounds);
0N/A }
0N/A }
0N/A else {
0N/A Rectangle beginRect = getPathBounds(tree, getPathForRow
0N/A (tree, beginRow));
4568N/A if (beginRect != null) {
4568N/A Rectangle visRect = tree.getVisibleRect();
4568N/A Rectangle testRect = beginRect;
4568N/A int beginY = beginRect.y;
4568N/A int maxY = beginY + visRect.height;
4568N/A
4568N/A for(int counter = beginRow + 1; counter <= endRow; counter++) {
4568N/A testRect = getPathBounds(tree,
4568N/A getPathForRow(tree, counter));
5532N/A if (testRect == null) {
5532N/A return;
5532N/A }
4568N/A if((testRect.y + testRect.height) > maxY)
4568N/A counter = endRow;
4568N/A }
4568N/A tree.scrollRectToVisible(new Rectangle(visRect.x, beginY, 1,
4568N/A testRect.y + testRect.height-
4568N/A beginY));
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /** Sets the preferred minimum size.
0N/A */
0N/A public void setPreferredMinSize(Dimension newSize) {
0N/A preferredMinSize = newSize;
0N/A }
0N/A
0N/A /** Returns the minimum preferred size.
0N/A */
0N/A public Dimension getPreferredMinSize() {
0N/A if(preferredMinSize == null)
0N/A return null;
0N/A return new Dimension(preferredMinSize);
0N/A }
0N/A
0N/A /** Returns the preferred size to properly display the tree,
3557N/A * this is a cover method for getPreferredSize(c, true).
0N/A */
0N/A public Dimension getPreferredSize(JComponent c) {
0N/A return getPreferredSize(c, true);
0N/A }
0N/A
0N/A /** Returns the preferred size to represent the tree in
3557N/A * <I>c</I>. If <I>checkConsistency</I> is true
3557N/A * <b>checkConsistency</b> is messaged first.
0N/A */
0N/A public Dimension getPreferredSize(JComponent c,
3557N/A boolean checkConsistency) {
0N/A Dimension pSize = this.getPreferredMinSize();
0N/A
0N/A if(!validCachedPreferredSize)
0N/A updateCachedPreferredSize();
0N/A if(tree != null) {
0N/A if(pSize != null)
0N/A return new Dimension(Math.max(pSize.width,
0N/A preferredSize.width),
0N/A Math.max(pSize.height, preferredSize.height));
0N/A return new Dimension(preferredSize.width, preferredSize.height);
0N/A }
0N/A else if(pSize != null)
0N/A return pSize;
0N/A else
0N/A return new Dimension(0, 0);
0N/A }
0N/A
0N/A /**
0N/A * Returns the minimum size for this component. Which will be
0N/A * the min preferred size or 0, 0.
0N/A */
0N/A public Dimension getMinimumSize(JComponent c) {
0N/A if(this.getPreferredMinSize() != null)
0N/A return this.getPreferredMinSize();
0N/A return new Dimension(0, 0);
0N/A }
0N/A
0N/A /**
0N/A * Returns the maximum size for this component, which will be the
0N/A * preferred size if the instance is currently in a JTree, or 0, 0.
0N/A */
0N/A public Dimension getMaximumSize(JComponent c) {
0N/A if(tree != null)
0N/A return getPreferredSize(tree);
0N/A if(this.getPreferredMinSize() != null)
0N/A return this.getPreferredMinSize();
0N/A return new Dimension(0, 0);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Messages to stop the editing session. If the UI the receiver
0N/A * is providing the look and feel for returns true from
0N/A * <code>getInvokesStopCellEditing</code>, stopCellEditing will
0N/A * invoked on the current editor. Then completeEditing will
0N/A * be messaged with false, true, false to cancel any lingering
0N/A * editing.
0N/A */
0N/A protected void completeEditing() {
0N/A /* If should invoke stopCellEditing, try that */
0N/A if(tree.getInvokesStopCellEditing() &&
0N/A stopEditingInCompleteEditing && editingComponent != null) {
0N/A cellEditor.stopCellEditing();
0N/A }
0N/A /* Invoke cancelCellEditing, this will do nothing if stopCellEditing
0N/A was successful. */
0N/A completeEditing(false, true, false);
0N/A }
0N/A
0N/A /**
0N/A * Stops the editing session. If messageStop is true the editor
0N/A * is messaged with stopEditing, if messageCancel is true the
0N/A * editor is messaged with cancelEditing. If messageTree is true
0N/A * the treeModel is messaged with valueForPathChanged.
0N/A */
0N/A protected void completeEditing(boolean messageStop,
0N/A boolean messageCancel,
0N/A boolean messageTree) {
0N/A if(stopEditingInCompleteEditing && editingComponent != null) {
0N/A Component oldComponent = editingComponent;
0N/A TreePath oldPath = editingPath;
0N/A TreeCellEditor oldEditor = cellEditor;
0N/A Object newValue = oldEditor.getCellEditorValue();
0N/A Rectangle editingBounds = getPathBounds(tree,
0N/A editingPath);
0N/A boolean requestFocus = (tree != null &&
0N/A (tree.hasFocus() || SwingUtilities.
0N/A findFocusOwner(editingComponent) != null));
0N/A
0N/A editingComponent = null;
0N/A editingPath = null;
0N/A if(messageStop)
0N/A oldEditor.stopCellEditing();
0N/A else if(messageCancel)
0N/A oldEditor.cancelCellEditing();
0N/A tree.remove(oldComponent);
0N/A if(editorHasDifferentSize) {
0N/A treeState.invalidatePathBounds(oldPath);
0N/A updateSize();
0N/A }
5532N/A else if (editingBounds != null) {
0N/A editingBounds.x = 0;
0N/A editingBounds.width = tree.getSize().width;
0N/A tree.repaint(editingBounds);
0N/A }
0N/A if(requestFocus)
0N/A tree.requestFocus();
0N/A if(messageTree)
0N/A treeModel.valueForPathChanged(oldPath, newValue);
0N/A }
0N/A }
0N/A
0N/A // cover method for startEditing that allows us to pass extra
0N/A // information into that method via a class variable
0N/A private boolean startEditingOnRelease(TreePath path,
0N/A MouseEvent event,
0N/A MouseEvent releaseEvent) {
0N/A this.releaseEvent = releaseEvent;
0N/A try {
0N/A return startEditing(path, event);
0N/A } finally {
0N/A this.releaseEvent = null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Will start editing for node if there is a cellEditor and
0N/A * shouldSelectCell returns true.<p>
0N/A * This assumes that path is valid and visible.
0N/A */
0N/A protected boolean startEditing(TreePath path, MouseEvent event) {
0N/A if (isEditing(tree) && tree.getInvokesStopCellEditing() &&
0N/A !stopEditing(tree)) {
0N/A return false;
0N/A }
0N/A completeEditing();
0N/A if(cellEditor != null && tree.isPathEditable(path)) {
0N/A int row = getRowForPath(tree, path);
0N/A
0N/A if(cellEditor.isCellEditable(event)) {
0N/A editingComponent = cellEditor.getTreeCellEditorComponent
0N/A (tree, path.getLastPathComponent(),
0N/A tree.isPathSelected(path), tree.isExpanded(path),
0N/A treeModel.isLeaf(path.getLastPathComponent()), row);
0N/A Rectangle nodeBounds = getPathBounds(tree, path);
5532N/A if (nodeBounds == null) {
5532N/A return false;
5532N/A }
0N/A
0N/A editingRow = row;
0N/A
0N/A Dimension editorSize = editingComponent.getPreferredSize();
0N/A
0N/A // Only allow odd heights if explicitly set.
0N/A if(editorSize.height != nodeBounds.height &&
0N/A getRowHeight() > 0)
0N/A editorSize.height = getRowHeight();
0N/A
0N/A if(editorSize.width != nodeBounds.width ||
0N/A editorSize.height != nodeBounds.height) {
0N/A // Editor wants different width or height, invalidate
0N/A // treeState and relayout.
0N/A editorHasDifferentSize = true;
0N/A treeState.invalidatePathBounds(path);
0N/A updateSize();
0N/A // To make sure x/y are updated correctly, fetch
0N/A // the bounds again.
0N/A nodeBounds = getPathBounds(tree, path);
5532N/A if (nodeBounds == null) {
5532N/A return false;
5532N/A }
0N/A }
0N/A else
0N/A editorHasDifferentSize = false;
0N/A tree.add(editingComponent);
0N/A editingComponent.setBounds(nodeBounds.x, nodeBounds.y,
0N/A nodeBounds.width,
0N/A nodeBounds.height);
0N/A editingPath = path;
0N/A if (editingComponent instanceof JComponent) {
0N/A ((JComponent)editingComponent).revalidate();
0N/A } else {
0N/A editingComponent.validate();
0N/A }
0N/A editingComponent.repaint();
0N/A if(cellEditor.shouldSelectCell(event)) {
0N/A stopEditingInCompleteEditing = false;
0N/A tree.setSelectionRow(row);
0N/A stopEditingInCompleteEditing = true;
0N/A }
0N/A
0N/A Component focusedComponent = SwingUtilities2.
0N/A compositeRequestFocus(editingComponent);
0N/A boolean selectAll = true;
0N/A
614N/A if(event != null) {
0N/A /* Find the component that will get forwarded all the
0N/A mouse events until mouseReleased. */
0N/A Point componentPoint = SwingUtilities.convertPoint
0N/A (tree, new Point(event.getX(), event.getY()),
0N/A editingComponent);
0N/A
0N/A /* Create an instance of BasicTreeMouseListener to handle
0N/A passing the mouse/motion events to the necessary
0N/A component. */
0N/A // We really want similar behavior to getMouseEventTarget,
0N/A // but it is package private.
0N/A Component activeComponent = SwingUtilities.
0N/A getDeepestComponentAt(editingComponent,
0N/A componentPoint.x, componentPoint.y);
0N/A if (activeComponent != null) {
0N/A MouseInputHandler handler =
0N/A new MouseInputHandler(tree, activeComponent,
0N/A event, focusedComponent);
0N/A
0N/A if (releaseEvent != null) {
0N/A handler.mouseReleased(releaseEvent);
0N/A }
0N/A
0N/A selectAll = false;
0N/A }
0N/A }
0N/A if (selectAll && focusedComponent instanceof JTextField) {
0N/A ((JTextField)focusedComponent).selectAll();
0N/A }
0N/A return true;
0N/A }
0N/A else
0N/A editingComponent = null;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A //
0N/A // Following are primarily for handling mouse events.
0N/A //
0N/A
0N/A /**
0N/A * If the <code>mouseX</code> and <code>mouseY</code> are in the
0N/A * expand/collapse region of the <code>row</code>, this will toggle
0N/A * the row.
0N/A */
0N/A protected void checkForClickInExpandControl(TreePath path,
0N/A int mouseX, int mouseY) {
0N/A if (isLocationInExpandControl(path, mouseX, mouseY)) {
0N/A handleExpandControlClick(path, mouseX, mouseY);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if <code>mouseX</code> and <code>mouseY</code> fall
0N/A * in the area of row that is used to expand/collapse the node and
0N/A * the node at <code>row</code> does not represent a leaf.
0N/A */
0N/A protected boolean isLocationInExpandControl(TreePath path,
0N/A int mouseX, int mouseY) {
0N/A if(path != null && !treeModel.isLeaf(path.getLastPathComponent())){
0N/A int boxWidth;
0N/A Insets i = tree.getInsets();
0N/A
0N/A if(getExpandedIcon() != null)
0N/A boxWidth = getExpandedIcon().getIconWidth();
0N/A else
0N/A boxWidth = 8;
0N/A
0N/A int boxLeftX = getRowX(tree.getRowForPath(path),
0N/A path.getPathCount() - 1);
0N/A
0N/A if (leftToRight) {
0N/A boxLeftX = boxLeftX + i.left - getRightChildIndent() + 1;
0N/A } else {
0N/A boxLeftX = tree.getWidth() - boxLeftX - i.right + getRightChildIndent() - 1;
0N/A }
0N/A
0N/A boxLeftX = findCenteredX(boxLeftX, boxWidth);
0N/A
0N/A return (mouseX >= boxLeftX && mouseX < (boxLeftX + boxWidth));
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Messaged when the user clicks the particular row, this invokes
0N/A * toggleExpandState.
0N/A */
0N/A protected void handleExpandControlClick(TreePath path, int mouseX,
0N/A int mouseY) {
0N/A toggleExpandState(path);
0N/A }
0N/A
0N/A /**
0N/A * Expands path if it is not expanded, or collapses row if it is expanded.
0N/A * If expanding a path and JTree scrolls on expand, ensureRowsAreVisible
0N/A * is invoked to scroll as many of the children to visible as possible
0N/A * (tries to scroll to last visible descendant of path).
0N/A */
0N/A protected void toggleExpandState(TreePath path) {
0N/A if(!tree.isExpanded(path)) {
0N/A int row = getRowForPath(tree, path);
0N/A
0N/A tree.expandPath(path);
0N/A updateSize();
0N/A if(row != -1) {
0N/A if(tree.getScrollsOnExpand())
0N/A ensureRowsAreVisible(row, row + treeState.
0N/A getVisibleChildCount(path));
0N/A else
0N/A ensureRowsAreVisible(row, row);
0N/A }
0N/A }
0N/A else {
0N/A tree.collapsePath(path);
0N/A updateSize();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returning true signifies a mouse event on the node should toggle
0N/A * the selection of only the row under mouse.
0N/A */
0N/A protected boolean isToggleSelectionEvent(MouseEvent event) {
0N/A return (SwingUtilities.isLeftMouseButton(event) &&
1735N/A BasicGraphicsUtils.isMenuShortcutKeyDown(event));
0N/A }
0N/A
0N/A /**
0N/A * Returning true signifies a mouse event on the node should select
0N/A * from the anchor point.
0N/A */
0N/A protected boolean isMultiSelectEvent(MouseEvent event) {
0N/A return (SwingUtilities.isLeftMouseButton(event) &&
0N/A event.isShiftDown());
0N/A }
0N/A
0N/A /**
0N/A * Returning true indicates the row under the mouse should be toggled
0N/A * based on the event. This is invoked after checkForClickInExpandControl,
0N/A * implying the location is not in the expand (toggle) control
0N/A */
0N/A protected boolean isToggleEvent(MouseEvent event) {
0N/A if(!SwingUtilities.isLeftMouseButton(event)) {
0N/A return false;
0N/A }
0N/A int clickCount = tree.getToggleClickCount();
0N/A
0N/A if(clickCount <= 0) {
0N/A return false;
0N/A }
0N/A return ((event.getClickCount() % clickCount) == 0);
0N/A }
0N/A
0N/A /**
0N/A * Messaged to update the selection based on a MouseEvent over a
0N/A * particular row. If the event is a toggle selection event, the
0N/A * row is either selected, or deselected. If the event identifies
0N/A * a multi selection event, the selection is updated from the
0N/A * anchor point. Otherwise the row is selected, and if the event
0N/A * specified a toggle event the row is expanded/collapsed.
0N/A */
0N/A protected void selectPathForEvent(TreePath path, MouseEvent event) {
0N/A /* Adjust from the anchor point. */
0N/A if(isMultiSelectEvent(event)) {
0N/A TreePath anchor = getAnchorSelectionPath();
0N/A int anchorRow = (anchor == null) ? -1 :
0N/A getRowForPath(tree, anchor);
0N/A
0N/A if(anchorRow == -1 || tree.getSelectionModel().
0N/A getSelectionMode() == TreeSelectionModel.
0N/A SINGLE_TREE_SELECTION) {
0N/A tree.setSelectionPath(path);
0N/A }
0N/A else {
0N/A int row = getRowForPath(tree, path);
0N/A TreePath lastAnchorPath = anchor;
0N/A
0N/A if (isToggleSelectionEvent(event)) {
0N/A if (tree.isRowSelected(anchorRow)) {
0N/A tree.addSelectionInterval(anchorRow, row);
0N/A } else {
0N/A tree.removeSelectionInterval(anchorRow, row);
0N/A tree.addSelectionInterval(row, row);
0N/A }
0N/A } else if(row < anchorRow) {
0N/A tree.setSelectionInterval(row, anchorRow);
0N/A } else {
0N/A tree.setSelectionInterval(anchorRow, row);
0N/A }
0N/A lastSelectedRow = row;
0N/A setAnchorSelectionPath(lastAnchorPath);
0N/A setLeadSelectionPath(path);
0N/A }
0N/A }
0N/A
0N/A // Should this event toggle the selection of this row?
0N/A /* Control toggles just this node. */
0N/A else if(isToggleSelectionEvent(event)) {
0N/A if(tree.isPathSelected(path))
0N/A tree.removeSelectionPath(path);
0N/A else
0N/A tree.addSelectionPath(path);
0N/A lastSelectedRow = getRowForPath(tree, path);
0N/A setAnchorSelectionPath(path);
0N/A setLeadSelectionPath(path);
0N/A }
0N/A
0N/A /* Otherwise set the selection to just this interval. */
0N/A else if(SwingUtilities.isLeftMouseButton(event)) {
0N/A tree.setSelectionPath(path);
0N/A if(isToggleEvent(event)) {
0N/A toggleExpandState(path);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * @return true if the node at <code>row</code> is a leaf.
0N/A */
0N/A protected boolean isLeaf(int row) {
0N/A TreePath path = getPathForRow(tree, row);
0N/A
0N/A if(path != null)
0N/A return treeModel.isLeaf(path.getLastPathComponent());
0N/A // Have to return something here...
0N/A return true;
0N/A }
0N/A
0N/A //
0N/A // The following selection methods (lead/anchor) are covers for the
0N/A // methods in JTree.
0N/A //
0N/A private void setAnchorSelectionPath(TreePath newPath) {
0N/A ignoreLAChange = true;
0N/A try {
0N/A tree.setAnchorSelectionPath(newPath);
0N/A } finally{
0N/A ignoreLAChange = false;
0N/A }
0N/A }
0N/A
0N/A private TreePath getAnchorSelectionPath() {
0N/A return tree.getAnchorSelectionPath();
0N/A }
0N/A
0N/A private void setLeadSelectionPath(TreePath newPath) {
0N/A setLeadSelectionPath(newPath, false);
0N/A }
0N/A
0N/A private void setLeadSelectionPath(TreePath newPath, boolean repaint) {
0N/A Rectangle bounds = repaint ?
0N/A getPathBounds(tree, getLeadSelectionPath()) : null;
0N/A
0N/A ignoreLAChange = true;
0N/A try {
0N/A tree.setLeadSelectionPath(newPath);
0N/A } finally {
0N/A ignoreLAChange = false;
0N/A }
0N/A leadRow = getRowForPath(tree, newPath);
0N/A
1173N/A if (repaint) {
1173N/A if (bounds != null) {
1173N/A tree.repaint(getRepaintPathBounds(bounds));
1173N/A }
0N/A bounds = getPathBounds(tree, newPath);
1173N/A if (bounds != null) {
1173N/A tree.repaint(getRepaintPathBounds(bounds));
1173N/A }
0N/A }
0N/A }
0N/A
1173N/A private Rectangle getRepaintPathBounds(Rectangle bounds) {
1173N/A if (UIManager.getBoolean("Tree.repaintWholeRow")) {
1173N/A bounds.x = 0;
1173N/A bounds.width = tree.getWidth();
1173N/A }
1173N/A return bounds;
1173N/A }
1173N/A
0N/A private TreePath getLeadSelectionPath() {
0N/A return tree.getLeadSelectionPath();
0N/A }
0N/A
1999N/A /**
1999N/A * Updates the lead row of the selection.
1999N/A * @since 1.7
1999N/A */
1999N/A protected void updateLeadSelectionRow() {
0N/A leadRow = getRowForPath(tree, getLeadSelectionPath());
0N/A }
0N/A
1999N/A /**
1999N/A * Returns the lead row of the selection.
1999N/A *
1999N/A * @return selection lead row
1999N/A * @since 1.7
1999N/A */
1999N/A protected int getLeadSelectionRow() {
0N/A return leadRow;
0N/A }
0N/A
0N/A /**
0N/A * Extends the selection from the anchor to make <code>newLead</code>
0N/A * the lead of the selection. This does not scroll.
0N/A */
0N/A private void extendSelection(TreePath newLead) {
0N/A TreePath aPath = getAnchorSelectionPath();
0N/A int aRow = (aPath == null) ? -1 :
0N/A getRowForPath(tree, aPath);
0N/A int newIndex = getRowForPath(tree, newLead);
0N/A
0N/A if(aRow == -1) {
0N/A tree.setSelectionRow(newIndex);
0N/A }
0N/A else {
0N/A if(aRow < newIndex) {
0N/A tree.setSelectionInterval(aRow, newIndex);
0N/A }
0N/A else {
0N/A tree.setSelectionInterval(newIndex, aRow);
0N/A }
0N/A setAnchorSelectionPath(aPath);
0N/A setLeadSelectionPath(newLead);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Invokes <code>repaint</code> on the JTree for the passed in TreePath,
0N/A * <code>path</code>.
0N/A */
0N/A private void repaintPath(TreePath path) {
0N/A if (path != null) {
0N/A Rectangle bounds = getPathBounds(tree, path);
0N/A if (bounds != null) {
0N/A tree.repaint(bounds.x, bounds.y, bounds.width, bounds.height);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Updates the TreeState in response to nodes expanding/collapsing.
0N/A */
0N/A public class TreeExpansionHandler implements TreeExpansionListener {
0N/A // NOTE: This class exists only for backward compatability. All
0N/A // its functionality has been moved into Handler. If you need to add
0N/A // new functionality add it to the Handler, but make sure this
0N/A // class calls into the Handler.
0N/A
0N/A /**
0N/A * Called whenever an item in the tree has been expanded.
0N/A */
0N/A public void treeExpanded(TreeExpansionEvent event) {
0N/A getHandler().treeExpanded(event);
0N/A }
0N/A
0N/A /**
0N/A * Called whenever an item in the tree has been collapsed.
0N/A */
0N/A public void treeCollapsed(TreeExpansionEvent event) {
0N/A getHandler().treeCollapsed(event);
0N/A }
0N/A } // BasicTreeUI.TreeExpansionHandler
0N/A
0N/A
0N/A /**
0N/A * Updates the preferred size when scrolling (if necessary).
0N/A */
0N/A public class ComponentHandler extends ComponentAdapter implements
0N/A ActionListener {
0N/A /** Timer used when inside a scrollpane and the scrollbar is
0N/A * adjusting. */
0N/A protected Timer timer;
0N/A /** ScrollBar that is being adjusted. */
0N/A protected JScrollBar scrollBar;
0N/A
0N/A public void componentMoved(ComponentEvent e) {
0N/A if(timer == null) {
0N/A JScrollPane scrollPane = getScrollPane();
0N/A
0N/A if(scrollPane == null)
0N/A updateSize();
0N/A else {
0N/A scrollBar = scrollPane.getVerticalScrollBar();
0N/A if(scrollBar == null ||
0N/A !scrollBar.getValueIsAdjusting()) {
0N/A // Try the horizontal scrollbar.
0N/A if((scrollBar = scrollPane.getHorizontalScrollBar())
0N/A != null && scrollBar.getValueIsAdjusting())
0N/A startTimer();
0N/A else
0N/A updateSize();
0N/A }
0N/A else
0N/A startTimer();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates, if necessary, and starts a Timer to check if need to
0N/A * resize the bounds.
0N/A */
0N/A protected void startTimer() {
0N/A if(timer == null) {
0N/A timer = new Timer(200, this);
0N/A timer.setRepeats(true);
0N/A }
0N/A timer.start();
0N/A }
0N/A
0N/A /**
0N/A * Returns the JScrollPane housing the JTree, or null if one isn't
0N/A * found.
0N/A */
0N/A protected JScrollPane getScrollPane() {
0N/A Component c = tree.getParent();
0N/A
0N/A while(c != null && !(c instanceof JScrollPane))
0N/A c = c.getParent();
0N/A if(c instanceof JScrollPane)
0N/A return (JScrollPane)c;
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Public as a result of Timer. If the scrollBar is null, or
0N/A * not adjusting, this stops the timer and updates the sizing.
0N/A */
0N/A public void actionPerformed(ActionEvent ae) {
0N/A if(scrollBar == null || !scrollBar.getValueIsAdjusting()) {
0N/A if(timer != null)
0N/A timer.stop();
0N/A updateSize();
0N/A timer = null;
0N/A scrollBar = null;
0N/A }
0N/A }
0N/A } // End of BasicTreeUI.ComponentHandler
0N/A
0N/A
0N/A /**
0N/A * Forwards all TreeModel events to the TreeState.
0N/A */
0N/A public class TreeModelHandler implements TreeModelListener {
0N/A
0N/A // NOTE: This class exists only for backward compatability. All
0N/A // its functionality has been moved into Handler. If you need to add
0N/A // new functionality add it to the Handler, but make sure this
0N/A // class calls into the Handler.
0N/A
0N/A public void treeNodesChanged(TreeModelEvent e) {
0N/A getHandler().treeNodesChanged(e);
0N/A }
0N/A
0N/A public void treeNodesInserted(TreeModelEvent e) {
0N/A getHandler().treeNodesInserted(e);
0N/A }
0N/A
0N/A public void treeNodesRemoved(TreeModelEvent e) {
0N/A getHandler().treeNodesRemoved(e);
0N/A }
0N/A
0N/A public void treeStructureChanged(TreeModelEvent e) {
0N/A getHandler().treeStructureChanged(e);
0N/A }
0N/A } // End of BasicTreeUI.TreeModelHandler
0N/A
0N/A
0N/A /**
0N/A * Listens for changes in the selection model and updates the display
0N/A * accordingly.
0N/A */
0N/A public class TreeSelectionHandler implements TreeSelectionListener {
0N/A
0N/A // NOTE: This class exists only for backward compatability. All
0N/A // its functionality has been moved into Handler. If you need to add
0N/A // new functionality add it to the Handler, but make sure this
0N/A // class calls into the Handler.
0N/A
0N/A /**
0N/A * Messaged when the selection changes in the tree we're displaying
0N/A * for. Stops editing, messages super and displays the changed paths.
0N/A */
0N/A public void valueChanged(TreeSelectionEvent event) {
0N/A getHandler().valueChanged(event);
0N/A }
0N/A }// End of BasicTreeUI.TreeSelectionHandler
0N/A
0N/A
0N/A /**
0N/A * Listener responsible for getting cell editing events and updating
0N/A * the tree accordingly.
0N/A */
0N/A public class CellEditorHandler implements CellEditorListener {
0N/A
0N/A // NOTE: This class exists only for backward compatability. All
0N/A // its functionality has been moved into Handler. If you need to add
0N/A // new functionality add it to the Handler, but make sure this
0N/A // class calls into the Handler.
0N/A
0N/A /** Messaged when editing has stopped in the tree. */
0N/A public void editingStopped(ChangeEvent e) {
0N/A getHandler().editingStopped(e);
0N/A }
0N/A
0N/A /** Messaged when editing has been canceled in the tree. */
0N/A public void editingCanceled(ChangeEvent e) {
0N/A getHandler().editingCanceled(e);
0N/A }
0N/A } // BasicTreeUI.CellEditorHandler
0N/A
0N/A
0N/A /**
0N/A * This is used to get mutliple key down events to appropriately generate
0N/A * events.
0N/A */
0N/A public class KeyHandler extends KeyAdapter {
0N/A
0N/A // NOTE: This class exists only for backward compatability. All
0N/A // its functionality has been moved into Handler. If you need to add
0N/A // new functionality add it to the Handler, but make sure this
0N/A // class calls into the Handler.
0N/A
0N/A // Also note these fields aren't use anymore, nor does Handler have
0N/A // the old functionality. This behavior worked around an old bug
0N/A // in JComponent that has long since been fixed.
0N/A
0N/A /** Key code that is being generated for. */
0N/A protected Action repeatKeyAction;
0N/A
0N/A /** Set to true while keyPressed is active. */
0N/A protected boolean isKeyDown;
0N/A
0N/A /**
0N/A * Invoked when a key has been typed.
0N/A *
0N/A * Moves the keyboard focus to the first element
0N/A * whose first letter matches the alphanumeric key
0N/A * pressed by the user. Subsequent same key presses
0N/A * move the keyboard focus to the next object that
0N/A * starts with the same letter.
0N/A */
0N/A public void keyTyped(KeyEvent e) {
0N/A getHandler().keyTyped(e);
0N/A }
0N/A
0N/A public void keyPressed(KeyEvent e) {
0N/A getHandler().keyPressed(e);
0N/A }
0N/A
0N/A public void keyReleased(KeyEvent e) {
0N/A getHandler().keyReleased(e);
0N/A }
0N/A } // End of BasicTreeUI.KeyHandler
0N/A
0N/A
0N/A /**
0N/A * Repaints the lead selection row when focus is lost/gained.
0N/A */
0N/A public class FocusHandler implements FocusListener {
0N/A // NOTE: This class exists only for backward compatability. All
0N/A // its functionality has been moved into Handler. If you need to add
0N/A // new functionality add it to the Handler, but make sure this
0N/A // class calls into the Handler.
0N/A
0N/A /**
0N/A * Invoked when focus is activated on the tree we're in, redraws the
0N/A * lead row.
0N/A */
0N/A public void focusGained(FocusEvent e) {
0N/A getHandler().focusGained(e);
0N/A }
0N/A
0N/A /**
0N/A * Invoked when focus is activated on the tree we're in, redraws the
0N/A * lead row.
0N/A */
0N/A public void focusLost(FocusEvent e) {
0N/A getHandler().focusLost(e);
0N/A }
0N/A } // End of class BasicTreeUI.FocusHandler
0N/A
0N/A
0N/A /**
0N/A * Class responsible for getting size of node, method is forwarded
0N/A * to BasicTreeUI method. X location does not include insets, that is
0N/A * handled in getPathBounds.
0N/A */
0N/A // This returns locations that don't include any Insets.
0N/A public class NodeDimensionsHandler extends
0N/A AbstractLayoutCache.NodeDimensions {
0N/A /**
0N/A * Responsible for getting the size of a particular node.
0N/A */
0N/A public Rectangle getNodeDimensions(Object value, int row,
0N/A int depth, boolean expanded,
0N/A Rectangle size) {
0N/A // Return size of editing component, if editing and asking
0N/A // for editing row.
0N/A if(editingComponent != null && editingRow == row) {
0N/A Dimension prefSize = editingComponent.
0N/A getPreferredSize();
0N/A int rh = getRowHeight();
0N/A
0N/A if(rh > 0 && rh != prefSize.height)
0N/A prefSize.height = rh;
0N/A if(size != null) {
0N/A size.x = getRowX(row, depth);
0N/A size.width = prefSize.width;
0N/A size.height = prefSize.height;
0N/A }
0N/A else {
0N/A size = new Rectangle(getRowX(row, depth), 0,
0N/A prefSize.width, prefSize.height);
0N/A }
0N/A return size;
0N/A }
0N/A // Not editing, use renderer.
0N/A if(currentCellRenderer != null) {
0N/A Component aComponent;
0N/A
0N/A aComponent = currentCellRenderer.getTreeCellRendererComponent
0N/A (tree, value, tree.isRowSelected(row),
0N/A expanded, treeModel.isLeaf(value), row,
0N/A false);
0N/A if(tree != null) {
0N/A // Only ever removed when UI changes, this is OK!
0N/A rendererPane.add(aComponent);
0N/A aComponent.validate();
0N/A }
0N/A Dimension prefSize = aComponent.getPreferredSize();
0N/A
0N/A if(size != null) {
0N/A size.x = getRowX(row, depth);
0N/A size.width = prefSize.width;
0N/A size.height = prefSize.height;
0N/A }
0N/A else {
0N/A size = new Rectangle(getRowX(row, depth), 0,
0N/A prefSize.width, prefSize.height);
0N/A }
0N/A return size;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * @return amount to indent the given row.
0N/A */
0N/A protected int getRowX(int row, int depth) {
0N/A return BasicTreeUI.this.getRowX(row, depth);
0N/A }
0N/A
0N/A } // End of class BasicTreeUI.NodeDimensionsHandler
0N/A
0N/A
0N/A /**
0N/A * TreeMouseListener is responsible for updating the selection
0N/A * based on mouse events.
0N/A */
0N/A public class MouseHandler extends MouseAdapter implements MouseMotionListener
0N/A {
0N/A // NOTE: This class exists only for backward compatability. All
0N/A // its functionality has been moved into Handler. If you need to add
0N/A // new functionality add it to the Handler, but make sure this
0N/A // class calls into the Handler.
0N/A
0N/A /**
0N/A * Invoked when a mouse button has been pressed on a component.
0N/A */
0N/A public void mousePressed(MouseEvent e) {
0N/A getHandler().mousePressed(e);
0N/A }
0N/A
0N/A public void mouseDragged(MouseEvent e) {
0N/A getHandler().mouseDragged(e);
0N/A }
0N/A
0N/A /**
0N/A * Invoked when the mouse button has been moved on a component
0N/A * (with no buttons no down).
0N/A * @since 1.4
0N/A */
0N/A public void mouseMoved(MouseEvent e) {
0N/A getHandler().mouseMoved(e);
0N/A }
0N/A
0N/A public void mouseReleased(MouseEvent e) {
0N/A getHandler().mouseReleased(e);
0N/A }
0N/A } // End of BasicTreeUI.MouseHandler
0N/A
0N/A
0N/A /**
0N/A * PropertyChangeListener for the tree. Updates the appropriate
0N/A * varaible, or TreeState, based on what changes.
0N/A */
0N/A public class PropertyChangeHandler implements
0N/A PropertyChangeListener {
0N/A
0N/A // NOTE: This class exists only for backward compatability. All
0N/A // its functionality has been moved into Handler. If you need to add
0N/A // new functionality add it to the Handler, but make sure this
0N/A // class calls into the Handler.
0N/A
0N/A public void propertyChange(PropertyChangeEvent event) {
0N/A getHandler().propertyChange(event);
0N/A }
0N/A } // End of BasicTreeUI.PropertyChangeHandler
0N/A
0N/A
0N/A /**
0N/A * Listener on the TreeSelectionModel, resets the row selection if
0N/A * any of the properties of the model change.
0N/A */
0N/A public class SelectionModelPropertyChangeHandler implements
0N/A PropertyChangeListener {
0N/A
0N/A // NOTE: This class exists only for backward compatability. All
0N/A // its functionality has been moved into Handler. If you need to add
0N/A // new functionality add it to the Handler, but make sure this
0N/A // class calls into the Handler.
0N/A
0N/A public void propertyChange(PropertyChangeEvent event) {
0N/A getHandler().propertyChange(event);
0N/A }
0N/A } // End of BasicTreeUI.SelectionModelPropertyChangeHandler
0N/A
0N/A
0N/A /**
0N/A * <code>TreeTraverseAction</code> is the action used for left/right keys.
0N/A * Will toggle the expandedness of a node, as well as potentially
0N/A * incrementing the selection.
0N/A */
0N/A public class TreeTraverseAction extends AbstractAction {
0N/A /** Determines direction to traverse, 1 means expand, -1 means
0N/A * collapse. */
0N/A protected int direction;
0N/A /** True if the selection is reset, false means only the lead path
0N/A * changes. */
0N/A private boolean changeSelection;
0N/A
0N/A public TreeTraverseAction(int direction, String name) {
0N/A this(direction, name, true);
0N/A }
0N/A
0N/A private TreeTraverseAction(int direction, String name,
0N/A boolean changeSelection) {
0N/A this.direction = direction;
0N/A this.changeSelection = changeSelection;
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A if (tree != null) {
0N/A SHARED_ACTION.traverse(tree, BasicTreeUI.this, direction,
0N/A changeSelection);
0N/A }
0N/A }
0N/A
0N/A public boolean isEnabled() { return (tree != null &&
0N/A tree.isEnabled()); }
0N/A } // BasicTreeUI.TreeTraverseAction
0N/A
0N/A
0N/A /** TreePageAction handles page up and page down events.
0N/A */
0N/A public class TreePageAction extends AbstractAction {
0N/A /** Specifies the direction to adjust the selection by. */
0N/A protected int direction;
0N/A /** True indicates should set selection from anchor path. */
0N/A private boolean addToSelection;
0N/A private boolean changeSelection;
0N/A
0N/A public TreePageAction(int direction, String name) {
0N/A this(direction, name, false, true);
0N/A }
0N/A
0N/A private TreePageAction(int direction, String name,
0N/A boolean addToSelection,
0N/A boolean changeSelection) {
0N/A this.direction = direction;
0N/A this.addToSelection = addToSelection;
0N/A this.changeSelection = changeSelection;
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A if (tree != null) {
0N/A SHARED_ACTION.page(tree, BasicTreeUI.this, direction,
0N/A addToSelection, changeSelection);
0N/A }
0N/A }
0N/A
0N/A public boolean isEnabled() { return (tree != null &&
0N/A tree.isEnabled()); }
0N/A
0N/A } // BasicTreeUI.TreePageAction
0N/A
0N/A
0N/A /** TreeIncrementAction is used to handle up/down actions. Selection
0N/A * is moved up or down based on direction.
0N/A */
0N/A public class TreeIncrementAction extends AbstractAction {
0N/A /** Specifies the direction to adjust the selection by. */
0N/A protected int direction;
0N/A /** If true the new item is added to the selection, if false the
0N/A * selection is reset. */
0N/A private boolean addToSelection;
0N/A private boolean changeSelection;
0N/A
0N/A public TreeIncrementAction(int direction, String name) {
0N/A this(direction, name, false, true);
0N/A }
0N/A
0N/A private TreeIncrementAction(int direction, String name,
0N/A boolean addToSelection,
0N/A boolean changeSelection) {
0N/A this.direction = direction;
0N/A this.addToSelection = addToSelection;
0N/A this.changeSelection = changeSelection;
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A if (tree != null) {
0N/A SHARED_ACTION.increment(tree, BasicTreeUI.this, direction,
0N/A addToSelection, changeSelection);
0N/A }
0N/A }
0N/A
0N/A public boolean isEnabled() { return (tree != null &&
0N/A tree.isEnabled()); }
0N/A
0N/A } // End of class BasicTreeUI.TreeIncrementAction
0N/A
0N/A /**
0N/A * TreeHomeAction is used to handle end/home actions.
0N/A * Scrolls either the first or last cell to be visible based on
0N/A * direction.
0N/A */
0N/A public class TreeHomeAction extends AbstractAction {
0N/A protected int direction;
0N/A /** Set to true if append to selection. */
0N/A private boolean addToSelection;
0N/A private boolean changeSelection;
0N/A
0N/A public TreeHomeAction(int direction, String name) {
0N/A this(direction, name, false, true);
0N/A }
0N/A
0N/A private TreeHomeAction(int direction, String name,
0N/A boolean addToSelection,
0N/A boolean changeSelection) {
0N/A this.direction = direction;
0N/A this.changeSelection = changeSelection;
0N/A this.addToSelection = addToSelection;
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A if (tree != null) {
0N/A SHARED_ACTION.home(tree, BasicTreeUI.this, direction,
0N/A addToSelection, changeSelection);
0N/A }
0N/A }
0N/A
0N/A public boolean isEnabled() { return (tree != null &&
0N/A tree.isEnabled()); }
0N/A
0N/A } // End of class BasicTreeUI.TreeHomeAction
0N/A
0N/A
0N/A /**
0N/A * For the first selected row expandedness will be toggled.
0N/A */
0N/A public class TreeToggleAction extends AbstractAction {
0N/A public TreeToggleAction(String name) {
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A if(tree != null) {
0N/A SHARED_ACTION.toggle(tree, BasicTreeUI.this);
0N/A }
0N/A }
0N/A
0N/A public boolean isEnabled() { return (tree != null &&
0N/A tree.isEnabled()); }
0N/A
0N/A } // End of class BasicTreeUI.TreeToggleAction
0N/A
0N/A
0N/A /**
0N/A * ActionListener that invokes cancelEditing when action performed.
0N/A */
0N/A public class TreeCancelEditingAction extends AbstractAction {
0N/A public TreeCancelEditingAction(String name) {
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A if(tree != null) {
0N/A SHARED_ACTION.cancelEditing(tree, BasicTreeUI.this);
0N/A }
0N/A }
0N/A
0N/A public boolean isEnabled() { return (tree != null &&
0N/A tree.isEnabled() &&
0N/A isEditing(tree)); }
0N/A } // End of class BasicTreeUI.TreeCancelEditingAction
0N/A
0N/A
0N/A /**
0N/A * MouseInputHandler handles passing all mouse events,
0N/A * including mouse motion events, until the mouse is released to
0N/A * the destination it is constructed with. It is assumed all the
0N/A * events are currently target at source.
0N/A */
0N/A public class MouseInputHandler extends Object implements
0N/A MouseInputListener
0N/A {
0N/A /** Source that events are coming from. */
0N/A protected Component source;
0N/A /** Destination that receives all events. */
0N/A protected Component destination;
0N/A private Component focusComponent;
0N/A private boolean dispatchedEvent;
0N/A
0N/A public MouseInputHandler(Component source, Component destination,
0N/A MouseEvent event){
0N/A this(source, destination, event, null);
0N/A }
0N/A
0N/A MouseInputHandler(Component source, Component destination,
0N/A MouseEvent event, Component focusComponent) {
0N/A this.source = source;
0N/A this.destination = destination;
0N/A this.source.addMouseListener(this);
0N/A this.source.addMouseMotionListener(this);
0N/A
0N/A SwingUtilities2.setSkipClickCount(destination,
0N/A event.getClickCount() - 1);
0N/A
0N/A /* Dispatch the editing event! */
0N/A destination.dispatchEvent(SwingUtilities.convertMouseEvent
0N/A (source, event, destination));
0N/A this.focusComponent = focusComponent;
0N/A }
0N/A
0N/A public void mouseClicked(MouseEvent e) {
0N/A if(destination != null) {
0N/A dispatchedEvent = true;
0N/A destination.dispatchEvent(SwingUtilities.convertMouseEvent
0N/A (source, e, destination));
0N/A }
0N/A }
0N/A
0N/A public void mousePressed(MouseEvent e) {
0N/A }
0N/A
0N/A public void mouseReleased(MouseEvent e) {
0N/A if(destination != null)
0N/A destination.dispatchEvent(SwingUtilities.convertMouseEvent
0N/A (source, e, destination));
0N/A removeFromSource();
0N/A }
0N/A
0N/A public void mouseEntered(MouseEvent e) {
0N/A if (!SwingUtilities.isLeftMouseButton(e)) {
0N/A removeFromSource();
0N/A }
0N/A }
0N/A
0N/A public void mouseExited(MouseEvent e) {
0N/A if (!SwingUtilities.isLeftMouseButton(e)) {
0N/A removeFromSource();
0N/A }
0N/A }
0N/A
0N/A public void mouseDragged(MouseEvent e) {
0N/A if(destination != null) {
0N/A dispatchedEvent = true;
0N/A destination.dispatchEvent(SwingUtilities.convertMouseEvent
0N/A (source, e, destination));
0N/A }
0N/A }
0N/A
0N/A public void mouseMoved(MouseEvent e) {
0N/A removeFromSource();
0N/A }
0N/A
0N/A protected void removeFromSource() {
0N/A if(source != null) {
0N/A source.removeMouseListener(this);
0N/A source.removeMouseMotionListener(this);
0N/A if (focusComponent != null &&
0N/A focusComponent == destination && !dispatchedEvent &&
0N/A (focusComponent instanceof JTextField)) {
0N/A ((JTextField)focusComponent).selectAll();
0N/A }
0N/A }
0N/A source = destination = null;
0N/A }
0N/A
0N/A } // End of class BasicTreeUI.MouseInputHandler
0N/A
0N/A private static final TransferHandler defaultTransferHandler = new TreeTransferHandler();
0N/A
614N/A static class TreeTransferHandler extends TransferHandler implements UIResource, Comparator<TreePath> {
0N/A
0N/A private JTree tree;
0N/A
0N/A /**
0N/A * Create a Transferable to use as the source for a data transfer.
0N/A *
0N/A * @param c The component holding the data to be transfered. This
0N/A * argument is provided to enable sharing of TransferHandlers by
0N/A * multiple components.
0N/A * @return The representation of the data to be transfered.
0N/A *
0N/A */
0N/A protected Transferable createTransferable(JComponent c) {
0N/A if (c instanceof JTree) {
0N/A tree = (JTree) c;
0N/A TreePath[] paths = tree.getSelectionPaths();
0N/A
0N/A if (paths == null || paths.length == 0) {
0N/A return null;
0N/A }
0N/A
0N/A StringBuffer plainBuf = new StringBuffer();
0N/A StringBuffer htmlBuf = new StringBuffer();
0N/A
0N/A htmlBuf.append("<html>\n<body>\n<ul>\n");
0N/A
0N/A TreeModel model = tree.getModel();
0N/A TreePath lastPath = null;
0N/A TreePath[] displayPaths = getDisplayOrderPaths(paths);
0N/A
614N/A for (TreePath path : displayPaths) {
0N/A Object node = path.getLastPathComponent();
0N/A boolean leaf = model.isLeaf(node);
0N/A String label = getDisplayString(path, true, leaf);
0N/A
0N/A plainBuf.append(label + "\n");
0N/A htmlBuf.append(" <li>" + label + "\n");
0N/A }
0N/A
0N/A // remove the last newline
0N/A plainBuf.deleteCharAt(plainBuf.length() - 1);
0N/A htmlBuf.append("</ul>\n</body>\n</html>");
0N/A
0N/A tree = null;
0N/A
0N/A return new BasicTransferable(plainBuf.toString(), htmlBuf.toString());
0N/A }
0N/A
0N/A return null;
0N/A }
0N/A
614N/A public int compare(TreePath o1, TreePath o2) {
614N/A int row1 = tree.getRowForPath(o1);
614N/A int row2 = tree.getRowForPath(o2);
0N/A return row1 - row2;
0N/A }
0N/A
0N/A String getDisplayString(TreePath path, boolean selected, boolean leaf) {
0N/A int row = tree.getRowForPath(path);
0N/A boolean hasFocus = tree.getLeadSelectionRow() == row;
0N/A Object node = path.getLastPathComponent();
0N/A return tree.convertValueToText(node, selected, tree.isExpanded(row),
0N/A leaf, row, hasFocus);
0N/A }
0N/A
0N/A /**
0N/A * Selection paths are in selection order. The conversion to
0N/A * HTML requires display order. This method resorts the paths
0N/A * to be in the display order.
0N/A */
0N/A TreePath[] getDisplayOrderPaths(TreePath[] paths) {
0N/A // sort the paths to display order rather than selection order
614N/A ArrayList<TreePath> selOrder = new ArrayList<TreePath>();
614N/A for (TreePath path : paths) {
614N/A selOrder.add(path);
0N/A }
0N/A Collections.sort(selOrder, this);
0N/A int n = selOrder.size();
0N/A TreePath[] displayPaths = new TreePath[n];
0N/A for (int i = 0; i < n; i++) {
614N/A displayPaths[i] = selOrder.get(i);
0N/A }
0N/A return displayPaths;
0N/A }
0N/A
0N/A public int getSourceActions(JComponent c) {
0N/A return COPY;
0N/A }
0N/A
0N/A }
0N/A
0N/A
0N/A private class Handler implements CellEditorListener, FocusListener,
0N/A KeyListener, MouseListener, MouseMotionListener,
0N/A PropertyChangeListener, TreeExpansionListener,
0N/A TreeModelListener, TreeSelectionListener,
0N/A BeforeDrag {
0N/A //
0N/A // KeyListener
0N/A //
0N/A private String prefix = "";
0N/A private String typedString = "";
0N/A private long lastTime = 0L;
0N/A
0N/A /**
0N/A * Invoked when a key has been typed.
0N/A *
0N/A * Moves the keyboard focus to the first element whose prefix matches the
0N/A * sequence of alphanumeric keys pressed by the user with delay less
0N/A * than value of <code>timeFactor</code> property (or 1000 milliseconds
0N/A * if it is not defined). Subsequent same key presses move the keyboard
0N/A * focus to the next object that starts with the same letter until another
0N/A * key is pressed, then it is treated as the prefix with appropriate number
0N/A * of the same letters followed by first typed another letter.
0N/A */
0N/A public void keyTyped(KeyEvent e) {
0N/A // handle first letter navigation
0N/A if(tree != null && tree.getRowCount()>0 && tree.hasFocus() &&
0N/A tree.isEnabled()) {
1735N/A if (e.isAltDown() || BasicGraphicsUtils.isMenuShortcutKeyDown(e) ||
0N/A isNavigationKey(e)) {
0N/A return;
0N/A }
0N/A boolean startingFromSelection = true;
0N/A
0N/A char c = e.getKeyChar();
0N/A
0N/A long time = e.getWhen();
0N/A int startingRow = tree.getLeadSelectionRow();
0N/A if (time - lastTime < timeFactor) {
0N/A typedString += c;
0N/A if((prefix.length() == 1) && (c == prefix.charAt(0))) {
0N/A // Subsequent same key presses move the keyboard focus to the next
0N/A // object that starts with the same letter.
0N/A startingRow++;
0N/A } else {
0N/A prefix = typedString;
0N/A }
0N/A } else {
0N/A startingRow++;
0N/A typedString = "" + c;
0N/A prefix = typedString;
0N/A }
0N/A lastTime = time;
0N/A
0N/A if (startingRow < 0 || startingRow >= tree.getRowCount()) {
0N/A startingFromSelection = false;
0N/A startingRow = 0;
0N/A }
0N/A TreePath path = tree.getNextMatch(prefix, startingRow,
0N/A Position.Bias.Forward);
0N/A if (path != null) {
0N/A tree.setSelectionPath(path);
0N/A int row = getRowForPath(tree, path);
0N/A ensureRowsAreVisible(row, row);
0N/A } else if (startingFromSelection) {
0N/A path = tree.getNextMatch(prefix, 0,
0N/A Position.Bias.Forward);
0N/A if (path != null) {
0N/A tree.setSelectionPath(path);
0N/A int row = getRowForPath(tree, path);
0N/A ensureRowsAreVisible(row, row);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Invoked when a key has been pressed.
0N/A *
0N/A * Checks to see if the key event is a navigation key to prevent
0N/A * dispatching these keys for the first letter navigation.
0N/A */
0N/A public void keyPressed(KeyEvent e) {
0N/A if (tree != null && isNavigationKey(e)) {
0N/A prefix = "";
0N/A typedString = "";
0N/A lastTime = 0L;
0N/A }
0N/A }
0N/A
0N/A public void keyReleased(KeyEvent e) {
0N/A }
0N/A
0N/A /**
0N/A * Returns whether or not the supplied key event maps to a key that is used for
0N/A * navigation. This is used for optimizing key input by only passing non-
0N/A * navigation keys to the first letter navigation mechanism.
0N/A */
0N/A private boolean isNavigationKey(KeyEvent event) {
0N/A InputMap inputMap = tree.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
0N/A KeyStroke key = KeyStroke.getKeyStrokeForEvent(event);
0N/A
614N/A return inputMap != null && inputMap.get(key) != null;
0N/A }
0N/A
0N/A
0N/A //
0N/A // PropertyChangeListener
0N/A //
0N/A public void propertyChange(PropertyChangeEvent event) {
0N/A if (event.getSource() == treeSelectionModel) {
0N/A treeSelectionModel.resetRowSelection();
0N/A }
0N/A else if(event.getSource() == tree) {
0N/A String changeName = event.getPropertyName();
0N/A
0N/A if (changeName == JTree.LEAD_SELECTION_PATH_PROPERTY) {
0N/A if (!ignoreLAChange) {
1999N/A updateLeadSelectionRow();
0N/A repaintPath((TreePath)event.getOldValue());
0N/A repaintPath((TreePath)event.getNewValue());
0N/A }
0N/A }
0N/A else if (changeName == JTree.ANCHOR_SELECTION_PATH_PROPERTY) {
0N/A if (!ignoreLAChange) {
0N/A repaintPath((TreePath)event.getOldValue());
0N/A repaintPath((TreePath)event.getNewValue());
0N/A }
0N/A }
0N/A if(changeName == JTree.CELL_RENDERER_PROPERTY) {
0N/A setCellRenderer((TreeCellRenderer)event.getNewValue());
0N/A redoTheLayout();
0N/A }
0N/A else if(changeName == JTree.TREE_MODEL_PROPERTY) {
0N/A setModel((TreeModel)event.getNewValue());
0N/A }
0N/A else if(changeName == JTree.ROOT_VISIBLE_PROPERTY) {
0N/A setRootVisible(((Boolean)event.getNewValue()).
0N/A booleanValue());
0N/A }
0N/A else if(changeName == JTree.SHOWS_ROOT_HANDLES_PROPERTY) {
0N/A setShowsRootHandles(((Boolean)event.getNewValue()).
0N/A booleanValue());
0N/A }
0N/A else if(changeName == JTree.ROW_HEIGHT_PROPERTY) {
0N/A setRowHeight(((Integer)event.getNewValue()).
0N/A intValue());
0N/A }
0N/A else if(changeName == JTree.CELL_EDITOR_PROPERTY) {
0N/A setCellEditor((TreeCellEditor)event.getNewValue());
0N/A }
0N/A else if(changeName == JTree.EDITABLE_PROPERTY) {
0N/A setEditable(((Boolean)event.getNewValue()).booleanValue());
0N/A }
0N/A else if(changeName == JTree.LARGE_MODEL_PROPERTY) {
0N/A setLargeModel(tree.isLargeModel());
0N/A }
0N/A else if(changeName == JTree.SELECTION_MODEL_PROPERTY) {
0N/A setSelectionModel(tree.getSelectionModel());
0N/A }
0N/A else if(changeName == "font") {
0N/A completeEditing();
0N/A if(treeState != null)
0N/A treeState.invalidateSizes();
0N/A updateSize();
0N/A }
0N/A else if (changeName == "componentOrientation") {
0N/A if (tree != null) {
0N/A leftToRight = BasicGraphicsUtils.isLeftToRight(tree);
0N/A redoTheLayout();
0N/A tree.treeDidChange();
0N/A
0N/A InputMap km = getInputMap(JComponent.WHEN_FOCUSED);
0N/A SwingUtilities.replaceUIInputMap(tree,
0N/A JComponent.WHEN_FOCUSED, km);
0N/A }
0N/A } else if ("dropLocation" == changeName) {
0N/A JTree.DropLocation oldValue = (JTree.DropLocation)event.getOldValue();
0N/A repaintDropLocation(oldValue);
0N/A repaintDropLocation(tree.getDropLocation());
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void repaintDropLocation(JTree.DropLocation loc) {
0N/A if (loc == null) {
0N/A return;
0N/A }
0N/A
0N/A Rectangle r;
0N/A
0N/A if (isDropLine(loc)) {
0N/A r = getDropLineRect(loc);
0N/A } else {
0N/A r = tree.getPathBounds(loc.getPath());
0N/A }
0N/A
0N/A if (r != null) {
0N/A tree.repaint(r);
0N/A }
0N/A }
0N/A
0N/A //
0N/A // MouseListener
0N/A //
0N/A
0N/A // Whether or not the mouse press (which is being considered as part
0N/A // of a drag sequence) also caused the selection change to be fully
0N/A // processed.
0N/A private boolean dragPressDidSelection;
0N/A
0N/A // Set to true when a drag gesture has been fully recognized and DnD
0N/A // begins. Use this to ignore further mouse events which could be
0N/A // delivered if DnD is cancelled (via ESCAPE for example)
0N/A private boolean dragStarted;
0N/A
0N/A // The path over which the press occurred and the press event itself
0N/A private TreePath pressedPath;
0N/A private MouseEvent pressedEvent;
0N/A
0N/A // Used to detect whether the press event causes a selection change.
0N/A // If it does, we won't try to start editing on the release.
0N/A private boolean valueChangedOnPress;
0N/A
0N/A private boolean isActualPath(TreePath path, int x, int y) {
0N/A if (path == null) {
0N/A return false;
0N/A }
0N/A
0N/A Rectangle bounds = getPathBounds(tree, path);
4568N/A if (bounds == null || y > (bounds.y + bounds.height)) {
0N/A return false;
0N/A }
0N/A
0N/A return (x >= bounds.x) && (x <= (bounds.x + bounds.width));
0N/A }
0N/A
0N/A public void mouseClicked(MouseEvent e) {
0N/A }
0N/A
0N/A public void mouseEntered(MouseEvent e) {
0N/A }
0N/A
0N/A public void mouseExited(MouseEvent e) {
0N/A }
0N/A
0N/A /**
0N/A * Invoked when a mouse button has been pressed on a component.
0N/A */
0N/A public void mousePressed(MouseEvent e) {
0N/A if (SwingUtilities2.shouldIgnore(e, tree)) {
0N/A return;
0N/A }
0N/A
0N/A // if we can't stop any ongoing editing, do nothing
0N/A if (isEditing(tree) && tree.getInvokesStopCellEditing()
0N/A && !stopEditing(tree)) {
0N/A return;
0N/A }
0N/A
0N/A completeEditing();
0N/A
0N/A pressedPath = getClosestPathForLocation(tree, e.getX(), e.getY());
0N/A
0N/A if (tree.getDragEnabled()) {
0N/A mousePressedDND(e);
0N/A } else {
0N/A SwingUtilities2.adjustFocus(tree);
0N/A handleSelection(e);
0N/A }
0N/A }
0N/A
0N/A private void mousePressedDND(MouseEvent e) {
0N/A pressedEvent = e;
0N/A boolean grabFocus = true;
0N/A dragStarted = false;
0N/A valueChangedOnPress = false;
0N/A
0N/A // if we have a valid path and this is a drag initiating event
0N/A if (isActualPath(pressedPath, e.getX(), e.getY()) &&
0N/A DragRecognitionSupport.mousePressed(e)) {
0N/A
0N/A dragPressDidSelection = false;
0N/A
1735N/A if (BasicGraphicsUtils.isMenuShortcutKeyDown(e)) {
0N/A // do nothing for control - will be handled on release
0N/A // or when drag starts
0N/A return;
0N/A } else if (!e.isShiftDown() && tree.isPathSelected(pressedPath)) {
0N/A // clicking on something that's already selected
0N/A // and need to make it the lead now
0N/A setAnchorSelectionPath(pressedPath);
0N/A setLeadSelectionPath(pressedPath, true);
0N/A return;
0N/A }
0N/A
0N/A dragPressDidSelection = true;
0N/A
0N/A // could be a drag initiating event - don't grab focus
0N/A grabFocus = false;
0N/A }
0N/A
0N/A if (grabFocus) {
0N/A SwingUtilities2.adjustFocus(tree);
0N/A }
0N/A
0N/A handleSelection(e);
0N/A }
0N/A
0N/A void handleSelection(MouseEvent e) {
0N/A if(pressedPath != null) {
0N/A Rectangle bounds = getPathBounds(tree, pressedPath);
0N/A
5532N/A if (bounds == null || e.getY() >= (bounds.y + bounds.height)) {
0N/A return;
0N/A }
0N/A
0N/A // Preferably checkForClickInExpandControl could take
0N/A // the Event to do this it self!
0N/A if(SwingUtilities.isLeftMouseButton(e)) {
0N/A checkForClickInExpandControl(pressedPath, e.getX(), e.getY());
0N/A }
0N/A
0N/A int x = e.getX();
0N/A
0N/A // Perhaps they clicked the cell itself. If so,
0N/A // select it.
0N/A if (x >= bounds.x && x < (bounds.x + bounds.width)) {
0N/A if (tree.getDragEnabled() || !startEditing(pressedPath, e)) {
0N/A selectPathForEvent(pressedPath, e);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void dragStarting(MouseEvent me) {
0N/A dragStarted = true;
0N/A
1735N/A if (BasicGraphicsUtils.isMenuShortcutKeyDown(me)) {
0N/A tree.addSelectionPath(pressedPath);
0N/A setAnchorSelectionPath(pressedPath);
0N/A setLeadSelectionPath(pressedPath, true);
0N/A }
0N/A
0N/A pressedEvent = null;
0N/A pressedPath = null;
0N/A }
0N/A
0N/A public void mouseDragged(MouseEvent e) {
0N/A if (SwingUtilities2.shouldIgnore(e, tree)) {
0N/A return;
0N/A }
0N/A
0N/A if (tree.getDragEnabled()) {
0N/A DragRecognitionSupport.mouseDragged(e, this);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Invoked when the mouse button has been moved on a component
0N/A * (with no buttons no down).
0N/A */
0N/A public void mouseMoved(MouseEvent e) {
0N/A }
0N/A
0N/A public void mouseReleased(MouseEvent e) {
0N/A if (SwingUtilities2.shouldIgnore(e, tree)) {
0N/A return;
0N/A }
0N/A
0N/A if (tree.getDragEnabled()) {
0N/A mouseReleasedDND(e);
0N/A }
0N/A
0N/A pressedEvent = null;
0N/A pressedPath = null;
0N/A }
0N/A
0N/A private void mouseReleasedDND(MouseEvent e) {
0N/A MouseEvent me = DragRecognitionSupport.mouseReleased(e);
0N/A if (me != null) {
0N/A SwingUtilities2.adjustFocus(tree);
0N/A if (!dragPressDidSelection) {
0N/A handleSelection(me);
0N/A }
0N/A }
0N/A
0N/A if (!dragStarted) {
0N/A
0N/A // Note: We don't give the tree a chance to start editing if the
0N/A // mouse press caused a selection change. Otherwise the default
0N/A // tree cell editor will start editing on EVERY press and
0N/A // release. If it turns out that this affects some editors, we
0N/A // can always parameterize this with a client property. ex:
0N/A //
0N/A // if (pressedPath != null &&
0N/A // (Boolean.TRUE == tree.getClientProperty("Tree.DnD.canEditOnValueChange") ||
0N/A // !valueChangedOnPress) && ...
0N/A if (pressedPath != null && !valueChangedOnPress &&
0N/A isActualPath(pressedPath, pressedEvent.getX(), pressedEvent.getY())) {
0N/A
0N/A startEditingOnRelease(pressedPath, pressedEvent, e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A //
0N/A // FocusListener
0N/A //
0N/A public void focusGained(FocusEvent e) {
0N/A if(tree != null) {
0N/A Rectangle pBounds;
0N/A
0N/A pBounds = getPathBounds(tree, tree.getLeadSelectionPath());
0N/A if(pBounds != null)
0N/A tree.repaint(getRepaintPathBounds(pBounds));
0N/A pBounds = getPathBounds(tree, getLeadSelectionPath());
0N/A if(pBounds != null)
0N/A tree.repaint(getRepaintPathBounds(pBounds));
0N/A }
0N/A }
0N/A
0N/A public void focusLost(FocusEvent e) {
0N/A focusGained(e);
0N/A }
0N/A
0N/A //
0N/A // CellEditorListener
0N/A //
0N/A public void editingStopped(ChangeEvent e) {
0N/A completeEditing(false, false, true);
0N/A }
0N/A
0N/A /** Messaged when editing has been canceled in the tree. */
0N/A public void editingCanceled(ChangeEvent e) {
0N/A completeEditing(false, false, false);
0N/A }
0N/A
0N/A
0N/A //
0N/A // TreeSelectionListener
0N/A //
0N/A public void valueChanged(TreeSelectionEvent event) {
0N/A valueChangedOnPress = true;
0N/A
0N/A // Stop editing
0N/A completeEditing();
0N/A // Make sure all the paths are visible, if necessary.
0N/A // PENDING: This should be tweaked when isAdjusting is added
0N/A if(tree.getExpandsSelectedPaths() && treeSelectionModel != null) {
0N/A TreePath[] paths = treeSelectionModel
0N/A .getSelectionPaths();
0N/A
0N/A if(paths != null) {
0N/A for(int counter = paths.length - 1; counter >= 0;
0N/A counter--) {
0N/A TreePath path = paths[counter].getParentPath();
0N/A boolean expand = true;
0N/A
0N/A while (path != null) {
0N/A // Indicates this path isn't valid anymore,
0N/A // we shouldn't attempt to expand it then.
0N/A if (treeModel.isLeaf(path.getLastPathComponent())){
0N/A expand = false;
0N/A path = null;
0N/A }
0N/A else {
0N/A path = path.getParentPath();
0N/A }
0N/A }
0N/A if (expand) {
0N/A tree.makeVisible(paths[counter]);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A TreePath oldLead = getLeadSelectionPath();
0N/A lastSelectedRow = tree.getMinSelectionRow();
0N/A TreePath lead = tree.getSelectionModel().getLeadSelectionPath();
0N/A setAnchorSelectionPath(lead);
0N/A setLeadSelectionPath(lead);
0N/A
0N/A TreePath[] changedPaths = event.getPaths();
0N/A Rectangle nodeBounds;
0N/A Rectangle visRect = tree.getVisibleRect();
0N/A boolean paintPaths = true;
0N/A int nWidth = tree.getWidth();
0N/A
0N/A if(changedPaths != null) {
0N/A int counter, maxCounter = changedPaths.length;
0N/A
0N/A if(maxCounter > 4) {
0N/A tree.repaint();
0N/A paintPaths = false;
0N/A }
0N/A else {
0N/A for (counter = 0; counter < maxCounter; counter++) {
0N/A nodeBounds = getPathBounds(tree,
0N/A changedPaths[counter]);
0N/A if(nodeBounds != null &&
0N/A visRect.intersects(nodeBounds))
0N/A tree.repaint(0, nodeBounds.y, nWidth,
0N/A nodeBounds.height);
0N/A }
0N/A }
0N/A }
0N/A if(paintPaths) {
0N/A nodeBounds = getPathBounds(tree, oldLead);
0N/A if(nodeBounds != null && visRect.intersects(nodeBounds))
0N/A tree.repaint(0, nodeBounds.y, nWidth, nodeBounds.height);
0N/A nodeBounds = getPathBounds(tree, lead);
0N/A if(nodeBounds != null && visRect.intersects(nodeBounds))
0N/A tree.repaint(0, nodeBounds.y, nWidth, nodeBounds.height);
0N/A }
0N/A }
0N/A
0N/A
0N/A //
0N/A // TreeExpansionListener
0N/A //
0N/A public void treeExpanded(TreeExpansionEvent event) {
0N/A if(event != null && tree != null) {
0N/A TreePath path = event.getPath();
0N/A
0N/A updateExpandedDescendants(path);
0N/A }
0N/A }
0N/A
0N/A public void treeCollapsed(TreeExpansionEvent event) {
0N/A if(event != null && tree != null) {
0N/A TreePath path = event.getPath();
0N/A
0N/A completeEditing();
0N/A if(path != null && tree.isVisible(path)) {
0N/A treeState.setExpandedState(path, false);
1999N/A updateLeadSelectionRow();
0N/A updateSize();
0N/A }
0N/A }
0N/A }
0N/A
0N/A //
0N/A // TreeModelListener
0N/A //
0N/A public void treeNodesChanged(TreeModelEvent e) {
0N/A if(treeState != null && e != null) {
0N/A TreePath parentPath = e.getTreePath();
0N/A int[] indices = e.getChildIndices();
0N/A if (indices == null || indices.length == 0) {
0N/A // The root has changed
0N/A treeState.treeNodesChanged(e);
0N/A updateSize();
0N/A }
0N/A else if (treeState.isExpanded(parentPath)) {
0N/A // Changed nodes are visible
0N/A // Find the minimum index, we only need paint from there
0N/A // down.
0N/A int minIndex = indices[0];
0N/A for (int i = indices.length - 1; i > 0; i--) {
0N/A minIndex = Math.min(indices[i], minIndex);
0N/A }
0N/A Object minChild = treeModel.getChild(
0N/A parentPath.getLastPathComponent(), minIndex);
0N/A TreePath minPath = parentPath.pathByAddingChild(minChild);
0N/A Rectangle minBounds = getPathBounds(tree, minPath);
0N/A
0N/A // Forward to the treestate
0N/A treeState.treeNodesChanged(e);
0N/A
0N/A // Mark preferred size as bogus.
0N/A updateSize0();
0N/A
0N/A // And repaint
0N/A Rectangle newMinBounds = getPathBounds(tree, minPath);
5532N/A if (minBounds == null || newMinBounds == null) {
5532N/A return;
5532N/A }
5532N/A
0N/A if (indices.length == 1 &&
0N/A newMinBounds.height == minBounds.height) {
0N/A tree.repaint(0, minBounds.y, tree.getWidth(),
0N/A minBounds.height);
0N/A }
0N/A else {
0N/A tree.repaint(0, minBounds.y, tree.getWidth(),
0N/A tree.getHeight() - minBounds.y);
0N/A }
0N/A }
0N/A else {
0N/A // Nodes that changed aren't visible. No need to paint
0N/A treeState.treeNodesChanged(e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void treeNodesInserted(TreeModelEvent e) {
0N/A if(treeState != null && e != null) {
0N/A treeState.treeNodesInserted(e);
0N/A
1999N/A updateLeadSelectionRow();
0N/A
0N/A TreePath path = e.getTreePath();
0N/A
0N/A if(treeState.isExpanded(path)) {
0N/A updateSize();
0N/A }
0N/A else {
0N/A // PENDING(sky): Need a method in TreeModelEvent
0N/A // that can return the count, getChildIndices allocs
0N/A // a new array!
0N/A int[] indices = e.getChildIndices();
0N/A int childCount = treeModel.getChildCount
0N/A (path.getLastPathComponent());
0N/A
0N/A if(indices != null && (childCount - indices.length) == 0)
0N/A updateSize();
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void treeNodesRemoved(TreeModelEvent e) {
0N/A if(treeState != null && e != null) {
0N/A treeState.treeNodesRemoved(e);
0N/A
1999N/A updateLeadSelectionRow();
0N/A
0N/A TreePath path = e.getTreePath();
0N/A
0N/A if(treeState.isExpanded(path) ||
0N/A treeModel.getChildCount(path.getLastPathComponent()) == 0)
0N/A updateSize();
0N/A }
0N/A }
0N/A
0N/A public void treeStructureChanged(TreeModelEvent e) {
0N/A if(treeState != null && e != null) {
0N/A treeState.treeStructureChanged(e);
0N/A
1999N/A updateLeadSelectionRow();
0N/A
0N/A TreePath pPath = e.getTreePath();
0N/A
0N/A if (pPath != null) {
0N/A pPath = pPath.getParentPath();
0N/A }
0N/A if(pPath == null || treeState.isExpanded(pPath))
0N/A updateSize();
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A
0N/A private static class Actions extends UIAction {
0N/A private static final String SELECT_PREVIOUS = "selectPrevious";
0N/A private static final String SELECT_PREVIOUS_CHANGE_LEAD =
0N/A "selectPreviousChangeLead";
0N/A private static final String SELECT_PREVIOUS_EXTEND_SELECTION =
0N/A "selectPreviousExtendSelection";
0N/A private static final String SELECT_NEXT = "selectNext";
0N/A private static final String SELECT_NEXT_CHANGE_LEAD =
0N/A "selectNextChangeLead";
0N/A private static final String SELECT_NEXT_EXTEND_SELECTION =
0N/A "selectNextExtendSelection";
0N/A private static final String SELECT_CHILD = "selectChild";
0N/A private static final String SELECT_CHILD_CHANGE_LEAD =
0N/A "selectChildChangeLead";
0N/A private static final String SELECT_PARENT = "selectParent";
0N/A private static final String SELECT_PARENT_CHANGE_LEAD =
0N/A "selectParentChangeLead";
0N/A private static final String SCROLL_UP_CHANGE_SELECTION =
0N/A "scrollUpChangeSelection";
0N/A private static final String SCROLL_UP_CHANGE_LEAD =
0N/A "scrollUpChangeLead";
0N/A private static final String SCROLL_UP_EXTEND_SELECTION =
0N/A "scrollUpExtendSelection";
0N/A private static final String SCROLL_DOWN_CHANGE_SELECTION =
0N/A "scrollDownChangeSelection";
0N/A private static final String SCROLL_DOWN_EXTEND_SELECTION =
0N/A "scrollDownExtendSelection";
0N/A private static final String SCROLL_DOWN_CHANGE_LEAD =
0N/A "scrollDownChangeLead";
0N/A private static final String SELECT_FIRST = "selectFirst";
0N/A private static final String SELECT_FIRST_CHANGE_LEAD =
0N/A "selectFirstChangeLead";
0N/A private static final String SELECT_FIRST_EXTEND_SELECTION =
0N/A "selectFirstExtendSelection";
0N/A private static final String SELECT_LAST = "selectLast";
0N/A private static final String SELECT_LAST_CHANGE_LEAD =
0N/A "selectLastChangeLead";
0N/A private static final String SELECT_LAST_EXTEND_SELECTION =
0N/A "selectLastExtendSelection";
0N/A private static final String TOGGLE = "toggle";
0N/A private static final String CANCEL_EDITING = "cancel";
0N/A private static final String START_EDITING = "startEditing";
0N/A private static final String SELECT_ALL = "selectAll";
0N/A private static final String CLEAR_SELECTION = "clearSelection";
0N/A private static final String SCROLL_LEFT = "scrollLeft";
0N/A private static final String SCROLL_RIGHT = "scrollRight";
0N/A private static final String SCROLL_LEFT_EXTEND_SELECTION =
0N/A "scrollLeftExtendSelection";
0N/A private static final String SCROLL_RIGHT_EXTEND_SELECTION =
0N/A "scrollRightExtendSelection";
0N/A private static final String SCROLL_RIGHT_CHANGE_LEAD =
0N/A "scrollRightChangeLead";
0N/A private static final String SCROLL_LEFT_CHANGE_LEAD =
0N/A "scrollLeftChangeLead";
0N/A private static final String EXPAND = "expand";
0N/A private static final String COLLAPSE = "collapse";
0N/A private static final String MOVE_SELECTION_TO_PARENT =
0N/A "moveSelectionToParent";
0N/A
0N/A // add the lead item to the selection without changing lead or anchor
0N/A private static final String ADD_TO_SELECTION = "addToSelection";
0N/A
0N/A // toggle the selected state of the lead item and move the anchor to it
0N/A private static final String TOGGLE_AND_ANCHOR = "toggleAndAnchor";
0N/A
0N/A // extend the selection to the lead item
0N/A private static final String EXTEND_TO = "extendTo";
0N/A
0N/A // move the anchor to the lead and ensure only that item is selected
0N/A private static final String MOVE_SELECTION_TO = "moveSelectionTo";
0N/A
0N/A Actions() {
0N/A super(null);
0N/A }
0N/A
0N/A Actions(String key) {
0N/A super(key);
0N/A }
0N/A
0N/A public boolean isEnabled(Object o) {
0N/A if (o instanceof JTree) {
0N/A if (getName() == CANCEL_EDITING) {
0N/A return ((JTree)o).isEditing();
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A JTree tree = (JTree)e.getSource();
0N/A BasicTreeUI ui = (BasicTreeUI)BasicLookAndFeel.getUIOfType(
0N/A tree.getUI(), BasicTreeUI.class);
0N/A if (ui == null) {
0N/A return;
0N/A }
0N/A String key = getName();
0N/A if (key == SELECT_PREVIOUS) {
0N/A increment(tree, ui, -1, false, true);
0N/A }
0N/A else if (key == SELECT_PREVIOUS_CHANGE_LEAD) {
0N/A increment(tree, ui, -1, false, false);
0N/A }
0N/A else if (key == SELECT_PREVIOUS_EXTEND_SELECTION) {
0N/A increment(tree, ui, -1, true, true);
0N/A }
0N/A else if (key == SELECT_NEXT) {
0N/A increment(tree, ui, 1, false, true);
0N/A }
0N/A else if (key == SELECT_NEXT_CHANGE_LEAD) {
0N/A increment(tree, ui, 1, false, false);
0N/A }
0N/A else if (key == SELECT_NEXT_EXTEND_SELECTION) {
0N/A increment(tree, ui, 1, true, true);
0N/A }
0N/A else if (key == SELECT_CHILD) {
0N/A traverse(tree, ui, 1, true);
0N/A }
0N/A else if (key == SELECT_CHILD_CHANGE_LEAD) {
0N/A traverse(tree, ui, 1, false);
0N/A }
0N/A else if (key == SELECT_PARENT) {
0N/A traverse(tree, ui, -1, true);
0N/A }
0N/A else if (key == SELECT_PARENT_CHANGE_LEAD) {
0N/A traverse(tree, ui, -1, false);
0N/A }
0N/A else if (key == SCROLL_UP_CHANGE_SELECTION) {
0N/A page(tree, ui, -1, false, true);
0N/A }
0N/A else if (key == SCROLL_UP_CHANGE_LEAD) {
0N/A page(tree, ui, -1, false, false);
0N/A }
0N/A else if (key == SCROLL_UP_EXTEND_SELECTION) {
0N/A page(tree, ui, -1, true, true);
0N/A }
0N/A else if (key == SCROLL_DOWN_CHANGE_SELECTION) {
0N/A page(tree, ui, 1, false, true);
0N/A }
0N/A else if (key == SCROLL_DOWN_EXTEND_SELECTION) {
0N/A page(tree, ui, 1, true, true);
0N/A }
0N/A else if (key == SCROLL_DOWN_CHANGE_LEAD) {
0N/A page(tree, ui, 1, false, false);
0N/A }
0N/A else if (key == SELECT_FIRST) {
0N/A home(tree, ui, -1, false, true);
0N/A }
0N/A else if (key == SELECT_FIRST_CHANGE_LEAD) {
0N/A home(tree, ui, -1, false, false);
0N/A }
0N/A else if (key == SELECT_FIRST_EXTEND_SELECTION) {
0N/A home(tree, ui, -1, true, true);
0N/A }
0N/A else if (key == SELECT_LAST) {
0N/A home(tree, ui, 1, false, true);
0N/A }
0N/A else if (key == SELECT_LAST_CHANGE_LEAD) {
0N/A home(tree, ui, 1, false, false);
0N/A }
0N/A else if (key == SELECT_LAST_EXTEND_SELECTION) {
0N/A home(tree, ui, 1, true, true);
0N/A }
0N/A else if (key == TOGGLE) {
0N/A toggle(tree, ui);
0N/A }
0N/A else if (key == CANCEL_EDITING) {
0N/A cancelEditing(tree, ui);
0N/A }
0N/A else if (key == START_EDITING) {
0N/A startEditing(tree, ui);
0N/A }
0N/A else if (key == SELECT_ALL) {
0N/A selectAll(tree, ui, true);
0N/A }
0N/A else if (key == CLEAR_SELECTION) {
0N/A selectAll(tree, ui, false);
0N/A }
0N/A else if (key == ADD_TO_SELECTION) {
0N/A if (ui.getRowCount(tree) > 0) {
0N/A int lead = ui.getLeadSelectionRow();
0N/A if (!tree.isRowSelected(lead)) {
0N/A TreePath aPath = ui.getAnchorSelectionPath();
0N/A tree.addSelectionRow(lead);
0N/A ui.setAnchorSelectionPath(aPath);
0N/A }
0N/A }
0N/A }
0N/A else if (key == TOGGLE_AND_ANCHOR) {
0N/A if (ui.getRowCount(tree) > 0) {
0N/A int lead = ui.getLeadSelectionRow();
0N/A TreePath lPath = ui.getLeadSelectionPath();
0N/A if (!tree.isRowSelected(lead)) {
0N/A tree.addSelectionRow(lead);
0N/A } else {
0N/A tree.removeSelectionRow(lead);
0N/A ui.setLeadSelectionPath(lPath);
0N/A }
0N/A ui.setAnchorSelectionPath(lPath);
0N/A }
0N/A }
0N/A else if (key == EXTEND_TO) {
0N/A extendSelection(tree, ui);
0N/A }
0N/A else if (key == MOVE_SELECTION_TO) {
0N/A if (ui.getRowCount(tree) > 0) {
0N/A int lead = ui.getLeadSelectionRow();
0N/A tree.setSelectionInterval(lead, lead);
0N/A }
0N/A }
0N/A else if (key == SCROLL_LEFT) {
0N/A scroll(tree, ui, SwingConstants.HORIZONTAL, -10);
0N/A }
0N/A else if (key == SCROLL_RIGHT) {
0N/A scroll(tree, ui, SwingConstants.HORIZONTAL, 10);
0N/A }
0N/A else if (key == SCROLL_LEFT_EXTEND_SELECTION) {
0N/A scrollChangeSelection(tree, ui, -1, true, true);
0N/A }
0N/A else if (key == SCROLL_RIGHT_EXTEND_SELECTION) {
0N/A scrollChangeSelection(tree, ui, 1, true, true);
0N/A }
0N/A else if (key == SCROLL_RIGHT_CHANGE_LEAD) {
0N/A scrollChangeSelection(tree, ui, 1, false, false);
0N/A }
0N/A else if (key == SCROLL_LEFT_CHANGE_LEAD) {
0N/A scrollChangeSelection(tree, ui, -1, false, false);
0N/A }
0N/A else if (key == EXPAND) {
0N/A expand(tree, ui);
0N/A }
0N/A else if (key == COLLAPSE) {
0N/A collapse(tree, ui);
0N/A }
0N/A else if (key == MOVE_SELECTION_TO_PARENT) {
0N/A moveSelectionToParent(tree, ui);
0N/A }
0N/A }
0N/A
0N/A private void scrollChangeSelection(JTree tree, BasicTreeUI ui,
0N/A int direction, boolean addToSelection,
0N/A boolean changeSelection) {
0N/A int rowCount;
0N/A
0N/A if((rowCount = ui.getRowCount(tree)) > 0 &&
0N/A ui.treeSelectionModel != null) {
0N/A TreePath newPath;
0N/A Rectangle visRect = tree.getVisibleRect();
0N/A
0N/A if (direction == -1) {
0N/A newPath = ui.getClosestPathForLocation(tree, visRect.x,
0N/A visRect.y);
0N/A visRect.x = Math.max(0, visRect.x - visRect.width);
0N/A }
0N/A else {
0N/A visRect.x = Math.min(Math.max(0, tree.getWidth() -
0N/A visRect.width), visRect.x + visRect.width);
0N/A newPath = ui.getClosestPathForLocation(tree, visRect.x,
0N/A visRect.y + visRect.height);
0N/A }
0N/A // Scroll
0N/A tree.scrollRectToVisible(visRect);
0N/A // select
0N/A if (addToSelection) {
0N/A ui.extendSelection(newPath);
0N/A }
0N/A else if(changeSelection) {
0N/A tree.setSelectionPath(newPath);
0N/A }
0N/A else {
0N/A ui.setLeadSelectionPath(newPath, true);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void scroll(JTree component, BasicTreeUI ui, int direction,
0N/A int amount) {
0N/A Rectangle visRect = component.getVisibleRect();
0N/A Dimension size = component.getSize();
0N/A if (direction == SwingConstants.HORIZONTAL) {
0N/A visRect.x += amount;
0N/A visRect.x = Math.max(0, visRect.x);
0N/A visRect.x = Math.min(Math.max(0, size.width - visRect.width),
0N/A visRect.x);
0N/A }
0N/A else {
0N/A visRect.y += amount;
0N/A visRect.y = Math.max(0, visRect.y);
0N/A visRect.y = Math.min(Math.max(0, size.width - visRect.height),
0N/A visRect.y);
0N/A }
0N/A component.scrollRectToVisible(visRect);
0N/A }
0N/A
0N/A private void extendSelection(JTree tree, BasicTreeUI ui) {
0N/A if (ui.getRowCount(tree) > 0) {
0N/A int lead = ui.getLeadSelectionRow();
0N/A
0N/A if (lead != -1) {
0N/A TreePath leadP = ui.getLeadSelectionPath();
0N/A TreePath aPath = ui.getAnchorSelectionPath();
0N/A int aRow = ui.getRowForPath(tree, aPath);
0N/A
0N/A if(aRow == -1)
0N/A aRow = 0;
0N/A tree.setSelectionInterval(aRow, lead);
0N/A ui.setLeadSelectionPath(leadP);
0N/A ui.setAnchorSelectionPath(aPath);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void selectAll(JTree tree, BasicTreeUI ui, boolean selectAll) {
0N/A int rowCount = ui.getRowCount(tree);
0N/A
0N/A if(rowCount > 0) {
0N/A if(selectAll) {
0N/A if (tree.getSelectionModel().getSelectionMode() ==
0N/A TreeSelectionModel.SINGLE_TREE_SELECTION) {
0N/A
0N/A int lead = ui.getLeadSelectionRow();
0N/A if (lead != -1) {
0N/A tree.setSelectionRow(lead);
0N/A } else if (tree.getMinSelectionRow() == -1) {
0N/A tree.setSelectionRow(0);
0N/A ui.ensureRowsAreVisible(0, 0);
0N/A }
0N/A return;
0N/A }
0N/A
0N/A TreePath lastPath = ui.getLeadSelectionPath();
0N/A TreePath aPath = ui.getAnchorSelectionPath();
0N/A
0N/A if(lastPath != null && !tree.isVisible(lastPath)) {
0N/A lastPath = null;
0N/A }
0N/A tree.setSelectionInterval(0, rowCount - 1);
0N/A if(lastPath != null) {
0N/A ui.setLeadSelectionPath(lastPath);
0N/A }
0N/A if(aPath != null && tree.isVisible(aPath)) {
0N/A ui.setAnchorSelectionPath(aPath);
0N/A }
0N/A }
0N/A else {
0N/A TreePath lastPath = ui.getLeadSelectionPath();
0N/A TreePath aPath = ui.getAnchorSelectionPath();
0N/A
0N/A tree.clearSelection();
0N/A ui.setAnchorSelectionPath(aPath);
0N/A ui.setLeadSelectionPath(lastPath);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void startEditing(JTree tree, BasicTreeUI ui) {
0N/A TreePath lead = ui.getLeadSelectionPath();
0N/A int editRow = (lead != null) ?
0N/A ui.getRowForPath(tree, lead) : -1;
0N/A
0N/A if(editRow != -1) {
0N/A tree.startEditingAtPath(lead);
0N/A }
0N/A }
0N/A
0N/A private void cancelEditing(JTree tree, BasicTreeUI ui) {
0N/A tree.cancelEditing();
0N/A }
0N/A
0N/A private void toggle(JTree tree, BasicTreeUI ui) {
0N/A int selRow = ui.getLeadSelectionRow();
0N/A
0N/A if(selRow != -1 && !ui.isLeaf(selRow)) {
0N/A TreePath aPath = ui.getAnchorSelectionPath();
0N/A TreePath lPath = ui.getLeadSelectionPath();
0N/A
0N/A ui.toggleExpandState(ui.getPathForRow(tree, selRow));
0N/A ui.setAnchorSelectionPath(aPath);
0N/A ui.setLeadSelectionPath(lPath);
0N/A }
0N/A }
0N/A
0N/A private void expand(JTree tree, BasicTreeUI ui) {
0N/A int selRow = ui.getLeadSelectionRow();
0N/A tree.expandRow(selRow);
0N/A }
0N/A
0N/A private void collapse(JTree tree, BasicTreeUI ui) {
0N/A int selRow = ui.getLeadSelectionRow();
0N/A tree.collapseRow(selRow);
0N/A }
0N/A
0N/A private void increment(JTree tree, BasicTreeUI ui, int direction,
0N/A boolean addToSelection,
0N/A boolean changeSelection) {
0N/A
0N/A // disable moving of lead unless in discontiguous mode
0N/A if (!addToSelection && !changeSelection &&
0N/A tree.getSelectionModel().getSelectionMode() !=
0N/A TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION) {
0N/A changeSelection = true;
0N/A }
0N/A
0N/A int rowCount;
0N/A
0N/A if(ui.treeSelectionModel != null &&
0N/A (rowCount = tree.getRowCount()) > 0) {
0N/A int selIndex = ui.getLeadSelectionRow();
0N/A int newIndex;
0N/A
0N/A if(selIndex == -1) {
0N/A if(direction == 1)
0N/A newIndex = 0;
0N/A else
0N/A newIndex = rowCount - 1;
0N/A }
0N/A else
0N/A /* Aparently people don't like wrapping;( */
0N/A newIndex = Math.min(rowCount - 1, Math.max
0N/A (0, (selIndex + direction)));
0N/A if(addToSelection && ui.treeSelectionModel.
0N/A getSelectionMode() != TreeSelectionModel.
0N/A SINGLE_TREE_SELECTION) {
0N/A ui.extendSelection(tree.getPathForRow(newIndex));
0N/A }
0N/A else if(changeSelection) {
0N/A tree.setSelectionInterval(newIndex, newIndex);
0N/A }
0N/A else {
0N/A ui.setLeadSelectionPath(tree.getPathForRow(newIndex),true);
0N/A }
0N/A ui.ensureRowsAreVisible(newIndex, newIndex);
0N/A ui.lastSelectedRow = newIndex;
0N/A }
0N/A }
0N/A
0N/A private void traverse(JTree tree, BasicTreeUI ui, int direction,
0N/A boolean changeSelection) {
0N/A
0N/A // disable moving of lead unless in discontiguous mode
0N/A if (!changeSelection &&
0N/A tree.getSelectionModel().getSelectionMode() !=
0N/A TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION) {
0N/A changeSelection = true;
0N/A }
0N/A
0N/A int rowCount;
0N/A
0N/A if((rowCount = tree.getRowCount()) > 0) {
0N/A int minSelIndex = ui.getLeadSelectionRow();
0N/A int newIndex;
0N/A
0N/A if(minSelIndex == -1)
0N/A newIndex = 0;
0N/A else {
0N/A /* Try and expand the node, otherwise go to next
0N/A node. */
0N/A if(direction == 1) {
0N/A TreePath minSelPath = ui.getPathForRow(tree, minSelIndex);
0N/A int childCount = tree.getModel().
0N/A getChildCount(minSelPath.getLastPathComponent());
0N/A newIndex = -1;
0N/A if (!ui.isLeaf(minSelIndex)) {
0N/A if (!tree.isExpanded(minSelIndex)) {
0N/A ui.toggleExpandState(minSelPath);
0N/A }
0N/A else if (childCount > 0) {
0N/A newIndex = Math.min(minSelIndex + 1, rowCount - 1);
0N/A }
0N/A }
0N/A }
0N/A /* Try to collapse node. */
0N/A else {
0N/A if(!ui.isLeaf(minSelIndex) &&
0N/A tree.isExpanded(minSelIndex)) {
0N/A ui.toggleExpandState(ui.getPathForRow
0N/A (tree, minSelIndex));
0N/A newIndex = -1;
0N/A }
0N/A else {
0N/A TreePath path = ui.getPathForRow(tree,
0N/A minSelIndex);
0N/A
0N/A if(path != null && path.getPathCount() > 1) {
0N/A newIndex = ui.getRowForPath(tree, path.
0N/A getParentPath());
0N/A }
0N/A else
0N/A newIndex = -1;
0N/A }
0N/A }
0N/A }
0N/A if(newIndex != -1) {
0N/A if(changeSelection) {
0N/A tree.setSelectionInterval(newIndex, newIndex);
0N/A }
0N/A else {
0N/A ui.setLeadSelectionPath(ui.getPathForRow(
0N/A tree, newIndex), true);
0N/A }
0N/A ui.ensureRowsAreVisible(newIndex, newIndex);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void moveSelectionToParent(JTree tree, BasicTreeUI ui) {
0N/A int selRow = ui.getLeadSelectionRow();
0N/A TreePath path = ui.getPathForRow(tree, selRow);
0N/A if (path != null && path.getPathCount() > 1) {
0N/A int newIndex = ui.getRowForPath(tree, path.getParentPath());
0N/A if (newIndex != -1) {
0N/A tree.setSelectionInterval(newIndex, newIndex);
0N/A ui.ensureRowsAreVisible(newIndex, newIndex);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void page(JTree tree, BasicTreeUI ui, int direction,
0N/A boolean addToSelection, boolean changeSelection) {
0N/A
0N/A // disable moving of lead unless in discontiguous mode
0N/A if (!addToSelection && !changeSelection &&
0N/A tree.getSelectionModel().getSelectionMode() !=
0N/A TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION) {
0N/A changeSelection = true;
0N/A }
0N/A
0N/A int rowCount;
0N/A
0N/A if((rowCount = ui.getRowCount(tree)) > 0 &&
0N/A ui.treeSelectionModel != null) {
0N/A Dimension maxSize = tree.getSize();
0N/A TreePath lead = ui.getLeadSelectionPath();
0N/A TreePath newPath;
0N/A Rectangle visRect = tree.getVisibleRect();
0N/A
0N/A if(direction == -1) {
0N/A // up.
0N/A newPath = ui.getClosestPathForLocation(tree, visRect.x,
0N/A visRect.y);
0N/A if(newPath.equals(lead)) {
0N/A visRect.y = Math.max(0, visRect.y - visRect.height);
0N/A newPath = tree.getClosestPathForLocation(visRect.x,
0N/A visRect.y);
0N/A }
0N/A }
0N/A else {
0N/A // down
0N/A visRect.y = Math.min(maxSize.height, visRect.y +
0N/A visRect.height - 1);
0N/A newPath = tree.getClosestPathForLocation(visRect.x,
0N/A visRect.y);
0N/A if(newPath.equals(lead)) {
0N/A visRect.y = Math.min(maxSize.height, visRect.y +
0N/A visRect.height - 1);
0N/A newPath = tree.getClosestPathForLocation(visRect.x,
0N/A visRect.y);
0N/A }
0N/A }
0N/A Rectangle newRect = ui.getPathBounds(tree, newPath);
5533N/A if (newRect != null) {
5533N/A newRect.x = visRect.x;
5533N/A newRect.width = visRect.width;
5533N/A if(direction == -1) {
5533N/A newRect.height = visRect.height;
5533N/A }
5533N/A else {
5533N/A newRect.y -= (visRect.height - newRect.height);
5533N/A newRect.height = visRect.height;
5533N/A }
5533N/A
5533N/A if(addToSelection) {
5533N/A ui.extendSelection(newPath);
5533N/A }
5533N/A else if(changeSelection) {
5533N/A tree.setSelectionPath(newPath);
5533N/A }
5533N/A else {
5533N/A ui.setLeadSelectionPath(newPath, true);
5533N/A }
5533N/A tree.scrollRectToVisible(newRect);
0N/A }
0N/A }
0N/A }
0N/A
5659N/A private void home(JTree tree, final BasicTreeUI ui, int direction,
0N/A boolean addToSelection, boolean changeSelection) {
0N/A
0N/A // disable moving of lead unless in discontiguous mode
0N/A if (!addToSelection && !changeSelection &&
0N/A tree.getSelectionModel().getSelectionMode() !=
0N/A TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION) {
0N/A changeSelection = true;
0N/A }
0N/A
5659N/A final int rowCount = ui.getRowCount(tree);
0N/A
0N/A if (rowCount > 0) {
0N/A if(direction == -1) {
0N/A ui.ensureRowsAreVisible(0, 0);
0N/A if (addToSelection) {
0N/A TreePath aPath = ui.getAnchorSelectionPath();
0N/A int aRow = (aPath == null) ? -1 :
0N/A ui.getRowForPath(tree, aPath);
0N/A
0N/A if (aRow == -1) {
0N/A tree.setSelectionInterval(0, 0);
0N/A }
0N/A else {
0N/A tree.setSelectionInterval(0, aRow);
0N/A ui.setAnchorSelectionPath(aPath);
0N/A ui.setLeadSelectionPath(ui.getPathForRow(tree, 0));
0N/A }
0N/A }
0N/A else if(changeSelection) {
0N/A tree.setSelectionInterval(0, 0);
0N/A }
0N/A else {
0N/A ui.setLeadSelectionPath(ui.getPathForRow(tree, 0),
0N/A true);
0N/A }
0N/A }
0N/A else {
0N/A ui.ensureRowsAreVisible(rowCount - 1, rowCount - 1);
0N/A if (addToSelection) {
0N/A TreePath aPath = ui.getAnchorSelectionPath();
0N/A int aRow = (aPath == null) ? -1 :
0N/A ui.getRowForPath(tree, aPath);
0N/A
0N/A if (aRow == -1) {
0N/A tree.setSelectionInterval(rowCount - 1,
0N/A rowCount -1);
0N/A }
0N/A else {
0N/A tree.setSelectionInterval(aRow, rowCount - 1);
0N/A ui.setAnchorSelectionPath(aPath);
0N/A ui.setLeadSelectionPath(ui.getPathForRow(tree,
0N/A rowCount -1));
0N/A }
0N/A }
0N/A else if(changeSelection) {
0N/A tree.setSelectionInterval(rowCount - 1, rowCount - 1);
0N/A }
0N/A else {
0N/A ui.setLeadSelectionPath(ui.getPathForRow(tree,
0N/A rowCount - 1), true);
0N/A }
5659N/A if (ui.isLargeModel()){
5659N/A SwingUtilities.invokeLater(new Runnable() {
5659N/A public void run() {
5659N/A ui.ensureRowsAreVisible(rowCount - 1, rowCount - 1);
5659N/A }
5659N/A });
5659N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A} // End of class BasicTreeUI