1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing.plaf.basic;
  27 
  28 import javax.swing.*;
  29 import javax.swing.event.*;
  30 import java.awt.*;
  31 import java.awt.event.*;
  32 import java.awt.datatransfer.*;
  33 import java.beans.*;
  34 import java.util.Enumeration;
  35 import java.util.Hashtable;
  36 import java.util.ArrayList;
  37 import java.util.Collections;
  38 import java.util.Comparator;
  39 import javax.swing.plaf.ComponentUI;
  40 import javax.swing.plaf.UIResource;
  41 import javax.swing.plaf.TreeUI;
  42 import javax.swing.tree.*;
  43 import javax.swing.text.Position;
  44 import javax.swing.plaf.basic.DragRecognitionSupport.BeforeDrag;
  45 import sun.awt.AWTAccessor;
  46 import sun.swing.SwingUtilities2;
  47 
  48 import sun.swing.DefaultLookup;
  49 import sun.swing.UIAction;
  50 
  51 /**
  52  * The basic L&F for a hierarchical data structure.
  53  *
  54  * @author Scott Violet
  55  * @author Shannon Hickey (drag and drop)
  56  */
  57 
  58 public class BasicTreeUI extends TreeUI
  59 {
  60     private static final StringBuilder BASELINE_COMPONENT_KEY =
  61         new StringBuilder("Tree.baselineComponent");
  62 
  63     // Old actions forward to an instance of this.
  64     static private final Actions SHARED_ACTION = new Actions();
  65 
  66     transient protected Icon        collapsedIcon;
  67     transient protected Icon        expandedIcon;
  68 
  69     /**
  70       * Color used to draw hash marks.  If <code>null</code> no hash marks
  71       * will be drawn.
  72       */
  73     private Color hashColor;
  74 
  75     /** Distance between left margin and where vertical dashes will be
  76       * drawn. */
  77     protected int               leftChildIndent;
  78     /** Distance to add to leftChildIndent to determine where cell
  79       * contents will be drawn. */
  80     protected int               rightChildIndent;
  81     /** Total distance that will be indented.  The sum of leftChildIndent
  82       * and rightChildIndent. */
  83     protected int               totalChildIndent;
  84 
  85     /** Minimum preferred size. */
  86     protected Dimension         preferredMinSize;
  87 
  88     /** Index of the row that was last selected. */
  89     protected int               lastSelectedRow;
  90 
  91     /** Component that we're going to be drawing into. */
  92     protected JTree             tree;
  93 
  94     /** Renderer that is being used to do the actual cell drawing. */
  95     transient protected TreeCellRenderer   currentCellRenderer;
  96 
  97     /** Set to true if the renderer that is currently in the tree was
  98      * created by this instance. */
  99     protected boolean           createdRenderer;
 100 
 101     /** Editor for the tree. */
 102     transient protected TreeCellEditor     cellEditor;
 103 
 104     /** Set to true if editor that is currently in the tree was
 105      * created by this instance. */
 106     protected boolean           createdCellEditor;
 107 
 108     /** Set to false when editing and shouldSelectCell() returns true meaning
 109       * the node should be selected before editing, used in completeEditing. */
 110     protected boolean           stopEditingInCompleteEditing;
 111 
 112     /** Used to paint the TreeCellRenderer. */
 113     protected CellRendererPane  rendererPane;
 114 
 115     /** Size needed to completely display all the nodes. */
 116     protected Dimension         preferredSize;
 117 
 118     /** Is the preferredSize valid? */
 119     protected boolean           validCachedPreferredSize;
 120 
 121     /** Object responsible for handling sizing and expanded issues. */
 122     // WARNING: Be careful with the bounds held by treeState. They are
 123     // always in terms of left-to-right. They get mapped to right-to-left
 124     // by the various methods of this class.
 125     protected AbstractLayoutCache  treeState;
 126 
 127 
 128     /** Used for minimizing the drawing of vertical lines. */
 129     protected Hashtable<TreePath,Boolean> drawingCache;
 130 
 131     /** True if doing optimizations for a largeModel. Subclasses that
 132      * don't support this may wish to override createLayoutCache to not
 133      * return a FixedHeightLayoutCache instance. */
 134     protected boolean           largeModel;
 135 
 136     /** Reponsible for telling the TreeState the size needed for a node. */
 137     protected AbstractLayoutCache.NodeDimensions     nodeDimensions;
 138 
 139     /** Used to determine what to display. */
 140     protected TreeModel         treeModel;
 141 
 142     /** Model maintaining the selection. */
 143     protected TreeSelectionModel treeSelectionModel;
 144 
 145     /** How much the depth should be offset to properly calculate
 146      * x locations. This is based on whether or not the root is visible,
 147      * and if the root handles are visible. */
 148     protected int               depthOffset;
 149 
 150     // Following 4 ivars are only valid when editing.
 151 
 152     /** When editing, this will be the Component that is doing the actual
 153       * editing. */
 154     protected Component         editingComponent;
 155 
 156     /** Path that is being edited. */
 157     protected TreePath          editingPath;
 158 
 159     /** Row that is being edited. Should only be referenced if
 160      * editingComponent is not null. */
 161     protected int               editingRow;
 162 
 163     /** Set to true if the editor has a different size than the renderer. */
 164     protected boolean           editorHasDifferentSize;
 165 
 166     /** Row correspondin to lead path. */
 167     private int                 leadRow;
 168     /** If true, the property change event for LEAD_SELECTION_PATH_PROPERTY,
 169      * or ANCHOR_SELECTION_PATH_PROPERTY will not generate a repaint. */
 170     private boolean             ignoreLAChange;
 171 
 172     /** Indicates the orientation. */
 173     private boolean             leftToRight;
 174 
 175     // Cached listeners
 176     private PropertyChangeListener propertyChangeListener;
 177     private PropertyChangeListener selectionModelPropertyChangeListener;
 178     private MouseListener mouseListener;
 179     private FocusListener focusListener;
 180     private KeyListener keyListener;
 181     /** Used for large models, listens for moved/resized events and
 182      * updates the validCachedPreferredSize bit accordingly. */
 183     private ComponentListener   componentListener;
 184     /** Listens for CellEditor events. */
 185     private CellEditorListener  cellEditorListener;
 186     /** Updates the display when the selection changes. */
 187     private TreeSelectionListener treeSelectionListener;
 188     /** Is responsible for updating the display based on model events. */
 189     private TreeModelListener treeModelListener;
 190     /** Updates the treestate as the nodes expand. */
 191     private TreeExpansionListener treeExpansionListener;
 192 
 193     /** UI property indicating whether to paint lines */
 194     private boolean paintLines = true;
 195 
 196     /** UI property for painting dashed lines */
 197     private boolean lineTypeDashed;
 198 
 199     /**
 200      * The time factor to treate the series of typed alphanumeric key
 201      * as prefix for first letter navigation.
 202      */
 203     private long timeFactor = 1000L;
 204 
 205     private Handler handler;
 206 
 207     /**
 208      * A temporary variable for communication between startEditingOnRelease
 209      * and startEditing.
 210      */
 211     private MouseEvent releaseEvent;
 212 
 213     public static ComponentUI createUI(JComponent x) {
 214         return new BasicTreeUI();
 215     }
 216 
 217 
 218     static void loadActionMap(LazyActionMap map) {
 219         map.put(new Actions(Actions.SELECT_PREVIOUS));
 220         map.put(new Actions(Actions.SELECT_PREVIOUS_CHANGE_LEAD));
 221         map.put(new Actions(Actions.SELECT_PREVIOUS_EXTEND_SELECTION));
 222 
 223         map.put(new Actions(Actions.SELECT_NEXT));
 224         map.put(new Actions(Actions.SELECT_NEXT_CHANGE_LEAD));
 225         map.put(new Actions(Actions.SELECT_NEXT_EXTEND_SELECTION));
 226 
 227         map.put(new Actions(Actions.SELECT_CHILD));
 228         map.put(new Actions(Actions.SELECT_CHILD_CHANGE_LEAD));
 229 
 230         map.put(new Actions(Actions.SELECT_PARENT));
 231         map.put(new Actions(Actions.SELECT_PARENT_CHANGE_LEAD));
 232 
 233         map.put(new Actions(Actions.SCROLL_UP_CHANGE_SELECTION));
 234         map.put(new Actions(Actions.SCROLL_UP_CHANGE_LEAD));
 235         map.put(new Actions(Actions.SCROLL_UP_EXTEND_SELECTION));
 236 
 237         map.put(new Actions(Actions.SCROLL_DOWN_CHANGE_SELECTION));
 238         map.put(new Actions(Actions.SCROLL_DOWN_EXTEND_SELECTION));
 239         map.put(new Actions(Actions.SCROLL_DOWN_CHANGE_LEAD));
 240 
 241         map.put(new Actions(Actions.SELECT_FIRST));
 242         map.put(new Actions(Actions.SELECT_FIRST_CHANGE_LEAD));
 243         map.put(new Actions(Actions.SELECT_FIRST_EXTEND_SELECTION));
 244 
 245         map.put(new Actions(Actions.SELECT_LAST));
 246         map.put(new Actions(Actions.SELECT_LAST_CHANGE_LEAD));
 247         map.put(new Actions(Actions.SELECT_LAST_EXTEND_SELECTION));
 248 
 249         map.put(new Actions(Actions.TOGGLE));
 250 
 251         map.put(new Actions(Actions.CANCEL_EDITING));
 252 
 253         map.put(new Actions(Actions.START_EDITING));
 254 
 255         map.put(new Actions(Actions.SELECT_ALL));
 256 
 257         map.put(new Actions(Actions.CLEAR_SELECTION));
 258 
 259         map.put(new Actions(Actions.SCROLL_LEFT));
 260         map.put(new Actions(Actions.SCROLL_RIGHT));
 261 
 262         map.put(new Actions(Actions.SCROLL_LEFT_EXTEND_SELECTION));
 263         map.put(new Actions(Actions.SCROLL_RIGHT_EXTEND_SELECTION));
 264 
 265         map.put(new Actions(Actions.SCROLL_RIGHT_CHANGE_LEAD));
 266         map.put(new Actions(Actions.SCROLL_LEFT_CHANGE_LEAD));
 267 
 268         map.put(new Actions(Actions.EXPAND));
 269         map.put(new Actions(Actions.COLLAPSE));
 270         map.put(new Actions(Actions.MOVE_SELECTION_TO_PARENT));
 271 
 272         map.put(new Actions(Actions.ADD_TO_SELECTION));
 273         map.put(new Actions(Actions.TOGGLE_AND_ANCHOR));
 274         map.put(new Actions(Actions.EXTEND_TO));
 275         map.put(new Actions(Actions.MOVE_SELECTION_TO));
 276 
 277         map.put(TransferHandler.getCutAction());
 278         map.put(TransferHandler.getCopyAction());
 279         map.put(TransferHandler.getPasteAction());
 280     }
 281 
 282 
 283     public BasicTreeUI() {
 284         super();
 285     }
 286 
 287     protected Color getHashColor() {
 288         return hashColor;
 289     }
 290 
 291     protected void setHashColor(Color color) {
 292         hashColor = color;
 293     }
 294 
 295     public void setLeftChildIndent(int newAmount) {
 296         leftChildIndent = newAmount;
 297         totalChildIndent = leftChildIndent + rightChildIndent;
 298         if(treeState != null)
 299             treeState.invalidateSizes();
 300         updateSize();
 301     }
 302 
 303     public int getLeftChildIndent() {
 304         return leftChildIndent;
 305     }
 306 
 307     public void setRightChildIndent(int newAmount) {
 308         rightChildIndent = newAmount;
 309         totalChildIndent = leftChildIndent + rightChildIndent;
 310         if(treeState != null)
 311             treeState.invalidateSizes();
 312         updateSize();
 313     }
 314 
 315     public int getRightChildIndent() {
 316         return rightChildIndent;
 317     }
 318 
 319     public void setExpandedIcon(Icon newG) {
 320         expandedIcon = newG;
 321     }
 322 
 323     public Icon getExpandedIcon() {
 324         return expandedIcon;
 325     }
 326 
 327     public void setCollapsedIcon(Icon newG) {
 328         collapsedIcon = newG;
 329     }
 330 
 331     public Icon getCollapsedIcon() {
 332         return collapsedIcon;
 333     }
 334 
 335     //
 336     // Methods for configuring the behavior of the tree. None of them
 337     // push the value to the JTree instance. You should really only
 338     // call these methods on the JTree.
 339     //
 340 
 341     /**
 342      * Updates the componentListener, if necessary.
 343      */
 344     protected void setLargeModel(boolean largeModel) {
 345         if(getRowHeight() < 1)
 346             largeModel = false;
 347         if(this.largeModel != largeModel) {
 348             completeEditing();
 349             this.largeModel = largeModel;
 350             treeState = createLayoutCache();
 351             configureLayoutCache();
 352             updateLayoutCacheExpandedNodesIfNecessary();
 353             updateSize();
 354         }
 355     }
 356 
 357     protected boolean isLargeModel() {
 358         return largeModel;
 359     }
 360 
 361     /**
 362      * Sets the row height, this is forwarded to the treeState.
 363      */
 364     protected void setRowHeight(int rowHeight) {
 365         completeEditing();
 366         if(treeState != null) {
 367             setLargeModel(tree.isLargeModel());
 368             treeState.setRowHeight(rowHeight);
 369             updateSize();
 370         }
 371     }
 372 
 373     protected int getRowHeight() {
 374         return (tree == null) ? -1 : tree.getRowHeight();
 375     }
 376 
 377     /**
 378      * Sets the TreeCellRenderer to <code>tcr</code>. This invokes
 379      * <code>updateRenderer</code>.
 380      */
 381     protected void setCellRenderer(TreeCellRenderer tcr) {
 382         completeEditing();
 383         updateRenderer();
 384         if(treeState != null) {
 385             treeState.invalidateSizes();
 386             updateSize();
 387         }
 388     }
 389 
 390     /**
 391      * Return currentCellRenderer, which will either be the trees
 392      * renderer, or defaultCellRenderer, which ever wasn't null.
 393      */
 394     protected TreeCellRenderer getCellRenderer() {
 395         return currentCellRenderer;
 396     }
 397 
 398     /**
 399      * Sets the TreeModel.
 400      */
 401     protected void setModel(TreeModel model) {
 402         completeEditing();
 403         if(treeModel != null && treeModelListener != null)
 404             treeModel.removeTreeModelListener(treeModelListener);
 405         treeModel = model;
 406         if(treeModel != null) {
 407             if(treeModelListener != null)
 408                 treeModel.addTreeModelListener(treeModelListener);
 409         }
 410         if(treeState != null) {
 411             treeState.setModel(model);
 412             updateLayoutCacheExpandedNodesIfNecessary();
 413             updateSize();
 414         }
 415     }
 416 
 417     protected TreeModel getModel() {
 418         return treeModel;
 419     }
 420 
 421     /**
 422      * Sets the root to being visible.
 423      */
 424     protected void setRootVisible(boolean newValue) {
 425         completeEditing();
 426         updateDepthOffset();
 427         if(treeState != null) {
 428             treeState.setRootVisible(newValue);
 429             treeState.invalidateSizes();
 430             updateSize();
 431         }
 432     }
 433 
 434     protected boolean isRootVisible() {
 435         return (tree != null) ? tree.isRootVisible() : false;
 436     }
 437 
 438     /**
 439      * Determines whether the node handles are to be displayed.
 440      */
 441     protected void setShowsRootHandles(boolean newValue) {
 442         completeEditing();
 443         updateDepthOffset();
 444         if(treeState != null) {
 445             treeState.invalidateSizes();
 446             updateSize();
 447         }
 448     }
 449 
 450     protected boolean getShowsRootHandles() {
 451         return (tree != null) ? tree.getShowsRootHandles() : false;
 452     }
 453 
 454     /**
 455      * Sets the cell editor.
 456      */
 457     protected void setCellEditor(TreeCellEditor editor) {
 458         updateCellEditor();
 459     }
 460 
 461     protected TreeCellEditor getCellEditor() {
 462         return (tree != null) ? tree.getCellEditor() : null;
 463     }
 464 
 465     /**
 466      * Configures the receiver to allow, or not allow, editing.
 467      */
 468     protected void setEditable(boolean newValue) {
 469         updateCellEditor();
 470     }
 471 
 472     protected boolean isEditable() {
 473         return (tree != null) ? tree.isEditable() : false;
 474     }
 475 
 476     /**
 477      * Resets the selection model. The appropriate listener are installed
 478      * on the model.
 479      */
 480     protected void setSelectionModel(TreeSelectionModel newLSM) {
 481         completeEditing();
 482         if(selectionModelPropertyChangeListener != null &&
 483            treeSelectionModel != null)
 484             treeSelectionModel.removePropertyChangeListener
 485                               (selectionModelPropertyChangeListener);
 486         if(treeSelectionListener != null && treeSelectionModel != null)
 487             treeSelectionModel.removeTreeSelectionListener
 488                                (treeSelectionListener);
 489         treeSelectionModel = newLSM;
 490         if(treeSelectionModel != null) {
 491             if(selectionModelPropertyChangeListener != null)
 492                 treeSelectionModel.addPropertyChangeListener
 493                               (selectionModelPropertyChangeListener);
 494             if(treeSelectionListener != null)
 495                 treeSelectionModel.addTreeSelectionListener
 496                                    (treeSelectionListener);
 497             if(treeState != null)
 498                 treeState.setSelectionModel(treeSelectionModel);
 499         }
 500         else if(treeState != null)
 501             treeState.setSelectionModel(null);
 502         if(tree != null)
 503             tree.repaint();
 504     }
 505 
 506     protected TreeSelectionModel getSelectionModel() {
 507         return treeSelectionModel;
 508     }
 509 
 510     //
 511     // TreeUI methods
 512     //
 513 
 514     /**
 515       * Returns the Rectangle enclosing the label portion that the
 516       * last item in path will be drawn into.  Will return null if
 517       * any component in path is currently valid.
 518       */
 519     public Rectangle getPathBounds(JTree tree, TreePath path) {
 520         if(tree != null && treeState != null) {
 521             return getPathBounds(path, tree.getInsets(), new Rectangle());
 522         }
 523         return null;
 524     }
 525 
 526     private Rectangle getPathBounds(TreePath path, Insets insets,
 527                                     Rectangle bounds) {
 528         bounds = treeState.getBounds(path, bounds);
 529         if (bounds != null) {
 530             if (leftToRight) {
 531                 bounds.x += insets.left;
 532             } else {
 533                 bounds.x = tree.getWidth() - (bounds.x + bounds.width) -
 534                         insets.right;
 535             }
 536             bounds.y += insets.top;
 537         }
 538         return bounds;
 539     }
 540 
 541     /**
 542       * Returns the path for passed in row.  If row is not visible
 543       * null is returned.
 544       */
 545     public TreePath getPathForRow(JTree tree, int row) {
 546         return (treeState != null) ? treeState.getPathForRow(row) : null;
 547     }
 548 
 549     /**
 550       * Returns the row that the last item identified in path is visible
 551       * at.  Will return -1 if any of the elements in path are not
 552       * currently visible.
 553       */
 554     public int getRowForPath(JTree tree, TreePath path) {
 555         return (treeState != null) ? treeState.getRowForPath(path) : -1;
 556     }
 557 
 558     /**
 559       * Returns the number of rows that are being displayed.
 560       */
 561     public int getRowCount(JTree tree) {
 562         return (treeState != null) ? treeState.getRowCount() : 0;
 563     }
 564 
 565     /**
 566       * Returns the path to the node that is closest to x,y.  If
 567       * there is nothing currently visible this will return null, otherwise
 568       * it'll always return a valid path.  If you need to test if the
 569       * returned object is exactly at x, y you should get the bounds for
 570       * the returned path and test x, y against that.
 571       */
 572     public TreePath getClosestPathForLocation(JTree tree, int x, int y) {
 573         if(tree != null && treeState != null) {
 574             // TreeState doesn't care about the x location, hence it isn't
 575             // adjusted
 576             y -= tree.getInsets().top;
 577             return treeState.getPathClosestTo(x, y);
 578         }
 579         return null;
 580     }
 581 
 582     /**
 583       * Returns true if the tree is being edited.  The item that is being
 584       * edited can be returned by getEditingPath().
 585       */
 586     public boolean isEditing(JTree tree) {
 587         return (editingComponent != null);
 588     }
 589 
 590     /**
 591       * Stops the current editing session.  This has no effect if the
 592       * tree isn't being edited.  Returns true if the editor allows the
 593       * editing session to stop.
 594       */
 595     public boolean stopEditing(JTree tree) {
 596         if(editingComponent != null && cellEditor.stopCellEditing()) {
 597             completeEditing(false, false, true);
 598             return true;
 599         }
 600         return false;
 601     }
 602 
 603     /**
 604       * Cancels the current editing session.
 605       */
 606     public void cancelEditing(JTree tree) {
 607         if(editingComponent != null) {
 608             completeEditing(false, true, false);
 609         }
 610     }
 611 
 612     /**
 613       * Selects the last item in path and tries to edit it.  Editing will
 614       * fail if the CellEditor won't allow it for the selected item.
 615       */
 616     public void startEditingAtPath(JTree tree, TreePath path) {
 617         tree.scrollPathToVisible(path);
 618         if(path != null && tree.isVisible(path))
 619             startEditing(path, null);
 620     }
 621 
 622     /**
 623      * Returns the path to the element that is being edited.
 624      */
 625     public TreePath getEditingPath(JTree tree) {
 626         return editingPath;
 627     }
 628 
 629     //
 630     // Install methods
 631     //
 632 
 633     public void installUI(JComponent c) {
 634         if ( c == null ) {
 635             throw new NullPointerException( "null component passed to BasicTreeUI.installUI()" );
 636         }
 637 
 638         tree = (JTree)c;
 639 
 640         prepareForUIInstall();
 641 
 642         // Boilerplate install block
 643         installDefaults();
 644         installKeyboardActions();
 645         installComponents();
 646         installListeners();
 647 
 648         completeUIInstall();
 649     }
 650 
 651     /**
 652      * Invoked after the <code>tree</code> instance variable has been
 653      * set, but before any defaults/listeners have been installed.
 654      */
 655     protected void prepareForUIInstall() {
 656         drawingCache = new Hashtable<TreePath,Boolean>(7);
 657 
 658         // Data member initializations
 659         leftToRight = BasicGraphicsUtils.isLeftToRight(tree);
 660         stopEditingInCompleteEditing = true;
 661         lastSelectedRow = -1;
 662         leadRow = -1;
 663         preferredSize = new Dimension();
 664 
 665         largeModel = tree.isLargeModel();
 666         if(getRowHeight() <= 0)
 667             largeModel = false;
 668         setModel(tree.getModel());
 669     }
 670 
 671     /**
 672      * Invoked from installUI after all the defaults/listeners have been
 673      * installed.
 674      */
 675     protected void completeUIInstall() {
 676         // Custom install code
 677 
 678         this.setShowsRootHandles(tree.getShowsRootHandles());
 679 
 680         updateRenderer();
 681 
 682         updateDepthOffset();
 683 
 684         setSelectionModel(tree.getSelectionModel());
 685 
 686         // Create, if necessary, the TreeState instance.
 687         treeState = createLayoutCache();
 688         configureLayoutCache();
 689 
 690         updateSize();
 691     }
 692 
 693     protected void installDefaults() {
 694         if(tree.getBackground() == null ||
 695            tree.getBackground() instanceof UIResource) {
 696             tree.setBackground(UIManager.getColor("Tree.background"));
 697         }
 698         if(getHashColor() == null || getHashColor() instanceof UIResource) {
 699             setHashColor(UIManager.getColor("Tree.hash"));
 700         }
 701         if (tree.getFont() == null || tree.getFont() instanceof UIResource)
 702             tree.setFont( UIManager.getFont("Tree.font") );
 703         // JTree's original row height is 16.  To correctly display the
 704         // contents on Linux we should have set it to 18, Windows 19 and
 705         // Solaris 20.  As these values vary so much it's too hard to
 706         // be backward compatable and try to update the row height, we're
 707         // therefor NOT going to adjust the row height based on font.  If the
 708         // developer changes the font, it's there responsibility to update
 709         // the row height.
 710 
 711         setExpandedIcon( (Icon)UIManager.get( "Tree.expandedIcon" ) );
 712         setCollapsedIcon( (Icon)UIManager.get( "Tree.collapsedIcon" ) );
 713 
 714         setLeftChildIndent(((Integer)UIManager.get("Tree.leftChildIndent")).
 715                            intValue());
 716         setRightChildIndent(((Integer)UIManager.get("Tree.rightChildIndent")).
 717                            intValue());
 718 
 719         LookAndFeel.installProperty(tree, "rowHeight",
 720                                     UIManager.get("Tree.rowHeight"));
 721 
 722         largeModel = (tree.isLargeModel() && tree.getRowHeight() > 0);
 723 
 724         Object scrollsOnExpand = UIManager.get("Tree.scrollsOnExpand");
 725         if (scrollsOnExpand != null) {
 726             LookAndFeel.installProperty(tree, "scrollsOnExpand", scrollsOnExpand);
 727         }
 728 
 729         paintLines = UIManager.getBoolean("Tree.paintLines");
 730         lineTypeDashed = UIManager.getBoolean("Tree.lineTypeDashed");
 731 
 732         Long l = (Long)UIManager.get("Tree.timeFactor");
 733         timeFactor = (l!=null) ? l.longValue() : 1000L;
 734 
 735         Object showsRootHandles = UIManager.get("Tree.showsRootHandles");
 736         if (showsRootHandles != null) {
 737             LookAndFeel.installProperty(tree,
 738                     JTree.SHOWS_ROOT_HANDLES_PROPERTY, showsRootHandles);
 739         }
 740     }
 741 
 742     protected void installListeners() {
 743         if ( (propertyChangeListener = createPropertyChangeListener())
 744              != null ) {
 745             tree.addPropertyChangeListener(propertyChangeListener);
 746         }
 747         if ( (mouseListener = createMouseListener()) != null ) {
 748             tree.addMouseListener(mouseListener);
 749             if (mouseListener instanceof MouseMotionListener) {
 750                 tree.addMouseMotionListener((MouseMotionListener)mouseListener);
 751             }
 752         }
 753         if ((focusListener = createFocusListener()) != null ) {
 754             tree.addFocusListener(focusListener);
 755         }
 756         if ((keyListener = createKeyListener()) != null) {
 757             tree.addKeyListener(keyListener);
 758         }
 759         if((treeExpansionListener = createTreeExpansionListener()) != null) {
 760             tree.addTreeExpansionListener(treeExpansionListener);
 761         }
 762         if((treeModelListener = createTreeModelListener()) != null &&
 763            treeModel != null) {
 764             treeModel.addTreeModelListener(treeModelListener);
 765         }
 766         if((selectionModelPropertyChangeListener =
 767             createSelectionModelPropertyChangeListener()) != null &&
 768            treeSelectionModel != null) {
 769             treeSelectionModel.addPropertyChangeListener
 770                 (selectionModelPropertyChangeListener);
 771         }
 772         if((treeSelectionListener = createTreeSelectionListener()) != null &&
 773            treeSelectionModel != null) {
 774             treeSelectionModel.addTreeSelectionListener(treeSelectionListener);
 775         }
 776 
 777         TransferHandler th = tree.getTransferHandler();
 778         if (th == null || th instanceof UIResource) {
 779             tree.setTransferHandler(defaultTransferHandler);
 780             // default TransferHandler doesn't support drop
 781             // so we don't want drop handling
 782             if (tree.getDropTarget() instanceof UIResource) {
 783                 tree.setDropTarget(null);
 784             }
 785         }
 786 
 787         LookAndFeel.installProperty(tree, "opaque", Boolean.TRUE);
 788     }
 789 
 790     protected void installKeyboardActions() {
 791         InputMap km = getInputMap(JComponent.
 792                                   WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
 793 
 794         SwingUtilities.replaceUIInputMap(tree, JComponent.
 795                                          WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
 796                                          km);
 797         km = getInputMap(JComponent.WHEN_FOCUSED);
 798         SwingUtilities.replaceUIInputMap(tree, JComponent.WHEN_FOCUSED, km);
 799 
 800         LazyActionMap.installLazyActionMap(tree, BasicTreeUI.class,
 801                                            "Tree.actionMap");
 802     }
 803 
 804     InputMap getInputMap(int condition) {
 805         if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
 806             return (InputMap)DefaultLookup.get(tree, this,
 807                                                "Tree.ancestorInputMap");
 808         }
 809         else if (condition == JComponent.WHEN_FOCUSED) {
 810             InputMap keyMap = (InputMap)DefaultLookup.get(tree, this,
 811                                                       "Tree.focusInputMap");
 812             InputMap rtlKeyMap;
 813 
 814             if (tree.getComponentOrientation().isLeftToRight() ||
 815                   ((rtlKeyMap = (InputMap)DefaultLookup.get(tree, this,
 816                   "Tree.focusInputMap.RightToLeft")) == null)) {
 817                 return keyMap;
 818             } else {
 819                 rtlKeyMap.setParent(keyMap);
 820                 return rtlKeyMap;
 821             }
 822         }
 823         return null;
 824     }
 825 
 826     /**
 827      * Intalls the subcomponents of the tree, which is the renderer pane.
 828      */
 829     protected void installComponents() {
 830         if ((rendererPane = createCellRendererPane()) != null) {
 831             tree.add( rendererPane );
 832         }
 833     }
 834 
 835     //
 836     // Create methods.
 837     //
 838 
 839     /**
 840      * Creates an instance of NodeDimensions that is able to determine
 841      * the size of a given node in the tree.
 842      */
 843     protected AbstractLayoutCache.NodeDimensions createNodeDimensions() {
 844         return new NodeDimensionsHandler();
 845     }
 846 
 847     /**
 848      * Creates a listener that is responsible that updates the UI based on
 849      * how the tree changes.
 850      */
 851     protected PropertyChangeListener createPropertyChangeListener() {
 852         return getHandler();
 853     }
 854 
 855     private Handler getHandler() {
 856         if (handler == null) {
 857             handler = new Handler();
 858         }
 859         return handler;
 860     }
 861 
 862     /**
 863      * Creates the listener responsible for updating the selection based on
 864      * mouse events.
 865      */
 866     protected MouseListener createMouseListener() {
 867         return getHandler();
 868     }
 869 
 870     /**
 871      * Creates a listener that is responsible for updating the display
 872      * when focus is lost/gained.
 873      */
 874     protected FocusListener createFocusListener() {
 875         return getHandler();
 876     }
 877 
 878     /**
 879      * Creates the listener reponsible for getting key events from
 880      * the tree.
 881      */
 882     protected KeyListener createKeyListener() {
 883         return getHandler();
 884     }
 885 
 886     /**
 887      * Creates the listener responsible for getting property change
 888      * events from the selection model.
 889      */
 890     protected PropertyChangeListener createSelectionModelPropertyChangeListener() {
 891         return getHandler();
 892     }
 893 
 894     /**
 895      * Creates the listener that updates the display based on selection change
 896      * methods.
 897      */
 898     protected TreeSelectionListener createTreeSelectionListener() {
 899         return getHandler();
 900     }
 901 
 902     /**
 903      * Creates a listener to handle events from the current editor.
 904      */
 905     protected CellEditorListener createCellEditorListener() {
 906         return getHandler();
 907     }
 908 
 909     /**
 910      * Creates and returns a new ComponentHandler. This is used for
 911      * the large model to mark the validCachedPreferredSize as invalid
 912      * when the component moves.
 913      */
 914     protected ComponentListener createComponentListener() {
 915         return new ComponentHandler();
 916     }
 917 
 918     /**
 919      * Creates and returns the object responsible for updating the treestate
 920      * when nodes expanded state changes.
 921      */
 922     protected TreeExpansionListener createTreeExpansionListener() {
 923         return getHandler();
 924     }
 925 
 926     /**
 927      * Creates the object responsible for managing what is expanded, as
 928      * well as the size of nodes.
 929      */
 930     protected AbstractLayoutCache createLayoutCache() {
 931         if(isLargeModel() && getRowHeight() > 0) {
 932             return new FixedHeightLayoutCache();
 933         }
 934         return new VariableHeightLayoutCache();
 935     }
 936 
 937     /**
 938      * Returns the renderer pane that renderer components are placed in.
 939      */
 940     protected CellRendererPane createCellRendererPane() {
 941         return new CellRendererPane();
 942     }
 943 
 944     /**
 945       * Creates a default cell editor.
 946       */
 947     protected TreeCellEditor createDefaultCellEditor() {
 948         if(currentCellRenderer != null &&
 949            (currentCellRenderer instanceof DefaultTreeCellRenderer)) {
 950             DefaultTreeCellEditor editor = new DefaultTreeCellEditor
 951                         (tree, (DefaultTreeCellRenderer)currentCellRenderer);
 952 
 953             return editor;
 954         }
 955         return new DefaultTreeCellEditor(tree, null);
 956     }
 957 
 958     /**
 959       * Returns the default cell renderer that is used to do the
 960       * stamping of each node.
 961       */
 962     protected TreeCellRenderer createDefaultCellRenderer() {
 963         return new DefaultTreeCellRenderer();
 964     }
 965 
 966     /**
 967      * Returns a listener that can update the tree when the model changes.
 968      */
 969     protected TreeModelListener createTreeModelListener() {
 970         return getHandler();
 971     }
 972 
 973     //
 974     // Uninstall methods
 975     //
 976 
 977     public void uninstallUI(JComponent c) {
 978         completeEditing();
 979 
 980         prepareForUIUninstall();
 981 
 982         uninstallDefaults();
 983         uninstallListeners();
 984         uninstallKeyboardActions();
 985         uninstallComponents();
 986 
 987         completeUIUninstall();
 988     }
 989 
 990     protected void prepareForUIUninstall() {
 991     }
 992 
 993     protected void completeUIUninstall() {
 994         if(createdRenderer) {
 995             tree.setCellRenderer(null);
 996         }
 997         if(createdCellEditor) {
 998             tree.setCellEditor(null);
 999         }
1000         cellEditor = null;
1001         currentCellRenderer = null;
1002         rendererPane = null;
1003         componentListener = null;
1004         propertyChangeListener = null;
1005         mouseListener = null;
1006         focusListener = null;
1007         keyListener = null;
1008         setSelectionModel(null);
1009         treeState = null;
1010         drawingCache = null;
1011         selectionModelPropertyChangeListener = null;
1012         tree = null;
1013         treeModel = null;
1014         treeSelectionModel = null;
1015         treeSelectionListener = null;
1016         treeExpansionListener = null;
1017     }
1018 
1019     protected void uninstallDefaults() {
1020         if (tree.getTransferHandler() instanceof UIResource) {
1021             tree.setTransferHandler(null);
1022         }
1023     }
1024 
1025     protected void uninstallListeners() {
1026         if(componentListener != null) {
1027             tree.removeComponentListener(componentListener);
1028         }
1029         if (propertyChangeListener != null) {
1030             tree.removePropertyChangeListener(propertyChangeListener);
1031         }
1032         if (mouseListener != null) {
1033             tree.removeMouseListener(mouseListener);
1034             if (mouseListener instanceof MouseMotionListener) {
1035                 tree.removeMouseMotionListener((MouseMotionListener)mouseListener);
1036             }
1037         }
1038         if (focusListener != null) {
1039             tree.removeFocusListener(focusListener);
1040         }
1041         if (keyListener != null) {
1042             tree.removeKeyListener(keyListener);
1043         }
1044         if(treeExpansionListener != null) {
1045             tree.removeTreeExpansionListener(treeExpansionListener);
1046         }
1047         if(treeModel != null && treeModelListener != null) {
1048             treeModel.removeTreeModelListener(treeModelListener);
1049         }
1050         if(selectionModelPropertyChangeListener != null &&
1051            treeSelectionModel != null) {
1052             treeSelectionModel.removePropertyChangeListener
1053                 (selectionModelPropertyChangeListener);
1054         }
1055         if(treeSelectionListener != null && treeSelectionModel != null) {
1056             treeSelectionModel.removeTreeSelectionListener
1057                                (treeSelectionListener);
1058         }
1059         handler = null;
1060     }
1061 
1062     protected void uninstallKeyboardActions() {
1063         SwingUtilities.replaceUIActionMap(tree, null);
1064         SwingUtilities.replaceUIInputMap(tree, JComponent.
1065                                          WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
1066                                          null);
1067         SwingUtilities.replaceUIInputMap(tree, JComponent.WHEN_FOCUSED, null);
1068     }
1069 
1070     /**
1071      * Uninstalls the renderer pane.
1072      */
1073     protected void uninstallComponents() {
1074         if(rendererPane != null) {
1075             tree.remove(rendererPane);
1076         }
1077     }
1078 
1079     /**
1080      * Recomputes the right margin, and invalidates any tree states
1081      */
1082     private void redoTheLayout() {
1083         if (treeState != null) {
1084             treeState.invalidateSizes();
1085         }
1086     }
1087 
1088     /**
1089      * Returns the baseline.
1090      *
1091      * @throws NullPointerException {@inheritDoc}
1092      * @throws IllegalArgumentException {@inheritDoc}
1093      * @see javax.swing.JComponent#getBaseline(int, int)
1094      * @since 1.6
1095      */
1096     public int getBaseline(JComponent c, int width, int height) {
1097         super.getBaseline(c, width, height);
1098         UIDefaults lafDefaults = UIManager.getLookAndFeelDefaults();
1099         Component renderer = (Component)lafDefaults.get(
1100                 BASELINE_COMPONENT_KEY);
1101         if (renderer == null) {
1102             TreeCellRenderer tcr = createDefaultCellRenderer();
1103             renderer = tcr.getTreeCellRendererComponent(
1104                     tree, "a", false, false, false, -1, false);
1105             lafDefaults.put(BASELINE_COMPONENT_KEY, renderer);
1106         }
1107         int rowHeight = tree.getRowHeight();
1108         int baseline;
1109         if (rowHeight > 0) {
1110             baseline = renderer.getBaseline(Integer.MAX_VALUE, rowHeight);
1111         }
1112         else {
1113             Dimension pref = renderer.getPreferredSize();
1114             baseline = renderer.getBaseline(pref.width, pref.height);
1115         }
1116         return baseline + tree.getInsets().top;
1117     }
1118 
1119     /**
1120      * Returns an enum indicating how the baseline of the component
1121      * changes as the size changes.
1122      *
1123      * @throws NullPointerException {@inheritDoc}
1124      * @see javax.swing.JComponent#getBaseline(int, int)
1125      * @since 1.6
1126      */
1127     public Component.BaselineResizeBehavior getBaselineResizeBehavior(
1128             JComponent c) {
1129         super.getBaselineResizeBehavior(c);
1130         return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
1131     }
1132 
1133     //
1134     // Painting routines.
1135     //
1136 
1137     public void paint(Graphics g, JComponent c) {
1138         if (tree != c) {
1139             throw new InternalError("incorrect component");
1140         }
1141 
1142         // Should never happen if installed for a UI
1143         if(treeState == null) {
1144             return;
1145         }
1146 
1147         Rectangle        paintBounds = g.getClipBounds();
1148         Insets           insets = tree.getInsets();
1149         TreePath         initialPath = getClosestPathForLocation
1150                                        (tree, 0, paintBounds.y);
1151         Enumeration      paintingEnumerator = treeState.getVisiblePathsFrom
1152                                               (initialPath);
1153         int              row = treeState.getRowForPath(initialPath);
1154         int              endY = paintBounds.y + paintBounds.height;
1155 
1156         drawingCache.clear();
1157 
1158         if(initialPath != null && paintingEnumerator != null) {
1159             TreePath   parentPath = initialPath;
1160 
1161             // Draw the lines, knobs, and rows
1162 
1163             // Find each parent and have them draw a line to their last child
1164             parentPath = parentPath.getParentPath();
1165             while(parentPath != null) {
1166                 paintVerticalPartOfLeg(g, paintBounds, insets, parentPath);
1167                 drawingCache.put(parentPath, Boolean.TRUE);
1168                 parentPath = parentPath.getParentPath();
1169             }
1170 
1171             boolean         done = false;
1172             // Information for the node being rendered.
1173             boolean         isExpanded;
1174             boolean         hasBeenExpanded;
1175             boolean         isLeaf;
1176             Rectangle       boundsBuffer = new Rectangle();
1177             Rectangle       bounds;
1178             TreePath        path;
1179             boolean         rootVisible = isRootVisible();
1180 
1181             while(!done && paintingEnumerator.hasMoreElements()) {
1182                 path = (TreePath)paintingEnumerator.nextElement();
1183                 if(path != null) {
1184                     isLeaf = treeModel.isLeaf(path.getLastPathComponent());
1185                     if(isLeaf)
1186                         isExpanded = hasBeenExpanded = false;
1187                     else {
1188                         isExpanded = treeState.getExpandedState(path);
1189                         hasBeenExpanded = tree.hasBeenExpanded(path);
1190                     }
1191                     bounds = getPathBounds(path, insets, boundsBuffer);
1192                     if(bounds == null)
1193                         // This will only happen if the model changes out
1194                         // from under us (usually in another thread).
1195                         // Swing isn't multithreaded, but I'll put this
1196                         // check in anyway.
1197                         return;
1198                     // See if the vertical line to the parent has been drawn.
1199                     parentPath = path.getParentPath();
1200                     if(parentPath != null) {
1201                         if(drawingCache.get(parentPath) == null) {
1202                             paintVerticalPartOfLeg(g, paintBounds,
1203                                                    insets, parentPath);
1204                             drawingCache.put(parentPath, Boolean.TRUE);
1205                         }
1206                         paintHorizontalPartOfLeg(g, paintBounds, insets,
1207                                                  bounds, path, row,
1208                                                  isExpanded,
1209                                                  hasBeenExpanded, isLeaf);
1210                     }
1211                     else if(rootVisible && row == 0) {
1212                         paintHorizontalPartOfLeg(g, paintBounds, insets,
1213                                                  bounds, path, row,
1214                                                  isExpanded,
1215                                                  hasBeenExpanded, isLeaf);
1216                     }
1217                     if(shouldPaintExpandControl(path, row, isExpanded,
1218                                                 hasBeenExpanded, isLeaf)) {
1219                         paintExpandControl(g, paintBounds, insets, bounds,
1220                                            path, row, isExpanded,
1221                                            hasBeenExpanded, isLeaf);
1222                     }
1223                     paintRow(g, paintBounds, insets, bounds, path,
1224                                  row, isExpanded, hasBeenExpanded, isLeaf);
1225                     if((bounds.y + bounds.height) >= endY)
1226                         done = true;
1227                 }
1228                 else {
1229                     done = true;
1230                 }
1231                 row++;
1232             }
1233         }
1234 
1235         paintDropLine(g);
1236 
1237         // Empty out the renderer pane, allowing renderers to be gc'ed.
1238         rendererPane.removeAll();
1239 
1240         drawingCache.clear();
1241     }
1242 
1243     /**
1244      * Tells if a {@code DropLocation} should be indicated by a line between
1245      * nodes. This is meant for {@code javax.swing.DropMode.INSERT} and
1246      * {@code javax.swing.DropMode.ON_OR_INSERT} drop modes.
1247      *
1248      * @param loc a {@code DropLocation}
1249      * @return {@code true} if the drop location should be shown as a line
1250      * @since 1.7
1251      */
1252     protected boolean isDropLine(JTree.DropLocation loc) {
1253         return loc != null && loc.getPath() != null && loc.getChildIndex() != -1;
1254     }
1255 
1256     /**
1257      * Paints the drop line.
1258      *
1259      * @param g {@code Graphics} object to draw on
1260      * @since 1.7
1261      */
1262     protected void paintDropLine(Graphics g) {
1263         JTree.DropLocation loc = tree.getDropLocation();
1264         if (!isDropLine(loc)) {
1265             return;
1266         }
1267 
1268         Color c = UIManager.getColor("Tree.dropLineColor");
1269         if (c != null) {
1270             g.setColor(c);
1271             Rectangle rect = getDropLineRect(loc);
1272             g.fillRect(rect.x, rect.y, rect.width, rect.height);
1273         }
1274     }
1275 
1276     /**
1277      * Returns a unbounding box for the drop line.
1278      *
1279      * @param loc a {@code DropLocation}
1280      * @return bounding box for the drop line
1281      * @since 1.7
1282      */
1283     protected Rectangle getDropLineRect(JTree.DropLocation loc) {
1284         Rectangle rect;
1285         TreePath path = loc.getPath();
1286         int index = loc.getChildIndex();
1287         boolean ltr = leftToRight;
1288 
1289         Insets insets = tree.getInsets();
1290 
1291         if (tree.getRowCount() == 0) {
1292             rect = new Rectangle(insets.left,
1293                                  insets.top,
1294                                  tree.getWidth() - insets.left - insets.right,
1295                                  0);
1296         } else {
1297             TreeModel model = getModel();
1298             Object root = model.getRoot();
1299 
1300             if (path.getLastPathComponent() == root
1301                     && index >= model.getChildCount(root)) {
1302 
1303                 rect = tree.getRowBounds(tree.getRowCount() - 1);
1304                 rect.y = rect.y + rect.height;
1305                 Rectangle xRect;
1306 
1307                 if (!tree.isRootVisible()) {
1308                     xRect = tree.getRowBounds(0);
1309                 } else if (model.getChildCount(root) == 0){
1310                     xRect = tree.getRowBounds(0);
1311                     xRect.x += totalChildIndent;
1312                     xRect.width -= totalChildIndent + totalChildIndent;
1313                 } else {
1314                     TreePath lastChildPath = path.pathByAddingChild(
1315                         model.getChild(root, model.getChildCount(root) - 1));
1316                     xRect = tree.getPathBounds(lastChildPath);
1317                 }
1318 
1319                 rect.x = xRect.x;
1320                 rect.width = xRect.width;
1321             } else {
1322                 rect = tree.getPathBounds(path.pathByAddingChild(
1323                     model.getChild(path.getLastPathComponent(), index)));
1324             }
1325         }
1326 
1327         if (rect.y != 0) {
1328             rect.y--;
1329         }
1330 
1331         if (!ltr) {
1332             rect.x = rect.x + rect.width - 100;
1333         }
1334 
1335         rect.width = 100;
1336         rect.height = 2;
1337 
1338         return rect;
1339     }
1340 
1341     /**
1342      * Paints the horizontal part of the leg. The receiver should
1343      * NOT modify <code>clipBounds</code>, or <code>insets</code>.<p>
1344      * NOTE: <code>parentRow</code> can be -1 if the root is not visible.
1345      */
1346     protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds,
1347                                             Insets insets, Rectangle bounds,
1348                                             TreePath path, int row,
1349                                             boolean isExpanded,
1350                                             boolean hasBeenExpanded, boolean
1351                                             isLeaf) {
1352         if (!paintLines) {
1353             return;
1354         }
1355 
1356         // Don't paint the legs for the root'ish node if the
1357         int depth = path.getPathCount() - 1;
1358         if((depth == 0 || (depth == 1 && !isRootVisible())) &&
1359            !getShowsRootHandles()) {
1360             return;
1361         }
1362 
1363         int clipLeft = clipBounds.x;
1364         int clipRight = clipBounds.x + clipBounds.width;
1365         int clipTop = clipBounds.y;
1366         int clipBottom = clipBounds.y + clipBounds.height;
1367         int lineY = bounds.y + bounds.height / 2;
1368 
1369         if (leftToRight) {
1370             int leftX = bounds.x - getRightChildIndent();
1371             int nodeX = bounds.x - getHorizontalLegBuffer();
1372 
1373             if(lineY >= clipTop
1374                     && lineY < clipBottom
1375                     && nodeX >= clipLeft
1376                     && leftX < clipRight
1377                     && leftX < nodeX) {
1378 
1379                 g.setColor(getHashColor());
1380                 paintHorizontalLine(g, tree, lineY, leftX, nodeX - 1);
1381             }
1382         } else {
1383             int nodeX = bounds.x + bounds.width + getHorizontalLegBuffer();
1384             int rightX = bounds.x + bounds.width + getRightChildIndent();
1385 
1386             if(lineY >= clipTop
1387                     && lineY < clipBottom
1388                     && rightX >= clipLeft
1389                     && nodeX < clipRight
1390                     && nodeX < rightX) {
1391 
1392                 g.setColor(getHashColor());
1393                 paintHorizontalLine(g, tree, lineY, nodeX, rightX - 1);
1394             }
1395         }
1396     }
1397 
1398     /**
1399      * Paints the vertical part of the leg. The receiver should
1400      * NOT modify <code>clipBounds</code>, <code>insets</code>.
1401      */
1402     protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds,
1403                                           Insets insets, TreePath path) {
1404         if (!paintLines) {
1405             return;
1406         }
1407 
1408         int depth = path.getPathCount() - 1;
1409         if (depth == 0 && !getShowsRootHandles() && !isRootVisible()) {
1410             return;
1411         }
1412         int lineX = getRowX(-1, depth + 1);
1413         if (leftToRight) {
1414             lineX = lineX - getRightChildIndent() + insets.left;
1415         }
1416         else {
1417             lineX = tree.getWidth() - lineX - insets.right +
1418                     getRightChildIndent() - 1;
1419         }
1420         int clipLeft = clipBounds.x;
1421         int clipRight = clipBounds.x + (clipBounds.width - 1);
1422 
1423         if (lineX >= clipLeft && lineX <= clipRight) {
1424             int clipTop = clipBounds.y;
1425             int clipBottom = clipBounds.y + clipBounds.height;
1426             Rectangle parentBounds = getPathBounds(tree, path);
1427             Rectangle lastChildBounds = getPathBounds(tree,
1428                                                      getLastChildPath(path));
1429 
1430             if(lastChildBounds == null)
1431                 // This shouldn't happen, but if the model is modified
1432                 // in another thread it is possible for this to happen.
1433                 // Swing isn't multithreaded, but I'll add this check in
1434                 // anyway.
1435                 return;
1436 
1437             int       top;
1438 
1439             if(parentBounds == null) {
1440                 top = Math.max(insets.top + getVerticalLegBuffer(),
1441                                clipTop);
1442             }
1443             else
1444                 top = Math.max(parentBounds.y + parentBounds.height +
1445                                getVerticalLegBuffer(), clipTop);
1446             if(depth == 0 && !isRootVisible()) {
1447                 TreeModel      model = getModel();
1448 
1449                 if(model != null) {
1450                     Object        root = model.getRoot();
1451 
1452                     if(model.getChildCount(root) > 0) {
1453                         parentBounds = getPathBounds(tree, path.
1454                                   pathByAddingChild(model.getChild(root, 0)));
1455                         if(parentBounds != null)
1456                             top = Math.max(insets.top + getVerticalLegBuffer(),
1457                                            parentBounds.y +
1458                                            parentBounds.height / 2);
1459                     }
1460                 }
1461             }
1462 
1463             int bottom = Math.min(lastChildBounds.y +
1464                                   (lastChildBounds.height / 2), clipBottom);
1465 
1466             if (top <= bottom) {
1467                 g.setColor(getHashColor());
1468                 paintVerticalLine(g, tree, lineX, top, bottom);
1469             }
1470         }
1471     }
1472 
1473     /**
1474      * Paints the expand (toggle) part of a row. The receiver should
1475      * NOT modify <code>clipBounds</code>, or <code>insets</code>.
1476      */
1477     protected void paintExpandControl(Graphics g,
1478                                       Rectangle clipBounds, Insets insets,
1479                                       Rectangle bounds, TreePath path,
1480                                       int row, boolean isExpanded,
1481                                       boolean hasBeenExpanded,
1482                                       boolean isLeaf) {
1483         Object       value = path.getLastPathComponent();
1484 
1485         // Draw icons if not a leaf and either hasn't been loaded,
1486         // or the model child count is > 0.
1487         if (!isLeaf && (!hasBeenExpanded ||
1488                         treeModel.getChildCount(value) > 0)) {
1489             int middleXOfKnob;
1490             if (leftToRight) {
1491                 middleXOfKnob = bounds.x - getRightChildIndent() + 1;
1492             } else {
1493                 middleXOfKnob = bounds.x + bounds.width + getRightChildIndent() - 1;
1494             }
1495             int middleYOfKnob = bounds.y + (bounds.height / 2);
1496 
1497             if (isExpanded) {
1498                 Icon expandedIcon = getExpandedIcon();
1499                 if(expandedIcon != null)
1500                   drawCentered(tree, g, expandedIcon, middleXOfKnob,
1501                                middleYOfKnob );
1502             }
1503             else {
1504                 Icon collapsedIcon = getCollapsedIcon();
1505                 if(collapsedIcon != null)
1506                   drawCentered(tree, g, collapsedIcon, middleXOfKnob,
1507                                middleYOfKnob);
1508             }
1509         }
1510     }
1511 
1512     /**
1513      * Paints the renderer part of a row. The receiver should
1514      * NOT modify <code>clipBounds</code>, or <code>insets</code>.
1515      */
1516     protected void paintRow(Graphics g, Rectangle clipBounds,
1517                             Insets insets, Rectangle bounds, TreePath path,
1518                             int row, boolean isExpanded,
1519                             boolean hasBeenExpanded, boolean isLeaf) {
1520         // Don't paint the renderer if editing this row.
1521         if(editingComponent != null && editingRow == row)
1522             return;
1523 
1524         int leadIndex;
1525 
1526         if(tree.hasFocus()) {
1527             leadIndex = getLeadSelectionRow();
1528         }
1529         else
1530             leadIndex = -1;
1531 
1532         Component component;
1533 
1534         component = currentCellRenderer.getTreeCellRendererComponent
1535                       (tree, path.getLastPathComponent(),
1536                        tree.isRowSelected(row), isExpanded, isLeaf, row,
1537                        (leadIndex == row));
1538 
1539         rendererPane.paintComponent(g, component, tree, bounds.x, bounds.y,
1540                                     bounds.width, bounds.height, true);
1541     }
1542 
1543     /**
1544      * Returns true if the expand (toggle) control should be drawn for
1545      * the specified row.
1546      */
1547     protected boolean shouldPaintExpandControl(TreePath path, int row,
1548                                                boolean isExpanded,
1549                                                boolean hasBeenExpanded,
1550                                                boolean isLeaf) {
1551         if(isLeaf)
1552             return false;
1553 
1554         int              depth = path.getPathCount() - 1;
1555 
1556         if((depth == 0 || (depth == 1 && !isRootVisible())) &&
1557            !getShowsRootHandles())
1558             return false;
1559         return true;
1560     }
1561 
1562     /**
1563      * Paints a vertical line.
1564      */
1565     protected void paintVerticalLine(Graphics g, JComponent c, int x, int top,
1566                                     int bottom) {
1567         if (lineTypeDashed) {
1568             drawDashedVerticalLine(g, x, top, bottom);
1569         } else {
1570             g.drawLine(x, top, x, bottom);
1571         }
1572     }
1573 
1574     /**
1575      * Paints a horizontal line.
1576      */
1577     protected void paintHorizontalLine(Graphics g, JComponent c, int y,
1578                                       int left, int right) {
1579         if (lineTypeDashed) {
1580             drawDashedHorizontalLine(g, y, left, right);
1581         } else {
1582             g.drawLine(left, y, right, y);
1583         }
1584     }
1585 
1586     /**
1587      * The vertical element of legs between nodes starts at the bottom of the
1588      * parent node by default.  This method makes the leg start below that.
1589      */
1590     protected int getVerticalLegBuffer() {
1591         return 0;
1592     }
1593 
1594     /**
1595      * The horizontal element of legs between nodes starts at the
1596      * right of the left-hand side of the child node by default.  This
1597      * method makes the leg end before that.
1598      */
1599     protected int getHorizontalLegBuffer() {
1600         return 0;
1601     }
1602 
1603     private int findCenteredX(int x, int iconWidth) {
1604         return leftToRight
1605                ? x - (int)Math.ceil(iconWidth / 2.0)
1606                : x - (int)Math.floor(iconWidth / 2.0);
1607     }
1608 
1609     //
1610     // Generic painting methods
1611     //
1612 
1613     // Draws the icon centered at (x,y)
1614     protected void drawCentered(Component c, Graphics graphics, Icon icon,
1615                                 int x, int y) {
1616         icon.paintIcon(c, graphics,
1617                       findCenteredX(x, icon.getIconWidth()),
1618                       y - icon.getIconHeight() / 2);
1619     }
1620 
1621     // This method is slow -- revisit when Java2D is ready.
1622     // assumes x1 <= x2
1623     protected void drawDashedHorizontalLine(Graphics g, int y, int x1, int x2){
1624         // Drawing only even coordinates helps join line segments so they
1625         // appear as one line.  This can be defeated by translating the
1626         // Graphics by an odd amount.
1627         x1 += (x1 % 2);
1628 
1629         for (int x = x1; x <= x2; x+=2) {
1630             g.drawLine(x, y, x, y);
1631         }
1632     }
1633 
1634     // This method is slow -- revisit when Java2D is ready.
1635     // assumes y1 <= y2
1636     protected void drawDashedVerticalLine(Graphics g, int x, int y1, int y2) {
1637         // Drawing only even coordinates helps join line segments so they
1638         // appear as one line.  This can be defeated by translating the
1639         // Graphics by an odd amount.
1640         y1 += (y1 % 2);
1641 
1642         for (int y = y1; y <= y2; y+=2) {
1643             g.drawLine(x, y, x, y);
1644         }
1645     }
1646 
1647     //
1648     // Various local methods
1649     //
1650 
1651     /**
1652      * Returns the location, along the x-axis, to render a particular row
1653      * at. The return value does not include any Insets specified on the JTree.
1654      * This does not check for the validity of the row or depth, it is assumed
1655      * to be correct and will not throw an Exception if the row or depth
1656      * doesn't match that of the tree.
1657      *
1658      * @param row Row to return x location for
1659      * @param depth Depth of the row
1660      * @return amount to indent the given row.
1661      * @since 1.5
1662      */
1663     protected int getRowX(int row, int depth) {
1664         return totalChildIndent * (depth + depthOffset);
1665     }
1666 
1667     /**
1668      * Makes all the nodes that are expanded in JTree expanded in LayoutCache.
1669      * This invokes updateExpandedDescendants with the root path.
1670      */
1671     protected void updateLayoutCacheExpandedNodes() {
1672         if(treeModel != null && treeModel.getRoot() != null)
1673             updateExpandedDescendants(new TreePath(treeModel.getRoot()));
1674     }
1675 
1676     private void updateLayoutCacheExpandedNodesIfNecessary() {
1677         if (treeModel != null && treeModel.getRoot() != null) {
1678             TreePath rootPath = new TreePath(treeModel.getRoot());
1679             if (tree.isExpanded(rootPath)) {
1680                 updateLayoutCacheExpandedNodes();
1681             } else {
1682                 treeState.setExpandedState(rootPath, false);
1683             }
1684         }
1685     }
1686 
1687     /**
1688      * Updates the expanded state of all the descendants of <code>path</code>
1689      * by getting the expanded descendants from the tree and forwarding
1690      * to the tree state.
1691      */
1692     protected void updateExpandedDescendants(TreePath path) {
1693         completeEditing();
1694         if(treeState != null) {
1695             treeState.setExpandedState(path, true);
1696 
1697             Enumeration   descendants = tree.getExpandedDescendants(path);
1698 
1699             if(descendants != null) {
1700                 while(descendants.hasMoreElements()) {
1701                     path = (TreePath)descendants.nextElement();
1702                     treeState.setExpandedState(path, true);
1703                 }
1704             }
1705             updateLeadSelectionRow();
1706             updateSize();
1707         }
1708     }
1709 
1710     /**
1711      * Returns a path to the last child of <code>parent</code>.
1712      */
1713     protected TreePath getLastChildPath(TreePath parent) {
1714         if(treeModel != null) {
1715             int         childCount = treeModel.getChildCount
1716                 (parent.getLastPathComponent());
1717 
1718             if(childCount > 0)
1719                 return parent.pathByAddingChild(treeModel.getChild
1720                            (parent.getLastPathComponent(), childCount - 1));
1721         }
1722         return null;
1723     }
1724 
1725     /**
1726      * Updates how much each depth should be offset by.
1727      */
1728     protected void updateDepthOffset() {
1729         if(isRootVisible()) {
1730             if(getShowsRootHandles())
1731                 depthOffset = 1;
1732             else
1733                 depthOffset = 0;
1734         }
1735         else if(!getShowsRootHandles())
1736             depthOffset = -1;
1737         else
1738             depthOffset = 0;
1739     }
1740 
1741     /**
1742       * Updates the cellEditor based on the editability of the JTree that
1743       * we're contained in.  If the tree is editable but doesn't have a
1744       * cellEditor, a basic one will be used.
1745       */
1746     protected void updateCellEditor() {
1747         TreeCellEditor        newEditor;
1748 
1749         completeEditing();
1750         if(tree == null)
1751             newEditor = null;
1752         else {
1753             if(tree.isEditable()) {
1754                 newEditor = tree.getCellEditor();
1755                 if(newEditor == null) {
1756                     newEditor = createDefaultCellEditor();
1757                     if(newEditor != null) {
1758                         tree.setCellEditor(newEditor);
1759                         createdCellEditor = true;
1760                     }
1761                 }
1762             }
1763             else
1764                 newEditor = null;
1765         }
1766         if(newEditor != cellEditor) {
1767             if(cellEditor != null && cellEditorListener != null)
1768                 cellEditor.removeCellEditorListener(cellEditorListener);
1769             cellEditor = newEditor;
1770             if(cellEditorListener == null)
1771                 cellEditorListener = createCellEditorListener();
1772             if(newEditor != null && cellEditorListener != null)
1773                 newEditor.addCellEditorListener(cellEditorListener);
1774             createdCellEditor = false;
1775         }
1776     }
1777 
1778     /**
1779       * Messaged from the tree we're in when the renderer has changed.
1780       */
1781     protected void updateRenderer() {
1782         if(tree != null) {
1783             TreeCellRenderer      newCellRenderer;
1784 
1785             newCellRenderer = tree.getCellRenderer();
1786             if(newCellRenderer == null) {
1787                 tree.setCellRenderer(createDefaultCellRenderer());
1788                 createdRenderer = true;
1789             }
1790             else {
1791                 createdRenderer = false;
1792                 currentCellRenderer = newCellRenderer;
1793                 if(createdCellEditor) {
1794                     tree.setCellEditor(null);
1795                 }
1796             }
1797         }
1798         else {
1799             createdRenderer = false;
1800             currentCellRenderer = null;
1801         }
1802         updateCellEditor();
1803     }
1804 
1805     /**
1806      * Resets the TreeState instance based on the tree we're providing the
1807      * look and feel for.
1808      */
1809     protected void configureLayoutCache() {
1810         if(treeState != null && tree != null) {
1811             if(nodeDimensions == null)
1812                 nodeDimensions = createNodeDimensions();
1813             treeState.setNodeDimensions(nodeDimensions);
1814             treeState.setRootVisible(tree.isRootVisible());
1815             treeState.setRowHeight(tree.getRowHeight());
1816             treeState.setSelectionModel(getSelectionModel());
1817             // Only do this if necessary, may loss state if call with
1818             // same model as it currently has.
1819             if(treeState.getModel() != tree.getModel())
1820                 treeState.setModel(tree.getModel());
1821             updateLayoutCacheExpandedNodesIfNecessary();
1822             // Create a listener to update preferred size when bounds
1823             // changes, if necessary.
1824             if(isLargeModel()) {
1825                 if(componentListener == null) {
1826                     componentListener = createComponentListener();
1827                     if(componentListener != null)
1828                         tree.addComponentListener(componentListener);
1829                 }
1830             }
1831             else if(componentListener != null) {
1832                 tree.removeComponentListener(componentListener);
1833                 componentListener = null;
1834             }
1835         }
1836         else if(componentListener != null) {
1837             tree.removeComponentListener(componentListener);
1838             componentListener = null;
1839         }
1840     }
1841 
1842     /**
1843      * Marks the cached size as being invalid, and messages the
1844      * tree with <code>treeDidChange</code>.
1845      */
1846     protected void updateSize() {
1847         validCachedPreferredSize = false;
1848         tree.treeDidChange();
1849     }
1850 
1851     private void updateSize0() {
1852         validCachedPreferredSize = false;
1853         tree.revalidate();
1854     }
1855 
1856     /**
1857      * Updates the <code>preferredSize</code> instance variable,
1858      * which is returned from <code>getPreferredSize()</code>.<p>
1859      * For left to right orientations, the size is determined from the
1860      * current AbstractLayoutCache. For RTL orientations, the preferred size
1861      * becomes the width minus the minimum x position.
1862      */
1863     protected void updateCachedPreferredSize() {
1864         if(treeState != null) {
1865             Insets               i = tree.getInsets();
1866 
1867             if(isLargeModel()) {
1868                 Rectangle            visRect = tree.getVisibleRect();
1869 
1870                 if (visRect.x == 0 && visRect.y == 0 &&
1871                         visRect.width == 0 && visRect.height == 0 &&
1872                         tree.getVisibleRowCount() > 0) {
1873                     // The tree doesn't have a valid bounds yet. Calculate
1874                     // based on visible row count.
1875                     visRect.width = 1;
1876                     visRect.height = tree.getRowHeight() *
1877                             tree.getVisibleRowCount();
1878                 } else {
1879                     visRect.x -= i.left;
1880                     visRect.y -= i.top;
1881                 }
1882                 // we should consider a non-visible area above
1883                 Component component = SwingUtilities.getUnwrappedParent(tree);
1884                 if (component instanceof JViewport) {
1885                     component = component.getParent();
1886                     if (component instanceof JScrollPane) {
1887                         JScrollPane pane = (JScrollPane) component;
1888                         JScrollBar bar = pane.getHorizontalScrollBar();
1889                         if ((bar != null) && bar.isVisible()) {
1890                             int height = bar.getHeight();
1891                             visRect.y -= height;
1892                             visRect.height += height;
1893                         }
1894                     }
1895                 }
1896                 preferredSize.width = treeState.getPreferredWidth(visRect);
1897             }
1898             else {
1899                 preferredSize.width = treeState.getPreferredWidth(null);
1900             }
1901             preferredSize.height = treeState.getPreferredHeight();
1902             preferredSize.width += i.left + i.right;
1903             preferredSize.height += i.top + i.bottom;
1904         }
1905         validCachedPreferredSize = true;
1906     }
1907 
1908     /**
1909       * Messaged from the VisibleTreeNode after it has been expanded.
1910       */
1911     protected void pathWasExpanded(TreePath path) {
1912         if(tree != null) {
1913             tree.fireTreeExpanded(path);
1914         }
1915     }
1916 
1917     /**
1918       * Messaged from the VisibleTreeNode after it has collapsed.
1919       */
1920     protected void pathWasCollapsed(TreePath path) {
1921         if(tree != null) {
1922             tree.fireTreeCollapsed(path);
1923         }
1924     }
1925 
1926     /**
1927       * Ensures that the rows identified by beginRow through endRow are
1928       * visible.
1929       */
1930     protected void ensureRowsAreVisible(int beginRow, int endRow) {
1931         if(tree != null && beginRow >= 0 && endRow < getRowCount(tree)) {
1932             boolean scrollVert = DefaultLookup.getBoolean(tree, this,
1933                               "Tree.scrollsHorizontallyAndVertically", false);
1934             if(beginRow == endRow) {
1935                 Rectangle     scrollBounds = getPathBounds(tree, getPathForRow
1936                                                            (tree, beginRow));
1937 
1938                 if(scrollBounds != null) {
1939                     if (!scrollVert) {
1940                         scrollBounds.x = tree.getVisibleRect().x;
1941                         scrollBounds.width = 1;
1942                     }
1943                     tree.scrollRectToVisible(scrollBounds);
1944                 }
1945             }
1946             else {
1947                 Rectangle   beginRect = getPathBounds(tree, getPathForRow
1948                                                       (tree, beginRow));
1949                 if (beginRect != null) {
1950                     Rectangle   visRect = tree.getVisibleRect();
1951                     Rectangle   testRect = beginRect;
1952                     int         beginY = beginRect.y;
1953                     int         maxY = beginY + visRect.height;
1954 
1955                     for(int counter = beginRow + 1; counter <= endRow; counter++) {
1956                             testRect = getPathBounds(tree,
1957                                     getPathForRow(tree, counter));
1958                         if (testRect == null) {
1959                             return;
1960                         }
1961                         if((testRect.y + testRect.height) > maxY)
1962                                 counter = endRow;
1963                             }
1964                         tree.scrollRectToVisible(new Rectangle(visRect.x, beginY, 1,
1965                                                       testRect.y + testRect.height-
1966                                                       beginY));
1967                 }
1968             }
1969         }
1970     }
1971 
1972     /** Sets the preferred minimum size.
1973       */
1974     public void setPreferredMinSize(Dimension newSize) {
1975         preferredMinSize = newSize;
1976     }
1977 
1978     /** Returns the minimum preferred size.
1979       */
1980     public Dimension getPreferredMinSize() {
1981         if(preferredMinSize == null)
1982             return null;
1983         return new Dimension(preferredMinSize);
1984     }
1985 
1986     /** Returns the preferred size to properly display the tree,
1987       * this is a cover method for getPreferredSize(c, true).
1988       */
1989     public Dimension getPreferredSize(JComponent c) {
1990         return getPreferredSize(c, true);
1991     }
1992 
1993     /** Returns the preferred size to represent the tree in
1994       * <I>c</I>.  If <I>checkConsistency</I> is true
1995       * <b>checkConsistency</b> is messaged first.
1996       */
1997     public Dimension getPreferredSize(JComponent c,
1998                                       boolean checkConsistency) {
1999         Dimension       pSize = this.getPreferredMinSize();
2000 
2001         if(!validCachedPreferredSize)
2002             updateCachedPreferredSize();
2003         if(tree != null) {
2004             if(pSize != null)
2005                 return new Dimension(Math.max(pSize.width,
2006                                               preferredSize.width),
2007                               Math.max(pSize.height, preferredSize.height));
2008             return new Dimension(preferredSize.width, preferredSize.height);
2009         }
2010         else if(pSize != null)
2011             return pSize;
2012         else
2013             return new Dimension(0, 0);
2014     }
2015 
2016     /**
2017       * Returns the minimum size for this component.  Which will be
2018       * the min preferred size or 0, 0.
2019       */
2020     public Dimension getMinimumSize(JComponent c) {
2021         if(this.getPreferredMinSize() != null)
2022             return this.getPreferredMinSize();
2023         return new Dimension(0, 0);
2024     }
2025 
2026     /**
2027       * Returns the maximum size for this component, which will be the
2028       * preferred size if the instance is currently in a JTree, or 0, 0.
2029       */
2030     public Dimension getMaximumSize(JComponent c) {
2031         if(tree != null)
2032             return getPreferredSize(tree);
2033         if(this.getPreferredMinSize() != null)
2034             return this.getPreferredMinSize();
2035         return new Dimension(0, 0);
2036     }
2037 
2038 
2039     /**
2040      * Messages to stop the editing session. If the UI the receiver
2041      * is providing the look and feel for returns true from
2042      * <code>getInvokesStopCellEditing</code>, stopCellEditing will
2043      * invoked on the current editor. Then completeEditing will
2044      * be messaged with false, true, false to cancel any lingering
2045      * editing.
2046      */
2047     protected void completeEditing() {
2048         /* If should invoke stopCellEditing, try that */
2049         if(tree.getInvokesStopCellEditing() &&
2050            stopEditingInCompleteEditing && editingComponent != null) {
2051             cellEditor.stopCellEditing();
2052         }
2053         /* Invoke cancelCellEditing, this will do nothing if stopCellEditing
2054            was successful. */
2055         completeEditing(false, true, false);
2056     }
2057 
2058     /**
2059       * Stops the editing session.  If messageStop is true the editor
2060       * is messaged with stopEditing, if messageCancel is true the
2061       * editor is messaged with cancelEditing. If messageTree is true
2062       * the treeModel is messaged with valueForPathChanged.
2063       */
2064     protected void completeEditing(boolean messageStop,
2065                                    boolean messageCancel,
2066                                    boolean messageTree) {
2067         if(stopEditingInCompleteEditing && editingComponent != null) {
2068             Component             oldComponent = editingComponent;
2069             TreePath              oldPath = editingPath;
2070             TreeCellEditor        oldEditor = cellEditor;
2071             Object                newValue = oldEditor.getCellEditorValue();
2072             Rectangle             editingBounds = getPathBounds(tree,
2073                                                                 editingPath);
2074             boolean               requestFocus = (tree != null &&
2075                                    (tree.hasFocus() || SwingUtilities.
2076                                     findFocusOwner(editingComponent) != null));
2077 
2078             editingComponent = null;
2079             editingPath = null;
2080             if(messageStop)
2081                 oldEditor.stopCellEditing();
2082             else if(messageCancel)
2083                 oldEditor.cancelCellEditing();
2084             tree.remove(oldComponent);
2085             if(editorHasDifferentSize) {
2086                 treeState.invalidatePathBounds(oldPath);
2087                 updateSize();
2088             }
2089             else if (editingBounds != null) {
2090                 editingBounds.x = 0;
2091                 editingBounds.width = tree.getSize().width;
2092                 tree.repaint(editingBounds);
2093             }
2094             if(requestFocus)
2095                 tree.requestFocus();
2096             if(messageTree)
2097                 treeModel.valueForPathChanged(oldPath, newValue);
2098         }
2099     }
2100 
2101     // cover method for startEditing that allows us to pass extra
2102     // information into that method via a class variable
2103     private boolean startEditingOnRelease(TreePath path,
2104                                           MouseEvent event,
2105                                           MouseEvent releaseEvent) {
2106         this.releaseEvent = releaseEvent;
2107         try {
2108             return startEditing(path, event);
2109         } finally {
2110             this.releaseEvent = null;
2111         }
2112     }
2113 
2114     /**
2115       * Will start editing for node if there is a cellEditor and
2116       * shouldSelectCell returns true.<p>
2117       * This assumes that path is valid and visible.
2118       */
2119     protected boolean startEditing(TreePath path, MouseEvent event) {
2120         if (isEditing(tree) && tree.getInvokesStopCellEditing() &&
2121                                !stopEditing(tree)) {
2122             return false;
2123         }
2124         completeEditing();
2125         if(cellEditor != null && tree.isPathEditable(path)) {
2126             int           row = getRowForPath(tree, path);
2127 
2128             if(cellEditor.isCellEditable(event)) {
2129                 editingComponent = cellEditor.getTreeCellEditorComponent
2130                       (tree, path.getLastPathComponent(),
2131                        tree.isPathSelected(path), tree.isExpanded(path),
2132                        treeModel.isLeaf(path.getLastPathComponent()), row);
2133                 Rectangle           nodeBounds = getPathBounds(tree, path);
2134                 if (nodeBounds == null) {
2135                     return false;
2136                 }
2137 
2138                 editingRow = row;
2139 
2140                 Dimension editorSize = editingComponent.getPreferredSize();
2141 
2142                 // Only allow odd heights if explicitly set.
2143                 if(editorSize.height != nodeBounds.height &&
2144                    getRowHeight() > 0)
2145                     editorSize.height = getRowHeight();
2146 
2147                 if(editorSize.width != nodeBounds.width ||
2148                    editorSize.height != nodeBounds.height) {
2149                     // Editor wants different width or height, invalidate
2150                     // treeState and relayout.
2151                     editorHasDifferentSize = true;
2152                     treeState.invalidatePathBounds(path);
2153                     updateSize();
2154                     // To make sure x/y are updated correctly, fetch
2155                     // the bounds again.
2156                     nodeBounds = getPathBounds(tree, path);
2157                     if (nodeBounds == null) {
2158                         return false;
2159                     }
2160                 }
2161                 else
2162                     editorHasDifferentSize = false;
2163                 tree.add(editingComponent);
2164                 editingComponent.setBounds(nodeBounds.x, nodeBounds.y,
2165                                            nodeBounds.width,
2166                                            nodeBounds.height);
2167                 editingPath = path;
2168                 AWTAccessor.getComponentAccessor().revalidateSynchronously(editingComponent);
2169                 editingComponent.repaint();
2170                 if(cellEditor.shouldSelectCell(event)) {
2171                     stopEditingInCompleteEditing = false;
2172                     tree.setSelectionRow(row);
2173                     stopEditingInCompleteEditing = true;
2174                 }
2175 
2176                 Component focusedComponent = SwingUtilities2.
2177                                  compositeRequestFocus(editingComponent);
2178                 boolean selectAll = true;
2179 
2180                 if(event != null) {
2181                     /* Find the component that will get forwarded all the
2182                        mouse events until mouseReleased. */
2183                     Point          componentPoint = SwingUtilities.convertPoint
2184                         (tree, new Point(event.getX(), event.getY()),
2185                          editingComponent);
2186 
2187                     /* Create an instance of BasicTreeMouseListener to handle
2188                        passing the mouse/motion events to the necessary
2189                        component. */
2190                     // We really want similar behavior to getMouseEventTarget,
2191                     // but it is package private.
2192                     Component activeComponent = SwingUtilities.
2193                                     getDeepestComponentAt(editingComponent,
2194                                        componentPoint.x, componentPoint.y);
2195                     if (activeComponent != null) {
2196                         MouseInputHandler handler =
2197                             new MouseInputHandler(tree, activeComponent,
2198                                                   event, focusedComponent);
2199 
2200                         if (releaseEvent != null) {
2201                             handler.mouseReleased(releaseEvent);
2202                         }
2203 
2204                         selectAll = false;
2205                     }
2206                 }
2207                 if (selectAll && focusedComponent instanceof JTextField) {
2208                     ((JTextField)focusedComponent).selectAll();
2209                 }
2210                 return true;
2211             }
2212             else
2213                 editingComponent = null;
2214         }
2215         return false;
2216     }
2217 
2218     //
2219     // Following are primarily for handling mouse events.
2220     //
2221 
2222     /**
2223      * If the <code>mouseX</code> and <code>mouseY</code> are in the
2224      * expand/collapse region of the <code>row</code>, this will toggle
2225      * the row.
2226      */
2227     protected void checkForClickInExpandControl(TreePath path,
2228                                                 int mouseX, int mouseY) {
2229       if (isLocationInExpandControl(path, mouseX, mouseY)) {
2230           handleExpandControlClick(path, mouseX, mouseY);
2231         }
2232     }
2233 
2234     /**
2235      * Returns true if <code>mouseX</code> and <code>mouseY</code> fall
2236      * in the area of row that is used to expand/collapse the node and
2237      * the node at <code>row</code> does not represent a leaf.
2238      */
2239     protected boolean isLocationInExpandControl(TreePath path,
2240                                                 int mouseX, int mouseY) {
2241         if(path != null && !treeModel.isLeaf(path.getLastPathComponent())){
2242             int                     boxWidth;
2243             Insets                  i = tree.getInsets();
2244 
2245             if(getExpandedIcon() != null)
2246                 boxWidth = getExpandedIcon().getIconWidth();
2247             else
2248                 boxWidth = 8;
2249 
2250             int boxLeftX = getRowX(tree.getRowForPath(path),
2251                                    path.getPathCount() - 1);
2252 
2253             if (leftToRight) {
2254                 boxLeftX = boxLeftX + i.left - getRightChildIndent() + 1;
2255             } else {
2256                 boxLeftX = tree.getWidth() - boxLeftX - i.right + getRightChildIndent() - 1;
2257             }
2258 
2259             boxLeftX = findCenteredX(boxLeftX, boxWidth);
2260 
2261             return (mouseX >= boxLeftX && mouseX < (boxLeftX + boxWidth));
2262         }
2263         return false;
2264     }
2265 
2266     /**
2267      * Messaged when the user clicks the particular row, this invokes
2268      * toggleExpandState.
2269      */
2270     protected void handleExpandControlClick(TreePath path, int mouseX,
2271                                             int mouseY) {
2272         toggleExpandState(path);
2273     }
2274 
2275     /**
2276      * Expands path if it is not expanded, or collapses row if it is expanded.
2277      * If expanding a path and JTree scrolls on expand, ensureRowsAreVisible
2278      * is invoked to scroll as many of the children to visible as possible
2279      * (tries to scroll to last visible descendant of path).
2280      */
2281     protected void toggleExpandState(TreePath path) {
2282         if(!tree.isExpanded(path)) {
2283             int       row = getRowForPath(tree, path);
2284 
2285             tree.expandPath(path);
2286             updateSize();
2287             if(row != -1) {
2288                 if(tree.getScrollsOnExpand())
2289                     ensureRowsAreVisible(row, row + treeState.
2290                                          getVisibleChildCount(path));
2291                 else
2292                     ensureRowsAreVisible(row, row);
2293             }
2294         }
2295         else {
2296             tree.collapsePath(path);
2297             updateSize();
2298         }
2299     }
2300 
2301     /**
2302      * Returning true signifies a mouse event on the node should toggle
2303      * the selection of only the row under mouse.
2304      */
2305     protected boolean isToggleSelectionEvent(MouseEvent event) {
2306         return (SwingUtilities.isLeftMouseButton(event) &&
2307                 BasicGraphicsUtils.isMenuShortcutKeyDown(event));
2308     }
2309 
2310     /**
2311      * Returning true signifies a mouse event on the node should select
2312      * from the anchor point.
2313      */
2314     protected boolean isMultiSelectEvent(MouseEvent event) {
2315         return (SwingUtilities.isLeftMouseButton(event) &&
2316                 event.isShiftDown());
2317     }
2318 
2319     /**
2320      * Returning true indicates the row under the mouse should be toggled
2321      * based on the event. This is invoked after checkForClickInExpandControl,
2322      * implying the location is not in the expand (toggle) control
2323      */
2324     protected boolean isToggleEvent(MouseEvent event) {
2325         if(!SwingUtilities.isLeftMouseButton(event)) {
2326             return false;
2327         }
2328         int           clickCount = tree.getToggleClickCount();
2329 
2330         if(clickCount <= 0) {
2331             return false;
2332         }
2333         return ((event.getClickCount() % clickCount) == 0);
2334     }
2335 
2336     /**
2337      * Messaged to update the selection based on a MouseEvent over a
2338      * particular row. If the event is a toggle selection event, the
2339      * row is either selected, or deselected. If the event identifies
2340      * a multi selection event, the selection is updated from the
2341      * anchor point. Otherwise the row is selected, and if the event
2342      * specified a toggle event the row is expanded/collapsed.
2343      */
2344     protected void selectPathForEvent(TreePath path, MouseEvent event) {
2345         /* Adjust from the anchor point. */
2346         if(isMultiSelectEvent(event)) {
2347             TreePath    anchor = getAnchorSelectionPath();
2348             int         anchorRow = (anchor == null) ? -1 :
2349                                     getRowForPath(tree, anchor);
2350 
2351             if(anchorRow == -1 || tree.getSelectionModel().
2352                       getSelectionMode() == TreeSelectionModel.
2353                       SINGLE_TREE_SELECTION) {
2354                 tree.setSelectionPath(path);
2355             }
2356             else {
2357                 int          row = getRowForPath(tree, path);
2358                 TreePath     lastAnchorPath = anchor;
2359 
2360                 if (isToggleSelectionEvent(event)) {
2361                     if (tree.isRowSelected(anchorRow)) {
2362                         tree.addSelectionInterval(anchorRow, row);
2363                     } else {
2364                         tree.removeSelectionInterval(anchorRow, row);
2365                         tree.addSelectionInterval(row, row);
2366                     }
2367                 } else if(row < anchorRow) {
2368                     tree.setSelectionInterval(row, anchorRow);
2369                 } else {
2370                     tree.setSelectionInterval(anchorRow, row);
2371                 }
2372                 lastSelectedRow = row;
2373                 setAnchorSelectionPath(lastAnchorPath);
2374                 setLeadSelectionPath(path);
2375             }
2376         }
2377 
2378         // Should this event toggle the selection of this row?
2379         /* Control toggles just this node. */
2380         else if(isToggleSelectionEvent(event)) {
2381             if(tree.isPathSelected(path))
2382                 tree.removeSelectionPath(path);
2383             else
2384                 tree.addSelectionPath(path);
2385             lastSelectedRow = getRowForPath(tree, path);
2386             setAnchorSelectionPath(path);
2387             setLeadSelectionPath(path);
2388         }
2389 
2390         /* Otherwise set the selection to just this interval. */
2391         else if(SwingUtilities.isLeftMouseButton(event)) {
2392             tree.setSelectionPath(path);
2393             if(isToggleEvent(event)) {
2394                 toggleExpandState(path);
2395             }
2396         }
2397     }
2398 
2399     /**
2400      * @return true if the node at <code>row</code> is a leaf.
2401      */
2402     protected boolean isLeaf(int row) {
2403         TreePath          path = getPathForRow(tree, row);
2404 
2405         if(path != null)
2406             return treeModel.isLeaf(path.getLastPathComponent());
2407         // Have to return something here...
2408         return true;
2409     }
2410 
2411     //
2412     // The following selection methods (lead/anchor) are covers for the
2413     // methods in JTree.
2414     //
2415     private void setAnchorSelectionPath(TreePath newPath) {
2416         ignoreLAChange = true;
2417         try {
2418             tree.setAnchorSelectionPath(newPath);
2419         } finally{
2420             ignoreLAChange = false;
2421         }
2422     }
2423 
2424     private TreePath getAnchorSelectionPath() {
2425         return tree.getAnchorSelectionPath();
2426     }
2427 
2428     private void setLeadSelectionPath(TreePath newPath) {
2429         setLeadSelectionPath(newPath, false);
2430     }
2431 
2432     private void setLeadSelectionPath(TreePath newPath, boolean repaint) {
2433         Rectangle       bounds = repaint ?
2434                             getPathBounds(tree, getLeadSelectionPath()) : null;
2435 
2436         ignoreLAChange = true;
2437         try {
2438             tree.setLeadSelectionPath(newPath);
2439         } finally {
2440             ignoreLAChange = false;
2441         }
2442         leadRow = getRowForPath(tree, newPath);
2443 
2444         if (repaint) {
2445             if (bounds != null) {
2446                 tree.repaint(getRepaintPathBounds(bounds));
2447             }
2448             bounds = getPathBounds(tree, newPath);
2449             if (bounds != null) {
2450                 tree.repaint(getRepaintPathBounds(bounds));
2451             }
2452         }
2453     }
2454 
2455     private Rectangle getRepaintPathBounds(Rectangle bounds) {
2456         if (UIManager.getBoolean("Tree.repaintWholeRow")) {
2457            bounds.x = 0;
2458            bounds.width = tree.getWidth();
2459         }
2460         return bounds;
2461     }
2462 
2463     private TreePath getLeadSelectionPath() {
2464         return tree.getLeadSelectionPath();
2465     }
2466 
2467     /**
2468      * Updates the lead row of the selection.
2469      * @since 1.7
2470      */
2471     protected void updateLeadSelectionRow() {
2472         leadRow = getRowForPath(tree, getLeadSelectionPath());
2473     }
2474 
2475     /**
2476      * Returns the lead row of the selection.
2477      *
2478      * @return selection lead row
2479      * @since 1.7
2480      */
2481     protected int getLeadSelectionRow() {
2482         return leadRow;
2483     }
2484 
2485     /**
2486      * Extends the selection from the anchor to make <code>newLead</code>
2487      * the lead of the selection. This does not scroll.
2488      */
2489     private void extendSelection(TreePath newLead) {
2490         TreePath           aPath = getAnchorSelectionPath();
2491         int                aRow = (aPath == null) ? -1 :
2492                                   getRowForPath(tree, aPath);
2493         int                newIndex = getRowForPath(tree, newLead);
2494 
2495         if(aRow == -1) {
2496             tree.setSelectionRow(newIndex);
2497         }
2498         else {
2499             if(aRow < newIndex) {
2500                 tree.setSelectionInterval(aRow, newIndex);
2501             }
2502             else {
2503                 tree.setSelectionInterval(newIndex, aRow);
2504             }
2505             setAnchorSelectionPath(aPath);
2506             setLeadSelectionPath(newLead);
2507         }
2508     }
2509 
2510     /**
2511      * Invokes <code>repaint</code> on the JTree for the passed in TreePath,
2512      * <code>path</code>.
2513      */
2514     private void repaintPath(TreePath path) {
2515         if (path != null) {
2516             Rectangle bounds = getPathBounds(tree, path);
2517             if (bounds != null) {
2518                 tree.repaint(bounds.x, bounds.y, bounds.width, bounds.height);
2519             }
2520         }
2521     }
2522 
2523     /**
2524      * Updates the TreeState in response to nodes expanding/collapsing.
2525      */
2526     public class TreeExpansionHandler implements TreeExpansionListener {
2527         // NOTE: This class exists only for backward compatibility. All
2528         // its functionality has been moved into Handler. If you need to add
2529         // new functionality add it to the Handler, but make sure this
2530         // class calls into the Handler.
2531 
2532         /**
2533          * Called whenever an item in the tree has been expanded.
2534          */
2535         public void treeExpanded(TreeExpansionEvent event) {
2536             getHandler().treeExpanded(event);
2537         }
2538 
2539         /**
2540          * Called whenever an item in the tree has been collapsed.
2541          */
2542         public void treeCollapsed(TreeExpansionEvent event) {
2543             getHandler().treeCollapsed(event);
2544         }
2545     } // BasicTreeUI.TreeExpansionHandler
2546 
2547 
2548     /**
2549      * Updates the preferred size when scrolling (if necessary).
2550      */
2551     public class ComponentHandler extends ComponentAdapter implements
2552                  ActionListener {
2553         /** Timer used when inside a scrollpane and the scrollbar is
2554          * adjusting. */
2555         protected Timer                timer;
2556         /** ScrollBar that is being adjusted. */
2557         protected JScrollBar           scrollBar;
2558 
2559         public void componentMoved(ComponentEvent e) {
2560             if(timer == null) {
2561                 JScrollPane   scrollPane = getScrollPane();
2562 
2563                 if(scrollPane == null)
2564                     updateSize();
2565                 else {
2566                     scrollBar = scrollPane.getVerticalScrollBar();
2567                     if(scrollBar == null ||
2568                         !scrollBar.getValueIsAdjusting()) {
2569                         // Try the horizontal scrollbar.
2570                         if((scrollBar = scrollPane.getHorizontalScrollBar())
2571                             != null && scrollBar.getValueIsAdjusting())
2572                             startTimer();
2573                         else
2574                             updateSize();
2575                     }
2576                     else
2577                         startTimer();
2578                 }
2579             }
2580         }
2581 
2582         /**
2583          * Creates, if necessary, and starts a Timer to check if need to
2584          * resize the bounds.
2585          */
2586         protected void startTimer() {
2587             if(timer == null) {
2588                 timer = new Timer(200, this);
2589                 timer.setRepeats(true);
2590             }
2591             timer.start();
2592         }
2593 
2594         /**
2595          * Returns the JScrollPane housing the JTree, or null if one isn't
2596          * found.
2597          */
2598         protected JScrollPane getScrollPane() {
2599             Component       c = tree.getParent();
2600 
2601             while(c != null && !(c instanceof JScrollPane))
2602                 c = c.getParent();
2603             if(c instanceof JScrollPane)
2604                 return (JScrollPane)c;
2605             return null;
2606         }
2607 
2608         /**
2609          * Public as a result of Timer. If the scrollBar is null, or
2610          * not adjusting, this stops the timer and updates the sizing.
2611          */
2612         public void actionPerformed(ActionEvent ae) {
2613             if(scrollBar == null || !scrollBar.getValueIsAdjusting()) {
2614                 if(timer != null)
2615                     timer.stop();
2616                 updateSize();
2617                 timer = null;
2618                 scrollBar = null;
2619             }
2620         }
2621     } // End of BasicTreeUI.ComponentHandler
2622 
2623 
2624     /**
2625      * Forwards all TreeModel events to the TreeState.
2626      */
2627     public class TreeModelHandler implements TreeModelListener {
2628 
2629         // NOTE: This class exists only for backward compatibility. All
2630         // its functionality has been moved into Handler. If you need to add
2631         // new functionality add it to the Handler, but make sure this
2632         // class calls into the Handler.
2633 
2634         public void treeNodesChanged(TreeModelEvent e) {
2635             getHandler().treeNodesChanged(e);
2636         }
2637 
2638         public void treeNodesInserted(TreeModelEvent e) {
2639             getHandler().treeNodesInserted(e);
2640         }
2641 
2642         public void treeNodesRemoved(TreeModelEvent e) {
2643             getHandler().treeNodesRemoved(e);
2644         }
2645 
2646         public void treeStructureChanged(TreeModelEvent e) {
2647             getHandler().treeStructureChanged(e);
2648         }
2649     } // End of BasicTreeUI.TreeModelHandler
2650 
2651 
2652     /**
2653      * Listens for changes in the selection model and updates the display
2654      * accordingly.
2655      */
2656     public class TreeSelectionHandler implements TreeSelectionListener {
2657 
2658         // NOTE: This class exists only for backward compatibility. All
2659         // its functionality has been moved into Handler. If you need to add
2660         // new functionality add it to the Handler, but make sure this
2661         // class calls into the Handler.
2662 
2663         /**
2664          * Messaged when the selection changes in the tree we're displaying
2665          * for.  Stops editing, messages super and displays the changed paths.
2666          */
2667         public void valueChanged(TreeSelectionEvent event) {
2668             getHandler().valueChanged(event);
2669         }
2670     }// End of BasicTreeUI.TreeSelectionHandler
2671 
2672 
2673     /**
2674      * Listener responsible for getting cell editing events and updating
2675      * the tree accordingly.
2676      */
2677     public class CellEditorHandler implements CellEditorListener {
2678 
2679         // NOTE: This class exists only for backward compatibility. All
2680         // its functionality has been moved into Handler. If you need to add
2681         // new functionality add it to the Handler, but make sure this
2682         // class calls into the Handler.
2683 
2684         /** Messaged when editing has stopped in the tree. */
2685         public void editingStopped(ChangeEvent e) {
2686             getHandler().editingStopped(e);
2687         }
2688 
2689         /** Messaged when editing has been canceled in the tree. */
2690         public void editingCanceled(ChangeEvent e) {
2691             getHandler().editingCanceled(e);
2692         }
2693     } // BasicTreeUI.CellEditorHandler
2694 
2695 
2696     /**
2697      * This is used to get multiple key down events to appropriately generate
2698      * events.
2699      */
2700     public class KeyHandler extends KeyAdapter {
2701 
2702         // NOTE: This class exists only for backward compatibility. All
2703         // its functionality has been moved into Handler. If you need to add
2704         // new functionality add it to the Handler, but make sure this
2705         // class calls into the Handler.
2706 
2707         // Also note these fields aren't use anymore, nor does Handler have
2708         // the old functionality. This behavior worked around an old bug
2709         // in JComponent that has long since been fixed.
2710 
2711         /** Key code that is being generated for. */
2712         protected Action              repeatKeyAction;
2713 
2714         /** Set to true while keyPressed is active. */
2715         protected boolean            isKeyDown;
2716 
2717         /**
2718          * Invoked when a key has been typed.
2719          *
2720          * Moves the keyboard focus to the first element
2721          * whose first letter matches the alphanumeric key
2722          * pressed by the user. Subsequent same key presses
2723          * move the keyboard focus to the next object that
2724          * starts with the same letter.
2725          */
2726         public void keyTyped(KeyEvent e) {
2727             getHandler().keyTyped(e);
2728         }
2729 
2730         public void keyPressed(KeyEvent e) {
2731             getHandler().keyPressed(e);
2732         }
2733 
2734         public void keyReleased(KeyEvent e) {
2735             getHandler().keyReleased(e);
2736         }
2737     } // End of BasicTreeUI.KeyHandler
2738 
2739 
2740     /**
2741      * Repaints the lead selection row when focus is lost/gained.
2742      */
2743     public class FocusHandler implements FocusListener {
2744         // NOTE: This class exists only for backward compatibility. All
2745         // its functionality has been moved into Handler. If you need to add
2746         // new functionality add it to the Handler, but make sure this
2747         // class calls into the Handler.
2748 
2749         /**
2750          * Invoked when focus is activated on the tree we're in, redraws the
2751          * lead row.
2752          */
2753         public void focusGained(FocusEvent e) {
2754             getHandler().focusGained(e);
2755         }
2756 
2757         /**
2758          * Invoked when focus is activated on the tree we're in, redraws the
2759          * lead row.
2760          */
2761         public void focusLost(FocusEvent e) {
2762             getHandler().focusLost(e);
2763         }
2764     } // End of class BasicTreeUI.FocusHandler
2765 
2766 
2767     /**
2768      * Class responsible for getting size of node, method is forwarded
2769      * to BasicTreeUI method. X location does not include insets, that is
2770      * handled in getPathBounds.
2771      */
2772     // This returns locations that don't include any Insets.
2773     public class NodeDimensionsHandler extends
2774                  AbstractLayoutCache.NodeDimensions {
2775         /**
2776          * Responsible for getting the size of a particular node.
2777          */
2778         public Rectangle getNodeDimensions(Object value, int row,
2779                                            int depth, boolean expanded,
2780                                            Rectangle size) {
2781             // Return size of editing component, if editing and asking
2782             // for editing row.
2783             if(editingComponent != null && editingRow == row) {
2784                 Dimension        prefSize = editingComponent.
2785                                               getPreferredSize();
2786                 int              rh = getRowHeight();
2787 
2788                 if(rh > 0 && rh != prefSize.height)
2789                     prefSize.height = rh;
2790                 if(size != null) {
2791                     size.x = getRowX(row, depth);
2792                     size.width = prefSize.width;
2793                     size.height = prefSize.height;
2794                 }
2795                 else {
2796                     size = new Rectangle(getRowX(row, depth), 0,
2797                                          prefSize.width, prefSize.height);
2798                 }
2799                 return size;
2800             }
2801             // Not editing, use renderer.
2802             if(currentCellRenderer != null) {
2803                 Component          aComponent;
2804 
2805                 aComponent = currentCellRenderer.getTreeCellRendererComponent
2806                     (tree, value, tree.isRowSelected(row),
2807                      expanded, treeModel.isLeaf(value), row,
2808                      false);
2809                 if(tree != null) {
2810                     // Only ever removed when UI changes, this is OK!
2811                     rendererPane.add(aComponent);
2812                     aComponent.validate();
2813                 }
2814                 Dimension        prefSize = aComponent.getPreferredSize();
2815 
2816                 if(size != null) {
2817                     size.x = getRowX(row, depth);
2818                     size.width = prefSize.width;
2819                     size.height = prefSize.height;
2820                 }
2821                 else {
2822                     size = new Rectangle(getRowX(row, depth), 0,
2823                                          prefSize.width, prefSize.height);
2824                 }
2825                 return size;
2826             }
2827             return null;
2828         }
2829 
2830         /**
2831          * @return amount to indent the given row.
2832          */
2833         protected int getRowX(int row, int depth) {
2834             return BasicTreeUI.this.getRowX(row, depth);
2835         }
2836 
2837     } // End of class BasicTreeUI.NodeDimensionsHandler
2838 
2839 
2840     /**
2841      * TreeMouseListener is responsible for updating the selection
2842      * based on mouse events.
2843      */
2844     public class MouseHandler extends MouseAdapter implements MouseMotionListener
2845  {
2846         // NOTE: This class exists only for backward compatibility. All
2847         // its functionality has been moved into Handler. If you need to add
2848         // new functionality add it to the Handler, but make sure this
2849         // class calls into the Handler.
2850 
2851         /**
2852          * Invoked when a mouse button has been pressed on a component.
2853          */
2854         public void mousePressed(MouseEvent e) {
2855             getHandler().mousePressed(e);
2856         }
2857 
2858         public void mouseDragged(MouseEvent e) {
2859             getHandler().mouseDragged(e);
2860         }
2861 
2862         /**
2863          * Invoked when the mouse button has been moved on a component
2864          * (with no buttons no down).
2865          * @since 1.4
2866          */
2867         public void mouseMoved(MouseEvent e) {
2868             getHandler().mouseMoved(e);
2869         }
2870 
2871         public void mouseReleased(MouseEvent e) {
2872             getHandler().mouseReleased(e);
2873         }
2874     } // End of BasicTreeUI.MouseHandler
2875 
2876 
2877     /**
2878      * PropertyChangeListener for the tree. Updates the appropriate
2879      * variable, or TreeState, based on what changes.
2880      */
2881     public class PropertyChangeHandler implements
2882                        PropertyChangeListener {
2883 
2884         // NOTE: This class exists only for backward compatibility. All
2885         // its functionality has been moved into Handler. If you need to add
2886         // new functionality add it to the Handler, but make sure this
2887         // class calls into the Handler.
2888 
2889         public void propertyChange(PropertyChangeEvent event) {
2890             getHandler().propertyChange(event);
2891         }
2892     } // End of BasicTreeUI.PropertyChangeHandler
2893 
2894 
2895     /**
2896      * Listener on the TreeSelectionModel, resets the row selection if
2897      * any of the properties of the model change.
2898      */
2899     public class SelectionModelPropertyChangeHandler implements
2900                       PropertyChangeListener {
2901 
2902         // NOTE: This class exists only for backward compatibility. All
2903         // its functionality has been moved into Handler. If you need to add
2904         // new functionality add it to the Handler, but make sure this
2905         // class calls into the Handler.
2906 
2907         public void propertyChange(PropertyChangeEvent event) {
2908             getHandler().propertyChange(event);
2909         }
2910     } // End of BasicTreeUI.SelectionModelPropertyChangeHandler
2911 
2912 
2913     /**
2914      * <code>TreeTraverseAction</code> is the action used for left/right keys.
2915      * Will toggle the expandedness of a node, as well as potentially
2916      * incrementing the selection.
2917      */
2918     @SuppressWarnings("serial") // Superclass is not serializable across versions
2919     public class TreeTraverseAction extends AbstractAction {
2920         /** Determines direction to traverse, 1 means expand, -1 means
2921           * collapse. */
2922         protected int direction;
2923         /** True if the selection is reset, false means only the lead path
2924          * changes. */
2925         private boolean changeSelection;
2926 
2927         public TreeTraverseAction(int direction, String name) {
2928             this(direction, name, true);
2929         }
2930 
2931         private TreeTraverseAction(int direction, String name,
2932                                    boolean changeSelection) {
2933             this.direction = direction;
2934             this.changeSelection = changeSelection;
2935         }
2936 
2937         public void actionPerformed(ActionEvent e) {
2938             if (tree != null) {
2939                 SHARED_ACTION.traverse(tree, BasicTreeUI.this, direction,
2940                                        changeSelection);
2941             }
2942         }
2943 
2944         public boolean isEnabled() { return (tree != null &&
2945                                              tree.isEnabled()); }
2946     } // BasicTreeUI.TreeTraverseAction
2947 
2948 
2949     /** TreePageAction handles page up and page down events.
2950       */
2951     @SuppressWarnings("serial") // Superclass is not serializable across versions
2952     public class TreePageAction extends AbstractAction {
2953         /** Specifies the direction to adjust the selection by. */
2954         protected int         direction;
2955         /** True indicates should set selection from anchor path. */
2956         private boolean       addToSelection;
2957         private boolean       changeSelection;
2958 
2959         public TreePageAction(int direction, String name) {
2960             this(direction, name, false, true);
2961         }
2962 
2963         private TreePageAction(int direction, String name,
2964                                boolean addToSelection,
2965                                boolean changeSelection) {
2966             this.direction = direction;
2967             this.addToSelection = addToSelection;
2968             this.changeSelection = changeSelection;
2969         }
2970 
2971         public void actionPerformed(ActionEvent e) {
2972             if (tree != null) {
2973                 SHARED_ACTION.page(tree, BasicTreeUI.this, direction,
2974                                    addToSelection, changeSelection);
2975             }
2976         }
2977 
2978         public boolean isEnabled() { return (tree != null &&
2979                                              tree.isEnabled()); }
2980 
2981     } // BasicTreeUI.TreePageAction
2982 
2983 
2984     /** TreeIncrementAction is used to handle up/down actions.  Selection
2985       * is moved up or down based on direction.
2986       */
2987     @SuppressWarnings("serial") // Superclass is not serializable across versions
2988     public class TreeIncrementAction extends AbstractAction  {
2989         /** Specifies the direction to adjust the selection by. */
2990         protected int         direction;
2991         /** If true the new item is added to the selection, if false the
2992          * selection is reset. */
2993         private boolean       addToSelection;
2994         private boolean       changeSelection;
2995 
2996         public TreeIncrementAction(int direction, String name) {
2997             this(direction, name, false, true);
2998         }
2999 
3000         private TreeIncrementAction(int direction, String name,
3001                                    boolean addToSelection,
3002                                     boolean changeSelection) {
3003             this.direction = direction;
3004             this.addToSelection = addToSelection;
3005             this.changeSelection = changeSelection;
3006         }
3007 
3008         public void actionPerformed(ActionEvent e) {
3009             if (tree != null) {
3010                 SHARED_ACTION.increment(tree, BasicTreeUI.this, direction,
3011                                         addToSelection, changeSelection);
3012             }
3013         }
3014 
3015         public boolean isEnabled() { return (tree != null &&
3016                                              tree.isEnabled()); }
3017 
3018     } // End of class BasicTreeUI.TreeIncrementAction
3019 
3020     /**
3021       * TreeHomeAction is used to handle end/home actions.
3022       * Scrolls either the first or last cell to be visible based on
3023       * direction.
3024       */
3025     @SuppressWarnings("serial") // Superclass is not serializable across versions
3026     public class TreeHomeAction extends AbstractAction {
3027         protected int            direction;
3028         /** Set to true if append to selection. */
3029         private boolean          addToSelection;
3030         private boolean          changeSelection;
3031 
3032         public TreeHomeAction(int direction, String name) {
3033             this(direction, name, false, true);
3034         }
3035 
3036         private TreeHomeAction(int direction, String name,
3037                                boolean addToSelection,
3038                                boolean changeSelection) {
3039             this.direction = direction;
3040             this.changeSelection = changeSelection;
3041             this.addToSelection = addToSelection;
3042         }
3043 
3044         public void actionPerformed(ActionEvent e) {
3045             if (tree != null) {
3046                 SHARED_ACTION.home(tree, BasicTreeUI.this, direction,
3047                                    addToSelection, changeSelection);
3048             }
3049         }
3050 
3051         public boolean isEnabled() { return (tree != null &&
3052                                              tree.isEnabled()); }
3053 
3054     } // End of class BasicTreeUI.TreeHomeAction
3055 
3056 
3057     /**
3058       * For the first selected row expandedness will be toggled.
3059       */
3060     @SuppressWarnings("serial") // Superclass is not serializable across versions
3061     public class TreeToggleAction extends AbstractAction {
3062         public TreeToggleAction(String name) {
3063         }
3064 
3065         public void actionPerformed(ActionEvent e) {
3066             if(tree != null) {
3067                 SHARED_ACTION.toggle(tree, BasicTreeUI.this);
3068             }
3069         }
3070 
3071         public boolean isEnabled() { return (tree != null &&
3072                                              tree.isEnabled()); }
3073 
3074     } // End of class BasicTreeUI.TreeToggleAction
3075 
3076 
3077     /**
3078      * ActionListener that invokes cancelEditing when action performed.
3079      */
3080     @SuppressWarnings("serial") // Superclass is not serializable across versions
3081     public class TreeCancelEditingAction extends AbstractAction {
3082         public TreeCancelEditingAction(String name) {
3083         }
3084 
3085         public void actionPerformed(ActionEvent e) {
3086             if(tree != null) {
3087                 SHARED_ACTION.cancelEditing(tree, BasicTreeUI.this);
3088             }
3089         }
3090 
3091         public boolean isEnabled() { return (tree != null &&
3092                                              tree.isEnabled() &&
3093                                              isEditing(tree)); }
3094     } // End of class BasicTreeUI.TreeCancelEditingAction
3095 
3096 
3097     /**
3098       * MouseInputHandler handles passing all mouse events,
3099       * including mouse motion events, until the mouse is released to
3100       * the destination it is constructed with. It is assumed all the
3101       * events are currently target at source.
3102       */
3103     public class MouseInputHandler extends Object implements
3104                      MouseInputListener
3105     {
3106         /** Source that events are coming from. */
3107         protected Component        source;
3108         /** Destination that receives all events. */
3109         protected Component        destination;
3110         private Component          focusComponent;
3111         private boolean            dispatchedEvent;
3112 
3113         public MouseInputHandler(Component source, Component destination,
3114                                       MouseEvent event){
3115             this(source, destination, event, null);
3116         }
3117 
3118         MouseInputHandler(Component source, Component destination,
3119                           MouseEvent event, Component focusComponent) {
3120             this.source = source;
3121             this.destination = destination;
3122             this.source.addMouseListener(this);
3123             this.source.addMouseMotionListener(this);
3124 
3125             SwingUtilities2.setSkipClickCount(destination,
3126                                               event.getClickCount() - 1);
3127 
3128             /* Dispatch the editing event! */
3129             destination.dispatchEvent(SwingUtilities.convertMouseEvent
3130                                           (source, event, destination));
3131             this.focusComponent = focusComponent;
3132         }
3133 
3134         public void mouseClicked(MouseEvent e) {
3135             if(destination != null) {
3136                 dispatchedEvent = true;
3137                 destination.dispatchEvent(SwingUtilities.convertMouseEvent
3138                                           (source, e, destination));
3139             }
3140         }
3141 
3142         public void mousePressed(MouseEvent e) {
3143         }
3144 
3145         public void mouseReleased(MouseEvent e) {
3146             if(destination != null)
3147                 destination.dispatchEvent(SwingUtilities.convertMouseEvent
3148                                           (source, e, destination));
3149             removeFromSource();
3150         }
3151 
3152         public void mouseEntered(MouseEvent e) {
3153             if (!SwingUtilities.isLeftMouseButton(e)) {
3154                 removeFromSource();
3155             }
3156         }
3157 
3158         public void mouseExited(MouseEvent e) {
3159             if (!SwingUtilities.isLeftMouseButton(e)) {
3160                 removeFromSource();
3161             }
3162         }
3163 
3164         public void mouseDragged(MouseEvent e) {
3165             if(destination != null) {
3166                 dispatchedEvent = true;
3167                 destination.dispatchEvent(SwingUtilities.convertMouseEvent
3168                                           (source, e, destination));
3169             }
3170         }
3171 
3172         public void mouseMoved(MouseEvent e) {
3173             removeFromSource();
3174         }
3175 
3176         protected void removeFromSource() {
3177             if(source != null) {
3178                 source.removeMouseListener(this);
3179                 source.removeMouseMotionListener(this);
3180                 if (focusComponent != null &&
3181                       focusComponent == destination && !dispatchedEvent &&
3182                       (focusComponent instanceof JTextField)) {
3183                     ((JTextField)focusComponent).selectAll();
3184                 }
3185             }
3186             source = destination = null;
3187         }
3188 
3189     } // End of class BasicTreeUI.MouseInputHandler
3190 
3191     private static final TransferHandler defaultTransferHandler = new TreeTransferHandler();
3192 
3193     @SuppressWarnings("serial") // JDK-implementation class
3194     static class TreeTransferHandler extends TransferHandler implements UIResource, Comparator<TreePath> {
3195 
3196         private JTree tree;
3197 
3198         /**
3199          * Create a Transferable to use as the source for a data transfer.
3200          *
3201          * @param c  The component holding the data to be transfered.  This
3202          *  argument is provided to enable sharing of TransferHandlers by
3203          *  multiple components.
3204          * @return  The representation of the data to be transfered.
3205          *
3206          */
3207         protected Transferable createTransferable(JComponent c) {
3208             if (c instanceof JTree) {
3209                 tree = (JTree) c;
3210                 TreePath[] paths = tree.getSelectionPaths();
3211 
3212                 if (paths == null || paths.length == 0) {
3213                     return null;
3214                 }
3215 
3216                 StringBuilder plainStr = new StringBuilder();
3217                 StringBuilder htmlStr = new StringBuilder();
3218 
3219                 htmlStr.append("<html>\n<body>\n<ul>\n");
3220 
3221                 TreeModel model = tree.getModel();
3222                 TreePath lastPath = null;
3223                 TreePath[] displayPaths = getDisplayOrderPaths(paths);
3224 
3225                 for (TreePath path : displayPaths) {
3226                     Object node = path.getLastPathComponent();
3227                     boolean leaf = model.isLeaf(node);
3228                     String label = getDisplayString(path, true, leaf);
3229 
3230                     plainStr.append(label + "\n");
3231                     htmlStr.append("  <li>" + label + "\n");
3232                 }
3233 
3234                 // remove the last newline
3235                 plainStr.deleteCharAt(plainStr.length() - 1);
3236                 htmlStr.append("</ul>\n</body>\n</html>");
3237 
3238                 tree = null;
3239 
3240                 return new BasicTransferable(plainStr.toString(), htmlStr.toString());
3241             }
3242 
3243             return null;
3244         }
3245 
3246         public int compare(TreePath o1, TreePath o2) {
3247             int row1 = tree.getRowForPath(o1);
3248             int row2 = tree.getRowForPath(o2);
3249             return row1 - row2;
3250         }
3251 
3252         String getDisplayString(TreePath path, boolean selected, boolean leaf) {
3253             int row = tree.getRowForPath(path);
3254             boolean hasFocus = tree.getLeadSelectionRow() == row;
3255             Object node = path.getLastPathComponent();
3256             return tree.convertValueToText(node, selected, tree.isExpanded(row),
3257                                            leaf, row, hasFocus);
3258         }
3259 
3260         /**
3261          * Selection paths are in selection order.  The conversion to
3262          * HTML requires display order.  This method resorts the paths
3263          * to be in the display order.
3264          */
3265         TreePath[] getDisplayOrderPaths(TreePath[] paths) {
3266             // sort the paths to display order rather than selection order
3267             ArrayList<TreePath> selOrder = new ArrayList<TreePath>();
3268             for (TreePath path : paths) {
3269                 selOrder.add(path);
3270             }
3271             Collections.sort(selOrder, this);
3272             int n = selOrder.size();
3273             TreePath[] displayPaths = new TreePath[n];
3274             for (int i = 0; i < n; i++) {
3275                 displayPaths[i] = selOrder.get(i);
3276             }
3277             return displayPaths;
3278         }
3279 
3280         public int getSourceActions(JComponent c) {
3281             return COPY;
3282         }
3283 
3284     }
3285 
3286 
3287     private class Handler implements CellEditorListener, FocusListener,
3288                   KeyListener, MouseListener, MouseMotionListener,
3289                   PropertyChangeListener, TreeExpansionListener,
3290                   TreeModelListener, TreeSelectionListener,
3291                   BeforeDrag {
3292         //
3293         // KeyListener
3294         //
3295         private String prefix = "";
3296         private String typedString = "";
3297         private long lastTime = 0L;
3298 
3299         /**
3300          * Invoked when a key has been typed.
3301          *
3302          * Moves the keyboard focus to the first element whose prefix matches the
3303          * sequence of alphanumeric keys pressed by the user with delay less
3304          * than value of <code>timeFactor</code> property (or 1000 milliseconds
3305          * if it is not defined). Subsequent same key presses move the keyboard
3306          * focus to the next object that starts with the same letter until another
3307          * key is pressed, then it is treated as the prefix with appropriate number
3308          * of the same letters followed by first typed another letter.
3309          */
3310         public void keyTyped(KeyEvent e) {
3311             // handle first letter navigation
3312             if(tree != null && tree.getRowCount()>0 && tree.hasFocus() &&
3313                tree.isEnabled()) {
3314                 if (e.isAltDown() || BasicGraphicsUtils.isMenuShortcutKeyDown(e) ||
3315                     isNavigationKey(e)) {
3316                     return;
3317                 }
3318                 boolean startingFromSelection = true;
3319 
3320                 char c = e.getKeyChar();
3321 
3322                 long time = e.getWhen();
3323                 int startingRow = tree.getLeadSelectionRow();
3324                 if (time - lastTime < timeFactor) {
3325                     typedString += c;
3326                     if((prefix.length() == 1) && (c == prefix.charAt(0))) {
3327                         // Subsequent same key presses move the keyboard focus to the next
3328                         // object that starts with the same letter.
3329                         startingRow++;
3330                     } else {
3331                         prefix = typedString;
3332                     }
3333                 } else {
3334                     startingRow++;
3335                     typedString = "" + c;
3336                     prefix = typedString;
3337                 }
3338                 lastTime = time;
3339 
3340                 if (startingRow < 0 || startingRow >= tree.getRowCount()) {
3341                     startingFromSelection = false;
3342                     startingRow = 0;
3343                 }
3344                 TreePath path = tree.getNextMatch(prefix, startingRow,
3345                                                   Position.Bias.Forward);
3346                 if (path != null) {
3347                     tree.setSelectionPath(path);
3348                     int row = getRowForPath(tree, path);
3349                     ensureRowsAreVisible(row, row);
3350                 } else if (startingFromSelection) {
3351                     path = tree.getNextMatch(prefix, 0,
3352                                              Position.Bias.Forward);
3353                     if (path != null) {
3354                         tree.setSelectionPath(path);
3355                         int row = getRowForPath(tree, path);
3356                         ensureRowsAreVisible(row, row);
3357                     }
3358                 }
3359             }
3360         }
3361 
3362         /**
3363          * Invoked when a key has been pressed.
3364          *
3365          * Checks to see if the key event is a navigation key to prevent
3366          * dispatching these keys for the first letter navigation.
3367          */
3368         public void keyPressed(KeyEvent e) {
3369             if (tree != null && isNavigationKey(e)) {
3370                 prefix = "";
3371                 typedString = "";
3372                 lastTime = 0L;
3373             }
3374         }
3375 
3376         public void keyReleased(KeyEvent e) {
3377         }
3378 
3379         /**
3380          * Returns whether or not the supplied key event maps to a key that is used for
3381          * navigation.  This is used for optimizing key input by only passing non-
3382          * navigation keys to the first letter navigation mechanism.
3383          */
3384         private boolean isNavigationKey(KeyEvent event) {
3385             InputMap inputMap = tree.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
3386             KeyStroke key = KeyStroke.getKeyStrokeForEvent(event);
3387 
3388             return inputMap != null && inputMap.get(key) != null;
3389         }
3390 
3391 
3392         //
3393         // PropertyChangeListener
3394         //
3395         public void propertyChange(PropertyChangeEvent event) {
3396             if (event.getSource() == treeSelectionModel) {
3397                 treeSelectionModel.resetRowSelection();
3398             }
3399             else if(event.getSource() == tree) {
3400                 String              changeName = event.getPropertyName();
3401 
3402                 if (changeName == JTree.LEAD_SELECTION_PATH_PROPERTY) {
3403                     if (!ignoreLAChange) {
3404                         updateLeadSelectionRow();
3405                         repaintPath((TreePath)event.getOldValue());
3406                         repaintPath((TreePath)event.getNewValue());
3407                     }
3408                 }
3409                 else if (changeName == JTree.ANCHOR_SELECTION_PATH_PROPERTY) {
3410                     if (!ignoreLAChange) {
3411                         repaintPath((TreePath)event.getOldValue());
3412                         repaintPath((TreePath)event.getNewValue());
3413                     }
3414                 }
3415                 if(changeName == JTree.CELL_RENDERER_PROPERTY) {
3416                     setCellRenderer((TreeCellRenderer)event.getNewValue());
3417                     redoTheLayout();
3418                 }
3419                 else if(changeName == JTree.TREE_MODEL_PROPERTY) {
3420                     setModel((TreeModel)event.getNewValue());
3421                 }
3422                 else if(changeName == JTree.ROOT_VISIBLE_PROPERTY) {
3423                     setRootVisible(((Boolean)event.getNewValue()).
3424                                    booleanValue());
3425                 }
3426                 else if(changeName == JTree.SHOWS_ROOT_HANDLES_PROPERTY) {
3427                     setShowsRootHandles(((Boolean)event.getNewValue()).
3428                                         booleanValue());
3429                 }
3430                 else if(changeName == JTree.ROW_HEIGHT_PROPERTY) {
3431                     setRowHeight(((Integer)event.getNewValue()).
3432                                  intValue());
3433                 }
3434                 else if(changeName == JTree.CELL_EDITOR_PROPERTY) {
3435                     setCellEditor((TreeCellEditor)event.getNewValue());
3436                 }
3437                 else if(changeName == JTree.EDITABLE_PROPERTY) {
3438                     setEditable(((Boolean)event.getNewValue()).booleanValue());
3439                 }
3440                 else if(changeName == JTree.LARGE_MODEL_PROPERTY) {
3441                     setLargeModel(tree.isLargeModel());
3442                 }
3443                 else if(changeName == JTree.SELECTION_MODEL_PROPERTY) {
3444                     setSelectionModel(tree.getSelectionModel());
3445                 }
3446                 else if(changeName == "font") {
3447                     completeEditing();
3448                     if(treeState != null)
3449                         treeState.invalidateSizes();
3450                     updateSize();
3451                 }
3452                 else if (changeName == "componentOrientation") {
3453                     if (tree != null) {
3454                         leftToRight = BasicGraphicsUtils.isLeftToRight(tree);
3455                         redoTheLayout();
3456                         tree.treeDidChange();
3457 
3458                         InputMap km = getInputMap(JComponent.WHEN_FOCUSED);
3459                         SwingUtilities.replaceUIInputMap(tree,
3460                                                 JComponent.WHEN_FOCUSED, km);
3461                     }
3462                 } else if ("dropLocation" == changeName) {
3463                     JTree.DropLocation oldValue = (JTree.DropLocation)event.getOldValue();
3464                     repaintDropLocation(oldValue);
3465                     repaintDropLocation(tree.getDropLocation());
3466                 }
3467             }
3468         }
3469 
3470         private void repaintDropLocation(JTree.DropLocation loc) {
3471             if (loc == null) {
3472                 return;
3473             }
3474 
3475             Rectangle r;
3476 
3477             if (isDropLine(loc)) {
3478                 r = getDropLineRect(loc);
3479             } else {
3480                 r = tree.getPathBounds(loc.getPath());
3481             }
3482 
3483             if (r != null) {
3484                 tree.repaint(r);
3485             }
3486         }
3487 
3488         //
3489         // MouseListener
3490         //
3491 
3492         // Whether or not the mouse press (which is being considered as part
3493         // of a drag sequence) also caused the selection change to be fully
3494         // processed.
3495         private boolean dragPressDidSelection;
3496 
3497         // Set to true when a drag gesture has been fully recognized and DnD
3498         // begins. Use this to ignore further mouse events which could be
3499         // delivered if DnD is cancelled (via ESCAPE for example)
3500         private boolean dragStarted;
3501 
3502         // The path over which the press occurred and the press event itself
3503         private TreePath pressedPath;
3504         private MouseEvent pressedEvent;
3505 
3506         // Used to detect whether the press event causes a selection change.
3507         // If it does, we won't try to start editing on the release.
3508         private boolean valueChangedOnPress;
3509 
3510         private boolean isActualPath(TreePath path, int x, int y) {
3511             if (path == null) {
3512                 return false;
3513             }
3514 
3515             Rectangle bounds = getPathBounds(tree, path);
3516             if (bounds == null || y > (bounds.y + bounds.height)) {
3517                 return false;
3518             }
3519 
3520             return (x >= bounds.x) && (x <= (bounds.x + bounds.width));
3521         }
3522 
3523         public void mouseClicked(MouseEvent e) {
3524         }
3525 
3526         public void mouseEntered(MouseEvent e) {
3527         }
3528 
3529         public void mouseExited(MouseEvent e) {
3530         }
3531 
3532         /**
3533          * Invoked when a mouse button has been pressed on a component.
3534          */
3535         public void mousePressed(MouseEvent e) {
3536             if (SwingUtilities2.shouldIgnore(e, tree)) {
3537                 return;
3538             }
3539 
3540             // if we can't stop any ongoing editing, do nothing
3541             if (isEditing(tree) && tree.getInvokesStopCellEditing()
3542                                 && !stopEditing(tree)) {
3543                 return;
3544             }
3545 
3546             completeEditing();
3547 
3548             pressedPath = getClosestPathForLocation(tree, e.getX(), e.getY());
3549 
3550             if (tree.getDragEnabled()) {
3551                 mousePressedDND(e);
3552             } else {
3553                 SwingUtilities2.adjustFocus(tree);
3554                 handleSelection(e);
3555             }
3556         }
3557 
3558         private void mousePressedDND(MouseEvent e) {
3559             pressedEvent = e;
3560             boolean grabFocus = true;
3561             dragStarted = false;
3562             valueChangedOnPress = false;
3563 
3564             // if we have a valid path and this is a drag initiating event
3565             if (isActualPath(pressedPath, e.getX(), e.getY()) &&
3566                     DragRecognitionSupport.mousePressed(e)) {
3567 
3568                 dragPressDidSelection = false;
3569 
3570                 if (BasicGraphicsUtils.isMenuShortcutKeyDown(e)) {
3571                     // do nothing for control - will be handled on release
3572                     // or when drag starts
3573                     return;
3574                 } else if (!e.isShiftDown() && tree.isPathSelected(pressedPath)) {
3575                     // clicking on something that's already selected
3576                     // and need to make it the lead now
3577                     setAnchorSelectionPath(pressedPath);
3578                     setLeadSelectionPath(pressedPath, true);
3579                     return;
3580                 }
3581 
3582                 dragPressDidSelection = true;
3583 
3584                 // could be a drag initiating event - don't grab focus
3585                 grabFocus = false;
3586             }
3587 
3588             if (grabFocus) {
3589                 SwingUtilities2.adjustFocus(tree);
3590             }
3591 
3592             handleSelection(e);
3593         }
3594 
3595         void handleSelection(MouseEvent e) {
3596             if(pressedPath != null) {
3597                 Rectangle bounds = getPathBounds(tree, pressedPath);
3598 
3599                 if (bounds == null || e.getY() >= (bounds.y + bounds.height)) {
3600                     return;
3601                 }
3602 
3603                 // Preferably checkForClickInExpandControl could take
3604                 // the Event to do this it self!
3605                 if(SwingUtilities.isLeftMouseButton(e)) {
3606                     checkForClickInExpandControl(pressedPath, e.getX(), e.getY());
3607                 }
3608 
3609                 int x = e.getX();
3610 
3611                 // Perhaps they clicked the cell itself. If so,
3612                 // select it.
3613                 if (x >= bounds.x && x < (bounds.x + bounds.width)) {
3614                     if (tree.getDragEnabled() || !startEditing(pressedPath, e)) {
3615                         selectPathForEvent(pressedPath, e);
3616                     }
3617                 }
3618             }
3619         }
3620 
3621         public void dragStarting(MouseEvent me) {
3622             dragStarted = true;
3623 
3624             if (BasicGraphicsUtils.isMenuShortcutKeyDown(me)) {
3625                 tree.addSelectionPath(pressedPath);
3626                 setAnchorSelectionPath(pressedPath);
3627                 setLeadSelectionPath(pressedPath, true);
3628             }
3629 
3630             pressedEvent = null;
3631             pressedPath = null;
3632         }
3633 
3634         public void mouseDragged(MouseEvent e) {
3635             if (SwingUtilities2.shouldIgnore(e, tree)) {
3636                 return;
3637             }
3638 
3639             if (tree.getDragEnabled()) {
3640                 DragRecognitionSupport.mouseDragged(e, this);
3641             }
3642         }
3643 
3644         /**
3645          * Invoked when the mouse button has been moved on a component
3646          * (with no buttons no down).
3647          */
3648         public void mouseMoved(MouseEvent e) {
3649         }
3650 
3651         public void mouseReleased(MouseEvent e) {
3652             if (SwingUtilities2.shouldIgnore(e, tree)) {
3653                 return;
3654             }
3655 
3656             if (tree.getDragEnabled()) {
3657                 mouseReleasedDND(e);
3658             }
3659 
3660             pressedEvent = null;
3661             pressedPath = null;
3662         }
3663 
3664         private void mouseReleasedDND(MouseEvent e) {
3665             MouseEvent me = DragRecognitionSupport.mouseReleased(e);
3666             if (me != null) {
3667                 SwingUtilities2.adjustFocus(tree);
3668                 if (!dragPressDidSelection) {
3669                     handleSelection(me);
3670                 }
3671             }
3672 
3673             if (!dragStarted) {
3674 
3675                 // Note: We don't give the tree a chance to start editing if the
3676                 // mouse press caused a selection change. Otherwise the default
3677                 // tree cell editor will start editing on EVERY press and
3678                 // release. If it turns out that this affects some editors, we
3679                 // can always parameterize this with a client property. ex:
3680                 //
3681                 // if (pressedPath != null &&
3682                 //         (Boolean.TRUE == tree.getClientProperty("Tree.DnD.canEditOnValueChange") ||
3683                 //          !valueChangedOnPress) && ...
3684                 if (pressedPath != null && !valueChangedOnPress &&
3685                         isActualPath(pressedPath, pressedEvent.getX(), pressedEvent.getY())) {
3686 
3687                     startEditingOnRelease(pressedPath, pressedEvent, e);
3688                 }
3689             }
3690         }
3691 
3692         //
3693         // FocusListener
3694         //
3695         public void focusGained(FocusEvent e) {
3696             if(tree != null) {
3697                 Rectangle                 pBounds;
3698 
3699                 pBounds = getPathBounds(tree, tree.getLeadSelectionPath());
3700                 if(pBounds != null)
3701                     tree.repaint(getRepaintPathBounds(pBounds));
3702                 pBounds = getPathBounds(tree, getLeadSelectionPath());
3703                 if(pBounds != null)
3704                     tree.repaint(getRepaintPathBounds(pBounds));
3705             }
3706         }
3707 
3708         public void focusLost(FocusEvent e) {
3709             focusGained(e);
3710         }
3711 
3712         //
3713         // CellEditorListener
3714         //
3715         public void editingStopped(ChangeEvent e) {
3716             completeEditing(false, false, true);
3717         }
3718 
3719         /** Messaged when editing has been canceled in the tree. */
3720         public void editingCanceled(ChangeEvent e) {
3721             completeEditing(false, false, false);
3722         }
3723 
3724 
3725         //
3726         // TreeSelectionListener
3727         //
3728         public void valueChanged(TreeSelectionEvent event) {
3729             valueChangedOnPress = true;
3730 
3731             // Stop editing
3732             completeEditing();
3733             // Make sure all the paths are visible, if necessary.
3734             // PENDING: This should be tweaked when isAdjusting is added
3735             if(tree.getExpandsSelectedPaths() && treeSelectionModel != null) {
3736                 TreePath[]           paths = treeSelectionModel
3737                                          .getSelectionPaths();
3738 
3739                 if(paths != null) {
3740                     for(int counter = paths.length - 1; counter >= 0;
3741                         counter--) {
3742                         TreePath path = paths[counter].getParentPath();
3743                         boolean expand = true;
3744 
3745                         while (path != null) {
3746                             // Indicates this path isn't valid anymore,
3747                             // we shouldn't attempt to expand it then.
3748                             if (treeModel.isLeaf(path.getLastPathComponent())){
3749                                 expand = false;
3750                                 path = null;
3751                             }
3752                             else {
3753                                 path = path.getParentPath();
3754                             }
3755                         }
3756                         if (expand) {
3757                             tree.makeVisible(paths[counter]);
3758                         }
3759                     }
3760                 }
3761             }
3762 
3763             TreePath oldLead = getLeadSelectionPath();
3764             lastSelectedRow = tree.getMinSelectionRow();
3765             TreePath lead = tree.getSelectionModel().getLeadSelectionPath();
3766             setAnchorSelectionPath(lead);
3767             setLeadSelectionPath(lead);
3768 
3769             TreePath[]       changedPaths = event.getPaths();
3770             Rectangle        nodeBounds;
3771             Rectangle        visRect = tree.getVisibleRect();
3772             boolean          paintPaths = true;
3773             int              nWidth = tree.getWidth();
3774 
3775             if(changedPaths != null) {
3776                 int              counter, maxCounter = changedPaths.length;
3777 
3778                 if(maxCounter > 4) {
3779                     tree.repaint();
3780                     paintPaths = false;
3781                 }
3782                 else {
3783                     for (counter = 0; counter < maxCounter; counter++) {
3784                         nodeBounds = getPathBounds(tree,
3785                                                    changedPaths[counter]);
3786                         if(nodeBounds != null &&
3787                            visRect.intersects(nodeBounds))
3788                             tree.repaint(0, nodeBounds.y, nWidth,
3789                                          nodeBounds.height);
3790                     }
3791                 }
3792             }
3793             if(paintPaths) {
3794                 nodeBounds = getPathBounds(tree, oldLead);
3795                 if(nodeBounds != null && visRect.intersects(nodeBounds))
3796                     tree.repaint(0, nodeBounds.y, nWidth, nodeBounds.height);
3797                 nodeBounds = getPathBounds(tree, lead);
3798                 if(nodeBounds != null && visRect.intersects(nodeBounds))
3799                     tree.repaint(0, nodeBounds.y, nWidth, nodeBounds.height);
3800             }
3801         }
3802 
3803 
3804         //
3805         // TreeExpansionListener
3806         //
3807         public void treeExpanded(TreeExpansionEvent event) {
3808             if(event != null && tree != null) {
3809                 TreePath      path = event.getPath();
3810 
3811                 updateExpandedDescendants(path);
3812             }
3813         }
3814 
3815         public void treeCollapsed(TreeExpansionEvent event) {
3816             if(event != null && tree != null) {
3817                 TreePath        path = event.getPath();
3818 
3819                 completeEditing();
3820                 if(path != null && tree.isVisible(path)) {
3821                     treeState.setExpandedState(path, false);
3822                     updateLeadSelectionRow();
3823                     updateSize();
3824                 }
3825             }
3826         }
3827 
3828         //
3829         // TreeModelListener
3830         //
3831         public void treeNodesChanged(TreeModelEvent e) {
3832             if(treeState != null && e != null) {
3833                 TreePath parentPath = SwingUtilities2.getTreePath(e, getModel());
3834                 int[] indices = e.getChildIndices();
3835                 if (indices == null || indices.length == 0) {
3836                     // The root has changed
3837                     treeState.treeNodesChanged(e);
3838                     updateSize();
3839                 }
3840                 else if (treeState.isExpanded(parentPath)) {
3841                     // Changed nodes are visible
3842                     // Find the minimum index, we only need paint from there
3843                     // down.
3844                     int minIndex = indices[0];
3845                     for (int i = indices.length - 1; i > 0; i--) {
3846                         minIndex = Math.min(indices[i], minIndex);
3847                     }
3848                     Object minChild = treeModel.getChild(
3849                             parentPath.getLastPathComponent(), minIndex);
3850                     TreePath minPath = parentPath.pathByAddingChild(minChild);
3851                     Rectangle minBounds = getPathBounds(tree, minPath);
3852 
3853                     // Forward to the treestate
3854                     treeState.treeNodesChanged(e);
3855 
3856                     // Mark preferred size as bogus.
3857                     updateSize0();
3858 
3859                     // And repaint
3860                     Rectangle newMinBounds = getPathBounds(tree, minPath);
3861                     if (minBounds == null || newMinBounds == null) {
3862                         return;
3863                     }
3864 
3865                     if (indices.length == 1 &&
3866                             newMinBounds.height == minBounds.height) {
3867                         tree.repaint(0, minBounds.y, tree.getWidth(),
3868                                      minBounds.height);
3869                     }
3870                     else {
3871                         tree.repaint(0, minBounds.y, tree.getWidth(),
3872                                      tree.getHeight() - minBounds.y);
3873                     }
3874                 }
3875                 else {
3876                     // Nodes that changed aren't visible.  No need to paint
3877                     treeState.treeNodesChanged(e);
3878                 }
3879             }
3880         }
3881 
3882         public void treeNodesInserted(TreeModelEvent e) {
3883             if(treeState != null && e != null) {
3884                 treeState.treeNodesInserted(e);
3885 
3886                 updateLeadSelectionRow();
3887 
3888                 TreePath       path = SwingUtilities2.getTreePath(e, getModel());
3889 
3890                 if(treeState.isExpanded(path)) {
3891                     updateSize();
3892                 }
3893                 else {
3894                     // PENDING(sky): Need a method in TreeModelEvent
3895                     // that can return the count, getChildIndices allocs
3896                     // a new array!
3897                     int[]      indices = e.getChildIndices();
3898                     int        childCount = treeModel.getChildCount
3899                                             (path.getLastPathComponent());
3900 
3901                     if(indices != null && (childCount - indices.length) == 0)
3902                         updateSize();
3903                 }
3904             }
3905         }
3906 
3907         public void treeNodesRemoved(TreeModelEvent e) {
3908             if(treeState != null && e != null) {
3909                 treeState.treeNodesRemoved(e);
3910 
3911                 updateLeadSelectionRow();
3912 
3913                 TreePath       path = SwingUtilities2.getTreePath(e, getModel());
3914 
3915                 if(treeState.isExpanded(path) ||
3916                    treeModel.getChildCount(path.getLastPathComponent()) == 0)
3917                     updateSize();
3918             }
3919         }
3920 
3921         public void treeStructureChanged(TreeModelEvent e) {
3922             if(treeState != null && e != null) {
3923                 treeState.treeStructureChanged(e);
3924 
3925                 updateLeadSelectionRow();
3926 
3927                 TreePath       pPath = SwingUtilities2.getTreePath(e, getModel());
3928 
3929                 if (pPath != null) {
3930                     pPath = pPath.getParentPath();
3931                 }
3932                 if(pPath == null || treeState.isExpanded(pPath))
3933                     updateSize();
3934             }
3935         }
3936     }
3937 
3938 
3939 
3940     private static class Actions extends UIAction {
3941         private static final String SELECT_PREVIOUS = "selectPrevious";
3942         private static final String SELECT_PREVIOUS_CHANGE_LEAD =
3943                              "selectPreviousChangeLead";
3944         private static final String SELECT_PREVIOUS_EXTEND_SELECTION =
3945                              "selectPreviousExtendSelection";
3946         private static final String SELECT_NEXT = "selectNext";
3947         private static final String SELECT_NEXT_CHANGE_LEAD =
3948                                     "selectNextChangeLead";
3949         private static final String SELECT_NEXT_EXTEND_SELECTION =
3950                                     "selectNextExtendSelection";
3951         private static final String SELECT_CHILD = "selectChild";
3952         private static final String SELECT_CHILD_CHANGE_LEAD =
3953                                     "selectChildChangeLead";
3954         private static final String SELECT_PARENT = "selectParent";
3955         private static final String SELECT_PARENT_CHANGE_LEAD =
3956                                     "selectParentChangeLead";
3957         private static final String SCROLL_UP_CHANGE_SELECTION =
3958                                     "scrollUpChangeSelection";
3959         private static final String SCROLL_UP_CHANGE_LEAD =
3960                                     "scrollUpChangeLead";
3961         private static final String SCROLL_UP_EXTEND_SELECTION =
3962                                     "scrollUpExtendSelection";
3963         private static final String SCROLL_DOWN_CHANGE_SELECTION =
3964                                     "scrollDownChangeSelection";
3965         private static final String SCROLL_DOWN_EXTEND_SELECTION =
3966                                     "scrollDownExtendSelection";
3967         private static final String SCROLL_DOWN_CHANGE_LEAD =
3968                                     "scrollDownChangeLead";
3969         private static final String SELECT_FIRST = "selectFirst";
3970         private static final String SELECT_FIRST_CHANGE_LEAD =
3971                                     "selectFirstChangeLead";
3972         private static final String SELECT_FIRST_EXTEND_SELECTION =
3973                                     "selectFirstExtendSelection";
3974         private static final String SELECT_LAST = "selectLast";
3975         private static final String SELECT_LAST_CHANGE_LEAD =
3976                                     "selectLastChangeLead";
3977         private static final String SELECT_LAST_EXTEND_SELECTION =
3978                                     "selectLastExtendSelection";
3979         private static final String TOGGLE = "toggle";
3980         private static final String CANCEL_EDITING = "cancel";
3981         private static final String START_EDITING = "startEditing";
3982         private static final String SELECT_ALL = "selectAll";
3983         private static final String CLEAR_SELECTION = "clearSelection";
3984         private static final String SCROLL_LEFT = "scrollLeft";
3985         private static final String SCROLL_RIGHT = "scrollRight";
3986         private static final String SCROLL_LEFT_EXTEND_SELECTION =
3987                                     "scrollLeftExtendSelection";
3988         private static final String SCROLL_RIGHT_EXTEND_SELECTION =
3989                                     "scrollRightExtendSelection";
3990         private static final String SCROLL_RIGHT_CHANGE_LEAD =
3991                                     "scrollRightChangeLead";
3992         private static final String SCROLL_LEFT_CHANGE_LEAD =
3993                                     "scrollLeftChangeLead";
3994         private static final String EXPAND = "expand";
3995         private static final String COLLAPSE = "collapse";
3996         private static final String MOVE_SELECTION_TO_PARENT =
3997                                     "moveSelectionToParent";
3998 
3999         // add the lead item to the selection without changing lead or anchor
4000         private static final String ADD_TO_SELECTION = "addToSelection";
4001 
4002         // toggle the selected state of the lead item and move the anchor to it
4003         private static final String TOGGLE_AND_ANCHOR = "toggleAndAnchor";
4004 
4005         // extend the selection to the lead item
4006         private static final String EXTEND_TO = "extendTo";
4007 
4008         // move the anchor to the lead and ensure only that item is selected
4009         private static final String MOVE_SELECTION_TO = "moveSelectionTo";
4010 
4011         Actions() {
4012             super(null);
4013         }
4014 
4015         Actions(String key) {
4016             super(key);
4017         }
4018 
4019         public boolean isEnabled(Object o) {
4020             if (o instanceof JTree) {
4021                 if (getName() == CANCEL_EDITING) {
4022                     return ((JTree)o).isEditing();
4023                 }
4024             }
4025             return true;
4026         }
4027 
4028         public void actionPerformed(ActionEvent e) {
4029             JTree tree = (JTree)e.getSource();
4030             BasicTreeUI ui = (BasicTreeUI)BasicLookAndFeel.getUIOfType(
4031                              tree.getUI(), BasicTreeUI.class);
4032             if (ui == null) {
4033                 return;
4034             }
4035             String key = getName();
4036             if (key == SELECT_PREVIOUS) {
4037                 increment(tree, ui, -1, false, true);
4038             }
4039             else if (key == SELECT_PREVIOUS_CHANGE_LEAD) {
4040                 increment(tree, ui, -1, false, false);
4041             }
4042             else if (key == SELECT_PREVIOUS_EXTEND_SELECTION) {
4043                 increment(tree, ui, -1, true, true);
4044             }
4045             else if (key == SELECT_NEXT) {
4046                 increment(tree, ui, 1, false, true);
4047             }
4048             else if (key == SELECT_NEXT_CHANGE_LEAD) {
4049                 increment(tree, ui, 1, false, false);
4050             }
4051             else if (key == SELECT_NEXT_EXTEND_SELECTION) {
4052                 increment(tree, ui, 1, true, true);
4053             }
4054             else if (key == SELECT_CHILD) {
4055                 traverse(tree, ui, 1, true);
4056             }
4057             else if (key == SELECT_CHILD_CHANGE_LEAD) {
4058                 traverse(tree, ui, 1, false);
4059             }
4060             else if (key == SELECT_PARENT) {
4061                 traverse(tree, ui, -1, true);
4062             }
4063             else if (key == SELECT_PARENT_CHANGE_LEAD) {
4064                 traverse(tree, ui, -1, false);
4065             }
4066             else if (key == SCROLL_UP_CHANGE_SELECTION) {
4067                 page(tree, ui, -1, false, true);
4068             }
4069             else if (key == SCROLL_UP_CHANGE_LEAD) {
4070                 page(tree, ui, -1, false, false);
4071             }
4072             else if (key == SCROLL_UP_EXTEND_SELECTION) {
4073                 page(tree, ui, -1, true, true);
4074             }
4075             else if (key == SCROLL_DOWN_CHANGE_SELECTION) {
4076                 page(tree, ui, 1, false, true);
4077             }
4078             else if (key == SCROLL_DOWN_EXTEND_SELECTION) {
4079                 page(tree, ui, 1, true, true);
4080             }
4081             else if (key == SCROLL_DOWN_CHANGE_LEAD) {
4082                 page(tree, ui, 1, false, false);
4083             }
4084             else if (key == SELECT_FIRST) {
4085                 home(tree, ui, -1, false, true);
4086             }
4087             else if (key == SELECT_FIRST_CHANGE_LEAD) {
4088                 home(tree, ui, -1, false, false);
4089             }
4090             else if (key == SELECT_FIRST_EXTEND_SELECTION) {
4091                 home(tree, ui, -1, true, true);
4092             }
4093             else if (key == SELECT_LAST) {
4094                 home(tree, ui, 1, false, true);
4095             }
4096             else if (key == SELECT_LAST_CHANGE_LEAD) {
4097                 home(tree, ui, 1, false, false);
4098             }
4099             else if (key == SELECT_LAST_EXTEND_SELECTION) {
4100                 home(tree, ui, 1, true, true);
4101             }
4102             else if (key == TOGGLE) {
4103                 toggle(tree, ui);
4104             }
4105             else if (key == CANCEL_EDITING) {
4106                 cancelEditing(tree, ui);
4107             }
4108             else if (key == START_EDITING) {
4109                 startEditing(tree, ui);
4110             }
4111             else if (key == SELECT_ALL) {
4112                 selectAll(tree, ui, true);
4113             }
4114             else if (key == CLEAR_SELECTION) {
4115                 selectAll(tree, ui, false);
4116             }
4117             else if (key == ADD_TO_SELECTION) {
4118                 if (ui.getRowCount(tree) > 0) {
4119                     int lead = ui.getLeadSelectionRow();
4120                     if (!tree.isRowSelected(lead)) {
4121                         TreePath aPath = ui.getAnchorSelectionPath();
4122                         tree.addSelectionRow(lead);
4123                         ui.setAnchorSelectionPath(aPath);
4124                     }
4125                 }
4126             }
4127             else if (key == TOGGLE_AND_ANCHOR) {
4128                 if (ui.getRowCount(tree) > 0) {
4129                     int lead = ui.getLeadSelectionRow();
4130                     TreePath lPath = ui.getLeadSelectionPath();
4131                     if (!tree.isRowSelected(lead)) {
4132                         tree.addSelectionRow(lead);
4133                     } else {
4134                         tree.removeSelectionRow(lead);
4135                         ui.setLeadSelectionPath(lPath);
4136                     }
4137                     ui.setAnchorSelectionPath(lPath);
4138                 }
4139             }
4140             else if (key == EXTEND_TO) {
4141                 extendSelection(tree, ui);
4142             }
4143             else if (key == MOVE_SELECTION_TO) {
4144                 if (ui.getRowCount(tree) > 0) {
4145                     int lead = ui.getLeadSelectionRow();
4146                     tree.setSelectionInterval(lead, lead);
4147                 }
4148             }
4149             else if (key == SCROLL_LEFT) {
4150                 scroll(tree, ui, SwingConstants.HORIZONTAL, -10);
4151             }
4152             else if (key == SCROLL_RIGHT) {
4153                 scroll(tree, ui, SwingConstants.HORIZONTAL, 10);
4154             }
4155             else if (key == SCROLL_LEFT_EXTEND_SELECTION) {
4156                 scrollChangeSelection(tree, ui, -1, true, true);
4157             }
4158             else if (key == SCROLL_RIGHT_EXTEND_SELECTION) {
4159                 scrollChangeSelection(tree, ui, 1, true, true);
4160             }
4161             else if (key == SCROLL_RIGHT_CHANGE_LEAD) {
4162                 scrollChangeSelection(tree, ui, 1, false, false);
4163             }
4164             else if (key == SCROLL_LEFT_CHANGE_LEAD) {
4165                 scrollChangeSelection(tree, ui, -1, false, false);
4166             }
4167             else if (key == EXPAND) {
4168                 expand(tree, ui);
4169             }
4170             else if (key == COLLAPSE) {
4171                 collapse(tree, ui);
4172             }
4173             else if (key == MOVE_SELECTION_TO_PARENT) {
4174                 moveSelectionToParent(tree, ui);
4175             }
4176         }
4177 
4178         private void scrollChangeSelection(JTree tree, BasicTreeUI ui,
4179                            int direction, boolean addToSelection,
4180                            boolean changeSelection) {
4181             int           rowCount;
4182 
4183             if((rowCount = ui.getRowCount(tree)) > 0 &&
4184                 ui.treeSelectionModel != null) {
4185                 TreePath          newPath;
4186                 Rectangle         visRect = tree.getVisibleRect();
4187 
4188                 if (direction == -1) {
4189                     newPath = ui.getClosestPathForLocation(tree, visRect.x,
4190                                                         visRect.y);
4191                     visRect.x = Math.max(0, visRect.x - visRect.width);
4192                 }
4193                 else {
4194                     visRect.x = Math.min(Math.max(0, tree.getWidth() -
4195                                    visRect.width), visRect.x + visRect.width);
4196                     newPath = ui.getClosestPathForLocation(tree, visRect.x,
4197                                                  visRect.y + visRect.height);
4198                 }
4199                 // Scroll
4200                 tree.scrollRectToVisible(visRect);
4201                 // select
4202                 if (addToSelection) {
4203                     ui.extendSelection(newPath);
4204                 }
4205                 else if(changeSelection) {
4206                     tree.setSelectionPath(newPath);
4207                 }
4208                 else {
4209                     ui.setLeadSelectionPath(newPath, true);
4210                 }
4211             }
4212         }
4213 
4214         private void scroll(JTree component, BasicTreeUI ui, int direction,
4215                             int amount) {
4216             Rectangle visRect = component.getVisibleRect();
4217             Dimension size = component.getSize();
4218             if (direction == SwingConstants.HORIZONTAL) {
4219                 visRect.x += amount;
4220                 visRect.x = Math.max(0, visRect.x);
4221                 visRect.x = Math.min(Math.max(0, size.width - visRect.width),
4222                                      visRect.x);
4223             }
4224             else {
4225                 visRect.y += amount;
4226                 visRect.y = Math.max(0, visRect.y);
4227                 visRect.y = Math.min(Math.max(0, size.width - visRect.height),
4228                                      visRect.y);
4229             }
4230             component.scrollRectToVisible(visRect);
4231         }
4232 
4233         private void extendSelection(JTree tree, BasicTreeUI ui) {
4234             if (ui.getRowCount(tree) > 0) {
4235                 int       lead = ui.getLeadSelectionRow();
4236 
4237                 if (lead != -1) {
4238                     TreePath      leadP = ui.getLeadSelectionPath();
4239                     TreePath      aPath = ui.getAnchorSelectionPath();
4240                     int           aRow = ui.getRowForPath(tree, aPath);
4241 
4242                     if(aRow == -1)
4243                         aRow = 0;
4244                     tree.setSelectionInterval(aRow, lead);
4245                     ui.setLeadSelectionPath(leadP);
4246                     ui.setAnchorSelectionPath(aPath);
4247                 }
4248             }
4249         }
4250 
4251         private void selectAll(JTree tree, BasicTreeUI ui, boolean selectAll) {
4252             int                   rowCount = ui.getRowCount(tree);
4253 
4254             if(rowCount > 0) {
4255                 if(selectAll) {
4256                     if (tree.getSelectionModel().getSelectionMode() ==
4257                             TreeSelectionModel.SINGLE_TREE_SELECTION) {
4258 
4259                         int lead = ui.getLeadSelectionRow();
4260                         if (lead != -1) {
4261                             tree.setSelectionRow(lead);
4262                         } else if (tree.getMinSelectionRow() == -1) {
4263                             tree.setSelectionRow(0);
4264                             ui.ensureRowsAreVisible(0, 0);
4265                         }
4266                         return;
4267                     }
4268 
4269                     TreePath      lastPath = ui.getLeadSelectionPath();
4270                     TreePath      aPath = ui.getAnchorSelectionPath();
4271 
4272                     if(lastPath != null && !tree.isVisible(lastPath)) {
4273                         lastPath = null;
4274                     }
4275                     tree.setSelectionInterval(0, rowCount - 1);
4276                     if(lastPath != null) {
4277                         ui.setLeadSelectionPath(lastPath);
4278                     }
4279                     if(aPath != null && tree.isVisible(aPath)) {
4280                         ui.setAnchorSelectionPath(aPath);
4281                     }
4282                 }
4283                 else {
4284                     TreePath      lastPath = ui.getLeadSelectionPath();
4285                     TreePath      aPath = ui.getAnchorSelectionPath();
4286 
4287                     tree.clearSelection();
4288                     ui.setAnchorSelectionPath(aPath);
4289                     ui.setLeadSelectionPath(lastPath);
4290                 }
4291             }
4292         }
4293 
4294         private void startEditing(JTree tree, BasicTreeUI ui) {
4295             TreePath   lead = ui.getLeadSelectionPath();
4296             int        editRow = (lead != null) ?
4297                                      ui.getRowForPath(tree, lead) : -1;
4298 
4299             if(editRow != -1) {
4300                 tree.startEditingAtPath(lead);
4301             }
4302         }
4303 
4304         private void cancelEditing(JTree tree, BasicTreeUI ui) {
4305             tree.cancelEditing();
4306         }
4307 
4308         private void toggle(JTree tree, BasicTreeUI ui) {
4309             int            selRow = ui.getLeadSelectionRow();
4310 
4311             if(selRow != -1 && !ui.isLeaf(selRow)) {
4312                 TreePath aPath = ui.getAnchorSelectionPath();
4313                 TreePath lPath = ui.getLeadSelectionPath();
4314 
4315                 ui.toggleExpandState(ui.getPathForRow(tree, selRow));
4316                 ui.setAnchorSelectionPath(aPath);
4317                 ui.setLeadSelectionPath(lPath);
4318             }
4319         }
4320 
4321         private void expand(JTree tree, BasicTreeUI ui) {
4322             int selRow = ui.getLeadSelectionRow();
4323             tree.expandRow(selRow);
4324         }
4325 
4326         private void collapse(JTree tree, BasicTreeUI ui) {
4327             int selRow = ui.getLeadSelectionRow();
4328             tree.collapseRow(selRow);
4329         }
4330 
4331         private void increment(JTree tree, BasicTreeUI ui, int direction,
4332                                boolean addToSelection,
4333                                boolean changeSelection) {
4334 
4335             // disable moving of lead unless in discontiguous mode
4336             if (!addToSelection && !changeSelection &&
4337                     tree.getSelectionModel().getSelectionMode() !=
4338                         TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION) {
4339                 changeSelection = true;
4340             }
4341 
4342             int              rowCount;
4343 
4344             if(ui.treeSelectionModel != null &&
4345                   (rowCount = tree.getRowCount()) > 0) {
4346                 int                  selIndex = ui.getLeadSelectionRow();
4347                 int                  newIndex;
4348 
4349                 if(selIndex == -1) {
4350                     if(direction == 1)
4351                         newIndex = 0;
4352                     else
4353                         newIndex = rowCount - 1;
4354                 }
4355                 else
4356                     /* Aparently people don't like wrapping;( */
4357                     newIndex = Math.min(rowCount - 1, Math.max
4358                                         (0, (selIndex + direction)));
4359                 if(addToSelection && ui.treeSelectionModel.
4360                         getSelectionMode() != TreeSelectionModel.
4361                         SINGLE_TREE_SELECTION) {
4362                     ui.extendSelection(tree.getPathForRow(newIndex));
4363                 }
4364                 else if(changeSelection) {
4365                     tree.setSelectionInterval(newIndex, newIndex);
4366                 }
4367                 else {
4368                     ui.setLeadSelectionPath(tree.getPathForRow(newIndex),true);
4369                 }
4370                 ui.ensureRowsAreVisible(newIndex, newIndex);
4371                 ui.lastSelectedRow = newIndex;
4372             }
4373         }
4374 
4375         private void traverse(JTree tree, BasicTreeUI ui, int direction,
4376                               boolean changeSelection) {
4377 
4378             // disable moving of lead unless in discontiguous mode
4379             if (!changeSelection &&
4380                     tree.getSelectionModel().getSelectionMode() !=
4381                         TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION) {
4382                 changeSelection = true;
4383             }
4384 
4385             int                rowCount;
4386 
4387             if((rowCount = tree.getRowCount()) > 0) {
4388                 int               minSelIndex = ui.getLeadSelectionRow();
4389                 int               newIndex;
4390 
4391                 if(minSelIndex == -1)
4392                     newIndex = 0;
4393                 else {
4394                     /* Try and expand the node, otherwise go to next
4395                        node. */
4396                     if(direction == 1) {
4397                         TreePath minSelPath = ui.getPathForRow(tree, minSelIndex);
4398                         int childCount = tree.getModel().
4399                             getChildCount(minSelPath.getLastPathComponent());
4400                         newIndex = -1;
4401                         if (!ui.isLeaf(minSelIndex)) {
4402                             if (!tree.isExpanded(minSelIndex)) {
4403                                 ui.toggleExpandState(minSelPath);
4404                             }
4405                             else if (childCount > 0) {
4406                                 newIndex = Math.min(minSelIndex + 1, rowCount - 1);
4407                             }
4408                         }
4409                     }
4410                     /* Try to collapse node. */
4411                     else {
4412                         if(!ui.isLeaf(minSelIndex) &&
4413                            tree.isExpanded(minSelIndex)) {
4414                             ui.toggleExpandState(ui.getPathForRow
4415                                               (tree, minSelIndex));
4416                             newIndex = -1;
4417                         }
4418                         else {
4419                             TreePath         path = ui.getPathForRow(tree,
4420                                                                   minSelIndex);
4421 
4422                             if(path != null && path.getPathCount() > 1) {
4423                                 newIndex = ui.getRowForPath(tree, path.
4424                                                          getParentPath());
4425                             }
4426                             else
4427                                 newIndex = -1;
4428                         }
4429                     }
4430                 }
4431                 if(newIndex != -1) {
4432                     if(changeSelection) {
4433                         tree.setSelectionInterval(newIndex, newIndex);
4434                     }
4435                     else {
4436                         ui.setLeadSelectionPath(ui.getPathForRow(
4437                                                     tree, newIndex), true);
4438                     }
4439                     ui.ensureRowsAreVisible(newIndex, newIndex);
4440                 }
4441             }
4442         }
4443 
4444         private void moveSelectionToParent(JTree tree, BasicTreeUI ui) {
4445             int selRow = ui.getLeadSelectionRow();
4446             TreePath path = ui.getPathForRow(tree, selRow);
4447             if (path != null && path.getPathCount() > 1) {
4448                 int  newIndex = ui.getRowForPath(tree, path.getParentPath());
4449                 if (newIndex != -1) {
4450                     tree.setSelectionInterval(newIndex, newIndex);
4451                     ui.ensureRowsAreVisible(newIndex, newIndex);
4452                 }
4453             }
4454         }
4455 
4456         private void page(JTree tree, BasicTreeUI ui, int direction,
4457                           boolean addToSelection, boolean changeSelection) {
4458 
4459             // disable moving of lead unless in discontiguous mode
4460             if (!addToSelection && !changeSelection &&
4461                     tree.getSelectionModel().getSelectionMode() !=
4462                         TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION) {
4463                 changeSelection = true;
4464             }
4465 
4466             int           rowCount;
4467 
4468             if((rowCount = ui.getRowCount(tree)) > 0 &&
4469                            ui.treeSelectionModel != null) {
4470                 Dimension         maxSize = tree.getSize();
4471                 TreePath          lead = ui.getLeadSelectionPath();
4472                 TreePath          newPath;
4473                 Rectangle         visRect = tree.getVisibleRect();
4474 
4475                 if(direction == -1) {
4476                     // up.
4477                     newPath = ui.getClosestPathForLocation(tree, visRect.x,
4478                                                          visRect.y);
4479                     if(newPath.equals(lead)) {
4480                         visRect.y = Math.max(0, visRect.y - visRect.height);
4481                         newPath = tree.getClosestPathForLocation(visRect.x,
4482                                                                  visRect.y);
4483                     }
4484                 }
4485                 else {
4486                     // down
4487                     visRect.y = Math.min(maxSize.height, visRect.y +
4488                                          visRect.height - 1);
4489                     newPath = tree.getClosestPathForLocation(visRect.x,
4490                                                              visRect.y);
4491                     if(newPath.equals(lead)) {
4492                         visRect.y = Math.min(maxSize.height, visRect.y +
4493                                              visRect.height - 1);
4494                         newPath = tree.getClosestPathForLocation(visRect.x,
4495                                                                  visRect.y);
4496                     }
4497                 }
4498                 Rectangle            newRect = ui.getPathBounds(tree, newPath);
4499                 if (newRect != null) {
4500                     newRect.x = visRect.x;
4501                     newRect.width = visRect.width;
4502                     if(direction == -1) {
4503                         newRect.height = visRect.height;
4504                     }
4505                     else {
4506                         newRect.y -= (visRect.height - newRect.height);
4507                         newRect.height = visRect.height;
4508                     }
4509 
4510                     if(addToSelection) {
4511                         ui.extendSelection(newPath);
4512                     }
4513                     else if(changeSelection) {
4514                         tree.setSelectionPath(newPath);
4515                     }
4516                     else {
4517                         ui.setLeadSelectionPath(newPath, true);
4518                     }
4519                     tree.scrollRectToVisible(newRect);
4520                 }
4521             }
4522         }
4523 
4524         private void home(JTree tree, final BasicTreeUI ui, int direction,
4525                           boolean addToSelection, boolean changeSelection) {
4526 
4527             // disable moving of lead unless in discontiguous mode
4528             if (!addToSelection && !changeSelection &&
4529                     tree.getSelectionModel().getSelectionMode() !=
4530                         TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION) {
4531                 changeSelection = true;
4532             }
4533 
4534             final int rowCount = ui.getRowCount(tree);
4535 
4536             if (rowCount > 0) {
4537                 if(direction == -1) {
4538                     ui.ensureRowsAreVisible(0, 0);
4539                     if (addToSelection) {
4540                         TreePath        aPath = ui.getAnchorSelectionPath();
4541                         int             aRow = (aPath == null) ? -1 :
4542                                         ui.getRowForPath(tree, aPath);
4543 
4544                         if (aRow == -1) {
4545                             tree.setSelectionInterval(0, 0);
4546                         }
4547                         else {
4548                             tree.setSelectionInterval(0, aRow);
4549                             ui.setAnchorSelectionPath(aPath);
4550                             ui.setLeadSelectionPath(ui.getPathForRow(tree, 0));
4551                         }
4552                     }
4553                     else if(changeSelection) {
4554                         tree.setSelectionInterval(0, 0);
4555                     }
4556                     else {
4557                         ui.setLeadSelectionPath(ui.getPathForRow(tree, 0),
4558                                                 true);
4559                     }
4560                 }
4561                 else {
4562                     ui.ensureRowsAreVisible(rowCount - 1, rowCount - 1);
4563                     if (addToSelection) {
4564                         TreePath        aPath = ui.getAnchorSelectionPath();
4565                         int             aRow = (aPath == null) ? -1 :
4566                                         ui.getRowForPath(tree, aPath);
4567 
4568                         if (aRow == -1) {
4569                             tree.setSelectionInterval(rowCount - 1,
4570                                                       rowCount -1);
4571                         }
4572                         else {
4573                             tree.setSelectionInterval(aRow, rowCount - 1);
4574                             ui.setAnchorSelectionPath(aPath);
4575                             ui.setLeadSelectionPath(ui.getPathForRow(tree,
4576                                                                rowCount -1));
4577                         }
4578                     }
4579                     else if(changeSelection) {
4580                         tree.setSelectionInterval(rowCount - 1, rowCount - 1);
4581                     }
4582                     else {
4583                         ui.setLeadSelectionPath(ui.getPathForRow(tree,
4584                                                           rowCount - 1), true);
4585                     }
4586                     if (ui.isLargeModel()){
4587                         SwingUtilities.invokeLater(new Runnable() {
4588                             public void run() {
4589                                 ui.ensureRowsAreVisible(rowCount - 1, rowCount - 1);
4590                             }
4591                         });
4592                     }
4593                 }
4594             }
4595         }
4596     }
4597 } // End of class BasicTreeUI