1 package com.ca.directory.jxplorer.tree; 2 3 import com.ca.commons.cbutil.CBIntText; 4 import com.ca.commons.naming.DN; 5 import com.ca.commons.naming.RDN; 6 7 import javax.naming.InvalidNameException ; 8 import javax.swing.tree.*; 9 import java.util.Enumeration ; 10 import java.util.logging.Level ; 11 import java.util.logging.Logger ; 12 13 21 22 public class SmartModel extends DefaultTreeModel 23 { 24 private static Logger log = Logger.getLogger(SmartModel.class.getName()); 25 26 public SmartModel(TreeNode root) { super(root); } 27 28 public SmartModel(TreeNode root, boolean asksAllowsChildren) { super(root, asksAllowsChildren); } 29 30 36 37 public DN getDNForPath(TreePath path) 38 { 39 if (path==null) return null; 40 41 DN newDN = new DN(); 42 43 Object [] cobbleStones = path.getPath(); 45 46 SmartNode myRoot = (SmartNode)cobbleStones[0]; 47 48 try 49 { 50 for (int i=0; i<cobbleStones.length; i++) 51 { 52 SmartNode sn = ((SmartNode) cobbleStones[i]); 53 54 RDN rdn = sn.getRDN(); 55 if (rdn.isEmpty() == false) 56 { 57 newDN.addChildRDN(rdn); 58 } 59 } 60 } 61 catch (InvalidNameException e) 62 { 63 log.log(Level.WARNING, "ERROR: getDNForPath(TreePath path) can't parse " + path.toString() + "\n ", e); 64 return null; 65 } 66 67 return newDN; 68 } 69 70 73 74 public DN getDNForNode(TreeNode node) 75 { 76 return getDNForPath(getPathForNode(node)); 77 } 78 79 86 87 public DN getDNForPath(TreeNode[] path) 88 { 89 return getDNForPath(new TreePath(path)); 90 } 91 92 95 96 public TreePath getPathForNode(TreeNode node) 97 { 98 TreeNode[] nodePath = getPathToRoot(node); 99 if (nodePath == null) { return null; } 100 return new TreePath(nodePath); 101 } 102 103 106 107 public TreePath getPathForDN(DN nodeDN) 108 { 109 SmartNode node = getNodeForDN(nodeDN); 110 if (node == null) { return null; } 111 return getPathForNode(node); 112 } 113 114 125 126 public SmartNode getNodeForDN(DN nodeDN) 127 { 128 if ((nodeDN==null)) return null; 129 130 if (nodeDN.size()==0) return (SmartNode)getRoot(); 131 132 SmartNode child = null; 133 SmartNode parent = (SmartNode)getRoot(); 134 135 137 for (int i=0;i< nodeDN.size();i++) 138 { 139 RDN rdn = nodeDN.getRDN(i); 140 141 Enumeration children = parent.children(); 142 parent = null; 143 144 while (children.hasMoreElements()) 146 { 147 child = (SmartNode)children.nextElement(); 148 149 if (child.rdnEquals(rdn)) 150 { 151 parent = child; break; 153 } 154 } 155 156 if (parent == null) { 158 return null; } 160 } 161 162 return parent; 164 } 165 166 167 168 181 182 184 protected String getUniqueCopyRDN(DN activeDN, DN copyDN) 185 { 186 RDN testRDN = copyDN.getLowestRDN(); 187 String testValue = copyDN.getLowestRDN().getRawVal(); 188 String testClass = copyDN.getLowestRDN().getAtt(); 189 190 String copyPrefix = testClass + "=" + CBIntText.get("Copy"); 192 String copyPrefix2 = CBIntText.get("of"); 193 194 boolean originalExists = false; 195 196 int copyNumber = 0; 197 198 SmartNode parent = getNodeForDN(activeDN); 199 200 if (parent != null) { 202 Enumeration children = parent.children(); 203 204 211 212 SmartNode child; 213 while (children.hasMoreElements()) 214 { 215 child = (SmartNode)children.nextElement(); 216 RDN childRDN = child.getRDN(); 217 String childRDNString = childRDN.toString(); 218 if ((!originalExists ) && childRDN.equals(testRDN)) { 220 originalExists = true; } 222 223 if (childRDNString.startsWith(copyPrefix)==true) { 225 if (childRDNString.endsWith(testValue)) { int startpos = copyPrefix.length() + 1; 229 if (childRDNString.charAt(startpos)=='(') 230 { 231 int endpos = childRDNString.indexOf(')', startpos); 232 if (endpos != -1) 233 { 234 String childCopyNumberText = childRDNString.substring(startpos+1,endpos); 235 try 236 { 237 int childCopyNumber = Integer.parseInt(childCopyNumberText); 238 if (childCopyNumber >= copyNumber) copyNumber = childCopyNumber + 1; } 241 catch (NumberFormatException e) { 243 if (copyNumber == 0) copyNumber = 2; 244 } 245 } 246 else 247 { 248 if (copyNumber == 0) copyNumber = 2; 249 } 250 } 251 else { if (copyNumber == 0) copyNumber = 2; 254 } 255 } 256 } 257 } 258 259 String returnValue; 260 261 if (originalExists) 262 { 263 if (copyNumber != 0) returnValue = copyPrefix + " (" + copyNumber + ") " + copyPrefix2 + " " + testValue; 265 else returnValue = copyPrefix + " " + copyPrefix2 + " " + testValue; 267 } 268 else 269 returnValue = testRDN.toString(); 270 271 return returnValue; 272 } 273 return testRDN.toString(); } 275 276 277 278 289 290 public boolean exists(DN nodeDN) 291 { 292 if ((nodeDN==null)) return false; 293 294 if (nodeDN.size()==0) return false; 295 296 RDN nodeRDN = nodeDN.getLowestRDN(); 297 SmartNode parent = getNodeForDN(nodeDN.parentDN()); 298 SmartNode child = null; 299 300 Enumeration children = parent.children(); 301 302 int matchCount = 0; 303 304 while (children.hasMoreElements()) { 306 child = (SmartNode)children.nextElement(); 307 308 if (child.rdnEquals(nodeRDN)) 309 { 310 matchCount++; 311 312 if(matchCount==2) 313 return true; 314 } 315 } 316 317 return false; 318 } 319 } | Popular Tags |