001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.contrib.tree.simple;
016    
017    import java.util.Collection;
018    import java.util.HashSet;
019    import java.util.Iterator;
020    import java.util.Set;
021    
022    import org.apache.tapestry.contrib.tree.model.IMutableTreeNode;
023    import org.apache.tapestry.contrib.tree.model.ITreeNode;
024    
025    /**
026     * @author ceco
027     */
028    public class TreeNode implements IMutableTreeNode
029    {
030    
031        private static final long serialVersionUID = 677919478017303186L;
032    
033        protected Set m_setChildren;
034        protected IMutableTreeNode m_objParentNode;
035    
036        /**
037         * Constructor for TreeNode.
038         */
039        public TreeNode()
040        {
041            this(null);
042        }
043    
044        public TreeNode(IMutableTreeNode parentNode)
045        {
046            super();
047            m_objParentNode = parentNode;
048            m_setChildren = new HashSet();
049        }
050    
051        public int getChildCount()
052        {
053            return m_setChildren.size();
054        }
055    
056        public ITreeNode getParent()
057        {
058            return m_objParentNode;
059        }
060    
061        public boolean getAllowsChildren()
062        {
063            return true;
064        }
065    
066        public boolean isLeaf()
067        {
068            return m_setChildren.size() == 0 ? true : false;
069        }
070    
071        public Collection children()
072        {
073            return m_setChildren;
074        }
075    
076        public void insert(IMutableTreeNode child)
077        {
078            child.setParent(this);
079            m_setChildren.add(child);
080        }
081    
082        public void remove(IMutableTreeNode node)
083        {
084            m_setChildren.remove(node);
085        }
086    
087        public void removeFromParent()
088        {
089            m_objParentNode.remove(this);
090            m_objParentNode = null;
091        }
092    
093        public void setParent(IMutableTreeNode newParent)
094        {
095            m_objParentNode = newParent;
096        }
097    
098        public void insert(Collection colChildren)
099        {
100            for(Iterator iter = colChildren.iterator(); iter.hasNext();)
101            {
102                IMutableTreeNode element = (IMutableTreeNode) iter.next();
103                element.setParent(this);
104                m_setChildren.add(element);
105            }
106        }
107    
108        /**
109         * @see org.apache.tapestry.contrib.tree.model.ITreeNode#containsChild(ITreeNode)
110         */
111        public boolean containsChild(ITreeNode node)
112        {
113            return m_setChildren.contains(node);
114        }
115    
116        /**
117         * @see org.apache.tapestry.contrib.tree.model.ITreeNode#getChildren()
118         */
119        public Collection getChildren()
120        {
121            return m_setChildren;
122        }
123    
124    }