Source for javax.swing.JTree

   1: /* JTree.java 
   2:    Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
   3:  
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package javax.swing;
  39: 
  40: import java.awt.Color;
  41: import java.awt.Cursor;
  42: import java.awt.Dimension;
  43: import java.awt.Font;
  44: import java.awt.FontMetrics;
  45: import java.awt.Point;
  46: import java.awt.Rectangle;
  47: import java.awt.event.FocusListener;
  48: import java.beans.PropertyChangeListener;
  49: import java.io.Serializable;
  50: import java.util.Enumeration;
  51: import java.util.Hashtable;
  52: import java.util.Iterator;
  53: import java.util.Locale;
  54: import java.util.Vector;
  55: 
  56: import javax.accessibility.Accessible;
  57: import javax.accessibility.AccessibleAction;
  58: import javax.accessibility.AccessibleComponent;
  59: import javax.accessibility.AccessibleContext;
  60: import javax.accessibility.AccessibleRole;
  61: import javax.accessibility.AccessibleSelection;
  62: import javax.accessibility.AccessibleState;
  63: import javax.accessibility.AccessibleStateSet;
  64: import javax.accessibility.AccessibleText;
  65: import javax.accessibility.AccessibleValue;
  66: import javax.swing.event.TreeExpansionEvent;
  67: import javax.swing.event.TreeExpansionListener;
  68: import javax.swing.event.TreeModelEvent;
  69: import javax.swing.event.TreeModelListener;
  70: import javax.swing.event.TreeSelectionEvent;
  71: import javax.swing.event.TreeSelectionListener;
  72: import javax.swing.event.TreeWillExpandListener;
  73: import javax.swing.plaf.TreeUI;
  74: import javax.swing.text.Position;
  75: import javax.swing.tree.DefaultMutableTreeNode;
  76: import javax.swing.tree.DefaultTreeModel;
  77: import javax.swing.tree.DefaultTreeSelectionModel;
  78: import javax.swing.tree.ExpandVetoException;
  79: import javax.swing.tree.TreeCellEditor;
  80: import javax.swing.tree.TreeCellRenderer;
  81: import javax.swing.tree.TreeModel;
  82: import javax.swing.tree.TreeNode;
  83: import javax.swing.tree.TreePath;
  84: import javax.swing.tree.TreeSelectionModel;
  85: 
  86: public class JTree extends JComponent implements Scrollable, Accessible
  87: {
  88: 
  89:   /**
  90:    * This class implements accessibility support for the JTree class. It 
  91:    * provides an implementation of the Java Accessibility API appropriate 
  92:    * to tree user-interface elements.
  93:    */
  94:   protected class AccessibleJTree extends JComponent.AccessibleJComponent
  95:       implements AccessibleSelection, TreeSelectionListener, TreeModelListener,
  96:       TreeExpansionListener
  97:   {
  98:     
  99:     /**
 100:      * This class implements accessibility support for the JTree child. It provides 
 101:      * an implementation of the Java Accessibility API appropriate to tree nodes.
 102:      */
 103:     protected class AccessibleJTreeNode extends AccessibleContext 
 104:        implements Accessible, AccessibleComponent, AccessibleSelection, 
 105:        AccessibleAction
 106:     {
 107:       
 108:       private JTree tree;
 109:       private TreePath tp;
 110:       private Accessible acc;
 111:       private AccessibleStateSet states;
 112:       private Vector selectionList;
 113:       private Vector actionList;
 114:       private TreeModel mod;
 115:       private Cursor cursor;
 116:       
 117:       /**
 118:        * Constructs an AccessibleJTreeNode
 119:        * 
 120:        * @param t - the current tree
 121:        * @param p - the current path to be dealt with
 122:        * @param ap - the accessible object to use
 123:        */
 124:       public AccessibleJTreeNode(JTree t, TreePath p, Accessible ap)
 125:       {
 126:         states = new AccessibleStateSet();
 127:         selectionList = new Vector();
 128:         actionList = new Vector();
 129:         mod = tree.getModel();
 130:         cursor = JTree.this.getCursor();
 131:                 
 132:         tree = t;
 133:         tp = p;
 134:         acc = ap;
 135:         
 136:         // Add all the children of this path that may already be
 137:         // selected to the selection list.
 138:         TreePath[] selected = tree.getSelectionPaths();
 139:         for (int i = 0; i < selected.length; i++)
 140:           {
 141:             TreePath sel = selected[i];
 142:             if ((sel.getParentPath()).equals(tp))
 143:               selectionList.add(sel);
 144:           }
 145:         
 146:         // Add all the actions available for a node to 
 147:         // the action list.
 148:         actionList.add("EXPAND");
 149:         actionList.add("COLLAPSE");
 150:         actionList.add("EDIT");
 151:         actionList.add("SELECT");
 152:         actionList.add("DESELECT");
 153:       }      
 154:       
 155:       /**
 156:        * Adds the specified selected item in the object to the object's
 157:        * selection.
 158:        * 
 159:        * @param i - the i-th child of this node.
 160:        */
 161:       public void addAccessibleSelection(int i)
 162:       {
 163:         if (mod != null)
 164:           {
 165:             Object child = mod.getChild(tp.getLastPathComponent(), i);
 166:             if (child != null)
 167:               {
 168:                 if (!states.contains(AccessibleState.MULTISELECTABLE))
 169:                   clearAccessibleSelection();
 170:                 selectionList.add(child);                  
 171:                 tree.addSelectionPath(tp.pathByAddingChild(child));
 172:               }
 173:           }
 174:       }
 175:       
 176:       /**
 177:        * Adds the specified focus listener to receive focus events 
 178:        * from this component.
 179:        * 
 180:        * @param l - the new focus listener
 181:        */
 182:       public void addFocusListener(FocusListener l)
 183:       {
 184:         tree.addFocusListener(l);
 185:       }
 186:       
 187:       /**
 188:        * Add a PropertyChangeListener to the listener list.
 189:        * 
 190:        * @param l - the new property change listener
 191:        */
 192:       public void addPropertyChangeListener(PropertyChangeListener l)
 193:       {
 194:         // Nothing to do here.
 195:       }
 196:       
 197:       /**
 198:        * Clears the selection in the object, so that nothing in the 
 199:        * object is selected.
 200:        */
 201:       public void clearAccessibleSelection()
 202:       {
 203:         selectionList.clear();
 204:       }
 205:       
 206:       /**
 207:        * Checks whether the specified point is within this object's 
 208:        * bounds, where the point's x and y coordinates are defined to be 
 209:        * relative to the coordinate system of the object. 
 210:        * 
 211:        * @param p - the point to check
 212:        * @return true if p is in the bounds
 213:        */
 214:       public boolean contains(Point p)
 215:       {
 216:         return getBounds().contains(p);
 217:       }
 218:       
 219:       /**
 220:        * Perform the specified Action on the tree node.
 221:        * 
 222:        * @param i - the i-th action to perform
 223:        * @return true if the the action was performed; else false.
 224:        */
 225:       public boolean doAccessibleAction(int i)
 226:       {
 227:         if (i >= actionList.size() || i < 0)
 228:           return false;
 229:         
 230:         if (actionList.get(i).equals("EXPAND"))
 231:           tree.expandPath(tp);
 232:         else if (actionList.get(i).equals("COLLAPSE"))
 233:           tree.collapsePath(tp);
 234:         else if (actionList.get(i).equals("SELECT"))
 235:           tree.addSelectionPath(tp);
 236:         else if (actionList.get(i).equals("DESELECT"))
 237:           tree.removeSelectionPath(tp);
 238:         else if (actionList.get(i).equals("EDIT"))
 239:           tree.startEditingAtPath(tp);
 240:         else
 241:           return false;
 242:         return true;
 243:       }
 244:       
 245:       /**
 246:        * Get the AccessibleAction associated with this object.
 247:        * 
 248:        * @return the action
 249:        */
 250:       public AccessibleAction getAccessibleAction()
 251:       {
 252:         return this;
 253:       }
 254:       
 255:       /**
 256:        * Returns the number of accessible actions available in this tree node.
 257:        * 
 258:        * @return the number of actions
 259:        */
 260:       public int getAccessibleActionCount()
 261:       {
 262:         return actionList.size();
 263:       }
 264:       
 265:       /**
 266:        * Return a description of the specified action of the tree node.
 267:        * 
 268:        * @param i - the i-th action's description
 269:        * @return a description of the action
 270:        */
 271:       public String getAccessibleActionDescription(int i)
 272:       {
 273:         if (i < 0 || i >= actionList.size())
 274:           return (actionList.get(i)).toString();
 275:         return super.getAccessibleDescription();
 276:       }
 277:       
 278:       /**
 279:        * Returns the Accessible child, if one exists, contained at the 
 280:        * local coordinate Point.
 281:        * 
 282:        * @param p - the point of the accessible
 283:        * @return the accessible at point p if it exists
 284:        */
 285:       public Accessible getAccessibleAt(Point p)
 286:       {
 287:         TreePath acc = tree.getClosestPathForLocation(p.x, p.y);
 288:         if (acc != null)
 289:           return new AccessibleJTreeNode(tree, acc, this);
 290:         return null;
 291:       }
 292:       
 293:       /**
 294:        * Return the specified Accessible child of the object.
 295:        * 
 296:        * @param i - the i-th child of the current path
 297:        * @return the child if it exists
 298:        */
 299:       public Accessible getAccessibleChild(int i)
 300:       {
 301:         if (mod != null)
 302:           {
 303:             Object child = mod.getChild(tp.getLastPathComponent(), i);
 304:             if (child != null)
 305:               return new AccessibleJTreeNode(tree, tp.pathByAddingChild(child),
 306:                                              acc);
 307:           }
 308:         return null;
 309:       }
 310:       
 311:       /**
 312:        * Returns the number of accessible children in the object.
 313:        * 
 314:        * @return the number of children the current node has
 315:        */
 316:       public int getAccessibleChildrenCount()
 317:       {
 318:         TreeModel mod = getModel();
 319:         if (mod != null)
 320:           return mod.getChildCount(tp.getLastPathComponent());
 321:         return 0;
 322:       }
 323:       
 324:       /**
 325:        * Get the AccessibleComponent associated with this object.
 326:        * 
 327:        * @return the accessible component if it is supported.
 328:        */
 329:       public AccessibleComponent getAccessibleComponent()
 330:       {
 331:         return this;
 332:       }
 333:       
 334:       /**
 335:        * Get the AccessibleContext associated with this tree node.
 336:        * 
 337:        * @return an instance of this class
 338:        */
 339:       public AccessibleContext getAccessibleContext()
 340:       {
 341:         return this;
 342:       }
 343:       
 344:       /**
 345:        * Get the accessible description of this object.
 346:        * 
 347:        * @return the accessible description
 348:        */
 349:       public String getAccessibleDescription()
 350:       {
 351:         return super.getAccessibleDescription();
 352:       }
 353:       
 354:       /**
 355:        * Get the index of this object in its accessible parent.
 356:        * 
 357:        * @return the index of this in the parent.
 358:        */
 359:       public int getAccessibleIndexInParent()
 360:       {
 361:         AccessibleContext parent = getAccessibleParent().getAccessibleContext();
 362:         if (parent != null)
 363:           for (int i = 0; i < parent.getAccessibleChildrenCount(); i++)
 364:             {
 365:               if ((parent.getAccessibleChild(i)).equals(this))
 366:                 return i;
 367:             }
 368:         return -1;
 369:       }
 370:       
 371:       /**
 372:        * Get the accessible name of this object.
 373:        * 
 374:        * @return the accessible name
 375:        */
 376:       public String getAccessibleName()
 377:       {
 378:         return super.getAccessibleName();
 379:       }
 380:       
 381:       /**
 382:        * Get the Accessible parent of this object.
 383:        * 
 384:        * @return the accessible parent if it exists.
 385:        */
 386:       public Accessible getAccessibleParent()
 387:       {
 388:         return super.getAccessibleParent();
 389:       }
 390:       
 391:       /**
 392:        * Get the role of this object.
 393:        * 
 394:        * @return the accessible role
 395:        */
 396:       public AccessibleRole getAccessibleRole()
 397:       {
 398:         return AccessibleJTree.this.getAccessibleRole();
 399:       }
 400:       
 401:       /**
 402:        * Get the AccessibleSelection associated with this object if one exists.
 403:        * 
 404:        * @return the accessible selection for this.
 405:        */
 406:       public AccessibleSelection getAccessibleSelection()
 407:       {
 408:         return this;
 409:       }
 410:       
 411:       /**
 412:        * Returns an Accessible representing the specified selected item 
 413:        * in the object.
 414:        * 
 415:        * @return the accessible representing a certain selected item.
 416:        */
 417:       public Accessible getAccessibleSelection(int i)
 418:       {
 419:         if (i > 0 && i < getAccessibleSelectionCount())
 420:             return new AccessibleJTreeNode(tree, 
 421:                   tp.pathByAddingChild(selectionList.get(i)), acc);
 422:         return null;
 423:       }
 424:       
 425:       /**
 426:        * Returns the number of items currently selected.
 427:        * 
 428:        * @return the number of items selected.
 429:        */
 430:       public int getAccessibleSelectionCount()
 431:       {
 432:         return selectionList.size();
 433:       }
 434:       
 435:       /**
 436:        * Get the state set of this object.
 437:        * 
 438:        * @return the state set for this object
 439:        */
 440:       public AccessibleStateSet getAccessibleStateSet()
 441:       {
 442:         if (isVisible())
 443:           states.add(AccessibleState.VISIBLE);
 444:         if (tree.isCollapsed(tp))
 445:           states.add(AccessibleState.COLLAPSED);
 446:         if (tree.isEditable())
 447:           states.add(AccessibleState.EDITABLE);
 448:         if (mod != null && 
 449:             !mod.isLeaf(tp.getLastPathComponent()))
 450:           states.add(AccessibleState.EXPANDABLE);
 451:         if (tree.isExpanded(tp))
 452:           states.add(AccessibleState.EXPANDED);
 453:         if (isFocusable())
 454:           states.add(AccessibleState.FOCUSABLE);
 455:         if (hasFocus())
 456:           states.add(AccessibleState.FOCUSED);
 457:         if (tree.getSelectionModel().getSelectionMode() != 
 458:           TreeSelectionModel.SINGLE_TREE_SELECTION)
 459:           states.add(AccessibleState.MULTISELECTABLE);
 460:         if (tree.isOpaque())
 461:           states.add(AccessibleState.OPAQUE);
 462:         if (tree.isPathSelected(tp))
 463:           states.add(AccessibleState.SELECTED);
 464:         if (isShowing())
 465:           states.add(AccessibleState.SHOWING);
 466: 
 467:         states.add(AccessibleState.SELECTABLE);
 468:         return states;
 469:       }
 470:       
 471:       /**
 472:        * Get the AccessibleText associated with this object if one exists.
 473:        * 
 474:        * @return the accessible text
 475:        */
 476:       public AccessibleText getAccessibleText()
 477:       {
 478:         return super.getAccessibleText();
 479:       }
 480:       
 481:       /**
 482:        * Get the AccessibleValue associated with this object if one exists.
 483:        * 
 484:        * @return the accessible value if it exists
 485:        */
 486:       public AccessibleValue getAccessibleValue()
 487:       {
 488:         return super.getAccessibleValue();
 489:       }
 490:       
 491:       /**
 492:        * Get the background color of this object.
 493:        * 
 494:        * @return the color of the background.
 495:        */
 496:       public Color getBackground()
 497:       {
 498:         return tree.getBackground();
 499:       }
 500:       
 501:       /**
 502:        * Gets the bounds of this object in the form of a Rectangle object.
 503:        * 
 504:        * @return the bounds of the current node.
 505:        */
 506:       public Rectangle getBounds()
 507:       {
 508:         return tree.getPathBounds(tp);
 509:       }
 510:       
 511:       /**
 512:        * Gets the Cursor of this object.
 513:        * 
 514:        * @return the cursor for the current node
 515:        */
 516:       public Cursor getCursor()
 517:       {
 518:         return cursor;
 519:       }
 520:       
 521:       /**
 522:        * Gets the Font of this object.
 523:        * 
 524:        * @return the font for the current node
 525:        */
 526:       public Font getFont()
 527:       {
 528:         return tree.getFont();
 529:       }
 530:       
 531:       /**
 532:        * Gets the FontMetrics of this object.
 533:        * 
 534:        * @param f - the current font.
 535:        * @return the font metrics for the given font.
 536:        */
 537:       public FontMetrics getFontMetrics(Font f)
 538:       {
 539:         return tree.getFontMetrics(f);
 540:       }
 541:       
 542:       /**
 543:        * Get the foreground color of this object.
 544:        * 
 545:        * @return the foreground for this object.
 546:        */
 547:       public Color getForeground()
 548:       {
 549:         return tree.getForeground();
 550:       }
 551:       
 552:       /**
 553:        * Gets the locale of the component.
 554:        * 
 555:        * @return the locale of the component.
 556:        */
 557:       public Locale getLocale()
 558:       {
 559:         return tree.getLocale();
 560:       }
 561:       
 562:       /**
 563:        * Gets the location of the object relative to the 
 564:        * parent in the form of a point specifying the object's 
 565:        * top-left corner in the screen's coordinate space. 
 566:        * 
 567:        * @return the location of the current node.
 568:        */
 569:       public Point getLocation()
 570:       {
 571:         return getLocationInJTree();
 572:       }
 573:       
 574:       /**
 575:        * Returns the location in the tree.
 576:        * 
 577:        * @return the location in the JTree.
 578:        */
 579:       protected Point getLocationInJTree()
 580:       {
 581:         Rectangle bounds = tree.getPathBounds(tp);
 582:         return new Point(bounds.x, bounds.y);
 583:       }
 584:       
 585:       /**
 586:        * Returns the location of the object on the screen.
 587:        * 
 588:        * @return the location of the object on the screen.
 589:        */
 590:       public Point getLocationOnScreen()
 591:       {
 592:         Point loc = getLocation();
 593:         SwingUtilities.convertPointToScreen(loc, tree);
 594:         return loc;
 595:       }
 596:       
 597:       /**
 598:        * Returns the size of this object in the form of a Dimension object.
 599:        * 
 600:        * @return the size of the object
 601:        */
 602:       public Dimension getSize()
 603:       {
 604:         Rectangle b = getBounds();
 605:         return b.getSize();
 606:       }
 607:       
 608:       /**
 609:        * Returns true if the current child of this object is selected.
 610:        * 
 611:        * @param i - the child of the current node
 612:        * @return true if the child is selected.
 613:        */
 614:       public boolean isAccessibleChildSelected(int i)
 615:       {
 616:         Object child = mod.getChild(tp.getLastPathComponent(), i);
 617:         if (child != null)
 618:           return tree.isPathSelected(tp.pathByAddingChild(child));
 619:         return false;
 620:       }
 621:       
 622:       /**
 623:        * Determines if the object is enabled.
 624:        * 
 625:        * @return true if the tree is enabled
 626:        */
 627:       public boolean isEnabled()
 628:       {
 629:         return tree.isEnabled();
 630:       }
 631:       
 632:       /**
 633:        * Returns whether this object can accept focus or not.
 634:        * 
 635:        * @return true, it is always focus traversable
 636:        */
 637:       public boolean isFocusTraversable()
 638:       {
 639:         return true;
 640:       }
 641:       
 642:       /**
 643:        * Determines if the object is showing.
 644:        * 
 645:        * @return true if the object is visible and the
 646:        * parent is visible.
 647:        */
 648:       public boolean isShowing()
 649:       {
 650:         return isVisible() && tree.isShowing();
 651:       }
 652:       
 653:       /**
 654:        * Determines if the object is visible.
 655:        * 
 656:        * @return true if the object is visible.
 657:        */
 658:       public boolean isVisible()
 659:       {
 660:         return tree.isVisible(tp);
 661:       }
 662:       
 663:       /**
 664:        * Removes the specified selected item in the object from the
 665:        * object's selection.
 666:        * 
 667:        * @param i - the specified item to remove
 668:        */
 669:       public void removeAccessibleSelection(int i)
 670:       {
 671:         if (mod != null)
 672:           {
 673:             Object child = mod.getChild(tp.getLastPathComponent(), i);
 674:             if (child != null)
 675:               {
 676:                 if (!states.contains(AccessibleState.MULTISELECTABLE))
 677:                   clearAccessibleSelection();
 678:                 if (selectionList.contains(child))
 679:                   {
 680:                     selectionList.remove(child);                  
 681:                     tree.removeSelectionPath(tp.pathByAddingChild(child));
 682:                   }
 683:               }
 684:           }
 685:       }
 686:       
 687:       /**
 688:        * Removes the specified focus listener so it no longer receives focus 
 689:        * events from this component.
 690:        * 
 691:        * @param l - the focus listener to remove
 692:        */
 693:       public void removeFocusListener(FocusListener l)
 694:       {
 695:         tree.removeFocusListener(l);
 696:       }
 697:       
 698:       /**
 699:        * Remove a PropertyChangeListener from the listener list.
 700:        * 
 701:        * @param l - the property change listener to remove.
 702:        */
 703:       public void removePropertyChangeListener(PropertyChangeListener l)
 704:       {
 705:         // Nothing to do here.
 706:       }
 707:       
 708:       /**
 709:        * Requests focus for this object.
 710:        */
 711:       public void requestFocus()
 712:       {
 713:         tree.requestFocus();
 714:       }
 715:       
 716:       /**
 717:        * Causes every selected item in the object to be selected if the object 
 718:        * supports multiple selections.
 719:        */
 720:       public void selectAllAccessibleSelection()
 721:       {
 722:         Object parent = tp.getLastPathComponent();
 723:         if (mod != null)
 724:           {
 725:             for (int i = 0; i < mod.getChildCount(parent); i++)
 726:               {
 727:                 Object child = mod.getChild(parent, i);
 728:                 if (child != null)
 729:                   {
 730:                     if (!states.contains(AccessibleState.MULTISELECTABLE))
 731:                       clearAccessibleSelection();
 732:                     if (selectionList.contains(child))
 733:                       {
 734:                         selectionList.add(child);
 735:                         tree.addSelectionPath(tp.pathByAddingChild(child));
 736:                       }
 737:                   }
 738:               }
 739:           }
 740:       }
 741:       
 742:       /**
 743:        * Set the accessible description of this object.
 744:        * 
 745:        * @param s - the string to set the accessible description to.
 746:        */
 747:       public void setAccessibleDescription(String s)
 748:       {
 749:         super.setAccessibleDescription(s);
 750:       }
 751:       
 752:       /**
 753:        * Set the localized accessible name of this object.
 754:        * 
 755:        * @param s - the string to set the accessible name to.
 756:        */
 757:       public void setAccessibleName(String s)
 758:       {
 759:         super.setAccessibleName(s);
 760:       }
 761:       
 762:       /**
 763:        * Set the background color of this object.
 764:        * 
 765:        * @param c - the color to set the background to.
 766:        */
 767:       public void setBackground(Color c)
 768:       {
 769:         // Nothing to do here.
 770:       }
 771:       
 772:       /**
 773:        * Sets the bounds of this object in the form of a Rectangle object.
 774:        * 
 775:        * @param r - the bounds to set the object o
 776:        */
 777:       public void setBounds(Rectangle r)
 778:       {
 779:         // Nothing to do here.
 780:       }
 781:       
 782:       /**
 783:        * Sets the Cursor of this object.
 784:        * 
 785:        * @param c - the new cursor
 786:        */
 787:       public void setCursor(Cursor c)
 788:       {
 789:         cursor = c;
 790:       }
 791:       
 792:       /**
 793:        * Sets the enabled state of the object.
 794:        * 
 795:        * @param b - boolean to enable or disable object
 796:        */
 797:       public void setEnabled(boolean b)
 798:       {
 799:          // Nothing to do here.
 800:       }
 801:       
 802:       /**
 803:        * Sets the Font of this object.
 804:        * 
 805:        * @param f - the new font.
 806:        */
 807:       public void setFont(Font f)
 808:       {
 809:          // Nothing to do here.
 810:       }
 811:       
 812:       /**
 813:        * Sets the foreground color of this object.
 814:        * 
 815:        * @param c - the new foreground color.
 816:        */
 817:       public void setForeground(Color c)
 818:       {
 819:         // Nothing to do here.
 820:       }
 821:       
 822:       /**
 823:        * Sets the location of the object relative to the parent.
 824:        * 
 825:        * @param p - the new location for the object.
 826:        */
 827:       public void setLocation(Point p)
 828:       {
 829:         // Nothing to do here.
 830:       }
 831:       
 832:       /**
 833:        * Resizes this object so that it has width and height.
 834:        * 
 835:        * @param d - the new size for the object.
 836:        */
 837:       public void setSize(Dimension d)
 838:       {
 839:         // Nothing to do here.
 840:       }
 841:       
 842:       /**
 843:        * Sets the visible state of the object.
 844:        * 
 845:        * @param b - sets the objects visibility.
 846:        */
 847:       public void setVisible(boolean b)
 848:       {
 849:         // Nothing to do here.
 850:       }
 851:     }
 852:     
 853:     /**
 854:      * Constructor
 855:      */
 856:     public AccessibleJTree()
 857:     {
 858:       // Nothing to do here.
 859:     }
 860:     
 861:     /**
 862:      * Adds the specified selected item in the object to the object's selection.
 863:      * 
 864:      * @param i - the row to add to the tree's selection
 865:      */
 866:     public void addAccessibleSelection(int i)
 867:     {
 868:       addSelectionInterval(i, i);
 869:     }
 870:     
 871:     /**
 872:      * Clears the selection in the object, so that nothing in the object is selected.
 873:      */
 874:     public void clearAccessibleSelection()
 875:     {
 876:       clearSelection();
 877:     }
 878:     
 879:     /**
 880:      * Fire a visible data property change notification.
 881:      */
 882:     public void fireVisibleDataPropertyChange()
 883:     {
 884:       treeDidChange();
 885:     }
 886:     
 887:     /**
 888:      * Returns the Accessible child, if one exists, contained at the local 
 889:      * coordinate Point.
 890:      * 
 891:      * @param p - the point of the accessible to get.
 892:      * @return the accessible at point p.
 893:      */
 894:     public Accessible getAccessibleAt(Point p)
 895:     {
 896:       TreePath tp = getClosestPathForLocation(p.x, p.y);
 897:       if (tp != null)
 898:         return new AccessibleJTreeNode(JTree.this, tp, null);
 899:       return null;
 900:     }
 901:     
 902:     /**
 903:      * Return the nth Accessible child of the object.
 904:      * 
 905:      * @param i - the accessible child to get
 906:      * @return the i-th child
 907:      */
 908:     public Accessible getAccessibleChild(int i)
 909:     {
 910:       return null;
 911:     }
 912:     
 913:     /**
 914:      * Returns the number of top-level children nodes of this JTree.
 915:      * 
 916:      * @return the number of top-level children
 917:      */
 918:     public int getAccessibleChildrenCount()
 919:     {
 920:       TreeModel model = getModel();
 921:       if (model != null)
 922:         return model.getChildCount(model.getRoot());
 923:       return 0;
 924:     }
 925:     
 926:     /**
 927:      * Get the index of this object in its accessible parent.
 928:      * 
 929:      * @return the index of this object.
 930:      */
 931:     public int getAccessibleIndexInParent()
 932:     {
 933:       return 0;
 934:     }
 935:     
 936:     /**
 937:      * Get the role of this object.
 938:      * 
 939:      * @return the role of this object
 940:      */
 941:     public AccessibleRole getAccessibleRole()
 942:     {
 943:       return AccessibleRole.TREE;
 944:     }
 945:     
 946:     /**
 947:      * Get the AccessibleSelection associated with this object.
 948:      * 
 949:      * @return the accessible selection of the tree
 950:      */
 951:     public AccessibleSelection getAccessibleSelection()
 952:     {
 953:       TreeModel mod = getModel();
 954:       if (mod != null)
 955:         return (new AccessibleJTreeNode(JTree.this, 
 956:                   new TreePath(mod.getRoot()), null)).getAccessibleSelection();
 957:       return null;
 958:     }
 959:     
 960:     /**
 961:      * Returns an Accessible representing the specified selected item in the object.
 962:      * 
 963:      * @return the i-th accessible in the selection
 964:      */
 965:     public Accessible getAccessibleSelection(int i)
 966:     {
 967:       TreeModel mod = getModel();
 968:       if (mod != null)
 969:         return (new AccessibleJTreeNode(JTree.this, 
 970:                   new TreePath(mod.getRoot()), null)).getAccessibleSelection(i);
 971:       return null;
 972:     }
 973:     
 974:     /**
 975:      * Returns the number of items currently selected.
 976:      * 
 977:      * @return the number of selected accessibles.
 978:      */
 979:     public int getAccessibleSelectionCount()
 980:     {
 981:       return getSelectionCount();
 982:     }
 983:     
 984:     /**
 985:      * Returns true if the current child of this object is selected.
 986:      * 
 987:      * @param i - the child of this object
 988:      * @return true if the i-th child is selected.
 989:      */
 990:     public boolean isAccessibleChildSelected(int i)
 991:     {
 992:       // Nothing to do here.
 993:       return false;
 994:     }
 995:     
 996:     /**
 997:      * Removes the specified selected item in the object from the object's
 998:      * selection.
 999:      * 
1000:      * @param i - the i-th selected item to remove
1001:      */
1002:     public void removeAccessibleSelection(int i)
1003:     {
1004:       removeSelectionInterval(i, i);
1005:     }
1006:     
1007:     /**
1008:      * Causes every selected item in the object to be selected if the object
1009:      * supports multiple selections.
1010:      */
1011:     public void selectAllAccessibleSelection()
1012:     {
1013:       if (getSelectionModel().getSelectionMode() != 
1014:         TreeSelectionModel.SINGLE_TREE_SELECTION)
1015:       addSelectionInterval(0, getVisibleRowCount());
1016:     }
1017:     
1018:     /**
1019:      * Tree Collapsed notification
1020:      * 
1021:      * @param e - the event
1022:      */
1023:     public void treeCollapsed(TreeExpansionEvent e)
1024:     {
1025:       fireTreeCollapsed(e.getPath());
1026:     }
1027:    
1028:     /**
1029:      * Tree Model Expansion notification.
1030:      * 
1031:      * @param e - the event
1032:      */
1033:     public void treeExpanded(TreeExpansionEvent e)
1034:     {
1035:       fireTreeExpanded(e.getPath());
1036:     }
1037:     
1038:     /**
1039:      * Tree Model Node change notification.
1040:      * 
1041:      * @param e - the event
1042:      */
1043:     public void treeNodesChanged(TreeModelEvent e)
1044:     {
1045:       // Nothing to do here.
1046:     }
1047:     
1048:     /**
1049:      * Tree Model Node change notification.
1050:      * 
1051:      * @param e - the event
1052:      */
1053:     public void treeNodesInserted(TreeModelEvent e)
1054:     {
1055:       // Nothing to do here.
1056:     }
1057:     
1058:     /**
1059:      * Tree Model Node change notification.
1060:      * 
1061:      * @param e - the event
1062:      */
1063:     public void treeNodesRemoved(TreeModelEvent e)
1064:     {
1065:       // Nothing to do here.
1066:     }
1067:     
1068:     /**
1069:      * Tree Model structure change change notification.
1070:      * 
1071:      * @param e - the event
1072:      */
1073:     public void treeStructureChanged(TreeModelEvent e)
1074:     {
1075:       // Nothing to do here.
1076:     }
1077:     
1078:     /**
1079:      * Tree Selection Listener value change method.
1080:      * 
1081:      * @param e - the event
1082:      */
1083:     public void valueChanged(TreeSelectionEvent e)
1084:     {
1085:       fireValueChanged(e);
1086:     }
1087:   }
1088:   
1089:   public static class DynamicUtilTreeNode extends DefaultMutableTreeNode
1090:   {
1091:     protected Object childValue;
1092: 
1093:     protected boolean loadedChildren;
1094: 
1095:     /**
1096:      * Currently not set or used by this class. It might be set and used in
1097:      * later versions of this class.
1098:      */
1099:     protected boolean hasChildren;
1100: 
1101:     public DynamicUtilTreeNode(Object value, Object children)
1102:     {
1103:       super(value);
1104:       childValue = children;
1105:       loadedChildren = false;
1106:     }
1107: 
1108:     public int getChildCount()
1109:     {
1110:       loadChildren();
1111:       return super.getChildCount();
1112:     }
1113: 
1114:     protected void loadChildren()
1115:     {
1116:       if (!loadedChildren)
1117:         {
1118:           createChildren(this, childValue);
1119:           loadedChildren = true;
1120:         }
1121:     }
1122: 
1123:     public Enumeration children()
1124:     {
1125:       loadChildren();
1126:       return super.children();
1127:     }
1128: 
1129:     /**
1130:      * Returns the child node at position <code>pos</code>. Subclassed
1131:      * here to load the children if necessary.
1132:      * 
1133:      * @param pos the position of the child node to fetch
1134:      * 
1135:      * @return the childnode at the specified position
1136:      */
1137:     public TreeNode getChildAt(int pos)
1138:     {
1139:       loadChildren();
1140:       return super.getChildAt(pos);
1141:     }
1142: 
1143:     public boolean isLeaf()
1144:     {
1145:       return (childValue == null || !(childValue instanceof Hashtable
1146:           || childValue instanceof Vector || childValue.getClass()
1147:           .isArray()));
1148:     }
1149: 
1150:     public static void createChildren(DefaultMutableTreeNode parent,
1151:                                       Object children)
1152:     {
1153:       if (children instanceof Hashtable)
1154:         {
1155:           Hashtable tab = (Hashtable) children;
1156:           Enumeration e = tab.keys();
1157:           while (e.hasMoreElements())
1158:             {
1159:               Object key = e.nextElement();
1160:               Object val = tab.get(key);
1161:               parent.add(new DynamicUtilTreeNode(key, val));
1162:             }
1163:         }
1164:       else if (children instanceof Vector)
1165:         {
1166:           Iterator i = ((Vector) children).iterator();
1167:           while (i.hasNext())
1168:             {
1169:               Object n = i.next();
1170:               parent.add(new DynamicUtilTreeNode(n, n));
1171:             }
1172:         }
1173:       else if (children != null && children.getClass().isArray())
1174:         {
1175:           Object[] arr = (Object[]) children;
1176:           for (int i = 0; i < arr.length; ++i)
1177:             parent.add(new DynamicUtilTreeNode(arr[i], arr[i]));
1178:         }
1179:     }
1180:   }
1181: 
1182:   /**
1183:    * Listens to the model of the JTree and updates the property 
1184:    * <code>expandedState</code> if nodes are removed or changed.
1185:    */
1186:   protected class TreeModelHandler implements TreeModelListener
1187:   {
1188: 
1189:     /**
1190:      * Creates a new instance of TreeModelHandler.
1191:      */
1192:     protected TreeModelHandler()
1193:     {
1194:       // Nothing to do here.
1195:     }
1196: 
1197:     /**
1198:      * Notifies when a node has changed in some ways. This does not include
1199:      * that a node has changed its location or changed it's children. It
1200:      * only means that some attributes of the node have changed that might
1201:      * affect its presentation.
1202:      * 
1203:      * This method is called after the actual change occured.
1204:      * 
1205:      * @param ev the TreeModelEvent describing the change
1206:      */
1207:     public void treeNodesChanged(TreeModelEvent ev)
1208:     {
1209:       // Nothing to do here.
1210:     }
1211: 
1212:     /**
1213:      * Notifies when a node is inserted into the tree.
1214:      * 
1215:      * This method is called after the actual change occured.
1216:      * 
1217:      * @param ev the TreeModelEvent describing the change
1218:      */
1219:     public void treeNodesInserted(TreeModelEvent ev)
1220:     {
1221:       // nothing to do here
1222:     }
1223: 
1224:     /**
1225:      * Notifies when a node is removed from the tree.
1226:      * 
1227:      * This method is called after the actual change occured.
1228:      *
1229:      * @param ev the TreeModelEvent describing the change
1230:      */
1231:     public void treeNodesRemoved(TreeModelEvent ev)
1232:     {
1233:       // TODO: The API docs suggest that this method should do something
1234:       // but I cannot really see what has to be done here ...
1235:     }
1236: 
1237:     /**
1238:      * Notifies when the structure of the tree is changed.
1239:      * 
1240:      * This method is called after the actual change occured.
1241:      * 
1242:      * @param ev the TreeModelEvent describing the change
1243:      */
1244:     public void treeStructureChanged(TreeModelEvent ev)
1245:     {
1246:       // Set state of new path.
1247:       TreePath path = ev.getTreePath();
1248:       setExpandedState(path, isExpanded(path));
1249:     }
1250:   }
1251: 
1252:   /**
1253:    * This redirects TreeSelectionEvents and rewrites the source of it to be
1254:    * this JTree. This is typically done when the tree model generates an
1255:    * event, but the JTree object associated with that model should be listed
1256:    * as the actual source of the event.
1257:    */
1258:   protected class TreeSelectionRedirector implements TreeSelectionListener,
1259:                                                      Serializable
1260:   {
1261:     /** The serial version UID. */
1262:     private static final long serialVersionUID = -3505069663646241664L;
1263: 
1264:     /**
1265:      * Creates a new instance of TreeSelectionRedirector
1266:      */
1267:     protected TreeSelectionRedirector()
1268:     {
1269:       // Nothing to do here.
1270:     }
1271: 
1272:     /**
1273:      * Notifies when the tree selection changes.
1274:      * 
1275:      * @param ev the TreeSelectionEvent that describes the change
1276:      */
1277:     public void valueChanged(TreeSelectionEvent ev)
1278:     {
1279:       TreeSelectionEvent rewritten = 
1280:         (TreeSelectionEvent) ev.cloneWithSource(JTree.this);
1281:       fireValueChanged(rewritten);
1282: 
1283:       // Only repaint the changed nodes.
1284:       TreePath[] changed = ev.getPaths();
1285:       for (int i = 0; i < changed.length; i++)
1286:         {
1287:           repaint(getPathBounds(changed[i]));
1288:         }
1289:     }
1290:   }
1291: 
1292:   /**
1293:    * A TreeModel that does not allow anything to be selected.
1294:    */
1295:   protected static class EmptySelectionModel extends DefaultTreeSelectionModel
1296:   {
1297:     /** The serial version UID. */
1298:     private static final long serialVersionUID = -5815023306225701477L;
1299: 
1300:     /**
1301:      * The shared instance of this model.
1302:      */
1303:     protected static final EmptySelectionModel sharedInstance =
1304:       new EmptySelectionModel();
1305: 
1306:     /**
1307:      * Creates a new instance of EmptySelectionModel.
1308:      */
1309:     protected EmptySelectionModel()
1310:     {
1311:       // Nothing to do here.
1312:     }
1313: 
1314:     /**
1315:      * Returns the shared instance of EmptySelectionModel.
1316:      * 
1317:      * @return the shared instance of EmptySelectionModel
1318:      */
1319:     public static EmptySelectionModel sharedInstance()
1320:     {
1321:       return sharedInstance;
1322:     }
1323: 
1324:     /**
1325:      * This catches attempts to set a selection and sets nothing instead.
1326:      * 
1327:      * @param paths not used here
1328:      */
1329:     public void setSelectionPaths(TreePath[] paths)
1330:     {
1331:       // We don't allow selections in this class.
1332:     }
1333: 
1334:     /**
1335:      * This catches attempts to add something to the selection.
1336:      * 
1337:      * @param paths not used here
1338:      */
1339:     public void addSelectionPaths(TreePath[] paths)
1340:     {
1341:       // We don't allow selections in this class.
1342:     }
1343: 
1344:     /**
1345:      * This catches attempts to remove something from the selection.
1346:      * 
1347:      * @param paths not used here
1348:      */
1349:     public void removeSelectionPaths(TreePath[] paths)
1350:     {
1351:       // We don't allow selections in this class.
1352:     }
1353:   }
1354: 
1355:   private static final long serialVersionUID = 7559816092864483649L;
1356: 
1357:   public static final String CELL_EDITOR_PROPERTY = "cellEditor";
1358: 
1359:   public static final String CELL_RENDERER_PROPERTY = "cellRenderer";
1360: 
1361:   public static final String EDITABLE_PROPERTY = "editable";
1362: 
1363:   public static final String INVOKES_STOP_CELL_EDITING_PROPERTY =
1364:     "invokesStopCellEditing";
1365: 
1366:   public static final String LARGE_MODEL_PROPERTY = "largeModel";
1367: 
1368:   public static final String ROOT_VISIBLE_PROPERTY = "rootVisible";
1369: 
1370:   public static final String ROW_HEIGHT_PROPERTY = "rowHeight";
1371: 
1372:   public static final String SCROLLS_ON_EXPAND_PROPERTY = "scrollsOnExpand";
1373: 
1374:   public static final String SELECTION_MODEL_PROPERTY = "selectionModel";
1375: 
1376:   public static final String SHOWS_ROOT_HANDLES_PROPERTY = "showsRootHandles";
1377: 
1378:   public static final String TOGGLE_CLICK_COUNT_PROPERTY = "toggleClickCount";
1379: 
1380:   public static final String TREE_MODEL_PROPERTY = "model";
1381: 
1382:   public static final String VISIBLE_ROW_COUNT_PROPERTY = "visibleRowCount";
1383: 
1384:   /** @since 1.3 */
1385:   public static final String ANCHOR_SELECTION_PATH_PROPERTY =
1386:     "anchorSelectionPath";
1387: 
1388:     /** @since 1.3 */
1389:   public static final String LEAD_SELECTION_PATH_PROPERTY = "leadSelectionPath";
1390: 
1391:   /** @since 1.3 */
1392:   public static final String EXPANDS_SELECTED_PATHS_PROPERTY =
1393:     "expandsSelectedPaths";
1394: 
1395:   private static final Object EXPANDED = new Object();
1396: 
1397:   private static final Object COLLAPSED = new Object();
1398: 
1399:   private boolean dragEnabled;
1400: 
1401:   private boolean expandsSelectedPaths;
1402: 
1403:   private TreePath anchorSelectionPath;
1404: 
1405:   /**
1406:    * This contains the state of all nodes in the tree. Al/ entries map the
1407:    * TreePath of a note to to its state. Valid states are EXPANDED and
1408:    * COLLAPSED. Nodes not in this Hashtable are assumed state COLLAPSED.
1409:    */
1410:   private Hashtable nodeStates = new Hashtable();
1411: 
1412:   protected transient TreeCellEditor cellEditor;
1413: 
1414:   protected transient TreeCellRenderer cellRenderer;
1415: 
1416:   protected boolean editable;
1417: 
1418:   protected boolean invokesStopCellEditing;
1419: 
1420:   protected boolean largeModel;
1421: 
1422:   protected boolean rootVisible;
1423: 
1424:   protected int rowHeight;
1425: 
1426:   protected boolean scrollsOnExpand;
1427: 
1428:   protected transient TreeSelectionModel selectionModel;
1429: 
1430:   protected boolean showsRootHandles;
1431: 
1432:   protected int toggleClickCount;
1433: 
1434:   protected transient TreeModel treeModel;
1435: 
1436:   protected int visibleRowCount;
1437: 
1438:   /**
1439:    * Handles TreeModelEvents to update the expandedState.
1440:    */
1441:   protected transient TreeModelListener treeModelListener;
1442: 
1443:   /**
1444:    * Redirects TreeSelectionEvents so that the source is this JTree.
1445:    */
1446:   protected TreeSelectionRedirector selectionRedirector =
1447:     new TreeSelectionRedirector();
1448: 
1449:   /**
1450:    * Indicates if the rowHeight property has been set by a client
1451:    * program or by the UI.
1452:    *
1453:    * @see #setUIProperty(String, Object)
1454:    * @see LookAndFeel#installProperty(JComponent, String, Object)
1455:    */
1456:   private boolean clientRowHeightSet = false;
1457: 
1458:   /**
1459:    * Indicates if the scrollsOnExpand property has been set by a client
1460:    * program or by the UI.
1461:    *
1462:    * @see #setUIProperty(String, Object)
1463:    * @see LookAndFeel#installProperty(JComponent, String, Object)
1464:    */
1465:   private boolean clientScrollsOnExpandSet = false;
1466: 
1467:   /**
1468:    * Indicates if the showsRootHandles property has been set by a client
1469:    * program or by the UI.
1470:    *
1471:    * @see #setUIProperty(String, Object)
1472:    * @see LookAndFeel#installProperty(JComponent, String, Object)
1473:    */
1474:   private boolean clientShowsRootHandlesSet = false;
1475: 
1476:   /**
1477:    * Creates a new <code>JTree</code> object.
1478:    */
1479:   public JTree()
1480:   {
1481:     this(createTreeModel(null));
1482:   }
1483: 
1484:   /**
1485:    * Creates a new <code>JTree</code> object.
1486:    * 
1487:    * @param value the initial nodes in the tree
1488:    */
1489:   public JTree(Hashtable value)
1490:   {
1491:     this(createTreeModel(value));
1492:   }
1493: 
1494:   /**
1495:    * Creates a new <code>JTree</code> object.
1496:    * 
1497:    * @param value the initial nodes in the tree
1498:    */
1499:   public JTree(Object[] value)
1500:   {
1501:     this(createTreeModel(value));
1502:   }
1503: 
1504:   /**
1505:    * Creates a new <code>JTree</code> object.
1506:    * 
1507:    * @param model the model to use
1508:    */
1509:   public JTree(TreeModel model)
1510:   {
1511:     setRootVisible(true);
1512:     // The setModel also calls the updateUI
1513:     setModel(model);
1514:     setSelectionModel(new EmptySelectionModel());
1515:     selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
1516:     
1517:     // The root node appears expanded by default.
1518:     nodeStates.put(new TreePath(model.getRoot()), EXPANDED);
1519:   }
1520: 
1521:   /**
1522:    * Creates a new <code>JTree</code> object.
1523:    * 
1524:    * @param root the root node
1525:    */
1526:   public JTree(TreeNode root)
1527:   {
1528:     this(root, false);
1529:   }
1530: 
1531:   /**
1532:    * Creates a new <code>JTree</code> object.
1533:    * 
1534:    * @param root the root node
1535:    * @param asksAllowChildren if false, all nodes without children are leaf
1536:    *        nodes. If true, only nodes that do not allow children are leaf
1537:    *        nodes.
1538:    */
1539:   public JTree(TreeNode root, boolean asksAllowChildren)
1540:   {
1541:     this(new DefaultTreeModel(root, asksAllowChildren));
1542:   }
1543: 
1544:   /**
1545:    * Creates a new <code>JTree</code> object.
1546:    * 
1547:    * @param value the initial nodes in the tree
1548:    */
1549:   public JTree(Vector value)
1550:   {
1551:     this(createTreeModel(value));
1552:   }
1553: 
1554:   public int getRowForPath(TreePath path)
1555:   {
1556:     TreeUI ui = getUI();
1557: 
1558:     if (ui != null)
1559:       return ui.getRowForPath(this, path);
1560: 
1561:     return -1;
1562:   }
1563: 
1564:   public TreePath getPathForRow(int row)
1565:   {
1566:     TreeUI ui = getUI();
1567:     return ui != null ? ui.getPathForRow(this, row) : null;
1568:   }
1569:   
1570:   /**
1571:    * Get the pathes that are displayes between the two given rows.
1572:    * 
1573:    * @param index0 the starting row, inclusive
1574:    * @param index1 the ending row, inclusive
1575:    * 
1576:    * @return the array of the tree pathes
1577:    */
1578:   protected TreePath[] getPathBetweenRows(int index0, int index1)
1579:   {
1580:     TreeUI ui = getUI();
1581: 
1582:     if (ui == null)
1583:       return null;
1584: 
1585:     int minIndex = Math.min(index0, index1);
1586:     int maxIndex = Math.max(index0, index1);
1587:     TreePath[] paths = new TreePath[maxIndex - minIndex + 1];
1588: 
1589:     for (int i = minIndex; i <= maxIndex; ++i)
1590:       paths[i - minIndex] = ui.getPathForRow(this, i);
1591: 
1592:     return paths;
1593:   }
1594: 
1595:   /**
1596:    * Creates a new <code>TreeModel</code> object.
1597:    * 
1598:    * @param value the values stored in the model
1599:    */
1600:   protected static TreeModel createTreeModel(Object value)
1601:   {
1602:     return new DefaultTreeModel(new DynamicUtilTreeNode(value, value));
1603:   }
1604: 
1605:   /**
1606:    * Return the UI associated with this <code>JTree</code> object.
1607:    * 
1608:    * @return the associated <code>TreeUI</code> object
1609:    */
1610:   public TreeUI getUI()
1611:   {
1612:     return (TreeUI) ui;
1613:   }
1614: 
1615:   /**
1616:    * Sets the UI associated with this <code>JTree</code> object.
1617:    * 
1618:    * @param ui the <code>TreeUI</code> to associate
1619:    */
1620:   public void setUI(TreeUI ui)
1621:   {
1622:     super.setUI(ui);
1623:   }
1624: 
1625:   /**
1626:    * This method resets the UI used to the Look and Feel defaults..
1627:    */
1628:   public void updateUI()
1629:   {
1630:     setUI((TreeUI) UIManager.getUI(this));
1631:   }
1632: 
1633:   /**
1634:    * This method returns the String ID of the UI class of Separator.
1635:    * 
1636:    * @return The UI class' String ID.
1637:    */
1638:   public String getUIClassID()
1639:   {
1640:     return "TreeUI";
1641:   }
1642: 
1643:   /**
1644:    * Gets the AccessibleContext associated with this
1645:    * <code>JTree</code>.
1646:    * 
1647:    * @return the associated context
1648:    */
1649:   public AccessibleContext getAccessibleContext()
1650:   {
1651:     return new AccessibleJTree();
1652:   }
1653: 
1654:   /**
1655:    * Returns the preferred viewport size.
1656:    * 
1657:    * @return the preferred size
1658:    */
1659:   public Dimension getPreferredScrollableViewportSize()
1660:   {
1661:     return getPreferredSize();
1662:   }
1663:   
1664:   /**
1665:    * Return the preferred scrolling amount (in pixels) for the given scrolling
1666:    * direction and orientation. This method handles a partially exposed row by
1667:    * returning the distance required to completely expose the item.
1668:    * 
1669:    * @param visibleRect the currently visible part of the component.
1670:    * @param orientation the scrolling orientation
1671:    * @param direction the scrolling direction (negative - up, positive -down).
1672:    *          The values greater than one means that more mouse wheel or similar
1673:    *          events were generated, and hence it is better to scroll the longer
1674:    *          distance.
1675:    * @author Audrius Meskauskas (audriusa@bioinformatics.org)
1676:    */
1677:   public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation,
1678:                                         int direction)
1679:   {
1680:     int delta;
1681: 
1682:     // Round so that the top would start from the row boundary
1683:     if (orientation == SwingConstants.VERTICAL)
1684:       {
1685:         // One pixel down, otherwise picks another row too high.
1686:         int row = getClosestRowForLocation(visibleRect.x, visibleRect.y + 1);
1687:         row = row + direction;
1688:         if (row < 0)
1689:           row = 0;
1690: 
1691:         Rectangle newTop = getRowBounds(row);
1692:         delta = newTop.y - visibleRect.y;
1693:       }
1694:     else
1695:       delta = direction * rowHeight == 0 ? 20 : rowHeight;
1696:     return delta;
1697:   }
1698: 
1699:   public int getScrollableBlockIncrement(Rectangle visibleRect,
1700:                                          int orientation, int direction)
1701:   {
1702:     return getScrollableUnitIncrement(visibleRect, orientation, direction);
1703:   }
1704: 
1705:   public boolean getScrollableTracksViewportHeight()
1706:   {
1707:     if (getParent() instanceof JViewport)
1708:       return ((JViewport) getParent()).getHeight() > getPreferredSize().height;
1709:     return false;
1710:   }
1711: 
1712:   public boolean getScrollableTracksViewportWidth()
1713:   {
1714:     if (getParent() instanceof JViewport)
1715:       return ((JViewport) getParent()).getWidth() > getPreferredSize().width;
1716:     return false;
1717:   }
1718: 
1719:   /**
1720:    * Adds a <code>TreeExpansionListener</code> object to the tree.
1721:    * 
1722:    * @param listener the listener to add
1723:    */
1724:   public void addTreeExpansionListener(TreeExpansionListener listener)
1725:   {
1726:     listenerList.add(TreeExpansionListener.class, listener);
1727:   }
1728: 
1729:   /**
1730:    * Removes a <code>TreeExpansionListener</code> object from the tree.
1731:    * 
1732:    * @param listener the listener to remove
1733:    */
1734:   public void removeTreeExpansionListener(TreeExpansionListener listener)
1735:   {
1736:     listenerList.remove(TreeExpansionListener.class, listener);
1737:   }
1738: 
1739:   /**
1740:    * Returns all added <code>TreeExpansionListener</code> objects.
1741:    * 
1742:    * @return an array of listeners
1743:    */
1744:   public TreeExpansionListener[] getTreeExpansionListeners()
1745:   {
1746:     return (TreeExpansionListener[]) getListeners(TreeExpansionListener.class);
1747:   }
1748: 
1749:   /**
1750:    * Notifies all listeners that the tree was collapsed.
1751:    * 
1752:    * @param path the path to the node that was collapsed
1753:    */
1754:   public void fireTreeCollapsed(TreePath path)
1755:   {
1756:     TreeExpansionEvent event = new TreeExpansionEvent(this, path);
1757:     TreeExpansionListener[] listeners = getTreeExpansionListeners();
1758: 
1759:     for (int index = 0; index < listeners.length; ++index)
1760:       listeners[index].treeCollapsed(event);
1761:   }
1762: 
1763:   /**
1764:    * Notifies all listeners that the tree was expanded.
1765:    * 
1766:    * @param path the path to the node that was expanded
1767:    */
1768:   public void fireTreeExpanded(TreePath path)
1769:   {
1770:     TreeExpansionEvent event = new TreeExpansionEvent(this, path);
1771:     TreeExpansionListener[] listeners = getTreeExpansionListeners();
1772: 
1773:     for (int index = 0; index < listeners.length; ++index)
1774:       listeners[index].treeExpanded(event);
1775:   }
1776: 
1777:   /**
1778:    * Adds a <code>TreeSelctionListener</code> object to the tree.
1779:    * 
1780:    * @param listener the listener to add
1781:    */
1782:   public void addTreeSelectionListener(TreeSelectionListener listener)
1783:   {
1784:     listenerList.add(TreeSelectionListener.class, listener);
1785:   }
1786: 
1787:   /**
1788:    * Removes a <code>TreeSelectionListener</code> object from the tree.
1789:    * 
1790:    * @param listener the listener to remove
1791:    */
1792:   public void removeTreeSelectionListener(TreeSelectionListener listener)
1793:   {
1794:     listenerList.remove(TreeSelectionListener.class, listener);
1795:   }
1796: 
1797:   /**
1798:    * Returns all added <code>TreeSelectionListener</code> objects.
1799:    * 
1800:    * @return an array of listeners
1801:    */
1802:   public TreeSelectionListener[] getTreeSelectionListeners()
1803:   {
1804:     return (TreeSelectionListener[]) 
1805:     getListeners(TreeSelectionListener.class);
1806:   }
1807: 
1808:   /**
1809:    * Notifies all listeners when the selection of the tree changed.
1810:    * 
1811:    * @param event the event to send
1812:    */
1813:   protected void fireValueChanged(TreeSelectionEvent event)
1814:   {
1815:     TreeSelectionListener[] listeners = getTreeSelectionListeners();
1816: 
1817:     for (int index = 0; index < listeners.length; ++index)
1818:       listeners[index].valueChanged(event);
1819:   }
1820: 
1821:   /**
1822:    * Adds a <code>TreeWillExpandListener</code> object to the tree.
1823:    * 
1824:    * @param listener the listener to add
1825:    */
1826:   public void addTreeWillExpandListener(TreeWillExpandListener listener)
1827:   {
1828:     listenerList.add(TreeWillExpandListener.class, listener);
1829:   }
1830: 
1831:   /**
1832:    * Removes a <code>TreeWillExpandListener</code> object from the tree.
1833:    * 
1834:    * @param listener the listener to remove
1835:    */
1836:   public void removeTreeWillExpandListener(TreeWillExpandListener listener)
1837:   {
1838:     listenerList.remove(TreeWillExpandListener.class, listener);
1839:   }
1840: 
1841:   /**
1842:    * Returns all added <code>TreeWillExpandListener</code> objects.
1843:    * 
1844:    * @return an array of listeners
1845:    */
1846:   public TreeWillExpandListener[] getTreeWillExpandListeners()
1847:   {
1848:     return (TreeWillExpandListener[]) 
1849:     getListeners(TreeWillExpandListener.class);
1850:   }
1851: 
1852:   /**
1853:    * Notifies all listeners that the tree will collapse.
1854:    * 
1855:    * @param path the path to the node that will collapse
1856:    */
1857:   public void fireTreeWillCollapse(TreePath path) throws ExpandVetoException
1858:   {
1859:     TreeExpansionEvent event = new TreeExpansionEvent(this, path);
1860:     TreeWillExpandListener[] listeners = getTreeWillExpandListeners();
1861: 
1862:     for (int index = 0; index < listeners.length; ++index)
1863:       listeners[index].treeWillCollapse(event);
1864:   }
1865: 
1866:   /**
1867:    * Notifies all listeners that the tree will expand.
1868:    * 
1869:    * @param path the path to the node that will expand
1870:    */
1871:   public void fireTreeWillExpand(TreePath path) throws ExpandVetoException
1872:   {
1873:     TreeExpansionEvent event = new TreeExpansionEvent(this, path);
1874:     TreeWillExpandListener[] listeners = getTreeWillExpandListeners();
1875: 
1876:     for (int index = 0; index < listeners.length; ++index)
1877:       listeners[index].treeWillExpand(event);
1878:   }
1879: 
1880:   /**
1881:    * Returns the model of this <code>JTree</code> object.
1882:    * 
1883:    * @return the associated <code>TreeModel</code>
1884:    */
1885:   public TreeModel getModel()
1886:   {
1887:     return treeModel;
1888:   }
1889: 
1890:   /**
1891:    * Sets the model to use in <code>JTree</code>.
1892:    * 
1893:    * @param model the <code>TreeModel</code> to use
1894:    */
1895:   public void setModel(TreeModel model)
1896:   {
1897:     if (treeModel == model)
1898:       return;
1899: 
1900:     // add treeModelListener to the new model
1901:     if (treeModelListener == null)
1902:       treeModelListener = createTreeModelListener();
1903:     if (model != null) // as setModel(null) is allowed
1904:       model.addTreeModelListener(treeModelListener);
1905: 
1906:     TreeModel oldValue = treeModel;
1907:     treeModel = model;
1908: 
1909:     firePropertyChange(TREE_MODEL_PROPERTY, oldValue, model);
1910:     updateUI();
1911:   }
1912: 
1913:   /**
1914:    * Checks if this <code>JTree</code> object is editable.
1915:    * 
1916:    * @return <code>true</code> if this tree object is editable,
1917:    *         <code>false</code> otherwise
1918:    */
1919:   public boolean isEditable()
1920:   {
1921:     return editable;
1922:   }
1923: 
1924:   /**
1925:    * Sets the <code>editable</code> property.
1926:    * 
1927:    * @param flag <code>true</code> to make this tree object editable,
1928:    *        <code>false</code> otherwise
1929:    */
1930:   public void setEditable(boolean flag)
1931:   {
1932:     if (editable == flag)
1933:       return;
1934: 
1935:     boolean oldValue = editable;
1936:     editable = flag;
1937:     firePropertyChange(EDITABLE_PROPERTY, oldValue, editable);
1938:   }
1939: 
1940:   /**
1941:    * Checks if the root element is visible.
1942:    * 
1943:    * @return <code>true</code> if the root element is visible,
1944:    *         <code>false</code> otherwise
1945:    */
1946:   public boolean isRootVisible()
1947:   {
1948:     return rootVisible;
1949:   }
1950: 
1951:   public void setRootVisible(boolean flag)
1952:   {
1953:     if (rootVisible == flag)
1954:       return;
1955: 
1956:     // If the root is currently selected, unselect it
1957:     if (rootVisible && !flag)
1958:       {
1959:         TreeSelectionModel model = getSelectionModel();
1960:         // The root is always shown in the first row
1961:         TreePath rootPath = getPathForRow(0);
1962:         model.removeSelectionPath(rootPath);
1963:       }
1964:     
1965:     boolean oldValue = rootVisible;
1966:     rootVisible = flag;
1967:     firePropertyChange(ROOT_VISIBLE_PROPERTY, oldValue, flag);
1968:     
1969:   }
1970: 
1971:   public boolean getShowsRootHandles()
1972:   {
1973:     return showsRootHandles;
1974:   }
1975: 
1976:   public void setShowsRootHandles(boolean flag)
1977:   {
1978:     clientShowsRootHandlesSet = true;
1979: 
1980:     if (showsRootHandles == flag)
1981:       return;
1982:     
1983:     boolean oldValue = showsRootHandles;
1984:     showsRootHandles = flag;
1985:     firePropertyChange(SHOWS_ROOT_HANDLES_PROPERTY, oldValue, flag);
1986:   }
1987: 
1988:   public TreeCellEditor getCellEditor()
1989:   {
1990:     return cellEditor;
1991:   }
1992: 
1993:   public void setCellEditor(TreeCellEditor editor)
1994:   {
1995:     if (cellEditor == editor)
1996:       return;
1997: 
1998:     TreeCellEditor oldValue = cellEditor;
1999:     cellEditor = editor;
2000:     firePropertyChange(CELL_EDITOR_PROPERTY, oldValue, editor);
2001:   }
2002: 
2003:   public TreeCellRenderer getCellRenderer()
2004:   {
2005:     return cellRenderer;
2006:   }
2007: 
2008:   public void setCellRenderer(TreeCellRenderer newRenderer)
2009:   {
2010:     if (cellRenderer == newRenderer)
2011:       return;
2012: 
2013:     TreeCellRenderer oldValue = cellRenderer;
2014:     cellRenderer = newRenderer;
2015:     firePropertyChange(CELL_RENDERER_PROPERTY, oldValue, newRenderer);
2016:   }
2017: 
2018:   public TreeSelectionModel getSelectionModel()
2019:   {
2020:     return selectionModel;
2021:   }
2022: 
2023:   public void setSelectionModel(TreeSelectionModel model)
2024:   {
2025:     if (selectionModel == model)
2026:       return;
2027: 
2028:     if (selectionModel != null)
2029:       selectionModel.removeTreeSelectionListener(selectionRedirector);
2030: 
2031:     TreeSelectionModel oldValue = selectionModel;
2032:     selectionModel = model;
2033: 
2034:     if (selectionModel != null)
2035:       selectionModel.addTreeSelectionListener(selectionRedirector);
2036: 
2037:     firePropertyChange(SELECTION_MODEL_PROPERTY, oldValue, model);
2038:     revalidate();
2039:     repaint();
2040:   }
2041: 
2042:   public int getVisibleRowCount()
2043:   {
2044:     return visibleRowCount;
2045:   }
2046: 
2047:   public void setVisibleRowCount(int rows)
2048:   {
2049:     if (visibleRowCount == rows)
2050:       return;
2051: 
2052:     int oldValue = visibleRowCount;
2053:     visibleRowCount = rows;
2054:     firePropertyChange(VISIBLE_ROW_COUNT_PROPERTY, oldValue, rows);
2055:   }
2056: 
2057:   public boolean isLargeModel()
2058:   {
2059:     return largeModel;
2060:   }
2061: 
2062:   public void setLargeModel(boolean large)
2063:   {
2064:     if (largeModel == large)
2065:       return;
2066: 
2067:     boolean oldValue = largeModel;
2068:     largeModel = large;
2069:     firePropertyChange(LARGE_MODEL_PROPERTY, oldValue, large);
2070:   }
2071: 
2072:   public int getRowHeight()
2073:   {
2074:     return rowHeight;
2075:   }
2076: 
2077:   public void setRowHeight(int height)
2078:   {
2079:     clientRowHeightSet = true;
2080: 
2081:     if (rowHeight == height)
2082:       return;
2083: 
2084:     int oldValue = rowHeight;
2085:     rowHeight = height;
2086:     firePropertyChange(ROW_HEIGHT_PROPERTY, oldValue, height);
2087:   }
2088: 
2089:   public boolean isFixedRowHeight()
2090:   {
2091:     return rowHeight > 0;
2092:   }
2093: 
2094:   public boolean getInvokesStopCellEditing()
2095:   {
2096:     return invokesStopCellEditing;
2097:   }
2098: 
2099:   public void setInvokesStopCellEditing(boolean invoke)
2100:   {
2101:     if (invokesStopCellEditing == invoke)
2102:       return;
2103: 
2104:     boolean oldValue = invokesStopCellEditing;
2105:     invokesStopCellEditing = invoke;
2106:     firePropertyChange(INVOKES_STOP_CELL_EDITING_PROPERTY, 
2107:                        oldValue, invoke);
2108:   }
2109: 
2110:   /**
2111:    * @since 1.3
2112:    */
2113:   public int getToggleClickCount()
2114:   {
2115:     return toggleClickCount;
2116:   }
2117: 
2118:   /**
2119:    * @since 1.3
2120:    */
2121:   public void setToggleClickCount(int count)
2122:   {
2123:     if (toggleClickCount == count)
2124:       return;
2125: 
2126:     int oldValue = toggleClickCount;
2127:     toggleClickCount = count;
2128:     firePropertyChange(TOGGLE_CLICK_COUNT_PROPERTY, oldValue, count);
2129:   }
2130: 
2131:   public void scrollPathToVisible(TreePath path)
2132:   {
2133:     if (path == null)
2134:       return;
2135:     Rectangle rect = getPathBounds(path);
2136:     scrollRectToVisible(rect);
2137:   }
2138: 
2139:   public void scrollRowToVisible(int row)
2140:   {
2141:     scrollPathToVisible(getPathForRow(row));
2142:   }
2143: 
2144:   public boolean getScrollsOnExpand()
2145:   {
2146:     return scrollsOnExpand;
2147:   }
2148: 
2149:   public void setScrollsOnExpand(boolean scroll)
2150:   {
2151:     clientScrollsOnExpandSet = true;
2152:     if (scrollsOnExpand == scroll)
2153:       return;
2154: 
2155:     boolean oldValue = scrollsOnExpand;
2156:     scrollsOnExpand = scroll;
2157:     firePropertyChange(SCROLLS_ON_EXPAND_PROPERTY, oldValue, scroll);
2158:   }
2159: 
2160:   public void setSelectionPath(TreePath path)
2161:   {
2162:     selectionModel.setSelectionPath(path);
2163:   }
2164: 
2165:   public void setSelectionPaths(TreePath[] paths)
2166:   {
2167:     selectionModel.setSelectionPaths(paths);
2168:   }
2169: 
2170:   public void setSelectionRow(int row)
2171:   {
2172:     TreePath path = getPathForRow(row);
2173: 
2174:     if (path != null)
2175:       selectionModel.setSelectionPath(path);
2176:   }
2177: 
2178:   public void setSelectionRows(int[] rows)
2179:   {
2180:     // Make sure we have an UI so getPathForRow() does not return null.
2181:     if (rows == null || getUI() == null)
2182:       return;
2183: 
2184:     TreePath[] paths = new TreePath[rows.length];
2185: 
2186:     for (int i = rows.length - 1; i >= 0; --i)
2187:       paths[i] = getPathForRow(rows[i]);
2188: 
2189:     setSelectionPaths(paths);
2190:   }
2191: 
2192:   public void setSelectionInterval(int index0, int index1)
2193:   {
2194:     TreePath[] paths = getPathBetweenRows(index0, index1);
2195: 
2196:     if (paths != null)
2197:       setSelectionPaths(paths);
2198:   }
2199: 
2200:   public void addSelectionPath(TreePath path)
2201:   {
2202:     selectionModel.addSelectionPath(path);
2203:   }
2204: 
2205:   public void addSelectionPaths(TreePath[] paths)
2206:   {
2207:     selectionModel.addSelectionPaths(paths);
2208:   }
2209: 
2210:   public void addSelectionRow(int row)
2211:   {
2212:     TreePath path = getPathForRow(row);
2213: 
2214:     if (path != null)
2215:       selectionModel.addSelectionPath(path);
2216:   }
2217: 
2218:   public void addSelectionRows(int[] rows)
2219:   {
2220:     // Make sure we have an UI so getPathForRow() does not return null.
2221:     if (rows == null || getUI() == null)
2222:       return;
2223: 
2224:     TreePath[] paths = new TreePath[rows.length];
2225: 
2226:     for (int i = rows.length - 1; i >= 0; --i)
2227:       paths[i] = getPathForRow(rows[i]);
2228: 
2229:     addSelectionPaths(paths);
2230:   }
2231:   
2232:   /**
2233:    * Select all rows between the two given indexes, inclusive. The method
2234:    * will not select the inner leaves and braches of the currently collapsed
2235:    * nodes in this interval.
2236:    * 
2237:    * @param index0 the starting row, inclusive
2238:    * @param index1 the ending row, inclusive
2239:    */
2240:   public void addSelectionInterval(int index0, int index1)
2241:   {
2242:     TreePath[] paths = getPathBetweenRows(index0, index1);
2243: 
2244:     if (paths != null)
2245:       addSelectionPaths(paths);
2246:   }
2247: 
2248:   public void removeSelectionPath(TreePath path)
2249:   {
2250:     selectionModel.removeSelectionPath(path);
2251:   }
2252: 
2253:   public void removeSelectionPaths(TreePath[] paths)
2254:   {
2255:     selectionModel.removeSelectionPaths(paths);
2256:   }
2257: 
2258:   public void removeSelectionRow(int row)
2259:   {
2260:     TreePath path = getPathForRow(row);
2261: 
2262:     if (path != null)
2263:       selectionModel.removeSelectionPath(path);
2264:   }
2265: 
2266:   public void removeSelectionRows(int[] rows)
2267:   {
2268:     if (rows == null || getUI() == null)
2269:       return;
2270: 
2271:     TreePath[] paths = new TreePath[rows.length];
2272: 
2273:     for (int i = rows.length - 1; i >= 0; --i)
2274:       paths[i] = getPathForRow(rows[i]);
2275: 
2276:     removeSelectionPaths(paths);
2277:   }
2278: 
2279:   public void removeSelectionInterval(int index0, int index1)
2280:   {
2281:     TreePath[] paths = getPathBetweenRows(index0, index1);
2282: 
2283:     if (paths != null)
2284:       removeSelectionPaths(paths);
2285:   }
2286: 
2287:   public void clearSelection()
2288:   {
2289:     selectionModel.clearSelection();
2290:     setLeadSelectionPath(null);
2291:   }
2292: 
2293:   public TreePath getLeadSelectionPath()
2294:   {
2295:     if (selectionModel == null)
2296:       return null;
2297:     else
2298:       return selectionModel.getLeadSelectionPath();
2299:   }
2300: 
2301:   /**
2302:    * @since 1.3
2303:    */
2304:   public void setLeadSelectionPath(TreePath path)
2305:   {
2306:     if (selectionModel != null)
2307:       {
2308:         TreePath oldValue = selectionModel.getLeadSelectionPath();
2309:         if (path.equals(oldValue))
2310:           return;
2311:        
2312:         // Repaint the previous and current rows with the lead selection path.
2313:         if (path != null)
2314:           {
2315:             repaint(getPathBounds(path));
2316:             selectionModel.addSelectionPath(path);
2317:           }
2318:         
2319:         if (oldValue!=null)
2320:           repaint(getPathBounds(oldValue));
2321:         
2322:         firePropertyChange(LEAD_SELECTION_PATH_PROPERTY, oldValue, path);
2323:       }
2324:   }
2325: 
2326:   /**
2327:    * @since 1.3
2328:    */
2329:   public TreePath getAnchorSelectionPath()
2330:   {
2331:     return anchorSelectionPath;
2332:   }
2333: 
2334:   /**
2335:    * @since 1.3
2336:    */
2337:   public void setAnchorSelectionPath(TreePath path)
2338:   {
2339:     if (anchorSelectionPath == path)
2340:       return;
2341: 
2342:     TreePath oldValue = anchorSelectionPath;
2343:     anchorSelectionPath = path;
2344:     firePropertyChange(ANCHOR_SELECTION_PATH_PROPERTY, oldValue, path);
2345:   }
2346: 
2347:   public int getLeadSelectionRow()
2348:   {
2349:     return selectionModel.getLeadSelectionRow();
2350:   }
2351: 
2352:   public int getMaxSelectionRow()
2353:   {
2354:     return selectionModel.getMaxSelectionRow();
2355:   }
2356: 
2357:   public int getMinSelectionRow()
2358:   {
2359:     return selectionModel.getMinSelectionRow();
2360:   }
2361: 
2362:   public int getSelectionCount()
2363:   {
2364:     return selectionModel.getSelectionCount();
2365:   }
2366: 
2367:   public TreePath getSelectionPath()
2368:   {
2369:     return selectionModel.getSelectionPath();
2370:   }
2371: 
2372:   public TreePath[] getSelectionPaths()
2373:   {
2374:     return selectionModel.getSelectionPaths();
2375:   }
2376: 
2377:   public int[] getSelectionRows()
2378:   {
2379:     return selectionModel.getSelectionRows();
2380:   }
2381: 
2382:   public boolean isPathSelected(TreePath path)
2383:   {
2384:     return selectionModel.isPathSelected(path);
2385:   }
2386: 
2387:   public boolean isRowSelected(int row)
2388:   {
2389:     return selectionModel.isPathSelected(getPathForRow(row));
2390:   }
2391: 
2392:   public boolean isSelectionEmpty()
2393:   {
2394:     return selectionModel.isSelectionEmpty();
2395:   }
2396: 
2397:   /**
2398:    * Return the value of the <code>dragEnabled</code> property.
2399:    * 
2400:    * @return the value
2401:    * 
2402:    * @since 1.4
2403:    */
2404:   public boolean getDragEnabled()
2405:   {
2406:     return dragEnabled;
2407:   }
2408: 
2409:   /**
2410:    * Set the <code>dragEnabled</code> property.
2411:    * 
2412:    * @param enabled new value
2413:    * 
2414:    * @since 1.4
2415:    */
2416:   public void setDragEnabled(boolean enabled)
2417:   {
2418:     dragEnabled = enabled;
2419:   }
2420: 
2421:   public int getRowCount()
2422:   {
2423:     TreeUI ui = getUI();
2424: 
2425:     if (ui != null)
2426:       return ui.getRowCount(this);
2427: 
2428:     return 0;
2429:   }
2430: 
2431:   public void collapsePath(TreePath path)
2432:   {
2433:     try
2434:       {
2435:         fireTreeWillCollapse(path);
2436:       }
2437:     catch (ExpandVetoException ev)
2438:       {
2439:         // We do nothing if attempt has been vetoed.
2440:       }
2441:     setExpandedState(path, false);
2442:     fireTreeCollapsed(path);
2443:   }
2444: 
2445:   public void collapseRow(int row)
2446:   {
2447:     if (row < 0 || row >= getRowCount())
2448:       return;
2449: 
2450:     TreePath path = getPathForRow(row);
2451: 
2452:     if (path != null)
2453:       collapsePath(path);
2454:   }
2455: 
2456:   public void expandPath(TreePath path)
2457:   {
2458:     // Don't expand if path is null
2459:     // or is already expanded.
2460:     if (path == null || isExpanded(path))
2461:       return;
2462: 
2463:     try
2464:       {
2465:         fireTreeWillExpand(path);
2466:       }
2467:     catch (ExpandVetoException ev)
2468:       {
2469:         // We do nothing if attempt has been vetoed.
2470:       }
2471: 
2472:     setExpandedState(path, true);
2473:     fireTreeExpanded(path);
2474:   }
2475: 
2476:   public void expandRow(int row)
2477:   {
2478:     if (row < 0 || row >= getRowCount())
2479:       return;
2480: 
2481:     TreePath path = getPathForRow(row);
2482: 
2483:     if (path != null)
2484:       expandPath(path);
2485:   }
2486: 
2487:   public boolean isCollapsed(TreePath path)
2488:   {
2489:     return !isExpanded(path);
2490:   }
2491: 
2492:   public boolean isCollapsed(int row)
2493:   {
2494:     if (row < 0 || row >= getRowCount())
2495:       return false;
2496: 
2497:     TreePath path = getPathForRow(row);
2498: 
2499:     if (path != null)
2500:       return isCollapsed(path);
2501: 
2502:     return false;
2503:   }
2504: 
2505:   public boolean isExpanded(TreePath path)
2506:   {
2507:     if (path == null)
2508:       return false;
2509: 
2510:     Object state = nodeStates.get(path);
2511: 
2512:     if ((state == null) || (state != EXPANDED))
2513:       return false;
2514: 
2515:     TreePath parent = path.getParentPath();
2516: 
2517:     if (parent != null)
2518:       return isExpanded(parent);
2519: 
2520:     return true;
2521:   }
2522: 
2523:   public boolean isExpanded(int row)
2524:   {
2525:     if (row < 0 || row >= getRowCount())
2526:       return false;
2527: 
2528:     TreePath path = getPathForRow(row);
2529: 
2530:     if (path != null)
2531:       return isExpanded(path);
2532: 
2533:     return false;
2534:   }
2535: 
2536:   /**
2537:    * @since 1.3
2538:    */
2539:   public boolean getExpandsSelectedPaths()
2540:   {
2541:     return expandsSelectedPaths;
2542:   }
2543: 
2544:   /**
2545:    * @since 1.3
2546:    */
2547:   public void setExpandsSelectedPaths(boolean flag)
2548:   {
2549:     if (expandsSelectedPaths == flag)
2550:       return;
2551: 
2552:     boolean oldValue = expandsSelectedPaths;
2553:     expandsSelectedPaths = flag;
2554:     firePropertyChange(EXPANDS_SELECTED_PATHS_PROPERTY, oldValue, flag);
2555:   }
2556: 
2557:   public Rectangle getPathBounds(TreePath path)
2558:   {
2559:     TreeUI ui = getUI();
2560: 
2561:     if (ui == null)
2562:       return null;
2563: 
2564:     return ui.getPathBounds(this, path);
2565:   }
2566: 
2567:   public Rectangle getRowBounds(int row)
2568:   {
2569:     TreePath path = getPathForRow(row);
2570: 
2571:     if (path != null)
2572:       return getPathBounds(path);
2573: 
2574:     return null;
2575:   }
2576: 
2577:   public boolean isEditing()
2578:   {
2579:     TreeUI ui = getUI();
2580: 
2581:     if (ui != null)
2582:       return ui.isEditing(this);
2583: 
2584:     return false;
2585:   }
2586: 
2587:   public boolean stopEditing()
2588:   {
2589:     TreeUI ui = getUI();
2590: 
2591:     if (isEditing())
2592:       if (ui != null)
2593:         return ui.stopEditing(this);
2594: 
2595:     return false;
2596:   }
2597: 
2598:   public void cancelEditing()
2599:   {
2600:     TreeUI ui = getUI();
2601:       
2602:     if (isEditing())
2603:       if (ui != null)
2604:         ui.cancelEditing(this);
2605:   }
2606: 
2607:   public void startEditingAtPath(TreePath path)
2608:   {
2609:     TreeUI ui = getUI();
2610: 
2611:     if (ui != null)
2612:       ui.startEditingAtPath(this, path);
2613:   }
2614: 
2615:   public TreePath getEditingPath()
2616:   {
2617:     TreeUI ui = getUI();
2618: 
2619:     if (ui != null)
2620:       return ui.getEditingPath(this);
2621: 
2622:     return null;
2623:   }
2624: 
2625:   public TreePath getPathForLocation(int x, int y)
2626:   {
2627:     TreePath path = getClosestPathForLocation(x, y);
2628: 
2629:     if (path != null)
2630:       {
2631:         Rectangle rect = getPathBounds(path);
2632: 
2633:         if ((rect != null) && rect.contains(x, y))
2634:           return path;
2635:       }
2636: 
2637:     return null;
2638:   }
2639: 
2640:   public int getRowForLocation(int x, int y)
2641:   {
2642:     TreePath path = getPathForLocation(x, y);
2643: 
2644:     if (path != null)
2645:       return getRowForPath(path);
2646: 
2647:     return -1;
2648:   }
2649: 
2650:   public TreePath getClosestPathForLocation(int x, int y)
2651:   {
2652:     TreeUI ui = getUI();
2653: 
2654:     if (ui != null)
2655:       return ui.getClosestPathForLocation(this, x, y);
2656: 
2657:     return null;
2658:   }
2659: 
2660:   public int getClosestRowForLocation(int x, int y)
2661:   {
2662:     TreePath path = getClosestPathForLocation(x, y);
2663: 
2664:     if (path != null)
2665:       return getRowForPath(path);
2666: 
2667:     return -1;
2668:   }
2669: 
2670:   public Object getLastSelectedPathComponent()
2671:   {
2672:     TreePath path = getSelectionPath();
2673: 
2674:     if (path != null)
2675:       return path.getLastPathComponent();
2676: 
2677:     return null;
2678:   }
2679: 
2680:   private void doExpandParents(TreePath path, boolean state)
2681:   {
2682:     TreePath parent = path.getParentPath();        
2683: 
2684:     if (!isExpanded(parent) && parent != null)
2685:       doExpandParents(parent, false);
2686:     
2687:     nodeStates.put(path, state ? EXPANDED : COLLAPSED);
2688:   }
2689: 
2690:   protected void setExpandedState(TreePath path, boolean state)
2691:   {
2692:     if (path == null)
2693:       return;
2694: 
2695:     doExpandParents(path, state);
2696:   }
2697: 
2698:   protected void clearToggledPaths()
2699:   {
2700:     nodeStates.clear();
2701:   }
2702: 
2703:   protected Enumeration getDescendantToggledPaths(TreePath parent)
2704:   {
2705:     if (parent == null)
2706:       return null;
2707: 
2708:     Enumeration nodes = nodeStates.keys();
2709:     Vector result = new Vector();
2710: 
2711:     while (nodes.hasMoreElements())
2712:       {
2713:         TreePath path = (TreePath) nodes.nextElement();
2714: 
2715:         if (path.isDescendant(parent))
2716:           result.addElement(path);
2717:       }
2718: 
2719:     return result.elements();
2720:   }
2721: 
2722:   public boolean hasBeenExpanded(TreePath path)
2723:   {
2724:     if (path == null)
2725:       return false;
2726: 
2727:     return nodeStates.get(path) != null;
2728:   }
2729: 
2730:   public boolean isVisible(TreePath path)
2731:   {
2732:     if (path == null)
2733:       return false;
2734: 
2735:     TreePath parent = path.getParentPath();
2736: 
2737:     if (parent == null)
2738:       return true; // Is root node.
2739: 
2740:     return isExpanded(parent);
2741:   }
2742: 
2743:   public void makeVisible(TreePath path)
2744:   {
2745:     if (path == null)
2746:       return;
2747:     
2748:     expandPath(path.getParentPath());
2749:   }
2750: 
2751:   public boolean isPathEditable(TreePath path)
2752:   {
2753:     return isEditable();
2754:   }
2755: 
2756:   /**
2757:    * Creates and returns an instance of {@link TreeModelHandler}.
2758:    * 
2759:    * @return an instance of {@link TreeModelHandler}
2760:    */
2761:   protected TreeModelListener createTreeModelListener()
2762:   {
2763:     return new TreeModelHandler();
2764:   }
2765: 
2766:   /**
2767:    * Returns a sample TreeModel that can be used in a JTree. This can be used
2768:    * in Bean- or GUI-Builders to show something interesting.
2769:    * 
2770:    * @return a sample TreeModel that can be used in a JTree
2771:    */
2772:   protected static TreeModel getDefaultTreeModel()
2773:   {
2774:     DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root node");
2775:     DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child node 1");
2776:     DefaultMutableTreeNode child11 =
2777:       new DefaultMutableTreeNode("Child node 1.1");
2778:     DefaultMutableTreeNode child12 =
2779:       new DefaultMutableTreeNode("Child node 1.2");
2780:     DefaultMutableTreeNode child13 =
2781:       new DefaultMutableTreeNode("Child node 1.3");
2782:     DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child node 2");
2783:     DefaultMutableTreeNode child21 =
2784:       new DefaultMutableTreeNode("Child node 2.1");
2785:     DefaultMutableTreeNode child22 =
2786:       new DefaultMutableTreeNode("Child node 2.2");
2787:     DefaultMutableTreeNode child23 =
2788:       new DefaultMutableTreeNode("Child node 2.3");
2789:     DefaultMutableTreeNode child24 =
2790:       new DefaultMutableTreeNode("Child node 2.4");
2791: 
2792:     DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("Child node 3");
2793:     root.add(child1);
2794:     root.add(child2);
2795:     root.add(child3);
2796:     child1.add(child11);
2797:     child1.add(child12);
2798:     child1.add(child13);
2799:     child2.add(child21);
2800:     child2.add(child22);
2801:     child2.add(child23);
2802:     child2.add(child24);
2803:     return new DefaultTreeModel(root);
2804:   }
2805: 
2806:   /**
2807:    * Converts the specified value to a String. This is used by the renderers
2808:    * of this JTree and its nodes.
2809:    * 
2810:    * This implementation simply returns <code>value.toString()</code> and
2811:    * ignores all other parameters. Subclass this method to control the
2812:    * conversion.
2813:    * 
2814:    * @param value the value that is converted to a String
2815:    * @param selected indicates if that value is selected or not
2816:    * @param expanded indicates if that value is expanded or not
2817:    * @param leaf indicates if that value is a leaf node or not
2818:    * @param row the row of the node
2819:    * @param hasFocus indicates if that node has focus or not
2820:    */
2821:   public String convertValueToText(Object value, boolean selected,
2822:                                    boolean expanded, boolean leaf, int row, boolean hasFocus)
2823:   {
2824:     return value.toString();
2825:   }
2826: 
2827:   /**
2828:    * A String representation of this JTree. This is intended to be used for
2829:    * debugging. The returned string may be empty but may not be
2830:    * <code>null</code>.
2831:    * 
2832:    * @return a String representation of this JTree
2833:    */
2834:   protected String paramString()
2835:   {
2836:     // TODO: this is completely legal, but it would possibly be nice
2837:     // to return some more content, like the tree structure, some properties
2838:     // etc ...
2839:     return "";
2840:   }
2841: 
2842:   /**
2843:    * Returns all TreePath objects which are a descendants of the given path
2844:    * and are exapanded at the moment of the execution of this method. If the
2845:    * state of any node is beeing toggled while this method is executing this
2846:    * change may be left unaccounted.
2847:    * 
2848:    * @param path The parent of this request
2849:    *
2850:    * @return An Enumeration containing TreePath objects
2851:    */
2852:   public Enumeration getExpandedDescendants(TreePath path)
2853:   {
2854:     Enumeration paths = nodeStates.keys();
2855:     Vector relevantPaths = new Vector();
2856:     while (paths.hasMoreElements())
2857:       {
2858:         TreePath nextPath = (TreePath) paths.nextElement();
2859:         if (nodeStates.get(nextPath) == EXPANDED
2860:             && path.isDescendant(nextPath))
2861:           {
2862:             relevantPaths.add(nextPath);
2863:           }
2864:       }
2865:     return relevantPaths.elements();
2866:   }
2867: 
2868:   /**
2869:    * Returns the next table element (beginning from the row
2870:    * <code>startingRow</code> that starts with <code>prefix</code>.
2871:    * Searching is done in the direction specified by <code>bias</code>.
2872:    * 
2873:    * @param prefix the prefix to search for in the cell values
2874:    * @param startingRow the index of the row where to start searching from
2875:    * @param bias the search direction, either {@link Position.Bias#Forward} or
2876:    *        {@link Position.Bias#Backward}
2877:    * 
2878:    * @return the path to the found element or -1 if no such element has been
2879:    *         found
2880:    * 
2881:    * @throws IllegalArgumentException if prefix is <code>null</code> or
2882:    *         startingRow is not valid
2883:    * 
2884:    * @since 1.4
2885:    */
2886:   public TreePath getNextMatch(String prefix, int startingRow,
2887:                                Position.Bias bias)
2888:   {
2889:     if (prefix == null)
2890:       throw new IllegalArgumentException("The argument 'prefix' must not be"
2891:                                          + " null.");
2892:     if (startingRow < 0)
2893:       throw new IllegalArgumentException("The argument 'startingRow' must not"
2894:                                          + " be less than zero.");
2895: 
2896:     int size = getRowCount();
2897:     if (startingRow > size)
2898:       throw new IllegalArgumentException("The argument 'startingRow' must not"
2899:                                          + " be greater than the number of"
2900:                                          + " elements in the TreeModel.");
2901: 
2902:     TreePath foundPath = null;
2903:     if (bias == Position.Bias.Forward)
2904:       {
2905:         for (int i = startingRow; i < size; i++)
2906:           {
2907:             TreePath path = getPathForRow(i);
2908:             Object o = path.getLastPathComponent();
2909:             // FIXME: in the following call to convertValueToText the
2910:             // last argument (hasFocus) should be done right.
2911:             String item = convertValueToText(o, isRowSelected(i),
2912:                                              isExpanded(i), treeModel.isLeaf(o),
2913:                                              i, false);
2914:             if (item.startsWith(prefix))
2915:               {
2916:                 foundPath = path;
2917:                 break;
2918:               }
2919:           }
2920:       }
2921:     else
2922:       {
2923:         for (int i = startingRow; i >= 0; i--)
2924:           {
2925:             TreePath path = getPathForRow(i);
2926:             Object o = path.getLastPathComponent();
2927:             // FIXME: in the following call to convertValueToText the
2928:             // last argument (hasFocus) should be done right.
2929:             String item = convertValueToText(o, isRowSelected(i),
2930:                                              isExpanded(i), treeModel.isLeaf(o), i, false);
2931:             if (item.startsWith(prefix))
2932:               {
2933:                 foundPath = path;
2934:                 break;
2935:               }
2936:           }
2937:       }
2938:     return foundPath;
2939:   }
2940: 
2941:   /**
2942:    * Removes any paths in the current set of selected paths that are
2943:    * descendants of <code>path</code>. If <code>includePath</code> is set
2944:    * to <code>true</code> and <code>path</code> itself is selected, then
2945:    * it will be removed too.
2946:    * 
2947:    * @param path the path from which selected descendants are to be removed
2948:    * @param includeSelected if <code>true</code> then <code>path</code> itself
2949:    *        will also be remove if it's selected
2950:    * 
2951:    * @return <code>true</code> if something has been removed,
2952:    *         <code>false</code> otherwise
2953:    * 
2954:    * @since 1.3
2955:    */
2956:   protected boolean removeDescendantSelectedPaths(TreePath path,
2957:                                                   boolean includeSelected)
2958:   {
2959:     boolean removedSomething = false;
2960:     TreePath[] selected = getSelectionPaths();
2961:     for (int index = 0; index < selected.length; index++)
2962:       {
2963:         if ((selected[index] == path && includeSelected)
2964:             || (selected[index].isDescendant(path)))
2965:           {
2966:             removeSelectionPath(selected[index]);
2967:             removedSomething = true;
2968:           }
2969:       }
2970:     return removedSomething;
2971:   }
2972:   
2973:   /**
2974:    * Removes any descendants of the TreePaths in toRemove that have been 
2975:    * expanded.
2976:    * 
2977:    * @param toRemove - Enumeration of TreePaths that need to be removed from
2978:    * cache of toggled tree paths.
2979:    */
2980:   protected void removeDescendantToggledPaths(Enumeration toRemove)
2981:   {
2982:     while (toRemove.hasMoreElements())
2983:       {
2984:         TreePath current = (TreePath) toRemove.nextElement();
2985:         Enumeration descendants = getDescendantToggledPaths(current);
2986:         
2987:         while (descendants.hasMoreElements())
2988:           {
2989:             TreePath currentDes = (TreePath) descendants.nextElement();
2990:             if (isExpanded(currentDes))
2991:                 nodeStates.remove(currentDes);
2992:           }
2993:       }
2994:   }
2995: 
2996:   /**
2997:    * <p>
2998:    * Sent when the tree has changed enough that we need to resize the bounds,
2999:    * but not enough that we need to remove the expanded node set (e.g nodes were
3000:    * expanded or collapsed, or nodes were inserted into the tree). You should
3001:    * never have to invoke this, the UI will invoke this as it needs to.
3002:    * </p>
3003:    * <p>
3004:    * If the tree uses {@link DefaultTreeModel}, you must call
3005:    * {@link DefaultTreeModel#reload(TreeNode)} or
3006:    * {@link DefaultTreeModel#reload()} after adding or removing nodes. Following
3007:    * the official Java 1.5 API standard, just calling treeDidChange, repaint()
3008:    * or revalidate() does <i>not</i> update the tree appearance properly.
3009:    * 
3010:    * @see DefaultTreeModel#reload()
3011:    */
3012:   public void treeDidChange()
3013:   {
3014:     repaint();
3015:   }
3016: 
3017:   /**
3018:    * Helper method for
3019:    * {@link LookAndFeel#installProperty(JComponent, String, Object)}.
3020:    * 
3021:    * @param propertyName the name of the property
3022:    * @param value the value of the property
3023:    *
3024:    * @throws IllegalArgumentException if the specified property cannot be set
3025:    *         by this method
3026:    * @throws ClassCastException if the property value does not match the
3027:    *         property type
3028:    * @throws NullPointerException if <code>c</code> or
3029:    *         <code>propertyValue</code> is <code>null</code>
3030:    */
3031:   void setUIProperty(String propertyName, Object value)
3032:   {
3033:     if (propertyName.equals("rowHeight"))
3034:       {
3035:         if (! clientRowHeightSet)
3036:           {
3037:             setRowHeight(((Integer) value).intValue());
3038:             clientRowHeightSet = false;
3039:           }
3040:       }
3041:     else if (propertyName.equals("scrollsOnExpand"))
3042:       {
3043:         if (! clientScrollsOnExpandSet)
3044:           {
3045:             setScrollsOnExpand(((Boolean) value).booleanValue());
3046:             clientScrollsOnExpandSet = false;
3047:           }
3048:       }
3049:     else if (propertyName.equals("showsRootHandles"))
3050:       {
3051:         if (! clientShowsRootHandlesSet)
3052:           {
3053:             setShowsRootHandles(((Boolean) value).booleanValue());
3054:             clientShowsRootHandlesSet = false;
3055:           }
3056:       }
3057:     else
3058:       {
3059:         super.setUIProperty(propertyName, value);
3060:       }
3061:   }
3062: }