KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > tree > DefaultMutableTreeNode


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.tree;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 /**
24  * Default implementation of {@link MutableTreeNode}.
25  *
26  * @author <a HREF="mailto:oliver@rossmueller.com">Oliver Rossmueller </a>
27  * @version $Revision: 1.5 $ $Date: 2005/02/24 01:46:20 $
28  *
29  * $Log: DefaultMutableTreeNode.java,v $
30  * Revision 1.5 2005/02/24 01:46:20 oros
31  * fixed constructor to use List instead of ArrayList
32  *
33  * Revision 1.4 2004/11/26 12:14:10 oros
34  * MYFACES-8: applied tree table patch by David Le Strat
35  *
36  *
37  */

38 public class DefaultMutableTreeNode implements MutableTreeNode
39 {
40
41     private List JavaDoc children = new ArrayList JavaDoc();
42
43     private Object JavaDoc userObject;
44
45     MutableTreeNode parent;
46
47     private boolean allowsChildren = true;
48
49     /**
50      * @param userObject The userObject.
51      */

52     public DefaultMutableTreeNode(Object JavaDoc userObject)
53     {
54         this.userObject = userObject;
55     }
56
57     /**
58      * @param children The children.
59      * @param allowsChildren The allowsChildren.
60      */

61     public DefaultMutableTreeNode(List JavaDoc children, boolean allowsChildren)
62     {
63         this.children = children;
64         this.allowsChildren = allowsChildren;
65     }
66
67     /**
68      * @param userObject The userobject.
69      * @param parent The parent.
70      * @param allowsChildren The allowsChildren.
71      */

72     public DefaultMutableTreeNode(Object JavaDoc userObject, MutableTreeNode parent, boolean allowsChildren)
73     {
74         this.userObject = userObject;
75         this.parent = parent;
76         this.allowsChildren = allowsChildren;
77     }
78
79     /**
80      * @see org.apache.myfaces.custom.tree.MutableTreeNode#insert(org.apache.myfaces.custom.tree.MutableTreeNode)
81      */

82     public void insert(MutableTreeNode child)
83     {
84         children.add(child);
85         child.setParent(this);
86     }
87
88     /**
89      * @see org.apache.myfaces.custom.tree.MutableTreeNode#insert(org.apache.myfaces.custom.tree.MutableTreeNode, int)
90      */

91     public void insert(MutableTreeNode child, int index)
92     {
93         children.add(index, child);
94         child.setParent(this);
95     }
96
97     /**
98      * @see org.apache.myfaces.custom.tree.MutableTreeNode#remove(int)
99      */

100     public void remove(int index)
101     {
102         MutableTreeNode child = (MutableTreeNode) children.remove(index);
103         child.setParent(null);
104     }
105
106     /**
107      * @see org.apache.myfaces.custom.tree.MutableTreeNode#remove(org.apache.myfaces.custom.tree.MutableTreeNode)
108      */

109     public void remove(MutableTreeNode node)
110     {
111         if (children.remove(node))
112         {
113             node.setParent(null);
114         }
115     }
116
117     /**
118      * @see org.apache.myfaces.custom.tree.MutableTreeNode#setUserObject(java.lang.Object)
119      */

120     public void setUserObject(Object JavaDoc object)
121     {
122         this.userObject = object;
123     }
124
125     /**
126      * @see org.apache.myfaces.custom.tree.TreeNode#getUserObject()
127      */

128     public Object JavaDoc getUserObject()
129     {
130         return userObject;
131     }
132
133     /**
134      * @see org.apache.myfaces.custom.tree.MutableTreeNode#removeFromParent()
135      */

136     public void removeFromParent()
137     {
138         if (parent == null)
139         {
140             return;
141         }
142         parent.remove(this);
143     }
144
145     /**
146      * @see org.apache.myfaces.custom.tree.MutableTreeNode#setParent(org.apache.myfaces.custom.tree.MutableTreeNode)
147      */

148     public void setParent(MutableTreeNode parent)
149     {
150         this.parent = parent;
151     }
152
153     /**
154      * @see org.apache.myfaces.custom.tree.TreeNode#getChildAt(int)
155      */

156     public TreeNode getChildAt(int index)
157     {
158         return (TreeNode) children.get(index);
159     }
160
161     /**
162      * @see org.apache.myfaces.custom.tree.TreeNode#getChildCount()
163      */

164     public int getChildCount()
165     {
166         return children.size();
167     }
168
169     /**
170      * @see org.apache.myfaces.custom.tree.TreeNode#getParent()
171      */

172     public TreeNode getParent()
173     {
174         return parent;
175     }
176
177     /**
178      * @see org.apache.myfaces.custom.tree.TreeNode#getIndex(org.apache.myfaces.custom.tree.TreeNode)
179      */

180     public int getIndex(TreeNode node)
181     {
182         return children.indexOf(node);
183     }
184
185     /**
186      * @see org.apache.myfaces.custom.tree.TreeNode#getAllowsChildren()
187      */

188     public boolean getAllowsChildren()
189     {
190         return allowsChildren;
191     }
192
193     /**
194      * @see org.apache.myfaces.custom.tree.TreeNode#isLeaf()
195      */

196     public boolean isLeaf()
197     {
198         return children.isEmpty();
199     }
200
201     /**
202      * @see org.apache.myfaces.custom.tree.TreeNode#children()
203      */

204     public Iterator JavaDoc children()
205     {
206         return Collections.unmodifiableCollection(children).iterator();
207     }
208
209     /**
210      * @see java.lang.Object#toString()
211      */

212     public String JavaDoc toString()
213     {
214         if (userObject != null)
215         {
216             return userObject.toString();
217         }
218         return super.toString();
219     }
220 }
Popular Tags