1 16 package org.apache.myfaces.custom.tree; 17 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 38 public class DefaultMutableTreeNode implements MutableTreeNode 39 { 40 41 private List children = new ArrayList (); 42 43 private Object userObject; 44 45 MutableTreeNode parent; 46 47 private boolean allowsChildren = true; 48 49 52 public DefaultMutableTreeNode(Object userObject) 53 { 54 this.userObject = userObject; 55 } 56 57 61 public DefaultMutableTreeNode(List children, boolean allowsChildren) 62 { 63 this.children = children; 64 this.allowsChildren = allowsChildren; 65 } 66 67 72 public DefaultMutableTreeNode(Object userObject, MutableTreeNode parent, boolean allowsChildren) 73 { 74 this.userObject = userObject; 75 this.parent = parent; 76 this.allowsChildren = allowsChildren; 77 } 78 79 82 public void insert(MutableTreeNode child) 83 { 84 children.add(child); 85 child.setParent(this); 86 } 87 88 91 public void insert(MutableTreeNode child, int index) 92 { 93 children.add(index, child); 94 child.setParent(this); 95 } 96 97 100 public void remove(int index) 101 { 102 MutableTreeNode child = (MutableTreeNode) children.remove(index); 103 child.setParent(null); 104 } 105 106 109 public void remove(MutableTreeNode node) 110 { 111 if (children.remove(node)) 112 { 113 node.setParent(null); 114 } 115 } 116 117 120 public void setUserObject(Object object) 121 { 122 this.userObject = object; 123 } 124 125 128 public Object getUserObject() 129 { 130 return userObject; 131 } 132 133 136 public void removeFromParent() 137 { 138 if (parent == null) 139 { 140 return; 141 } 142 parent.remove(this); 143 } 144 145 148 public void setParent(MutableTreeNode parent) 149 { 150 this.parent = parent; 151 } 152 153 156 public TreeNode getChildAt(int index) 157 { 158 return (TreeNode) children.get(index); 159 } 160 161 164 public int getChildCount() 165 { 166 return children.size(); 167 } 168 169 172 public TreeNode getParent() 173 { 174 return parent; 175 } 176 177 180 public int getIndex(TreeNode node) 181 { 182 return children.indexOf(node); 183 } 184 185 188 public boolean getAllowsChildren() 189 { 190 return allowsChildren; 191 } 192 193 196 public boolean isLeaf() 197 { 198 return children.isEmpty(); 199 } 200 201 204 public Iterator children() 205 { 206 return Collections.unmodifiableCollection(children).iterator(); 207 } 208 209 212 public String toString() 213 { 214 if (userObject != null) 215 { 216 return userObject.toString(); 217 } 218 return super.toString(); 219 } 220 } | Popular Tags |