KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > tree > simple > TreeNode


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

15 package org.apache.tapestry.contrib.tree.simple;
16
17 import java.util.Collection JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.apache.tapestry.contrib.tree.model.IMutableTreeNode;
23 import org.apache.tapestry.contrib.tree.model.ITreeNode;
24
25 /**
26  * @author ceco
27  */

28 public class TreeNode implements IMutableTreeNode {
29
30     protected Set JavaDoc m_setChildren;
31     protected IMutableTreeNode m_objParentNode;
32     
33     /**
34      * Constructor for TreeNode.
35      */

36     public TreeNode() {
37         this(null);
38     }
39     public TreeNode(IMutableTreeNode parentNode) {
40         super();
41         m_objParentNode = parentNode;
42         m_setChildren = new HashSet JavaDoc();
43     }
44
45
46     public int getChildCount() {
47         return m_setChildren.size();
48     }
49
50     public ITreeNode getParent() {
51         return m_objParentNode;
52     }
53
54     public boolean getAllowsChildren() {
55         return true;
56     }
57
58     public boolean isLeaf() {
59         return m_setChildren.size() == 0 ? true:false;
60     }
61
62     public Collection JavaDoc children() {
63         return m_setChildren;
64     }
65
66
67     public void insert(IMutableTreeNode child) {
68         child.setParent(this);
69         m_setChildren.add(child);
70     }
71
72     public void remove(IMutableTreeNode node) {
73         m_setChildren.remove(node);
74     }
75
76     public void removeFromParent() {
77         m_objParentNode.remove(this);
78         m_objParentNode = null;
79     }
80
81     public void setParent(IMutableTreeNode newParent) {
82         m_objParentNode = newParent;
83     }
84
85     public void insert(Collection JavaDoc colChildren){
86         for (Iterator JavaDoc iter = colChildren.iterator(); iter.hasNext();) {
87             IMutableTreeNode element = (IMutableTreeNode) iter.next();
88             element.setParent(this);
89             m_setChildren.add(element);
90         }
91     }
92
93     /**
94      * @see org.apache.tapestry.contrib.tree.model.ITreeNode#containsChild(ITreeNode)
95      */

96     public boolean containsChild(ITreeNode node) {
97         return m_setChildren.contains(node);
98     }
99
100     /**
101      * @see org.apache.tapestry.contrib.tree.model.ITreeNode#getChildren()
102      */

103     public Collection JavaDoc getChildren() {
104         return m_setChildren;
105     }
106
107 }
108
Popular Tags