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 private static final long serialVersionUID = 677919478017303186L; 031 032 protected Set m_setChildren; 033 protected IMutableTreeNode m_objParentNode; 034 035 /** 036 * Constructor for TreeNode. 037 */ 038 public TreeNode() { 039 this(null); 040 } 041 public TreeNode(IMutableTreeNode parentNode) { 042 super(); 043 m_objParentNode = parentNode; 044 m_setChildren = new HashSet(); 045 } 046 047 048 public int getChildCount() { 049 return m_setChildren.size(); 050 } 051 052 public ITreeNode getParent() { 053 return m_objParentNode; 054 } 055 056 public boolean getAllowsChildren() { 057 return true; 058 } 059 060 public boolean isLeaf() { 061 return m_setChildren.size() == 0 ? true:false; 062 } 063 064 public Collection children() { 065 return m_setChildren; 066 } 067 068 069 public void insert(IMutableTreeNode child) { 070 child.setParent(this); 071 m_setChildren.add(child); 072 } 073 074 public void remove(IMutableTreeNode node) { 075 m_setChildren.remove(node); 076 } 077 078 public void removeFromParent() { 079 m_objParentNode.remove(this); 080 m_objParentNode = null; 081 } 082 083 public void setParent(IMutableTreeNode newParent) { 084 m_objParentNode = newParent; 085 } 086 087 public void insert(Collection colChildren){ 088 for (Iterator iter = colChildren.iterator(); iter.hasNext();) { 089 IMutableTreeNode element = (IMutableTreeNode) iter.next(); 090 element.setParent(this); 091 m_setChildren.add(element); 092 } 093 } 094 095 /** 096 * @see org.apache.tapestry.contrib.tree.model.ITreeNode#containsChild(ITreeNode) 097 */ 098 public boolean containsChild(ITreeNode node) { 099 return m_setChildren.contains(node); 100 } 101 102 /** 103 * @see org.apache.tapestry.contrib.tree.model.ITreeNode#getChildren() 104 */ 105 public Collection getChildren() { 106 return m_setChildren; 107 } 108 109 }