1 16 17 18 package org.apache.webapp.admin; 19 20 21 import java.io.Serializable ; 22 import java.util.HashMap ; 23 24 25 35 36 public class TreeControl implements Serializable { 37 38 39 41 42 45 public TreeControl() { 46 47 super(); 48 setRoot(null); 49 50 } 51 52 53 58 public TreeControl(TreeControlNode root) { 59 60 super(); 61 setRoot(root); 62 63 } 64 65 66 68 69 72 protected HashMap registry = new HashMap (); 73 74 75 78 protected TreeControlNode selected = null; 79 80 81 83 84 87 protected TreeControlNode root = null; 88 89 public TreeControlNode getRoot() { 90 return (this.root); 91 } 92 93 protected void setRoot(TreeControlNode root) { 94 if (this.root != null) 95 removeNode(this.root); 96 if (root != null) 97 addNode(root); 98 root.setLast(true); 99 this.root = root; 100 } 101 102 103 107 public int getWidth() { 108 109 if (root == null) 110 return (0); 111 else 112 return (getWidth(root)); 113 114 } 115 116 117 119 120 126 public TreeControlNode findNode(String name) { 127 128 synchronized (registry) { 129 return ((TreeControlNode) registry.get(name)); 130 } 131 132 } 133 134 135 142 public void selectNode(String name) { 143 144 if (selected != null) { 145 selected.setSelected(false); 146 selected = null; 147 } 148 selected = findNode(name); 149 if (selected != null) 150 selected.setSelected(true); 151 152 } 153 154 155 157 158 166 void addNode(TreeControlNode node) throws IllegalArgumentException { 167 168 synchronized (registry) { 169 String name = node.getName(); 170 if (registry.containsKey(name)) 171 throw new IllegalArgumentException ("Name '" + name + 172 "' is not unique"); 173 node.setTree(this); 174 registry.put(name, node); 175 } 176 177 } 178 179 180 185 int getWidth(TreeControlNode node) { 186 187 int width = node.getWidth(); 188 if (!node.isExpanded()) 189 return (width); 190 TreeControlNode children[] = node.findChildren(); 191 for (int i = 0; i < children.length; i++) { 192 int current = getWidth(children[i]); 193 if (current > width) 194 width = current; 195 } 196 return (width); 197 198 } 199 200 201 208 void removeNode(TreeControlNode node) { 209 210 synchronized (registry) { 211 TreeControlNode children[] = node.findChildren(); 212 for (int i = 0; i < children.length; i++) 213 removeNode(children[i]); 214 TreeControlNode parent = node.getParent(); 215 if (parent != null) { 216 parent.removeChild(node); 217 } 218 node.setParent(null); 219 node.setTree(null); 220 if (node == this.root) { 221 this.root = null; 222 } 223 registry.remove(node.getName()); 224 } 225 226 } 227 228 229 } 230 | Popular Tags |