1 61 62 package org.objectweb.jonas.webapp.taglib; 63 64 import java.io.Serializable ; 65 import java.util.HashMap ; 66 67 77 78 public class TreeControl implements Serializable { 79 public static final String ID_PREFIX = "treenode"; 80 81 83 86 public TreeControl() { 87 super(); 88 setRoot(null); 89 } 90 91 96 public TreeControl(TreeControlNode root) { 97 super(); 98 setRoot(root); 99 } 100 101 103 106 protected HashMap registry = new HashMap (); 107 108 111 protected TreeControlNode selected = null; 112 113 protected int mi_Id = 0; 115 116 118 121 protected TreeControlNode root = null; 122 123 public TreeControlNode getRoot() { 124 return (this.root); 125 } 126 127 protected void setRoot(TreeControlNode root) { 128 if (this.root != null) { 129 removeNode(this.root); 130 } 131 if (root != null) { 132 addNode(root); 133 } 134 root.setLast(true); 135 this.root = root; 136 } 137 138 142 public int getWidth() { 143 if (root == null) { 144 return (0); 145 } 146 else { 147 return (getWidth(root)); 148 } 149 } 150 151 153 159 public TreeControlNode findNode(String name) { 160 synchronized (registry) { 161 return ((TreeControlNode) registry.get(name)); 162 } 163 } 164 165 172 public void selectNode(String name) { 173 if (selected != null) { 174 selected.setSelected(false); 175 selected = null; 176 } 177 selected = findNode(name); 178 if (selected != null) { 179 selected.setSelected(true); 180 } 181 } 182 183 188 public TreeControlNode getSelected() { 189 return selected; 190 } 191 192 195 public void expandSelectedParents() { 196 TreeControlNode oCur = getSelected(); 197 while (oCur != null){ 198 oCur = oCur.getParent(); 199 if (oCur != null) { 200 oCur.setExpanded(true); 201 } 202 } 203 } 204 205 public String newId() { 206 StringBuffer sbRet = new StringBuffer (ID_PREFIX); 207 sbRet.append(mi_Id); 208 mi_Id++; 209 return sbRet.toString(); 210 } 211 212 214 222 void addNode(TreeControlNode node) 223 throws IllegalArgumentException { 224 synchronized (registry) { 225 String name = node.getName(); 226 if (registry.containsKey(name)) { 227 throw new IllegalArgumentException ("Name '" + name + "' is not unique"); 228 } 229 node.setTree(this); 230 registry.put(name, node); 231 autoRefresh(node); 233 } 234 } 235 236 241 int getWidth(TreeControlNode node) { 242 int width = node.getWidth(); 243 if (!node.isExpanded()) { 244 return (width); 245 } 246 TreeControlNode children[] = node.findChildren(); 247 for (int i = 0; i < children.length; i++) { 248 int current = getWidth(children[i]); 249 if (current > width) { 250 width = current; 251 } 252 } 253 return (width); 254 } 255 256 263 void removeNode(TreeControlNode node) { 264 synchronized (registry) { 265 TreeControlNode children[] = node.findChildren(); 266 for (int i = 0; i < children.length; i++) { 267 removeNode(children[i]); 268 } 269 TreeControlNode parent = node.getParent(); 270 if (parent != null) { 271 parent.removeChild(node); 272 } 273 node.setParent(null); 274 node.setTree(null); 275 if (node == this.root) { 276 this.root = null; 277 } 278 registry.remove(node.getName()); 279 addRemovedList(node); 281 } 282 } 283 284 288 private HashMap m_RemovedList = null; 289 290 293 public void disableAutoRefresh() { 294 if (m_RemovedList != null) { 295 m_RemovedList.clear(); 296 } 297 m_RemovedList = null; 298 } 299 300 309 public void enableAutoRefresh() { 310 m_RemovedList = new HashMap (); 311 } 312 313 318 void addRemovedList(TreeControlNode p_RemovedNode) { 319 if (m_RemovedList != null) { 320 m_RemovedList.put(p_RemovedNode.getName(), p_RemovedNode); 321 } 322 } 323 324 329 protected void autoRefresh(TreeControlNode p_AddedNode) { 330 if (m_RemovedList != null) { 331 TreeControlNode oRemove = (TreeControlNode) m_RemovedList.get(p_AddedNode.getName()); 332 if (oRemove != null) { 333 p_AddedNode.setExpanded(oRemove.isExpanded()); 334 } 335 } 336 } 337 } 338 | Popular Tags |