1 61 62 63 package org.nextime.ion.backoffice.tree; 64 65 66 import java.io.Serializable ; 67 import java.util.HashMap ; 68 69 70 80 81 public class TreeControl implements Serializable { 82 83 84 86 87 90 public TreeControl() { 91 92 super(); 93 setRoot(null); 94 95 } 96 97 98 103 public TreeControl(TreeControlNode root) { 104 105 super(); 106 setRoot(root); 107 108 } 109 110 111 113 114 117 protected HashMap registry = new HashMap (); 118 119 120 123 protected TreeControlNode selected = null; 124 125 126 128 129 132 protected TreeControlNode root = null; 133 134 public TreeControlNode getRoot() { 135 return (this.root); 136 } 137 138 protected void setRoot(TreeControlNode root) { 139 if (this.root != null) 140 removeNode(this.root); 141 if (root != null) 142 addNode(root); 143 root.setLast(true); 144 this.root = root; 145 } 146 147 148 152 public int getWidth() { 153 154 if (root == null) 155 return (0); 156 else 157 return (getWidth(root)); 158 159 } 160 161 162 164 165 171 public TreeControlNode findNode(String name) { 172 173 synchronized (registry) { 174 return ((TreeControlNode) registry.get(name)); 175 } 176 177 } 178 179 public TreeControlNode getSelected() { 180 return selected; 181 } 182 183 184 191 public void selectNode(String name) { 192 193 if (selected != null) { 194 selected.setSelected(false); 195 selected = null; 196 } 197 selected = findNode(name); 198 if (selected != null) 199 selected.setSelected(true); 200 201 } 202 203 204 206 207 215 void addNode(TreeControlNode node) throws IllegalArgumentException { 216 217 synchronized (registry) { 218 String name = node.getName(); 219 if (registry.containsKey(name)) 220 throw new IllegalArgumentException ("Name '" + name + 221 "' is not unique"); 222 node.setTree(this); 223 registry.put(name, node); 224 } 225 226 } 227 228 229 234 int getWidth(TreeControlNode node) { 235 236 int width = node.getWidth(); 237 if (!node.isExpanded()) 238 return (width); 239 TreeControlNode children[] = node.findChildren(); 240 for (int i = 0; i < children.length; i++) { 241 int current = getWidth(children[i]); 242 if (current > width) 243 width = current; 244 } 245 return (width); 246 247 } 248 249 250 257 void removeNode(TreeControlNode node) { 258 259 synchronized (registry) { 260 TreeControlNode children[] = node.findChildren(); 261 for (int i = 0; i < children.length; i++) 262 removeNode(children[i]); 263 TreeControlNode parent = node.getParent(); 264 if (parent != null) { 265 parent.removeChild(node); 266 } 267 node.setParent(null); 268 node.setTree(null); 269 if (node == this.root) { 270 this.root = null; 271 } 272 registry.remove(node.getName()); 273 } 274 275 } 276 277 278 } 279 | Popular Tags |