View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.tree;
20  
21  import java.io.Serializable;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  /**
28   * Default implementation of {@link MutableTreeNode}.
29   *
30   * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller </a>
31   * @version $Revision: 472638 $ $Date: 2006-11-08 15:54:13 -0500 (Wed, 08 Nov 2006) $
32   */
33  public class DefaultMutableTreeNode implements MutableTreeNode, Serializable
34  {
35  
36      private List children = new ArrayList();
37  
38      private Object userObject;
39  
40      MutableTreeNode parent;
41  
42      private boolean allowsChildren = true;
43  
44      /**
45       * @param userObject The userObject.
46       */
47      public DefaultMutableTreeNode(Object userObject)
48      {
49          this.userObject = userObject;
50      }
51  
52      /**
53       * @param children The children.
54       * @param allowsChildren The allowsChildren.
55       */
56      public DefaultMutableTreeNode(List children, boolean allowsChildren)
57      {
58          this.children = children;
59          this.allowsChildren = allowsChildren;
60      }
61  
62      /**
63       * @param userObject The userobject.
64       * @param parent The parent.
65       * @param allowsChildren The allowsChildren.
66       */
67      public DefaultMutableTreeNode(Object userObject, MutableTreeNode parent, boolean allowsChildren)
68      {
69          this.userObject = userObject;
70          this.parent = parent;
71          this.allowsChildren = allowsChildren;
72      }
73  
74      /**
75       * @see org.apache.myfaces.custom.tree.MutableTreeNode#insert(org.apache.myfaces.custom.tree.MutableTreeNode)
76       */
77      public void insert(MutableTreeNode child)
78      {
79          children.add(child);
80          child.setParent(this);
81      }
82  
83      /**
84       * @see org.apache.myfaces.custom.tree.MutableTreeNode#insert(org.apache.myfaces.custom.tree.MutableTreeNode, int)
85       */
86      public void insert(MutableTreeNode child, int index)
87      {
88          children.add(index, child);
89          child.setParent(this);
90      }
91  
92      /**
93       * @see org.apache.myfaces.custom.tree.MutableTreeNode#remove(int)
94       */
95      public void remove(int index)
96      {
97          MutableTreeNode child = (MutableTreeNode) children.remove(index);
98          child.setParent(null);
99      }
100 
101     /**
102      * @see org.apache.myfaces.custom.tree.MutableTreeNode#remove(org.apache.myfaces.custom.tree.MutableTreeNode)
103      */
104     public void remove(MutableTreeNode node)
105     {
106         if (children.remove(node))
107         {
108             node.setParent(null);
109         }
110     }
111 
112     /**
113      * @see org.apache.myfaces.custom.tree.MutableTreeNode#setUserObject(java.lang.Object)
114      */
115     public void setUserObject(Object object)
116     {
117         this.userObject = object;
118     }
119 
120     /**
121      * @see org.apache.myfaces.custom.tree.TreeNode#getUserObject()
122      */
123     public Object getUserObject()
124     {
125         return userObject;
126     }
127 
128     /**
129      * @see org.apache.myfaces.custom.tree.MutableTreeNode#removeFromParent()
130      */
131     public void removeFromParent()
132     {
133         if (parent == null)
134         {
135             return;
136         }
137         parent.remove(this);
138     }
139 
140     /**
141      * @see org.apache.myfaces.custom.tree.MutableTreeNode#setParent(org.apache.myfaces.custom.tree.MutableTreeNode)
142      */
143     public void setParent(MutableTreeNode parent)
144     {
145         this.parent = parent;
146     }
147 
148     /**
149      * @see org.apache.myfaces.custom.tree.TreeNode#getChildAt(int)
150      */
151     public TreeNode getChildAt(int index)
152     {
153         return (TreeNode) children.get(index);
154     }
155 
156     /**
157      * @see org.apache.myfaces.custom.tree.TreeNode#getChildCount()
158      */
159     public int getChildCount()
160     {
161         return children.size();
162     }
163 
164     /**
165      * @see org.apache.myfaces.custom.tree.TreeNode#getParent()
166      */
167     public TreeNode getParent()
168     {
169         return parent;
170     }
171 
172     /**
173      * @see org.apache.myfaces.custom.tree.TreeNode#getIndex(org.apache.myfaces.custom.tree.TreeNode)
174      */
175     public int getIndex(TreeNode node)
176     {
177         return children.indexOf(node);
178     }
179 
180     /**
181      * @see org.apache.myfaces.custom.tree.TreeNode#getAllowsChildren()
182      */
183     public boolean getAllowsChildren()
184     {
185         return allowsChildren;
186     }
187 
188     /**
189      * @see org.apache.myfaces.custom.tree.TreeNode#isLeaf()
190      */
191     public boolean isLeaf()
192     {
193         return children.isEmpty();
194     }
195 
196     /**
197      * @see org.apache.myfaces.custom.tree.TreeNode#children()
198      */
199     public Iterator children()
200     {
201         return children == null ? 
202                 null : Collections.unmodifiableCollection(children).iterator();
203     }
204 
205     /**
206      * @see java.lang.Object#toString()
207      */
208     public String toString()
209     {
210         if (userObject != null)
211         {
212             return userObject.toString();
213         }
214         return super.toString();
215     }
216 }