KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > tree > DefaultMutableTreeNode


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: DefaultMutableTreeNode.java,v $
11    Revision 1.6 2004/05/04 11:17:40 bobintetley
12    (Laurent Martell) DefaultMutableTreeNode/DefaultTreeModel fixes
13
14    Revision 1.5 2003/12/14 09:13:39 bobintetley
15    Added CVS log to source headers
16
17 */

18
19 package swingwtx.swing.tree;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.NoSuchElementException JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 public class DefaultMutableTreeNode implements MutableTreeNode {
27
28     /** The SWT TreeItem peer */
29     public org.eclipse.swt.widgets.TreeItem peer = null;
30     
31     /** An empty enumeration */
32     static public final Enumeration JavaDoc EMPTY_ENUMERATION = new Enumeration JavaDoc() {
33         public Object JavaDoc nextElement() {
34             throw new NoSuchElementException JavaDoc("No elements");
35         }
36         public boolean hasMoreElements() { return false; }
37     };
38
39     protected MutableTreeNode parent;
40     protected boolean allowsChildren;
41     protected Vector JavaDoc children;
42     protected Object JavaDoc userObject;
43
44     public DefaultMutableTreeNode() {
45     this(null);
46     }
47     
48     public DefaultMutableTreeNode(Object JavaDoc userObject) {
49     this(userObject, true);
50     }
51
52     public DefaultMutableTreeNode(Object JavaDoc userObject, boolean allowsChildren) {
53     super();
54     this.allowsChildren = allowsChildren;
55     this.userObject = userObject;
56         parent = null;
57     }
58
59     public void insert(MutableTreeNode newChild, int childIndex) {
60         MutableTreeNode oldParent = (MutableTreeNode) newChild.getParent();
61         if (oldParent != null)
62             oldParent.remove(newChild);
63         newChild.setParent(this);
64         if (children == null)
65             children = new Vector JavaDoc();
66         children.insertElementAt(newChild, childIndex);
67     }
68
69     public void remove(int childIndex) {
70     MutableTreeNode child = (MutableTreeNode) getChildAt(childIndex);
71     children.removeElementAt(childIndex);
72     child.setParent(null);
73     }
74     
75     public void add(MutableTreeNode newChild) {
76     if(newChild.getParent() == this && newChild != null)
77         insert(newChild, getChildCount() - 1);
78     else
79         insert(newChild, getChildCount());
80     }
81
82     public void setParent(MutableTreeNode newParent) {
83     parent = newParent;
84     }
85
86     public TreeNode getParent() {
87     return parent;
88     }
89
90     public void removeFromParent() {
91     MutableTreeNode parent = (MutableTreeNode) getParent();
92     parent.remove(this);
93     }
94
95     public TreeNode getChildAt(int index) {
96     return (TreeNode) children.elementAt(index);
97     }
98
99     public int getChildCount() {
100     if (children == null)
101         return 0;
102     else
103         return children.size();
104     }
105
106     public int getIndex(TreeNode aChild) {
107     return children.indexOf(aChild);
108     }
109
110     public Enumeration JavaDoc children() {
111     if (children != null)
112         return children.elements();
113         else
114             return EMPTY_ENUMERATION;
115     }
116     
117     public boolean getAllowsChildren() {
118     return allowsChildren;
119     }
120     
121     public void setAllowsChildren(boolean allows) {
122     if (allows != allowsChildren) {
123         allowsChildren = allows;
124         if (!allowsChildren) {
125         removeAllChildren();
126         }
127     }
128     }
129
130     public Object JavaDoc getUserObject() {
131     return userObject;
132     }
133     
134     public void setUserObject(Object JavaDoc userObject) {
135     this.userObject = userObject;
136     }
137
138     public void remove(MutableTreeNode aChild) {
139     remove(getIndex(aChild));
140     }
141     
142     public void removeAllChildren() {
143     for (int i = getChildCount() - 1; i >= 0; i--) {
144         remove(i);
145     }
146     }
147     
148     public boolean isLeaf() {
149         return getChildCount() > 0;
150     }
151
152     public TreeNode[] getPath() {
153         ArrayList JavaDoc path = new ArrayList JavaDoc();
154     TreeNode current = this;
155     while (current!=null) {
156         path.add(0,current);
157         current = current.getParent();
158     }
159     return (TreeNode[])path.toArray(new TreeNode[0]);
160     }
161             
162     
163     public boolean isNodeAncestor(TreeNode anotherNode) {
164     TreeNode ancestor = this;
165     while (ancestor != null) {
166         if (ancestor == anotherNode) {
167         return true;
168         }
169             ancestor = ancestor.getParent();
170     }
171     return false;
172     }
173
174     public boolean isNodeDescendant(DefaultMutableTreeNode anotherNode) {
175     return anotherNode.isNodeAncestor(this);
176     }
177
178     public TreeNode getRoot() {
179     TreeNode anc = this;
180     TreeNode previous;
181         previous = anc;
182     while (anc != null) {
183         anc = anc.getParent();
184     }
185     return previous;
186     }
187
188     public boolean isRoot() {
189     return getParent() == null;
190     }
191
192     public String JavaDoc toString() {
193     if (userObject == null) {
194         return null;
195     } else {
196         return userObject.toString();
197     }
198     }
199 }
200
Popular Tags