1 23 24 package com.sun.enterprise.tools.admingui.tree; 25 26 import com.iplanet.jato.RequestManager; 27 import com.iplanet.jato.RequestContext; 28 import com.iplanet.jato.ModelManager; 29 import com.iplanet.jato.model.TreeModelBase; 30 import com.iplanet.jato.model.ModelControlException; 31 import com.iplanet.jato.model.ValidationException; 32 import com.iplanet.jato.util.StringTokenizer2; 33 34 import com.sun.enterprise.tools.admingui.util.Util; 35 import com.sun.enterprise.tools.guiframework.view.ViewDescriptorManager; 36 37 import java.util.List ; 38 import java.util.ListIterator ; 39 import java.lang.Thread ; 40 import java.io.InputStream ; 41 import java.io.Serializable ; 42 import javax.servlet.*; 43 44 45 public class TreeModelBaseImpl extends TreeModelBase implements IndexTreeModel, Serializable { 46 47 public TreeModelBaseImpl(String treeFileName, String stateDataName) { 48 super(); 49 this.treeFileName = treeFileName; 50 this.stateDataName = stateDataName; 51 } 52 53 public String getStateDataName() { 54 return stateDataName; 55 } 56 57 public String getName() { 58 return name; 59 } 60 61 public void setName(String value) { 62 name=value; 63 } 64 65 public void root() throws ModelControlException { 66 if (root == null) { 67 throw new RuntimeException ("The root node is null!!!"); 68 } 69 setCurrentNode(root); 70 setNodeLevel(ROOT_NODE_LEVEL); 71 setIterationComplete(false); 72 } 73 74 public String getNodeName() throws ModelControlException { 75 IndexTreeNode node=(IndexTreeNode)getCurrentNode(); 76 if (node!=null) 77 return node.getName(); 78 else 79 return null; 80 } 81 82 public String getNodeType() throws ModelControlException { 83 IndexTreeNode node=(IndexTreeNode)getCurrentNode(); 84 if (node!=null) 85 return node.getType(); 86 else 87 return null; 88 } 89 90 public IndexTreeNode getNode(String nodePath) throws NodeNotFoundException { 91 if (Util.isLoggableFINEST()) { 92 Util.logFINEST("IndexTreeModelImpl.getNode : " + nodePath); 93 } 94 95 String [] nodeIDs=StringTokenizer2.tokenize(nodePath,"."); 96 IndexTreeNode node = getRoot(); 97 for (int i = 1; i < nodeIDs.length; i++) { 98 for (int j=0; j < node.getChildren().size(); j++) { 99 IndexTreeNode child=(IndexTreeNode)node.getChildren().get(j); 100 if (String.valueOf(child.getID()).equals(nodeIDs[i])) { 101 node = child; 102 break; 103 } 104 } 105 } 106 if (!node.getPath().equals(nodePath)) { 108 throw new NodeNotFoundException("Incorrect node (" + node.getPath() + " != "+nodePath+")"); 109 } 110 return node; 111 } 112 113 public IndexTreeNode getNodeByName(String nodeNamePath) { 114 String [] nodeIDs=StringTokenizer2.tokenize(nodeNamePath,"."); 115 IndexTreeNode node = getRoot(); 116 for (int i = 1; i < nodeIDs.length; i++) { 117 for (int j=0; j < node.getChildren().size(); j++) { 118 IndexTreeNode child=(IndexTreeNode)node.getChildren().get(j); 119 if (String.valueOf(child.getName()).equals(nodeIDs[i])) { 120 node = child; 121 break; 122 } 123 } 124 } 125 if (!node.getNamePath().equals(nodeNamePath)) { 127 throw new NodeNotFoundException("Incorrect node (" + 128 node.getNamePath() + " != "+nodeNamePath+")"); 129 } 130 return node; 131 } 132 133 public IndexTreeNode getNodeByUniqueID(String id, String childName, IndexTreeNode node) { 134 if (node == null) 135 node = getRoot(); 136 String uniqueID = node.getUniqueID(); 137 if (uniqueID == null) { 139 return null; 141 } 142 List children = node.getChildren(); 143 if (uniqueID != null && uniqueID.equals(id)) { 144 if (childName != null) { 145 IndexTreeNode childNode = node.getChild(childName); 146 if (childNode != null) 147 return childNode; 148 } else { 149 return node; 150 } 151 } 152 for (int i=0; i < children.size(); i++) { 154 IndexTreeNode child = (IndexTreeNode)children.get(i); 155 IndexTreeNode childNode = getNodeByUniqueID(id, childName, child); 156 if (childNode != null) { 157 return childNode; 158 } 159 } 160 return null; 161 } 162 163 public void beforeRoot() throws ModelControlException { 164 setCurrentNode((Object )null); 165 setNodeLevel(UNDEFINED_NODE_LEVEL); 166 setIterationComplete(false); 167 } 168 169 public Object getCurrentNode() { 170 return super.getCurrentNode(); 171 } 172 173 public void setCurrentNode(String nodeID) throws ModelControlException, Exception { 174 setCurrentNode(getNode(nodeID)); 175 } 176 177 public void setCurrentNode(IndexTreeNode node) { 178 super.setCurrentNode(node); 179 } 180 181 public String getNodeID() { 182 IndexTreeNode node=(IndexTreeNode)getCurrentNode(); 183 if (node!=null) 184 return node.getPath(); 185 else 186 return null; 187 } 188 189 public String getNodeHID() { 190 IndexTreeNode node=(IndexTreeNode)getCurrentNode(); 192 if (node != null) 193 return node.getHighlightID(); 194 else 195 return "0"; 196 } 197 198 public String getIconName(boolean expanded) { 199 IndexTreeNode node=(IndexTreeNode)getCurrentNode(); 201 if (node != null) 202 return node.getIconName(expanded); 203 else 204 return "props.gif"; 205 } 206 207 public boolean isParentNode() { 208 IndexTreeNode node=(IndexTreeNode)getCurrentNode(); 209 if (node!=null) 210 return node.getChildren().size()>0; 211 else 212 return false; 213 } 214 215 public boolean isChildNode() { 216 IndexTreeNode node=(IndexTreeNode)getCurrentNode(); 217 if (node!=null) 218 return node.getParent()!=null; 219 else 220 return false; 221 } 222 223 public boolean firstChild() throws ModelControlException { 224 IndexTreeNode node=(IndexTreeNode)getCurrentNode(); 225 if (node!=null && isParentNode()) { 226 setCurrentNode(node.getChildren().get(0)); 227 incrementNodeLevel(); 228 return true; 229 } 230 else { 231 return false; 232 } 233 } 234 235 public boolean parent() throws ModelControlException { 236 IndexTreeNode node=(IndexTreeNode)getCurrentNode(); 237 if (node!=null && isChildNode()) { 238 setCurrentNode(node.getParent()); 239 decrementNodeLevel(); 240 return true; 241 } 242 else { 243 return false; 244 } 245 } 246 247 public boolean nextSibling() throws ModelControlException { 248 IndexTreeNode node=(IndexTreeNode)getCurrentNode(); 249 if (node!=null) { 250 IndexTreeNode nextNode=node.getNextSibling(); 251 if (nextNode!=null) { 252 setCurrentNode(node.getNextSibling()); 253 return true; 254 } 255 else 256 return false; 257 } 258 else { 259 return false; 260 } 261 } 262 263 public Object getValue(String name) { 264 Object [] values=getValues(name); 265 if (values==null || values.length==0) 266 return null; 267 else 268 return values[0]; 269 } 270 271 public void setValue(String name, Object value) 272 throws ValidationException { 273 setValues(name,new Object [] {value}); 274 } 275 276 public Object [] getValues(String name) { 277 if (getCurrentNode()!=null) { 278 if (name.equals("name")) { 279 return new Object [] { 280 ((IndexTreeNode)getCurrentNode()).getName()}; 281 } else { 282 283 return new Object [] { 284 ((IndexTreeNode)getCurrentNode()).getAttribute(name)}; 285 } 286 } 287 else 288 return null; 289 290 } 291 292 public void setValues(String name, Object [] values) throws ValidationException { 293 if (getCurrentNode() != null) 294 ((IndexTreeNode)getCurrentNode()).setAttribute(name,values); 295 } 296 297 298 protected void createIndexData() { 299 try { 300 InputStream inStream = getClass().getClassLoader().getResourceAsStream(treeFileName); 302 if (inStream == null) { 303 inStream = RequestManager.getRequestContext().getServletContext(). 305 getResourceAsStream(treeFileName); 306 } 307 308 TreeReader reader = new TreeReader(inStream, 309 ViewDescriptorManager.getInstance().getDTDURLBase()); 310 reader.populate(this); root(); 312 } catch (Exception ex) { 313 if (Util.isLoggableINFO()) { 314 Util.logINFO("ERROR IN READING TREE FILE: "+treeFileName+" "+ex.getMessage()); 315 ex.printStackTrace(); 316 } 317 } 318 } 319 320 321 public int getNextNodeID() { 322 return nodeCount++; 323 } 324 325 public boolean isInitialExpand() { 326 boolean b = initialExpand; 327 initialExpand = false; 328 return b; 329 } 330 331 public String getType(String string) { 332 IndexTreeNode node=(IndexTreeNode)getCurrentNode(); 333 if (node != null) 334 return node.getType(); 335 else 336 return ROOT; 337 } 338 339 public IndexTreeNode getRoot() { 340 return root; 341 } 342 343 public void setRoot(IndexTreeNode node) { 344 root = node; 345 } 346 347 private String treeFileName; 348 private String stateDataName; 349 private boolean initialExpand = true; 350 private String name; 351 private int nodeCount = 0; 352 private IndexTreeNode root = null; } 354 | Popular Tags |