source: trunk/gcc/libjava/javax/swing/tree/DefaultMutableTreeNode.java

Last change on this file was 1389, checked in by bird, 21 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 19.8 KB
Line 
1/* DefaultMutableTreeNode.java --
2 Copyright (C) 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38package javax.swing.tree;
39
40// Imports
41import java.io.*;
42import java.util.*;
43
44/**
45 * DefaultMutableTreeNode
46 * @author Andrew Selkirk
47 */
48public class DefaultMutableTreeNode implements Cloneable, MutableTreeNode, Serializable {
49
50 //-------------------------------------------------------------
51 // Variables --------------------------------------------------
52 //-------------------------------------------------------------
53
54 /**
55 * EMPTY_ENUMERATION
56 */
57 public static final Enumeration EMPTY_ENUMERATION = null; // TODO
58
59 /**
60 * parent
61 */
62 protected MutableTreeNode parent = null;
63
64 /**
65 * children
66 */
67 protected Vector children = new Vector();
68
69 /**
70 * userObject
71 */
72 protected transient Object userObject = "";
73
74 /**
75 * allowsChildren
76 */
77 protected boolean allowsChildren = true;
78
79
80 //-------------------------------------------------------------
81 // Initialization ---------------------------------------------
82 //-------------------------------------------------------------
83
84 /**
85 * Constructor DefaultMutableTreeNode
86 */
87 public DefaultMutableTreeNode() {
88 // TODO
89 } // DefaultMutableTreeNode()
90
91 /**
92 * Constructor DefaultMutableTreeNode
93 * @param value0 TODO
94 */
95 public DefaultMutableTreeNode(Object userObject) {
96 this.userObject = userObject;
97 } // DefaultMutableTreeNode()
98
99 /**
100 * Constructor DefaultMutableTreeNode
101 * @param value0 TODO
102 * @param value1 TODO
103 */
104 public DefaultMutableTreeNode(Object userObject, boolean allowsChildren) {
105 this.userObject = userObject;
106 this.allowsChildren = allowsChildren;
107 } // DefaultMutableTreeNode()
108
109
110 //-------------------------------------------------------------
111 // Methods ----------------------------------------------------
112 //-------------------------------------------------------------
113
114 /**
115 * clone
116 * @returns Object
117 */
118 public Object clone() {
119 return null; // TODO
120 } // clone()
121
122 /**
123 * toString
124 * @returns String
125 */
126 public String toString() {
127 if (userObject == null) {
128 return null;
129 } // if
130 return userObject.toString();
131 } // toString()
132
133 /**
134 * add
135 * @param value0 TODO
136 */
137 public void add(MutableTreeNode child) {
138 children.add(child);
139 child.setParent(this);
140 } // add()
141
142 /**
143 * getParent
144 * @returns TreeNode
145 */
146 public TreeNode getParent() {
147 return parent;
148 } // getParent()
149
150 /**
151 * remove
152 * @param value0 TODO
153 */
154 public void remove(int index) {
155 children.remove(index);
156 } // remove()
157
158 /**
159 * remove
160 * @param value0 TODO
161 */
162 public void remove(MutableTreeNode node) {
163 children.remove(node);
164 } // remove()
165
166 /**
167 * writeObject
168 * @param value0 TODO
169 * @exception IOException TODO
170 */
171 private void writeObject(ObjectOutputStream value0) throws IOException {
172 // TODO
173 } // writeObject()
174
175 /**
176 * readObject
177 * @param value0 TODO
178 * @exception IOException TODO
179 * @exception ClassNotFoundException TODO
180 */
181 private void readObject(ObjectInputStream value0) throws IOException, ClassNotFoundException {
182 // TODO
183 } // readObject()
184
185 /**
186 * insert
187 * @param value0 TODO
188 * @param value1 TODO
189 */
190 public void insert(MutableTreeNode node, int index) {
191 children.insertElementAt(node, index);
192 } // insert()
193
194 /**
195 * getPath
196 * @returns TreeNode[]
197 */
198 public TreeNode[] getPath() {
199
200 // Variables
201 TreeNode[] path;
202 int size;
203 int index;
204 TreeNode current;
205
206 // Determine length of Path
207 size = getLevel() + 1;
208
209 // Create Path
210 path = new TreeNode[size];
211 current = this;
212 for (index = size - 1; index >= 0; index--) {
213 path[index] = current;
214 current = current.getParent();
215 } // for
216
217 // Return Path
218 return path;
219
220 } // getPath()
221
222 /**
223 * children
224 * @returns Enumeration
225 */
226 public Enumeration children() {
227 return children.elements();
228 } // children()
229
230 /**
231 * setParent
232 * @param value0 TODO
233 */
234 public void setParent(MutableTreeNode node) {
235 parent = node;
236 } // setParent()
237
238 /**
239 * getChildAt
240 * @param value0 TODO
241 * @returns TreeNode
242 */
243 public TreeNode getChildAt(int index) {
244 return (TreeNode) children.elementAt(index);
245 } // getChildAt()
246
247 /**
248 * getChildCount
249 * @returns int
250 */
251 public int getChildCount() {
252 return children.size();
253 } // getChildCount()
254
255 /**
256 * getIndex
257 * @param value0 TODO
258 * @returns int
259 */
260 public int getIndex(TreeNode node) {
261 return children.indexOf(node);
262 } // getIndex()
263
264 /**
265 * setAllowsChildren
266 * @param value0 TODO
267 */
268 public void setAllowsChildren(boolean allowsChildren) {
269 this.allowsChildren = allowsChildren;
270 } // setAllowsChildren()
271
272 /**
273 * getAllowsChildren
274 * @returns boolean
275 */
276 public boolean getAllowsChildren() {
277 return allowsChildren;
278 } // getAllowsChildren()
279
280 /**
281 * setUserObject
282 * @param value0 TODO
283 */
284 public void setUserObject(Object userObject) {
285 this.userObject = userObject;
286 } // setUserObject()
287
288 /**
289 * getUserObject
290 * @returns Object
291 */
292 public Object getUserObject() {
293 return userObject;
294 } // getUserObject()
295
296 /**
297 * removeFromParent
298 */
299 public void removeFromParent() {
300 parent = null;
301 // TODO
302 } // removeFromParent()
303
304 /**
305 * removeAllChildren
306 */
307 public void removeAllChildren() {
308 children.removeAllElements();
309 } // removeAllChildren()
310
311 /**
312 * isNodeAncestor
313 * @param value0 TODO
314 * @returns boolean
315 */
316 public boolean isNodeAncestor(TreeNode node) {
317
318 // Variables
319 TreeNode current;
320
321 // Sanity Check
322 if (node == null) {
323 return false;
324 } // if
325
326 // Search For Ancestor
327 current = this;
328 while (current != null && current != node) {
329 current = current.getParent();
330 } // while
331
332 // Check for Ancestor
333 if (current == node) {
334 return true;
335 } // if
336
337 // Otherwise, no
338 return false;
339
340 } // isNodeAncestor()
341
342 /**
343 * isNodeDescendant
344 * @param value0 TODO
345 * @returns boolean
346 */
347 public boolean isNodeDescendant(DefaultMutableTreeNode node) {
348
349 // Variables
350 TreeNode current;
351
352 // Sanity Check
353 if (node == null) {
354 return false;
355 } // if
356
357 // Search For Descendant
358 current = node;
359 while (current != null && current != this) {
360 current = current.getParent();
361 } // while
362
363 // Check for Descendant
364 if (current == this) {
365 return true;
366 } // if
367
368 // Otherwise, no
369 return false;
370
371 } // isNodeDescendant()
372
373 /**
374 * getSharedAncestor
375 * @param value0 TODO
376 * @returns TreeNode
377 */
378 public TreeNode getSharedAncestor(DefaultMutableTreeNode node) {
379
380 // Variables
381 ArrayList list;
382 TreeNode current;
383
384 // Get List of Path Elements for this node
385 current = this;
386 list = new ArrayList();
387 while (current != null) {
388 list.add(current);
389 current = current.getParent();
390 } // while
391
392 // Check if any path element of node are in list
393 current = node;
394 while (current != null) {
395 if (list.contains(current) == true) {
396 return current;
397 } // if
398 current = current.getParent();
399 } // while
400
401 // Unable to locate shared ancestor
402 return null;
403
404 } // getSharedAncestor()
405
406 /**
407 * isNodeRelated
408 * @param value0 TODO
409 * @returns boolean
410 */
411 public boolean isNodeRelated(DefaultMutableTreeNode node) {
412
413 // Sanity Check
414 if (node == null) {
415 return false;
416 } // if
417
418 // Check for the same root
419 if (node.getRoot() == getRoot()) {
420 return true;
421 } // if
422
423 // Nodes are not related
424 return false;
425
426 } // isNodeRelated()
427
428 /**
429 * getDepth
430 * @returns int
431 */
432 public int getDepth() {
433
434 // Variables
435 TreeNode node;
436 int depth;
437 int current;
438 int size;
439 Stack stack;
440 int index;
441
442 // Check for children
443 if (allowsChildren == false || children.size() == 0) {
444 return 0;
445 } // if
446
447 // Process Depths
448 stack = new Stack();
449 stack.push(new Integer(0));
450 node = getChildAt(0);
451//System.out.println(" * Descend: 0-0");
452 depth = 0;
453 current = 1;
454 while (stack.empty() == false) {
455
456 // Check if node has children
457 if (node.getChildCount() != 0) {
458 node = node.getChildAt(0);
459 stack.push(new Integer(0));
460 current++;
461// System.out.println(" * Descend: 0-" + current);
462
463 // Check for next sibling
464 } else {
465
466 // Check Depth
467 if (current > depth) {
468 depth = current;
469 } // if
470
471 do {
472
473 // Traverse to Parent
474 node = node.getParent();
475 size = node.getChildCount();
476 current--;
477 index = ((Integer) stack.pop()).intValue();
478// System.out.println(" * Ascend from: " + index + "-" + current);
479 index++;
480
481 } while (index >= size && node != this);
482
483 // Check for child
484 if (index < size) {
485 node = node.getChildAt(index);
486 stack.push(new Integer(index));
487 current++;
488// System.out.println(" * Descend: " + index + "-" + current);
489 } // if
490
491 } // if
492
493 } // while
494
495 return depth;
496
497 } // getDepth()
498
499 static Random random = new Random(System.currentTimeMillis());
500
501 public static void growTree(DefaultMutableTreeNode root) {
502
503 // Variables
504 int size;
505 int index;
506 DefaultMutableTreeNode node;
507 DefaultMutableTreeNode current;
508
509 current = root;
510 index = 0;
511// while (current != root) {
512 do {
513
514// if (random.nextInt(3) < 2) {
515 if (random.nextBoolean()) {
516 node = new DefaultMutableTreeNode(String.valueOf(index));
517 index++;
518 current.add(node);
519 current = node;
520 } else {
521 current = (DefaultMutableTreeNode) current.getParent();
522 } // if
523
524// } // while
525 } while (current != root && current != null);
526
527 System.out.println("Number of nodes: " + index);
528
529/*
530 // Calc # children
531 size = random.nextInt(4);
532
533 for (index = 0; index < size; index++) {
534
535 // Create Node
536 node = new DefaultMutableTreeNode(String.valueOf(index));
537 growTree(node);
538
539 // Add Node to root
540 root.add(node);
541
542 } // for
543*/
544 } // growTree()
545
546 public static void main(String[] argv) {
547/*
548 DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("node1");
549 DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("node2");
550 DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("node3");
551 DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("node4");
552 DefaultMutableTreeNode node5 = new DefaultMutableTreeNode("node5");
553 DefaultMutableTreeNode node6 = new DefaultMutableTreeNode("node6");
554 DefaultMutableTreeNode node7 = new DefaultMutableTreeNode("node7");
555 DefaultMutableTreeNode node8 = new DefaultMutableTreeNode("node8");
556
557 node1.add(node2);
558 node1.add(node3);
559 node2.add(node4);
560 node2.add(node5);
561 node3.add(node6);
562 node3.add(node7);
563 node5.add(node8);
564
565 System.out.println("Depth (node1): " + node1.getDepth());
566 System.out.println("Depth (node2): " + node2.getDepth());
567 System.out.println("Depth (node3): " + node3.getDepth());
568*/
569
570 System.out.println("Create tree...");
571 DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
572 growTree(root);
573 System.out.println("Find depth...");
574 System.out.println("Depth (root): " + root.getDepth());
575
576 } // main
577
578 /**
579 * getLevel
580 * @returns int
581 */
582 public int getLevel() {
583
584 // Variables
585 TreeNode current;
586 int count;
587
588 // Lookup Parent
589 count = -1;
590 current = this;
591 do {
592 current = current.getParent();
593 count++;
594 } while (current != null);
595
596 return count;
597
598 } // getLevel()
599
600 /**
601 * getPathToRoot
602 * @param value0 TODO
603 * @param value1 TODO
604 * @returns TreeNode[]
605 */
606 protected TreeNode[] getPathToRoot(TreeNode value0, int value1) {
607 return null; // TODO
608 } // getPathToRoot()
609
610 /**
611 * getUserObjectPath
612 * @returns Object[]
613 */
614 public Object[] getUserObjectPath() {
615
616 // Variables
617 TreeNode[] path;
618 Object[] object;
619 int size;
620 int index;
621
622 // Get Path for Tree Nodes
623 path = getPath();
624
625 // Construct Object Path
626 object = new Object[path.length];
627 for (index = 0; index < path.length; index++) {
628 object[index] = ((DefaultMutableTreeNode) path[index]).getUserObject();
629 } // for
630
631 // Return Object Path
632 return object;
633
634 } // getUserObjectPath()
635
636 /**
637 * getRoot
638 * @returns TreeNode
639 */
640 public TreeNode getRoot() {
641
642 // Variables
643 TreeNode current;
644 TreeNode check;
645
646 // Lookup Parent
647 current = this;
648 check = current.getParent();
649 while (check != null) {
650 current = check;
651 check = current.getParent();
652 } // while
653
654 return current;
655
656 } // getRoot()
657
658 /**
659 * isRoot
660 * @returns boolean
661 */
662 public boolean isRoot() {
663 return (parent == null);
664 } // isRoot()
665
666 /**
667 * getNextNode
668 * @returns DefaultMutableTreeNode
669 */
670 public DefaultMutableTreeNode getNextNode() {
671 return null; // TODO
672 } // getNextNode()
673
674 /**
675 * getPreviousNode
676 * @returns DefaultMutableTreeNode
677 */
678 public DefaultMutableTreeNode getPreviousNode() {
679 return null; // TODO
680 } // getPreviousNode()
681
682 /**
683 * preorderEnumeration
684 * @returns Enumeration
685 */
686 public Enumeration preorderEnumeration() {
687 return null; // TODO
688 } // preorderEnumeration()
689
690 /**
691 * postorderEnumeration
692 * @returns Enumeration
693 */
694 public Enumeration postorderEnumeration() {
695 return null; // TODO
696 } // postorderEnumeration()
697
698 /**
699 * breadthFirstEnumeration
700 * @returns Enumeration
701 */
702 public Enumeration breadthFirstEnumeration() {
703 return null; // TODO
704 } // breadthFirstEnumeration()
705
706 /**
707 * depthFirstEnumeration
708 * @returns Enumeration
709 */
710 public Enumeration depthFirstEnumeration() {
711 return null; // TODO
712 } // depthFirstEnumeration()
713
714 /**
715 * pathFromAncestorEnumeration
716 * @param value0 TODO
717 * @returns Enumeration
718 */
719 public Enumeration pathFromAncestorEnumeration(TreeNode value0) {
720 return null; // TODO
721 } // pathFromAncestorEnumeration()
722
723 /**
724 * isNodeChild
725 * @param value0 TODO
726 * @returns boolean
727 */
728 public boolean isNodeChild(TreeNode node) {
729
730 // Variables
731 TreeNode current;
732 int index;
733
734 // Sanity Check
735 if (node == null) {
736 return false;
737 } // if
738
739 // Process Path
740 current = node;
741 while (current != null) {
742 if (current == this) {
743 return true;
744 } // if
745 current = current.getParent();
746 } // while
747
748 // Node not located in path, not child
749 return false;
750
751 } // isNodeChild()
752
753 /**
754 * getFirstChild
755 * @returns TreeNode
756 */
757 public TreeNode getFirstChild() {
758 return (TreeNode) children.firstElement();
759 } // getFirstChild()
760
761 /**
762 * getLastChild
763 * @returns TreeNode
764 */
765 public TreeNode getLastChild() {
766 return (TreeNode) children.lastElement();
767 } // getLastChild()
768
769 /**
770 * getChildAfter
771 * @param value0 TODO
772 * @returns TreeNode
773 */
774 public TreeNode getChildAfter(TreeNode node) {
775
776 // Variables
777 int index;
778
779 // Check node
780 if (node == null || node.getParent() != this) {
781 throw new IllegalArgumentException();
782 } // if
783
784 // Get index of child node
785 index = getIndex(node);
786
787 // Check for child after
788 index++;
789 if (index == getChildCount()) {
790 return null;
791 } // if
792
793 // Retrieve Child After
794 return getChildAt(index);
795
796 } // getChildAfter()
797
798 /**
799 * getChildBefore
800 * @param value0 TODO
801 * @returns TreeNode
802 */
803 public TreeNode getChildBefore(TreeNode node) {
804
805 // Variables
806 int index;
807
808 // Check node
809 if (node == null || node.getParent() != this) {
810 throw new IllegalArgumentException();
811 } // if
812
813 // Get index of child node
814 index = getIndex(node);
815
816 // Check for child before
817 index--;
818 if (index < 0) {
819 return null;
820 } // if
821
822 // Retrieve Child Before
823 return getChildAt(index);
824
825 } // getChildBefore()
826
827 /**
828 * isNodeSibling
829 * @param value0 TODO
830 * @returns boolean
831 */
832 public boolean isNodeSibling(TreeNode node) {
833
834 // Variables
835 int index;
836
837 // Check for null
838 if (node == null) {
839 return false;
840 } // if
841
842 // Check if nodes share a parent
843 if (node.getParent() == getParent() && getParent() != null) {
844 return true;
845 } // if
846
847 // Nodes are not siblings
848 return false;
849
850 } // isNodeSibling()
851
852 /**
853 * getSiblingCount
854 * @returns int
855 */
856 public int getSiblingCount() {
857
858 // Variables
859
860 // Check for no parent
861 if (parent == null) {
862 return 1;
863 } // if
864
865 // Calculate sibling count from parent's child count
866 return parent.getChildCount();
867
868 } // getSiblingCount()
869
870 /**
871 * getNextSibling
872 * @returns DefaultMutableTreeNode
873 */
874 public DefaultMutableTreeNode getNextSibling() {
875
876 // Variables
877 int index;
878 int size;
879
880 // Check for Parent
881 if (parent == null) {
882 return null;
883 } // if
884
885 // Get Index of this node
886 index = parent.getIndex(this);
887
888 // Check for Next Sibling
889 size = parent.getChildCount();
890 index++;
891 if (index == size) {
892 return null;
893 } // if
894
895 return (DefaultMutableTreeNode) parent.getChildAt(index);
896
897 } // getNextSibling()
898
899 /**
900 * getPreviousSibling
901 * @returns DefaultMutableTreeNode
902 */
903 public DefaultMutableTreeNode getPreviousSibling() {
904
905 // Variables
906 int index;
907
908 // Check for Parent
909 if (parent == null) {
910 return null;
911 } // if
912
913 // Get Index of this node
914 index = parent.getIndex(this);
915
916 // Check for Previous Sibling
917 index--;
918 if (index < 0) {
919 return null;
920 } // if
921
922 return (DefaultMutableTreeNode) parent.getChildAt(index);
923
924 } // getPreviousSibling()
925
926 /**
927 * isLeaf
928 * @returns boolean
929 */
930 public boolean isLeaf() {
931 return (children.size() == 0); // TODO: check allowsChildren??
932 } // isLeaf()
933
934 /**
935 * getFirstLeaf
936 * @returns DefaultMutableTreeNode
937 */
938 public DefaultMutableTreeNode getFirstLeaf() {
939
940 // Variables
941 TreeNode current;
942
943 current = this;
944 while (current.getChildCount() > 0) {
945 current = current.getChildAt(0);
946 } // while
947
948 return (DefaultMutableTreeNode) current;
949
950 } // getFirstLeaf()
951
952 /**
953 * getLastLeaf
954 * @returns DefaultMutableTreeNode
955 */
956 public DefaultMutableTreeNode getLastLeaf() {
957
958 // Variables
959 TreeNode current;
960 int size;
961
962 current = this;
963 size = current.getChildCount();
964 while (size > 0) {
965 current = current.getChildAt(size - 1);
966 size = current.getChildCount();
967 } // while
968
969 return (DefaultMutableTreeNode) current;
970
971 } // getLastLeaf()
972
973 /**
974 * getNextLeaf
975 * @returns DefaultMutableTreeNode
976 */
977 public DefaultMutableTreeNode getNextLeaf() {
978 return null; // TODO
979 } // getNextLeaf()
980
981 /**
982 * getPreviousLeaf
983 * @returns DefaultMutableTreeNode
984 */
985 public DefaultMutableTreeNode getPreviousLeaf() {
986 return null; // TODO
987 } // getPreviousLeaf()
988
989 /**
990 * getLeafCount
991 * @returns int
992 */
993 public int getLeafCount() {
994
995 // Variables
996 Enumeration enum;
997 int count;
998 TreeNode current;
999
1000 // Get Enumeration of all descendants
1001 enum = depthFirstEnumeration();
1002
1003 // Process Nodes
1004 count = 0;
1005 while (enum.hasMoreElements() == true) {
1006 current = (TreeNode) enum.nextElement();
1007 if (current.isLeaf() == true) {
1008 count++;
1009 } // if
1010 } // if
1011
1012 return count;
1013
1014 } // getLeafCount()
1015
1016
1017} // DefaultMutableTreeNode
Note: See TracBrowser for help on using the repository browser.