| 1 7 8 package javax.swing.tree; 9 11 import java.io.*; 12 import java.util.*; 13 14 15 70 public class DefaultMutableTreeNode extends Object implements Cloneable , 71 MutableTreeNode , Serializable 72 { 73 74 78 static public final Enumeration<TreeNode > EMPTY_ENUMERATION 79 = new Enumeration<TreeNode >() { 80 public boolean hasMoreElements() { return false; } 81 public TreeNode nextElement() { 82 throw new NoSuchElementException("No more elements"); 83 } 84 }; 85 86 87 protected MutableTreeNode parent; 88 89 90 protected Vector children; 91 92 93 transient protected Object userObject; 94 95 96 protected boolean allowsChildren; 97 98 99 103 public DefaultMutableTreeNode() { 104 this(null); 105 } 106 107 114 public DefaultMutableTreeNode(Object userObject) { 115 this(userObject, true); 116 } 117 118 128 public DefaultMutableTreeNode(Object userObject, boolean allowsChildren) { 129 super(); 130 parent = null; 131 this.allowsChildren = allowsChildren; 132 this.userObject = userObject; 133 } 134 135 136 140 159 public void insert(MutableTreeNode newChild, int childIndex) { 160 if (!allowsChildren) { 161 throw new IllegalStateException ("node does not allow children"); 162 } else if (newChild == null) { 163 throw new IllegalArgumentException ("new child is null"); 164 } else if (isNodeAncestor(newChild)) { 165 throw new IllegalArgumentException ("new child is an ancestor"); 166 } 167 168 MutableTreeNode oldParent = (MutableTreeNode )newChild.getParent(); 169 170 if (oldParent != null) { 171 oldParent.remove(newChild); 172 } 173 newChild.setParent(this); 174 if (children == null) { 175 children = new Vector(); 176 } 177 children.insertElementAt(newChild, childIndex); 178 } 179 180 190 public void remove(int childIndex) { 191 MutableTreeNode child = (MutableTreeNode )getChildAt(childIndex); 192 children.removeElementAt(childIndex); 193 child.setParent(null); 194 } 195 196 205 public void setParent(MutableTreeNode newParent) { 206 parent = newParent; 207 } 208 209 214 public TreeNode getParent() { 215 return parent; 216 } 217 218 226 public TreeNode getChildAt(int index) { 227 if (children == null) { 228 throw new ArrayIndexOutOfBoundsException ("node has no children"); 229 } 230 return (TreeNode )children.elementAt(index); 231 } 232 233 238 public int getChildCount() { 239 if (children == null) { 240 return 0; 241 } else { 242 return children.size(); 243 } 244 } 245 246 259 public int getIndex(TreeNode aChild) { 260 if (aChild == null) { 261 throw new IllegalArgumentException ("argument is null"); 262 } 263 264 if (!isNodeChild(aChild)) { 265 return -1; 266 } 267 return children.indexOf(aChild); } 269 270 277 public Enumeration children() { 278 if (children == null) { 279 return EMPTY_ENUMERATION; 280 } else { 281 return children.elements(); 282 } 283 } 284 285 294 public void setAllowsChildren(boolean allows) { 295 if (allows != allowsChildren) { 296 allowsChildren = allows; 297 if (!allowsChildren) { 298 removeAllChildren(); 299 } 300 } 301 } 302 303 308 public boolean getAllowsChildren() { 309 return allowsChildren; 310 } 311 312 320 public void setUserObject(Object userObject) { 321 this.userObject = userObject; 322 } 323 324 331 public Object getUserObject() { 332 return userObject; 333 } 334 335 336 340 345 public void removeFromParent() { 346 MutableTreeNode parent = (MutableTreeNode )getParent(); 347 if (parent != null) { 348 parent.remove(this); 349 } 350 } 351 352 360 public void remove(MutableTreeNode aChild) { 361 if (aChild == null) { 362 throw new IllegalArgumentException ("argument is null"); 363 } 364 365 if (!isNodeChild(aChild)) { 366 throw new IllegalArgumentException ("argument is not a child"); 367 } 368 remove(getIndex(aChild)); } 370 371 375 public void removeAllChildren() { 376 for (int i = getChildCount()-1; i >= 0; i--) { 377 remove(i); 378 } 379 } 380 381 392 public void add(MutableTreeNode newChild) { 393 if(newChild != null && newChild.getParent() == this) 394 insert(newChild, getChildCount() - 1); 395 else 396 insert(newChild, getChildCount()); 397 } 398 399 400 401 405 418 public boolean isNodeAncestor(TreeNode anotherNode) { 419 if (anotherNode == null) { 420 return false; 421 } 422 423 TreeNode ancestor = this; 424 425 do { 426 if (ancestor == anotherNode) { 427 return true; 428 } 429 } while((ancestor = ancestor.getParent()) != null); 430 431 return false; 432 } 433 434 447 public boolean isNodeDescendant(DefaultMutableTreeNode anotherNode) { 448 if (anotherNode == null) 449 return false; 450 451 return anotherNode.isNodeAncestor(this); 452 } 453 454 466 public TreeNode getSharedAncestor(DefaultMutableTreeNode aNode) { 467 if (aNode == this) { 468 return this; 469 } else if (aNode == null) { 470 return null; 471 } 472 473 int level1, level2, diff; 474 TreeNode node1, node2; 475 476 level1 = getLevel(); 477 level2 = aNode.getLevel(); 478 479 if (level2 > level1) { 480 diff = level2 - level1; 481 node1 = aNode; 482 node2 = this; 483 } else { 484 diff = level1 - level2; 485 node1 = this; 486 node2 = aNode; 487 } 488 489 while (diff > 0) { 491 node1 = node1.getParent(); 492 diff--; 493 } 494 495 500 do { 501 if (node1 == node2) { 502 return node1; 503 } 504 node1 = node1.getParent(); 505 node2 = node2.getParent(); 506 } while (node1 != null); 509 if (node1 != null || node2 != null) { 510 throw new Error ("nodes should be null"); 511 } 512 513 return null; 514 } 515 516 517 526 public boolean isNodeRelated(DefaultMutableTreeNode aNode) { 527 return (aNode != null) && (getRoot() == aNode.getRoot()); 528 } 529 530 531 541 public int getDepth() { 542 Object last = null; 543 Enumeration enum_ = breadthFirstEnumeration(); 544 545 while (enum_.hasMoreElements()) { 546 last = enum_.nextElement(); 547 } 548 549 if (last == null) { 550 throw new Error ("nodes should be null"); 551 } 552 553 return ((DefaultMutableTreeNode )last).getLevel() - getLevel(); 554 } 555 556 557 558 565 public int getLevel() { 566 TreeNode ancestor; 567 int levels = 0; 568 569 ancestor = this; 570 while((ancestor = ancestor.getParent()) != null){ 571 levels++; 572 } 573 574 return levels; 575 } 576 577 578 586 public TreeNode [] getPath() { 587 return getPathToRoot(this, 0); 588 } 589 590 602 protected TreeNode [] getPathToRoot(TreeNode aNode, int depth) { 603 TreeNode [] retNodes; 604 605 607 if(aNode == null) { 608 if(depth == 0) 609 return null; 610 else 611 retNodes = new TreeNode [depth]; 612 } 613 else { 614 depth++; 615 retNodes = getPathToRoot(aNode.getParent(), depth); 616 retNodes[retNodes.length - depth] = aNode; 617 } 618 return retNodes; 619 } 620 621 626 public Object [] getUserObjectPath() { 627 TreeNode [] realPath = getPath(); 628 Object [] retPath = new Object [realPath.length]; 629 630 for(int counter = 0; counter < realPath.length; counter++) 631 retPath[counter] = ((DefaultMutableTreeNode )realPath[counter]) 632 .getUserObject(); 633 return retPath; 634 } 635 636 643 public TreeNode getRoot() { 644 TreeNode ancestor = this; 645 TreeNode previous; 646 647 do { 648 previous = ancestor; 649 ancestor = ancestor.getParent(); 650 } while (ancestor != null); 651 652 return previous; 653 } 654 655 656 663 public boolean isRoot() { 664 return getParent() == null; 665 } 666 667 668 678 public DefaultMutableTreeNode getNextNode() { 679 if (getChildCount() == 0) { 680 DefaultMutableTreeNode nextSibling = getNextSibling(); 682 683 if (nextSibling == null) { 684 DefaultMutableTreeNode aNode = (DefaultMutableTreeNode )getParent(); 685 686 do { 687 if (aNode == null) { 688 return null; 689 } 690 691 nextSibling = aNode.getNextSibling(); 692 if (nextSibling != null) { 693 return nextSibling; 694 } 695 696 aNode = (DefaultMutableTreeNode )aNode.getParent(); 697 } while(true); 698 } else { 699 return nextSibling; 700 } 701 } else { 702 return (DefaultMutableTreeNode )getChildAt(0); 703 } 704 } 705 706 707 718 public DefaultMutableTreeNode getPreviousNode() { 719 DefaultMutableTreeNode previousSibling; 720 DefaultMutableTreeNode myParent = (DefaultMutableTreeNode )getParent(); 721 722 if (myParent == null) { 723 return null; 724 } 725 726 previousSibling = getPreviousSibling(); 727 728 if (previousSibling != null) { 729 if (previousSibling.getChildCount() == 0) 730 return previousSibling; 731 else 732 return previousSibling.getLastLeaf(); 733 } else { 734 return myParent; 735 } 736 } 737 738 749 public Enumeration preorderEnumeration() { 750 return new PreorderEnumeration(this); 751 } 752 753 766 public Enumeration postorderEnumeration() { 767 return new PostorderEnumeration(this); 768 } 769 770 781 public Enumeration breadthFirstEnumeration() { 782 return new BreadthFirstEnumeration(this); 783 } 784 785 798 public Enumeration depthFirstEnumeration() { 799 return postorderEnumeration(); 800 } 801 802 822 public Enumeration pathFromAncestorEnumeration(TreeNode ancestor) { 823 return new PathBetweenNodesEnumeration(ancestor, this); 824 } 825 826 827 831 838 public boolean isNodeChild(TreeNode aNode) { 839 boolean retval; 840 841 if (aNode == null) { 842 retval = false; 843 } else { 844 if (getChildCount() == 0) { 845 retval = false; 846 } else { 847 retval = (aNode.getParent() == this); 848 } 849 } 850 851 return retval; 852 } 853 854 855 862 public TreeNode getFirstChild() { 863 if (getChildCount() == 0) { 864 throw new NoSuchElementException("node has no children"); 865 } 866 return getChildAt(0); 867 } 868 869 870 877 public TreeNode getLastChild() { 878 if (getChildCount() == 0) { 879 throw new NoSuchElementException("node has no children"); 880 } 881 return getChildAt(getChildCount()-1); 882 } 883 884 885 899 public TreeNode getChildAfter(TreeNode aChild) { 900 if (aChild == null) { 901 throw new IllegalArgumentException ("argument is null"); 902 } 903 904 int index = getIndex(aChild); 906 if (index == -1) { 907 throw new IllegalArgumentException ("node is not a child"); 908 } 909 910 if (index < getChildCount() - 1) { 911 return getChildAt(index + 1); 912 } else { 913 return null; 914 } 915 } 916 917 918 930 public TreeNode getChildBefore(TreeNode aChild) { 931 if (aChild == null) { 932 throw new IllegalArgumentException ("argument is null"); 933 } 934 935 int index = getIndex(aChild); 937 if (index == -1) { 938 throw new IllegalArgumentException ("argument is not a child"); 939 } 940 941 if (index > 0) { 942 return getChildAt(index - 1); 943 } else { 944 return null; 945 } 946 } 947 948 949 953 954 962 public boolean isNodeSibling(TreeNode anotherNode) { 963 boolean retval; 964 965 if (anotherNode == null) { 966 retval = false; 967 } else if (anotherNode == this) { 968 retval = true; 969 } else { 970 TreeNode myParent = getParent(); 971 retval = (myParent != null && myParent == anotherNode.getParent()); 972 973 if (retval && !((DefaultMutableTreeNode )getParent()) 974 .isNodeChild(anotherNode)) { 975 throw new Error ("sibling has different parent"); 976 } 977 } 978 979 return retval; 980 } 981 982 983 990 public int getSiblingCount() { 991 TreeNode myParent = getParent(); 992 993 if (myParent == null) { 994 return 1; 995 } else { 996 return myParent.getChildCount(); 997 } 998 } 999 1000 1001 |