1 package com.ca.directory.jxplorer.tree; 2 3 import com.ca.commons.cbutil.CBResourceLoader; 4 import com.ca.commons.cbutil.CBUtility; 5 import com.ca.commons.naming.*; 6 import com.ca.directory.jxplorer.JXplorer; 7 8 import javax.naming.NamingEnumeration ; 9 import javax.swing.*; 10 import javax.swing.tree.DefaultMutableTreeNode ; 11 import java.awt.datatransfer.*; 12 import java.io.File ; 13 import java.io.IOException ; 14 import java.text.CollationKey ; 15 import java.text.Collator ; 16 import java.util.*; 17 import java.util.logging.Level ; 18 import java.util.logging.Logger ; 19 20 33 34 40 41 public class SmartNode extends DefaultMutableTreeNode implements Transferable, Comparable 42 { 43 private static RDN emptyRDN = new RDN(); 44 45 public RDN rdn = emptyRDN; 46 47 50 String nodeObjectClass = null; 51 52 boolean dummy = false; 53 boolean root = false; 54 boolean alwaysRefresh = false; 55 boolean blankRoot = false; boolean structural = false; 59 60 static Hashtable icons = new Hashtable(16); static boolean useIcons; 62 63 public static final String ROOTNAME = "World"; 64 public static final String DUMMYMESSAGE = "reading..."; 65 66 ImageIcon icon = null; 67 68 JPopupMenu menu = null; 70 private static boolean initialised = false; 71 72 final public static DataFlavor UNICODETEXT = DataFlavor.getTextPlainUnicodeFlavor(); 73 74 DataFlavor[] flavours = { UNICODETEXT }; 75 76 private static Collator myCollator = Collator.getInstance(); 78 79 private CollationKey collationKey; 81 82 private static Logger log = Logger.getLogger(SmartNode.class.getName()); 83 84 90 91 92 95 static public void init(CBResourceLoader resourceLoader) 96 { 97 if (initialised) return; initialised = true; 100 String iconPath = JXplorer.getProperty("dir.icons"); 101 102 if (iconPath == null) 103 { 104 useIcons = false; 105 return; 106 } 107 108 String [] extensions = {"jpg","gif","jpeg"}; 109 110 String [] iconFiles = CBUtility.readFilteredDirectory(iconPath, extensions); 111 112 117 if (iconFiles == null) 118 { 119 log.warning("can't find icon directory " + iconPath + " trying to find /icons directory"); 120 iconPath = JXplorer.localDir + "icons" + File.separator; 121 iconFiles = CBUtility.readFilteredDirectory(iconPath, extensions); 122 if (iconFiles == null) 123 { 124 log.warning("Can't find icon directory; check 'dir.icons=' line in dxconfig.txt."); 125 return; 126 } 127 log.warning("Recovered! - iconPath reset to " + iconPath); 128 JXplorer.myProperties.setProperty("dir.icons", iconPath); 129 } 130 131 for (int i=0; i<iconFiles.length; i++) 132 { 133 String stem = iconFiles[i].substring(0,iconFiles[i].lastIndexOf('.')); 134 icons.put(stem.toLowerCase(), new ImageIcon(iconPath + iconFiles[i])); 136 } 137 138 140 try 141 { 142 143 String [] extraIcons = resourceLoader.getPrefixedResources("icons/"); 144 for (int i=0; i<extraIcons.length; i++) 145 { 146 String iconName = extraIcons[i]; 147 String stem = iconName.substring(6); 148 int endpos = stem.lastIndexOf('.'); 149 if (stem.length() > 0 && endpos != -1) 150 { 151 stem = stem.substring(0,endpos).toLowerCase(); 153 byte[] b = resourceLoader.getResource(iconName); 154 icons.put(stem, new ImageIcon(b)); 155 } 156 } 157 } 158 catch (Exception e) 159 { 160 log.warning("Error trying to load icons from resource files: " + e); 161 } 162 useIcons = icons.containsKey("default"); 164 } 165 166 171 172 public SmartNode() 173 { 174 super(); 175 log.finer("created null SmartNode (I)"); 176 dummy = true; 177 nodeObjectClass="default"; 178 } 180 181 185 186 public SmartNode(String rdnString) 187 { 188 super(); 189 log.finer("created SmartNode (II) :" + rdnString); 190 update(rdnString); 191 } 192 193 197 198 public SmartNode(RDN rdn) 199 { 200 super(); 201 log.finer("created SmartNode (IIb) :" + rdn); 202 update(rdn); 203 } 204 205 212 213 public SmartNode(SmartNode S) 214 { 215 super(); 216 log.finer("created SmartNode (III) :" + S.toString()); 217 nodeObjectClass = new String (S.nodeObjectClass); 219 icon = S.icon; 220 update(S.getRDN()); 221 dummy = S.dummy; 222 } 223 224 232 233 236 public SmartNode(String RDN, DXAttribute objectClasses) 237 { 238 super(); 239 log.finer("created SmartNode (IV) :" + RDN); 240 241 update(RDN); 242 243 setTrueObjectClass(objectClasses); 244 245 } 246 247 254 255 public void setTrueObjectClass(DXAttribute objectClasses) 256 { 257 try 258 { 259 NamingEnumeration obClasses = objectClasses.getAll(); 260 while (obClasses.hasMoreElements()) { 262 String value = obClasses.nextElement().toString(); 263 if (setTrueObjectClass(value)) 264 break; 265 } 266 } 267 catch (javax.naming.NamingException e) 268 { 269 log.log(Level.WARNING, "Naming Exception parsing " + rdn +"\n", e); 270 } 271 } 272 273 278 279 public boolean setTrueObjectClass(String value) 280 { 281 value = value.toLowerCase(); 282 if (icons.containsKey(value)) 283 { 284 nodeObjectClass = value; 285 icon = (ImageIcon) icons.get(nodeObjectClass); 286 287 return true; 288 } 289 else 290 return false; 291 } 292 293 300 301 public void update(String rdn) 302 { 303 try 304 { 305 update(new RDN(rdn)); 306 } 307 catch (Exception e) 308 { 309 log.warning("unexpected error in SmartNode:update() " + e.toString()); 310 e.printStackTrace(); 311 } } 313 314 public void update(RDN newRDN) 315 { 316 317 if (newRDN==null) 318 setRdn(emptyRDN); 319 else 320 setRdn(newRDN); 321 322 if (rdn.isEmpty()) { 324 nodeObjectClass="default"; 325 } 326 327 if (nodeObjectClass == null) nodeObjectClass = rdn.getAtt(0); 330 333 336 boolean sortByNamingAttribute = ("true".equals(JXplorer.getProperty("sort.by.naming.attribute"))); 338 339 if (rdn.isMultiValued()) 340 { 341 StringBuffer key = new StringBuffer (rdn.toString().length()); 342 for (int i=0; i<rdn.size(); i++) 343 { 344 if (sortByNamingAttribute) 345 key.append(rdn.getRawVal(i)).append(rdn.getAtt(i)); 346 else 347 key.append(rdn.getAtt(i)).append(rdn.getRawVal(i)); 348 } 349 collationKey = myCollator.getCollationKey(key.toString().toLowerCase()); 350 } 351 else 352 { 353 if (sortByNamingAttribute) 354 collationKey = myCollator.getCollationKey(nodeObjectClass + getDistinguishedValue().toLowerCase()); 355 else 356 collationKey = myCollator.getCollationKey(getDistinguishedValue().toLowerCase() + nodeObjectClass); 357 } 358 } 359 360 366 367 public void copyChildren(Enumeration children) 368 { 369 while (children.hasMoreElements()) 370 { 371 SmartNode A = new SmartNode((SmartNode)children.nextElement()); 372 add(A); 373 } 374 } 375 376 380 381 public RDN getRDN() { 382 return (blankRoot==true)?emptyRDN:rdn; } 383 384 390 391 public String toString() 392 { 393 if (blankRoot==true) return ROOTNAME; 394 if (dummy) return DUMMYMESSAGE; 395 return rdn.toString(); 399 } 400 401 406 407 public String getObjectClass() { return nodeObjectClass; } 408 409 413 414 417 public String getDistinguishedValue() 418 { 419 if (rdn.isMultiValued()) 420 { 421 int size = rdn.size(); 422 StringBuffer val = new StringBuffer (); 423 for (int i=0; i<size; i++) 424 { 425 if (i>0) 426 val.append(" + "); 427 val.append(rdn.getRawVal(i)); 428 } 429 return val.toString(); 430 } 431 else 432 return rdn.getRawVal(0); 433 } 434 435 441 442 public String getIconName() { return nodeObjectClass; } 443 444 451 452 public boolean isDummy() { return dummy; } 453 454 459 460 public boolean hasDummy() 461 { 462 if (getChildCount() == 0) return false; 463 464 return ((SmartNode)getChildAt(0)).isDummy(); 465 } 466 467 471 472 public String getDummyMessage() 473 { 474 return DUMMYMESSAGE; 475 } 476 477 483 484 public boolean isRoot() { return root; } 485 486 492 493 public boolean isBlankRoot() { return blankRoot; } 494 495 499 500 public String getBlankRootName() { return ROOTNAME; } 501 502 506 507 public boolean isAlwaysRefresh() { return alwaysRefresh; } 508 509 513 514 public void setAlwaysRefresh(boolean state) 515 { 516 alwaysRefresh = state; 517 } 518 519 524 525 public void setRoot(boolean state) 526 { 527 root = state; 528 blankRoot = false; 529 if ((root == true) && ("".equals(rdn.toString()))) 530 { 531 update(""); 532 blankRoot = true; 533 nodeObjectClass = ROOTNAME.toLowerCase(); 534 } 535 } 536 537 545 546 public ImageIcon getIcon() 547 { 548 if (icon != null) return icon; 549 550 icon = (ImageIcon) icons.get(nodeObjectClass.toLowerCase()); 551 552 if (icon == null) 553 icon = (ImageIcon) icons.get("default"); 555 return icon; 556 } 557 558 562 563 public void setIcon(ImageIcon newIcon) { icon = newIcon; } 564 565 569 570 public boolean hasChild(SmartNode n) { return hasChild(n.toString());} 571 572 576 577 public boolean hasChild(RDN r) { return hasChild(r.toString());} 578 579 580 584 585 public boolean hasChild(String testRDN) 586 { 587 Enumeration children = children(); 588 while (children.hasMoreElements()) 589 { 590 if (testRDN.equalsIgnoreCase(children.nextElement().toString())) 591 { 592 return true; 593 } 594 } 595 return false; 596 } 597 598 public boolean isStructural() { return structural; } 599 600 public void setStructural(boolean val) { structural = val; } 601 602 603 607 633 634 641 public int compareTo(Object o) 642 throws ClassCastException 643 { 644 if (o == null) 645 return -1; 646 if (((SmartNode)o).collationKey == null) 647 return -1; 648 if (collationKey == null) 649 return +1; 651 return collationKey.compareTo(((SmartNode)o).collationKey); 652 } 653 654 659 660 public void sort() 661 { 662 TreeSet sortedSet = new TreeSet(); 663 Enumeration kids = children(); 664 665 while (kids.hasMoreElements()) 666 { 667 Object kid = kids.nextElement(); 668 sortedSet.add(kid); 670 } 671 removeAllChildren(); 672 673 Iterator sortedKids = sortedSet.iterator(); 674 while (sortedKids.hasNext()) 675 { 676 SmartNode newNode = (SmartNode)sortedKids.next(); 677 add(newNode); 679 } 680 } 681 682 683 689 690 public boolean rdnEquals(RDN testRDN) 691 { 692 return (testRDN.equals(rdn)); 693 } 694 695 700 701 public void setPopupMenu(JPopupMenu popupMenu) { menu = popupMenu; } 702 703 709 710 711 public JPopupMenu getPopupMenu() { return menu; } 712 713 714 715 718 public boolean isMultiValued() 719 { 720 return rdn.isMultiValued(); 721 } 722 723 727 public DN getDN() 728 { 729 if (root) return new DN(); 730 731 DN ret = ((SmartNode)getParent()).getDN(); 732 ret.add(getRDN()); 733 return ret; 734 } 735 736 public Object getTransferData(DataFlavor df) 737 throws UnsupportedFlavorException, IOException 738 { 739 if (df.equals(flavours[0]) == false) throw new UnsupportedFlavorException(df); 741 String dn = getDN().toString(); 742 return dn; 743 } 744 745 public DataFlavor[] getTransferDataFlavors() 748 { 749 return flavours; 750 } 751 752 public boolean isDataFlavorSupported(DataFlavor flavour) 754 { 755 for (int i=0; i<flavours.length; i++) 756 if ( flavours[i].equals(flavour) ) return true; 757 return false; 758 } 759 760 public void setRdn(RDN rdn) 761 { 762 this.rdn = rdn; 763 } 764 765 } 766 | Popular Tags |