1 package com.ca.directory.jxplorer.tree; 2 3 import com.ca.commons.cbutil.*; 4 import com.ca.commons.naming.*; 5 import com.ca.directory.jxplorer.*; 6 import com.ca.directory.jxplorer.event.JXplorerEvent; 7 import com.ca.directory.jxplorer.event.JXplorerEventGenerator; 8 import com.ca.directory.jxplorer.search.SearchGUI; 9 import com.ca.directory.jxplorer.viewer.AttributeDisplay; 10 import com.ca.directory.jxplorer.viewer.PluggableEditor; 11 12 import javax.naming.*; 13 import javax.naming.directory.*; 14 import javax.swing.*; 15 import javax.swing.event.*; 16 import javax.swing.tree.*; 17 import java.awt.*; 18 import java.awt.datatransfer.Transferable ; 19 import java.awt.datatransfer.UnsupportedFlavorException ; 20 import java.awt.dnd.*; 21 import java.awt.dnd.peer.DragSourceContextPeer; 22 import java.awt.event.*; 23 import java.io.IOException ; 24 import java.util.Enumeration ; 25 import java.util.Vector ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 29 30 38 39 42 public class SmartTree extends JTree 43 implements TreeSelectionListener, DataListener, 44 TreeExpansionListener, JXplorerEventGenerator, 45 DragGestureListener, DropTargetListener, DragSourceListener 46 47 { 48 boolean setup = false; boolean activated = false; 51 52 public static String NODATA = CBIntText.get("cn=no entries"); 53 54 JXplorerEventGenerator eventPublisher; 57 59 Frame owner; 61 SmartNode root; SmartNode rootDNBase; 64 DN rootDN; 66 SmartModel treeModel; 68 DN currentDN; 70 SmartPopupTool popupTreeTool; 72 DefaultTreeCellEditor treeEditor; 74 TreeCellEditor innerEditor; 76 SmartTreeCellRenderer treeRenderer; 78 public boolean rootSet = false; 80 DataSource treeDataSource; Vector treeDataSinks = new Vector (); 83 85 public DXEntry entry; 87 String name; 89 AttributeDisplay pluggableEditorSource = null; 91 private SearchGUI searchGUI = null; 92 static int treeNo = 0; 93 94 95 public boolean dragging = false; 97 98 private DragSource dragSource = null; 99 private Point cursorLocation = null; 101 102 106 public int numOfResults = 0; 107 108 private static Logger log = Logger.getLogger(SmartTree.class.getName()); 109 110 111 120 121 public SmartTree(Frame Owner, String name, CBResourceLoader resourceLoader) 122 { 123 treeNo++; 124 owner = Owner; 125 126 this.name = name; 127 128 setRoot(NODATA); 129 130 setup = true; 132 SmartNode.init(resourceLoader); 133 134 137 138 treeRenderer = new SmartTreeCellRenderer(); 139 setCellRenderer(treeRenderer); 140 141 144 145 treeEditor = new SmartTreeCellEditor(this, treeRenderer); 147 148 setCellEditor(treeEditor); 149 150 treeModel = new SmartModel(root); 151 setModel(treeModel); 152 153 registerPopupTool(new SmartPopupTool(this)); 154 getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); 156 addTreeSelectionListener(this); 157 addTreeExpansionListener(this); 158 159 162 setTreeMouseListener(); 163 setTreeCellEditorListener(); 164 165 setEditable(true); 167 setupDragAndDrop(); 168 } 169 170 public String getName() 171 { 172 return name; 173 } 174 175 public String toString() 176 { 177 return name; 178 } 179 180 183 protected SmartNode getSelectedNode() 184 { 185 if (getSelectionPath() == null) return null; 186 return (SmartNode) getSelectionPath().getLastPathComponent(); 187 } 188 189 192 193 public void registerPopupTool(SmartPopupTool tool) 194 { 195 popupTreeTool = tool; 196 } 197 198 204 205 public Vector getAllNodes(DN start) 206 { 207 if (start == null) return new Vector (0); SmartNode apex = treeModel.getNodeForDN(start); if (apex == null) return new Vector (0); 211 try 212 { 213 Vector result = new Vector (10); 214 215 result.add(start); 217 Enumeration children = apex.children(); while (children.hasMoreElements()) 219 { 220 DN next = new DN(start); 221 next.addChildRDN(((SmartNode) children.nextElement()).getRDN()); 222 result.addAll(getAllNodes(next)); 223 } 224 return result; 225 } 226 catch (Exception e) 227 { 228 log.log(Level.WARNING, "error in SmartTree dump: ", e); 229 return new Vector (0); 230 } 231 } 232 233 246 247 public void setRoot(String rootString) 248 { 249 if (rootString == null) 250 rootString = ""; 251 252 rootDN = new DN(rootString); setRoot(rootDN); 254 } 255 256 269 270 public void setRoot(DN rootDN) 271 { 272 rootSet = true; 273 if (rootDN == null) rootDN = new DN(); 275 278 279 if (NODATA.equals(rootDN.toString())) 280 { 281 rootDN = new DN(NODATA); 282 root = new SmartNode(NODATA); 283 rootDNBase = root; 284 rootSet = false; 285 if (treeModel != null) 286 { 287 treeModel.setRoot(root); 288 treeModel.reload(); 289 } 290 return; 291 } 292 293 this.rootDN = rootDN; 294 295 root = new SmartNode(""); root.setRoot(true); 297 root.setStructural(true); 298 299 treeModel.setRoot(root); 301 303 SmartNode parent = root; 304 rootDNBase = root; 305 306 for (int i = 0; i < rootDN.size(); i++) 307 { 308 SmartNode child = new SmartNode(rootDN.getRDN(i)); 309 child.setStructural(true); 310 parent.add(child); 311 parent = child; 312 } 313 rootDNBase = parent; 314 rootDNBase.add(new SmartNode()); 316 treeModel.reload(); 317 318 if (rootDN.size() > 0) 319 expandPath(treeModel.getPathForNode(rootDNBase)); 320 else 321 collapseRow(0); 322 323 324 } 325 326 327 330 331 public void expandDN(DN dn) 332 { 333 TreePath path = treeModel.getPathForDN(dn); 334 335 expandPath(path.getParentPath()); } 337 338 342 343 public void expandRoot() 344 { 345 expandRow(0); 346 } 347 348 354 355 public DN getRootDN() 356 { 357 return (rootSet) ? rootDN : null; 358 } 359 360 363 public SmartNode getRootNode() 364 { 365 return root; 366 } 367 368 373 public SmartNode getLowestRootNode() 374 { 375 return rootDNBase; 376 } 377 378 381 382 public SmartModel getTreeModel() 383 { 384 return treeModel; 385 } 386 387 393 394 public void registerDataSource(DataSource s) 395 { 396 if (s == null) return; 398 log.fine("registering data source for tree " + getName()); 399 treeDataSource = s; 400 401 treeDataSource.addDataListener(this); 403 404 } 406 407 412 413 public DataSource getDataSource() 414 { 415 return treeDataSource; 416 } 417 418 429 430 public void registerDataSink(DataSink s) 431 { 432 treeDataSinks.addElement(s); 433 if (s instanceof AttributeDisplay) 434 registerPluggableEditorSource((AttributeDisplay) s); 435 } 436 437 441 public void clearTree() 442 { 443 root.removeAllChildren(); 444 setRoot(NODATA); 445 treeModel.setRoot(root); 446 treeModel.reload(); 447 clearEntry(); 448 for (int i = 0; i < treeDataSinks.size(); i++) 449 ((DataSink) treeDataSinks.elementAt(i)).displayEntry(null, null); 450 } 451 452 453 460 461 public void addCutting(SmartNode parent, NamingEnumeration children) 462 { 463 if (parent == null) 465 { 466 return; 467 } 468 if (children == null) 469 { 470 log.warning("null child list in addCutting...!"); 471 return; 472 } 473 474 if (children != null) 475 { 476 if (parent.getChildCount() == 1 && ((SmartNode) parent.getChildAt(0)).isDummy()) 478 { 479 parent.removeAllChildren(); 480 } 481 while (children.hasMoreElements()) 483 { 484 NameClassPair np = (NameClassPair) children.nextElement(); 485 486 SmartNode child; 487 488 491 DN temp = new DN(np.getName()); 492 child = new SmartNode(temp.getRDN(temp.size() - 1)); 493 494 if (parent.hasChild(child.toString()) == false) { 496 parent.add(child); 497 499 501 if (np instanceof SearchResult) 502 { 503 doObjectClassSpecificHandling(child, ((SearchResult) np)); 504 } 505 506 } 507 508 511 if (child.getAllowsChildren()) 512 child.add(new SmartNode()); 514 515 } 517 518 parent.sort(); 519 520 treeModel.nodeStructureChanged(parent); 521 } 522 } 523 524 public void registerPluggableEditorSource(AttributeDisplay display) 525 { 526 pluggableEditorSource = display; 527 } 528 529 546 547 protected void doObjectClassSpecificHandling(SmartNode child, SearchResult ocs) 548 { 549 if (ocs == null) return; Attributes atts = ocs.getAttributes(); 551 552 if (atts == null || atts.size() == 0) return; 554 Attribute OC; 555 556 try { 558 OC = atts.get("objectClass"); 559 if (OC == null) 560 OC = atts.get("objectclass"); 561 562 if (OC == null) { 564 Attribute test = (Attribute) atts.getAll().next(); 565 if ("objectclass".equals(test.getID().toLowerCase())) 566 OC = test; 567 } 568 569 if (OC == null) return; 571 572 if ((OC instanceof DXAttribute) == false) 573 OC = new DXAttribute(OC); 574 OC = DXAttributes.getAllObjectClasses((DXAttribute) OC); 576 doObjectClassSpecificHandling(child, OC); 577 } 578 catch (Exception e) 579 { 580 log.warning("Warning error doing object class specific handling for tree nodes: " + e); 581 } 582 } 583 584 585 602 protected void doObjectClassSpecificHandling(SmartNode child, Attribute OC) 603 { 604 if (OC instanceof DXAttribute == false) 605 OC = new DXAttribute(OC); 606 child.setTrueObjectClass((DXAttribute) OC); 608 610 616 if (pluggableEditorSource != null) 617 { 618 PluggableEditor editor = pluggableEditorSource.getUniqueEditor(OC); 619 620 if (editor != null) 621 { 622 623 if (editor.hideSubEntries(child.toString())) 624 child.setAllowsChildren(false); 625 626 ImageIcon newIcon = editor.getTreeIcon(child.toString()); 627 if (newIcon != null) 628 child.setIcon(newIcon); 629 630 if (editor.getPopupMenu(child.toString()) != null) 631 child.setPopupMenu(editor.getPopupMenu(child.toString())); 632 } 633 } 634 } 635 636 637 643 644 public SmartNode addNode(DN newDN) 645 { 646 if (newDN == null) return null; 647 648 SmartNode parent, child = null; 649 650 if (rootDN.toString().equals(NODATA)) 651 { 652 setRoot(""); } 654 655 659 parent = root; 660 RDN rdn; 661 662 for (int i = 0; i < newDN.size(); i++) 663 { 664 rdn = newDN.getRDN(i); 665 Enumeration children = parent.children(); 666 667 child = null; 668 while (children.hasMoreElements()) 669 { 670 child = (SmartNode) children.nextElement(); 671 672 if (child.isDummy()) parent.remove(child); 674 else if (child.rdnEquals(rdn)) break; 676 677 child = null; 678 } 679 680 if (child == null) { 682 child = new SmartNode(rdn); if (i < newDN.size() - 1) 684 { 685 child.setStructural(true); 686 } 687 parent.add(child); parent.sort(); 689 treeModel.nodeStructureChanged(parent); 690 691 parent = child; 692 } 693 else 694 { 695 parent = child; if (i == newDN.size() - 1) 697 child.setStructural(false); 698 } 699 } 700 702 return parent; 703 } 704 705 706 709 710 public void refresh(DN dn) 711 { 712 if (dn != null) { 714 treeDataSource.getChildren(dn); 715 } 716 } 717 718 719 726 727 public void refreshEditorPane() 728 { 729 pluggableEditorSource.refreshEditors(entry, treeDataSource); 731 } 732 733 734 740 741 public DN getCurrentDN() 742 { 743 return (currentDN == null) ? rootDN : currentDN; 744 } 745 746 747 751 752 public void clearEntry() 753 { 754 setEntry(null); 755 pluggableEditorSource.displayEntry(null, treeDataSource); 756 } 757 758 759 766 767 public void collapse() 768 { 769 770 String dn = rootDN.toString(); 771 772 if (dn.equals(NODATA)) return; 774 try 775 { 776 NamingEnumeration en = treeDataSource.getChildren(rootDN).getEnumeration(); 777 778 if (en != null) 779 { 780 clearTree(); 781 setRoot(dn); 782 addCutting(rootDNBase, en); 783 } 784 } 785 catch (NamingException e) 786 { 787 CBUtility.error(CBIntText.get("threaded broker error: "), e); 788 } } 790 791 792 798 799 protected void deleteTreeNode(SmartNode apex) 800 { 801 treeModel.removeNodeFromParent(apex); 802 } 803 804 813 814 public void renameTreeNode(SmartNode node, DN newDN) 815 { 816 node.update(newDN.getLowestRDN()); 817 } 818 819 828 829 public void moveTreeNode(SmartNode node, DN to) 830 { 831 DN from = treeModel.getDNForNode(node); 832 if (from.sharesParent(to)) { 834 renameTreeNode(node, to); 835 } 836 else 837 { 838 SmartNode parent = (SmartNode) node.getParent(); 840 treeModel.removeNodeFromParent(node); 841 treeModel.nodeStructureChanged(parent); 842 843 node.update(to.getLowestRDN()); 845 846 parent = treeModel.getNodeForDN(to.parentDN()); 848 849 if (parent.getChildCount() == 1 && ((SmartNode) parent.getChildAt(0)).isDummy()) 850 { 851 return; } 853 854 parent.add(node); 855 parent.sort(); 856 treeModel.nodeStructureChanged(parent); 857 } 858 } 859 860 869 870 public void copyTreeNode(SmartNode node, DN to) 871 { 872 SmartNode parent = treeModel.getNodeForDN(to.parentDN()); 874 875 if (parent == null) 877 { 878 CBUtility.error(this, CBIntText.get("unable to copy node {0}.", new String []{node.toString()}), null); 879 return; 880 } 881 882 SmartNode newCopy = copyTreeNodes(node, parent); 884 885 889 890 if (newCopy.getRDN().equals(to.getLowestRDN()) == false) 891 { 892 newCopy.update(to.getLowestRDN()); 895 } 896 897 parent.sort(); 898 treeModel.nodeStructureChanged(parent); 900 } 901 902 913 914 public SmartNode copyTreeNodes(SmartNode from, SmartNode toParent) 915 { 916 SmartNode fromCopy = new SmartNode(from); 918 922 923 if (toParent.hasDummy()) 924 return fromCopy; 925 926 toParent.add(fromCopy); 928 Enumeration children = from.children(); while (children.hasMoreElements()) 930 { 931 SmartNode child = (SmartNode) children.nextElement(); 932 copyTreeNodes(child, fromCopy); } 934 fromCopy.sort(); 935 return fromCopy; 936 } 937 938 944 945 public void expandAll() 946 { 947 int rows = 0; 948 while (rows != getRowCount()) { 950 rows = getRowCount(); 951 for (int i = 0; i < rows; i++) 952 expandRow(i); 953 } 954 955 } 956 957 964 965 public JTree getTree() 966 { 967 return this; 968 } 969 970 975 976 public void makeNewEntry(DN parentDN) 977 { 978 if (treeDataSource.getSchemaOps() == null) 979 { 980 JOptionPane.showMessageDialog(owner, CBIntText.get("Because there is no schema currently published by the\ndirectory, adding a new entry is unavailable."), CBIntText.get("No Schema"), JOptionPane.INFORMATION_MESSAGE); 981 return; 982 } 983 else 984 { 985 987 SmartNode parent = treeModel.getNodeForDN(parentDN); 988 DN childDN = null; 989 if (parent == null) 990 { 991 log.warning("unable to find " + parentDN + " in tree!"); 992 return; 993 } 994 995 if (parent.getChildCount() > 0) 996 { 997 SmartNode child = (SmartNode) parent.getChildAt(0); 998 if ((child != null) && (child.isDummy() == false)) 999 { 1000 RDN childRDN = child.getRDN(); 1001 childDN = new DN(parentDN); 1002 try 1003 { 1004 childDN.addChildRDN(childRDN); 1005 } 1006 catch (InvalidNameException e) 1007 { 1008 log.log(Level.WARNING, "ERROR: makeNewEntry(DN parentDN) " + parentDN, e); 1009 } 1010 } 1011 else { 1013 refresh(parentDN); 1014 } 1015 1016 } 1017 1018 1020 DataSink editor = null; 1021 for (int i = 0; i < treeDataSinks.size(); i++) 1022 { 1023 if (((DataSink) treeDataSinks.get(i)).canCreateEntry()) 1024 editor = (DataSink) treeDataSinks.get(i); 1025 } 1026 1027 if (editor == null) 1028 { 1029 CBUtility.error("Unable to create a new entry!", new Exception ("No available entry editors")); 1030 return; 1031 } 1032 1033 1035 1036 NewEntryWin userData = new NewEntryWin(parentDN, childDN, treeDataSource, editor, owner); 1037 1038 userData.setSize(400, 300); 1039 CBUtility.center(userData, owner); userData.setVisible(true); 1041 } 1042 } 1043 1044 1055 1056 1061 1062 public SmartPopupTool getPopupTool() 1063 { 1064 return popupTreeTool; 1065 } 1066 1067 public void registerEventPublisher(JXplorerEventGenerator gen) 1068 { 1069 eventPublisher = gen; 1070 } 1071 1072 public void fireJXplorerEvent(JXplorerEvent e) 1073 { 1074 if (eventPublisher != null) 1075 eventPublisher.fireJXplorerEvent(e); 1076 } 1077 1078 1079 public boolean isModifiable() 1080 { 1081 return treeDataSource.isModifiable(); 1082 } 1083 1084 public DirContext getDirContext() 1085 { 1086 return (treeDataSource == null) ? null : treeDataSource.getDirContext(); 1087 1088 } 1089 1090 1095 1096 public void modifyEntry(DXEntry oldEntry, DXEntry newEntry) 1097 { 1098 if (oldEntry == null && newEntry == null) return; 1100 treeDataSource.modifyEntry(oldEntry, newEntry); } 1102 1103 1104 1107 1108 1112 public void copyTree(DN oldNodeDN, DN newNodeDN) 1113 { 1114 1115 1120 1125 String uniqueRDN = treeModel.getUniqueCopyRDN(newNodeDN, oldNodeDN); 1126 1127 try 1129 { 1130 newNodeDN.addChildRDN(uniqueRDN); 1131 } 1132 catch (javax.naming.InvalidNameException e) 1133 { 1134 CBUtility.error(this, CBIntText.get("Unable to add {0} due to bad name", new String []{newNodeDN.toString()}), e); 1135 return; 1136 } 1137 1138 treeDataSource.copyTree(oldNodeDN, newNodeDN); } 1140 1141 1142 1148 1149 protected void displayExpandedNodeResult(DataQuery result) 1150 { 1151 1153 SmartNode node = treeModel.getNodeForDN(result.requestDN()); 1154 1155 if (node == null) 1156 { 1157 node = addNode(result.requestDN()); 1158 } 1159 1160 try 1161 { 1162 1172 if (node.isAlwaysRefresh() && result.getEnumeration().size() == 0) 1173 { 1174 if (node.getChildCount() != 1 || ((SmartNode) node.getChildAt(0)).isDummy() == false) 1175 { 1176 return; } 1178 } 1179 1180 1182 node.removeAllChildren(); 1183 1184 1186 addCutting(node, result.getEnumeration()); 1187 1188 1190 if (node == getLowestRootNode() && node.getChildCount() == 0) 1191 { 1192 pluggableEditorSource.displaySpecialEntry(null, treeDataSource, JXplorer.getProperty("null.entry.editor")); 1193 } 1194 1195 1196 1198 expandPath(treeModel.getPathForDN(result.requestDN())); 1199 1200 } 1201 catch (NamingException e) 1202 { 1203 result.setException(e); node.removeAllChildren(); 1205 } 1206 } 1207 1208 1209 1218 1219 public void readAndExpandDN(DN dn) 1220 { 1221 if (currentDN.size() < rootDN.size()) 1222 { refresh(rootDN); 1225 setSelectionPath(treeModel.getPathForDN(rootDN)); 1226 } 1227 1228 if (dn.size() < rootDN.size() || !dn.getPrefix(rootDN.size()).toString().equalsIgnoreCase(rootDN.toString())) 1229 { 1230 1232 JOptionPane.showMessageDialog(owner, CBIntText.get("The entry {0}\nwill not be displayed because it is either above the baseDN\n{1}\n" + 1233 "that you are connected with or it has a different prefix.", new String []{dn.toString(), rootDN.toString()}), 1234 CBIntText.get("Display Error"), JOptionPane.ERROR_MESSAGE); 1235 return; 1236 } 1237 1238 log.warning("Opening '" + dn + "' from root DN '" + getRootDN()); 1240 1242 for (int level = 1; level <= dn.size(); level++) 1243 { 1244 DN ancestor = (DN) dn.getPrefix(level); 1245 1246 if (ancestor.size() >= rootDN.size()) 1247 { 1248 SmartNode node = treeModel.getNodeForDN(ancestor); 1249 1250 1254 if (node == null) 1255 { 1256 treeDataSource.getChildren(ancestor.parentDN()); 1257 } 1258 1261 else if (node.isStructural()) 1262 { 1263 treeDataSource.getChildren(ancestor); } 1265 1269 else if (node.isDummy()) 1270 { 1271 treeDataSource.getChildren(ancestor.parentDN()); 1272 } 1273 1276 else 1277 { 1278 treeDataSource.getChildren(ancestor.parentDN()); 1280 } 1281 } 1282 } 1283 treeDataSource.getEntry(dn); 1284 } 1285 1286 1287 1294 1295 public void displayReadNodeResult(DataQuery node) 1296 { 1297 if (treeDataSinks.size() == 0) 1299 { 1300 log.warning("no data sink in display Node"); 1301 return; 1302 } 1303 setEntry(null); 1304 1305 try 1306 { 1307 if (node != null) 1308 { 1309 setEntry(node.getEntry()); 1310 currentDN = node.requestDN(); 1311 } 1312 else 1313 { 1314 currentDN = null; 1315 } 1316 1317 publishData(entry, treeDataSource); 1318 1319 1320 } 1321 catch (NamingException e) 1322 { 1323 CBUtility.error("unexpected naming error trying to \ndisplay: " + node, e); 1324 } 1325 1326 1327 if (node != null) 1328 { 1329 TreePath current = treeModel.getPathForDN(node.requestDN()); 1330 1331 if (current == null) 1332 { 1333 log.warning("Unable to find tree path for DN: " + node.requestDN()); 1334 return; 1335 } 1336 1337 1340 if (isExpanded(current.getParentPath()) == false) 1342 expandPath(current.getParentPath()); 1343 1344 1348 if (current.equals(getSelectionPath()) == false) 1349 { 1350 setSelectionPath(current); 1351 } 1352 1353 if (currentDN.size() <= rootDN.size()) 1354 rootDN = currentDN; 1357 } 1358 } 1359 1360 1363 public void setEntry(DXEntry newEntry) 1364 { 1365 entry = newEntry; 1366 } 1367 1368 1377 public void publishData(DXEntry entry, DataSource dataSource) 1378 { 1379 for (int i = 0; i < treeDataSinks.size(); i++) 1380 { 1381 ((DataSink) treeDataSinks.elementAt(i)).displayEntry(entry, treeDataSource); 1382 } 1383 } 1384 1385 1386 1389 1390 protected void displayModifyResult(DataQuery result) 1391 { 1392 1393 try 1394 { 1395 if (result.getStatus() == true) 1396 { 1397 DXEntry oldEntry = result.oldEntry(); 1398 DXEntry newEntry = result.newEntry(); 1399 1400 1402 if (oldEntry == null || (newEntry != null) && (newEntry.isNewEntry())) { 1404 SmartNode node = addNode(newEntry.getDN()); 1405 node.add(new SmartNode()); doObjectClassSpecificHandling(node, newEntry.getAllObjectClasses()); 1407 1408 1415 newEntry.setStatus(DXEntry.NEW_WRITTEN); 1416 publishData(newEntry, treeDataSource); 1417 } 1418 else if (newEntry == null) { 1420 deleteTreeNode(treeModel.getNodeForDN(oldEntry.getDN())); 1421 } 1422 else if (oldEntry.getDN().equals(newEntry.getDN()) == false) { 1424 SmartNode node = treeModel.getNodeForDN(oldEntry.getDN()); 1425 1426 1431 1432 if (node != null) 1433 { 1434 moveTreeNode(node, newEntry.getDN()); 1435 treeModel.nodeChanged(node); 1436 } 1437 1438 1442 1443 if (newEntry.size() == 0) 1444 { 1445 newEntry.put(oldEntry.getAll()); 1446 } 1447 1448 1451 treeDataSource.getEntry(newEntry.getDN()); 1452 } 1453 else { 1455 1458 treeDataSource.getEntry(newEntry.getDN()); 1459 1460 } 1461 } 1463 } 1464 catch (NamingException e) 1465 { 1466 result.setException(e); } 1468 catch (Exception e) 1469 { 1470 e.printStackTrace(); 1471 1472 } 1473 } 1474 1475 1482 1483 protected void displayCopyResult(DataQuery result) 1484 { 1485 try 1486 { 1487 if (result.getStatus() == true) 1488 { 1489 copyTreeNode(treeModel.getNodeForDN(result.oldDN()), result.requestDN()); 1490 } 1491 } 1492 catch (NamingException e) 1493 { 1494 result.setException(e); } 1496 } 1497 1498 1505 1506 protected void displaySearchResult(DataQuery result) 1507 { 1508 1509 setNumOfResults(0); 1513 1514 try 1515 { 1516 NamingEnumeration results = result.getEnumeration(); 1517 1518 while (results.hasMoreElements()) 1519 { 1520 SearchResult sr = (SearchResult) results.nextElement(); 1521 String search = sr.getName(); 1523 if (search == null || search.length() == 0) 1524 { 1525 addNode(new DN(SmartTree.NODATA)); 1526 } 1527 else 1528 { 1529 DN searchDN = new DN(search); 1530 addNode(searchDN); 1531 numOfResults++; 1532 } 1533 } 1534 if (owner instanceof JXplorer) 1536 ((JXplorer) owner).setStatus("Number of search results: " + String.valueOf(numOfResults)); 1537 1538 expandAll(); 1539 1540 } 1541 catch (NamingException e) 1542 { 1543 result.setException(e); } 1545 } 1546 1547 1550 1551 public int getNumOfResults() 1552 { 1553 return numOfResults; 1554 } 1555 1556 1561 1562 public void setNumOfResults(int numOfResults) 1563 { 1564 this.numOfResults = numOfResults; 1565 } 1566 1567 1572 1578 1584 1585 1589 1590 protected void setTreeCellEditorListener() 1591 { 1592 1593 1599 CellEditorListener cl = new CellEditorListener() 1600 { 1601 public void editingCanceled(ChangeEvent e) 1602 { 1603 changeDN(); 1604 } 1605 1606 public void editingStopped(ChangeEvent e) 1607 { 1608 changeDN(); 1609 } 1610 1611 1616 1617 protected void changeDN() 1618 { 1619 if (isActive() == false) 1621 return; 1622 1623 RDN rdn = (RDN) treeEditor.getCellEditorValue(); 1624 1625 DN newDN = new DN(currentDN); 1626 1627 newDN.setRDN(rdn, newDN.size() - 1); 1628 1629 if (currentDN.toString().equals(newDN.toString())) 1631 return; 1632 1633 1635 if (treeModel.exists(newDN)) 1636 { 1637 new CBErrorWin(owner, "The name you are trying to use already exists - " + 1638 "please choose another name or delete the original entry.", 1639 "Name already exists"); 1640 refresh(currentDN.parentDN()); 1641 return; 1642 } 1643 1644 treeDataSource.modifyEntry(new DXEntry(currentDN), new DXEntry(newDN)); 1646 } 1647 }; 1648 1649 treeEditor.addCellEditorListener(cl); 1650 } 1651 1652 1657 1658 protected void setTreeMouseListener() 1659 { 1660 MouseListener ml = new MouseAdapter() 1661 { 1662 public void mousePressed(MouseEvent e) 1663 { 1664 if (!doPopupStuff(e)) super.mousePressed(e); 1665 } 1666 1667 public void mouseReleased(MouseEvent e) 1668 { 1669 if (!doPopupStuff(e)) super.mouseReleased(e); 1670 } 1671 1672 public boolean doPopupStuff(MouseEvent e) 1673 { 1674 if (isActive() == false) return false; 1676 if (e.isPopupTrigger() == false) return false; 1677 1678 TreePath path = getPathForLocation(e.getX(), e.getY()); 1679 if (path == null) 1680 { 1681 return false; 1682 } 1683 1684 setSelectionPath(path); 1686 1688 DN thisDN = treeModel.getDNForPath(path); 1689 if (thisDN.equals(currentDN) == false) 1690 { 1691 currentDN = thisDN; 1692 } 1693 1694 if (treeDataSource != null) 1695 { 1696 popupTreeTool.setModifiable(treeDataSource.isModifiable()); 1698 if (getSelectedNode().getPopupMenu() != null) 1700 getSelectedNode().getPopupMenu().show(SmartTree.this, e.getX(), e.getY()); 1701 else 1702 { Toolkit toolKit = Toolkit.getDefaultToolkit(); 1704 1705 popupTreeTool.show(SmartTree.this, e.getX(), e.getY()); 1707 if ((int) popupTreeTool.getLocationOnScreen().getY() > toolKit.getScreenSize().height - (popupTreeTool.getHeight() + 30)) { 1709 popupTreeTool.show(SmartTree.this, e.getX(), e.getY() - popupTreeTool.getHeight()); } 1711 } 1712 } 1713 return true; 1714 } 1715 }; 1716 addMouseListener(ml); 1717 } 1718 1719 1724 1725 public void treeCollapsed(TreeExpansionEvent e) 1726 { 1727 } 1728 1729 1736 1737 public void treeExpanded(TreeExpansionEvent e) 1738 { 1739 if (isActive() == false) return; SmartNode current = (SmartNode) e.getPath().getLastPathComponent(); 1741 1742 1743 try 1744 { 1745 if (((SmartNode) current.getFirstChild()).isDummy() == true) 1746 { 1747 treeDataSource.getChildren(treeModel.getDNForNode(current)); 1748 } 1749 else if (current.isAlwaysRefresh()) 1750 { 1751 treeDataSource.getChildren(treeModel.getDNForNode(current)); 1752 } 1753 } 1754 catch (java.util.NoSuchElementException err) 1755 { 1756 } } 1758 1759 1764 1765 public void valueChanged(TreeSelectionEvent e) 1766 { 1767 if (isActive() == false) return; 1769 if (getSelectionPath() == null) 1770 return; 1771 1772 if (e.isAddedPath() == false) { 1774 setSelectionPath(null); displayReadNodeResult(null); } 1777 else { 1779 DN addedDN = treeModel.getDNForPath(getSelectionPath()); 1780 1781 if (addedDN.equals(currentDN) == false) 1782 { 1783 treeDataSource.getEntry(treeModel.getDNForNode(getSelectedNode())); 1784 } 1785 } 1786 } 1787 1788 1789 1794 1795 protected boolean isActive() 1796 { 1797 if (treeDataSource == null) return false; 1798 if (treeDataSource.isActive() == false) return false; 1799 if (rootSet == false) return false; 1800 return true; 1801 } 1802 1803 1808 1809 public void dataReady(DataQuery result) 1810 { 1811 int type = result.getType(); 1812 1813 if (result.hasException()) 1814 { 1815 String exception = result.getException().toString(); if (exception.indexOf("Socket closed") > -1) 1817 if (owner instanceof JXplorer) 1818 ((JXplorer) owner).setDisconnectView(); 1819 1820 CBUtility.error("Unable to perform " + result.getTypeString() + " operation.", result.getException()); 1821 1822 if (type == DataQuery.LIST) { 1824 SmartNode node = treeModel.getNodeForDN(result.requestDN()); 1825 if (!node.isAlwaysRefresh()) { 1827 node.removeAllChildren(); 1828 treeModel.nodeStructureChanged(node); 1829 } 1830 } 1831 1832 return; 1833 } 1834 else 1835 { 1836 switch (type) 1837 { 1838 case DataQuery.LIST: 1839 displayExpandedNodeResult(result); 1840 break; 1841 1842 case DataQuery.COPY: 1843 displayCopyResult(result); 1844 break; 1845 1846 case DataQuery.MODIFY: 1847 displayModifyResult(result); 1848 break; 1849 1850 case DataQuery.SEARCH: 1851 displaySearchResult(result); 1852 break; 1853 1854 case DataQuery.READENTRY: 1855 displayReadNodeResult(result); 1856 break; 1857 } 1858 1859 if (result.hasException()) 1860 { 1861 CBUtility.error("Exception occurred during tree display of " + result.getTypeString() + ".\n\n(Error caught by display tree)", result.getException()); 1862 return; 1863 } 1864 } 1865 } 1866 1867 1868 public void validate() 1869 { 1870 super.validate(); 1871 } 1872 1873 1879 1880 protected void setupDragAndDrop() 1881 { 1882 1885 if (JXplorer.isSolaris()) return; 1886 1887 if (!JXplorer.getProperty("option.drag.and.drop").equals("true")) 1889 return; 1890 1891 1895 1896 dragSource = new DragSource() 1897 { 1898 protected DragSourceContext createDragSourceContext 1899 (DragSourceContextPeer dscp, DragGestureEvent dgl, Cursor dragCursor, 1900 Image dragImage, Point imageOffset, Transferable t, 1901 DragSourceListener dsl) 1902 { 1903 return new DragSourceContext(dscp, dgl, dragCursor, dragImage, imageOffset, t, dsl) 1904 { 1905 protected void updateCurrentCursor(int dropOp, int targetAct, int status) 1906 { 1907 } 1908 }; 1909 } 1910 }; 1911 1912 1913 DragGestureRecognizer dgr = dragSource.createDefaultDragGestureRecognizer(this, 1914 DnDConstants.ACTION_COPY_OR_MOVE, this); 1915 1916 1917 1921 1923 dgr.setSourceActions(dgr.getSourceActions() + InputEvent.BUTTON1_MASK); 1924 1925 1928 new DropTarget(this, this); 1929 1930 } 1931 1932 1935 public void dragGestureRecognized(DragGestureEvent e) 1936 { 1937 SmartNode dragNode = getSelectedNode(); 1939 if (dragNode != null) 1940 { 1941 dragging = true; 1942 1943 Transferable transferable = (Transferable ) dragNode; 1945 1946 Cursor cursor = DragSource.DefaultCopyDrop; 1948 int action = e.getDragAction(); 1949 if (action == DnDConstants.ACTION_MOVE) 1950 cursor = DragSource.DefaultMoveDrop; 1951 1952 dragSource.startDrag(e, cursor, transferable, this); 1954 } 1955 } 1956 1957 1960 public void dragDropEnd(DragSourceDropEvent dsde) 1961 { 1962 dragging = false; 1963 } 1964 1965 1968 public void dragEnter(DragSourceDragEvent dsde) 1969 { 1970 setCursor(dsde); 1971 } 1972 1973 1976 public void dragOver(DragSourceDragEvent dsde) 1977 { 1978 setCursor(dsde); 1979 } 1980 1981 1984 public void dropActionChanged(DragSourceDragEvent dsde) 1985 { 1986 } 1987 1988 1991 public void dragExit(DragSourceEvent dsde) 1992 { 1993 } 1994 1995 2004 private void setCursor(DragSourceDragEvent dsde) 2005 { 2006 if (cursorLocation == null) return; 2008 2009 TreePath destinationPath = 2010 getPathForLocation(cursorLocation.x, cursorLocation.y); 2011 2012 DragSourceContext dsc = dsde.getDragSourceContext(); 2014 2015 if (testDropTarget(destinationPath, getSelectionPath()) == null && dsde.getDropAction() == 1) 2017 dsc.setCursor(DragSource.DefaultCopyDrop); 2018 2019 else if (dsde.getDropAction() == 1) 2021 dsc.setCursor(DragSource.DefaultCopyNoDrop); 2022 2023 else if (testDropTarget(destinationPath, getSelectionPath()) == null && dsde.getDropAction() == 2) 2025 dsc.setCursor(DragSource.DefaultMoveDrop); 2026 2027 else 2029 dsc.setCursor(DragSource.DefaultMoveNoDrop); 2030 } 2031 2032 2033 2036 public void drop(DropTargetDropEvent e) 2037 { 2038 try 2039 { 2040 Transferable tr = e.getTransferable(); 2041 2042 if (!tr.isDataFlavorSupported(SmartNode.UNICODETEXT)) 2044 { 2045 e.rejectDrop(); 2046 return; 2047 } 2048 2049 String bloop = tr.getTransferData(SmartNode.UNICODETEXT).toString(); 2051 2052 Point loc = e.getLocation(); 2054 TreePath destinationPath = getPathForLocation(loc.x, loc.y); 2055 2056 final String msg = testDropTarget(destinationPath, getSelectionPath()); 2057 2058 if (msg != null) 2059 { 2060 e.rejectDrop(); 2061 2062 SwingUtilities.invokeLater(new Runnable () 2063 { 2064 public void run() 2065 { 2066 CBUtility.error(msg); 2068 } 2069 }); 2070 2071 return; 2072 } 2073 2074 SmartNode newParent = (SmartNode) destinationPath.getLastPathComponent(); 2075 DN parentDN = treeModel.getDNForNode(newParent); 2076 2077 SmartNode oldNode = (SmartNode) getSelectedNode(); 2078 DN oldDN = treeModel.getDNForNode(oldNode); 2079 2080 int action = e.getDropAction(); 2081 boolean copyAction = (action == DnDConstants.ACTION_COPY); 2082 2083 if (copyAction) 2084 { 2085 if (System.getProperty("java.version").startsWith("1.4.0")) 2086 popupTreeTool.dragCopy(oldDN, parentDN); 2087 else 2088 popupTreeTool.copy(oldDN, parentDN); } 2090 else 2091 { 2092 if (System.getProperty("java.version").startsWith("1.4.0")) 2093 popupTreeTool.dragMove(oldDN, parentDN); 2094 else 2095 popupTreeTool.move(oldDN, parentDN); } 2097 2098 e.acceptDrop(action); 2099 e.getDropTargetContext().dropComplete(true); 2100 } 2101 catch (IOException io) 2102 { 2103 e.rejectDrop(); 2104 } 2105 catch (UnsupportedFlavorException ufe) 2106 { 2107 e.rejectDrop(); 2108 } 2109 } 2111 2112 2115 public void dragEnter(DropTargetDragEvent e) 2116 { 2117 } 2118 2119 2122 public void dragExit(DropTargetEvent e) 2123 { 2124 } 2125 2126 2129 public void dragOver(DropTargetDragEvent e) 2130 { 2131 cursorLocation = e.getLocation(); 2133 } 2134 2135 2138 public void dropActionChanged(DropTargetDragEvent e) 2139 { 2140 } 2141 2142 2143 2150 2151 private String testDropTarget(TreePath destination, TreePath dropper) 2152 { 2153 2155 boolean destinationPathIsNull = destination == null; 2157 if (destinationPathIsNull) 2158 return CBIntText.get("Invalid drop location."); 2159 2160 2165 if (destination.equals(dropper)) 2166 return CBIntText.get("Destination cannot be same as source"); 2167 2168 if (dropper.isDescendant(destination)) 2170 return CBIntText.get("Destination node cannot be a descendant."); 2171 2172 if (dropper.getParentPath().equals(destination)) 2174 return CBIntText.get("Destination node cannot be a parent."); 2175 2176 return null; 2177 } 2178 2179 2180 2183 public void openDeleteBookmarkDialog() 2184 { 2185 BookMarks bm = new BookMarks((JXplorer) owner); 2186 bm.getDeleteDialog(); 2187 } 2188 2189 2192 public void openEditBookmarkDialog() 2193 { 2194 BookMarks bm = new BookMarks((JXplorer) owner); 2195 bm.getEditDialog(); 2196 } 2197 2198 2203 public void openAddBookmarkDialog(DN dn) 2204 { 2205 BookMarks bm = new BookMarks((JXplorer) owner); 2206 BookMarks.AddDialog addDialog = bm.getAddDialog(dn.toString(), false); 2207 addDialog.setVisible(true); 2208 } 2209 2210 2213 public void openSearch() 2214 { 2215 openSearch(currentDN); 2216 } 2217 2218 2223 public void openSearch(DN dn) 2224 { 2225 JXplorer jx = (JXplorer) owner; 2226 if (searchGUI == null) 2227 searchGUI = new SearchGUI(dn, jx); 2228 2229 searchGUI.setBaseDN(dn); 2230 searchGUI.setVisible(true); 2231 } 2232 2233 2236 public SearchGUI getSearchGUI() 2237 { 2238 return searchGUI; 2239 } 2240 2241 2246 public void setSearchGUI(SearchGUI searchGUI) 2247 { 2248 this.searchGUI = searchGUI; 2249 } 2250} | Popular Tags |