1 26 27 package org.objectweb.util.browser.core.common; 28 29 30 import org.objectweb.util.browser.core.naming.DefaultEntry; 31 import org.objectweb.util.browser.core.naming.DefaultName; 32 import org.objectweb.util.browser.core.naming.InitialContextContainer; 33 import org.objectweb.util.browser.core.naming.InitialContextContainerFactory; 34 import org.objectweb.util.browser.core.naming.InitialEntry; 35 import org.objectweb.util.browser.api.Context; 36 import org.objectweb.util.browser.api.DropAction; 37 import org.objectweb.util.browser.api.Entry; 38 import org.objectweb.util.browser.api.Table; 39 import org.objectweb.util.browser.api.Tree; 40 import org.objectweb.util.browser.api.TreeView; 41 import org.objectweb.util.browser.api.Panel; 42 import org.objectweb.util.browser.api.Wrapper; 43 import org.objectweb.util.browser.api.Info; 44 import org.objectweb.util.browser.core.api.BrowserProperty; 45 import org.objectweb.util.browser.core.api.ContextContainer; 46 import org.objectweb.util.browser.core.api.ContextContainerFactory; 47 import org.objectweb.util.browser.core.api.ContextProperty; 48 import org.objectweb.util.browser.core.api.ExtendedEntry; 49 import org.objectweb.util.browser.core.api.Role; 50 import org.objectweb.util.browser.core.api.RoleManagement; 51 import org.objectweb.util.browser.core.api.StatusBar; 52 import org.objectweb.util.browser.core.api.ViewPanel; 53 import org.objectweb.util.browser.core.api.TreeConfiguration; 54 import org.objectweb.util.browser.core.dnd.DefaultDropTreeView; 55 import org.objectweb.util.browser.core.dnd.EntryViewTransferable; 56 import org.objectweb.util.browser.core.panel.WhitePanel; 57 import org.objectweb.util.browser.core.xmlparser.ContextXMLParser; 58 import org.objectweb.util.browser.core.xmlparser.XMLParser; 59 import org.objectweb.util.browser.core.xmlparser.BrowserXMLParser; 60 61 import org.objectweb.util.browser.core.popup.DefaultAction; 62 63 64 import java.util.Arrays ; 65 import java.util.Comparator ; 66 import java.util.Enumeration ; 67 import java.util.HashMap ; 68 import java.util.Iterator ; 69 import java.util.List ; 70 import java.util.Map ; 71 import java.util.Vector ; 72 import java.io.IOException ; 73 74 75 import java.awt.datatransfer.DataFlavor ; 76 import java.awt.datatransfer.Transferable ; 77 import java.awt.datatransfer.UnsupportedFlavorException ; 78 import java.awt.dnd.Autoscroll ; 79 import java.awt.dnd.DnDConstants ; 80 import java.awt.dnd.DragGestureEvent ; 81 import java.awt.dnd.DragGestureListener ; 82 import java.awt.dnd.DragSource ; 83 import java.awt.dnd.DragSourceContext ; 84 import java.awt.dnd.DragSourceDragEvent ; 85 import java.awt.dnd.DragSourceDropEvent ; 86 import java.awt.dnd.DragSourceEvent ; 87 import java.awt.dnd.DragSourceListener ; 88 import java.awt.dnd.DropTarget ; 89 import java.awt.dnd.DropTargetDragEvent ; 90 import java.awt.dnd.DropTargetDropEvent ; 91 import java.awt.dnd.DropTargetEvent ; 92 import java.awt.dnd.DropTargetListener ; 93 import java.awt.dnd.InvalidDnDOperationException ; 94 95 96 import javax.swing.AbstractAction ; 97 import javax.swing.ButtonGroup ; 98 import javax.swing.JCheckBoxMenuItem ; 99 import javax.swing.JMenu ; 100 import javax.swing.JMenuBar ; 101 import javax.swing.JMenuItem ; 102 import javax.swing.JOptionPane ; 103 import javax.swing.JRadioButtonMenuItem ; 104 import javax.swing.JToolBar ; 105 import javax.swing.JTree ; 106 import javax.swing.JPopupMenu ; 107 import javax.swing.Icon ; 108 import javax.swing.event.TreeWillExpandListener ; 109 import javax.swing.event.TreeSelectionListener ; 110 import javax.swing.event.TreeSelectionEvent ; 111 import javax.swing.event.TreeExpansionEvent ; 112 import javax.swing.event.TreeExpansionListener ; 113 114 import java.awt.Dimension ; 115 import java.awt.Insets ; 116 import java.awt.Point ; 117 import java.awt.Component ; 118 import java.awt.Rectangle ; 119 import java.awt.event.ActionEvent ; 120 import java.awt.event.MouseAdapter ; 121 import java.awt.event.MouseEvent ; 122 123 124 import javax.swing.tree.DefaultMutableTreeNode ; 125 import javax.swing.tree.DefaultTreeModel ; 126 import javax.swing.tree.DefaultTreeSelectionModel ; 127 import javax.swing.tree.DefaultTreeCellRenderer ; 128 import javax.swing.tree.TreeNode ; 129 import javax.swing.tree.TreePath ; 130 import javax.swing.tree.TreeSelectionModel ; 131 132 140 public class DynamicTree 141 extends JTree 142 implements TreeConfiguration, Tree, Autoscroll , RoleManagement { 143 144 150 151 protected AdminCustomization custom; 152 153 154 protected ViewPanel viewPanel_; 155 156 157 protected DefaultMutableTreeNode rootNode; 158 159 160 protected ContextContainer graphicalInitialContext_; 161 162 163 protected Vector initialContext_; 164 165 166 protected DefaultTreeModel treeModel; 167 168 169 protected DefaultTreeSelectionModel selectionModel; 170 171 172 protected DefaultTreeCellRenderer treeCellRenderer; 173 174 175 protected Map nodeState_; 176 177 178 protected String selectedNode_; 179 180 181 protected TreePath selectedTreePath_; 182 183 184 protected Panel blankPanel_; 185 186 187 protected ContextProperty contextProperty_; 188 189 190 protected XMLParser browserProperty_; 191 192 193 protected TreeView currentTreeView_; 194 195 196 protected boolean popupEnabled_; 197 198 199 protected boolean dndEnabled_; 200 201 202 protected boolean multipleRolesEnabled_; 203 204 205 protected StatusBar statusBar_; 206 207 208 protected JMenuBar menuBar_; 209 210 211 protected JToolBar toolBar_ = null; 212 213 214 protected int nbElementsToRemove_ = 0; 215 216 217 protected JMenu actionMenu_, roleMenu_; 218 219 220 protected JPopupMenu currentJPopupMenu_ = null; 221 222 223 protected Vector alreadyInInitialContext_ = null; 224 225 227 228 protected DragSource dragSource_; 229 230 231 protected DragGestureListener dragGestureListener_; 232 233 234 protected DragSourceListener dragSourceListener_; 235 236 237 protected int dragAction_ = DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK; 238 239 240 protected int acceptableActions_ = DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK; 241 242 243 protected DropTarget dropTarget_; 244 245 246 protected DropTargetListener dropTargetListener_; 247 248 254 257 private void buildDnD(){ 258 dragSource_ = DragSource.getDefaultDragSource(); 260 dragSourceListener_ = new TreeDragSourceListener(); 261 dragGestureListener_ = new TreeDragGestureListener(); 262 dragSource_.createDefaultDragGestureRecognizer(this, dragAction_, dragGestureListener_); 263 264 dropTargetListener_ = new TreeDropTargetListener(); 266 dropTarget_ = new DropTarget (this, acceptableActions_, dropTargetListener_, true); 267 } 268 269 274 private void build(ContextContainer initialContext, boolean initAll) { 275 graphicalInitialContext_ = initialContext; 276 if(initialContext instanceof InitialContextContainer) 277 ((InitialContextContainer)initialContext).setDynamicTree(this); 278 279 rootNode = new DefaultMutableTreeNode (new DefaultEntry(graphicalInitialContext_,new DefaultName("Root"),null)); 280 treeModel = new MyTreeModel(rootNode); 281 setModel(treeModel); 282 283 if (initAll) { 284 nodeState_ = new HashMap (); 285 initialContext_ = new Vector (); 286 alreadyInInitialContext_ = new Vector (); 287 selectedNode_ = ""; 288 blankPanel_ = new WhitePanel(); 289 selectionModel = new DefaultTreeSelectionModel (); 290 selectionModel.setSelectionMode( 291 TreeSelectionModel.SINGLE_TREE_SELECTION); 292 setSelectionModel(selectionModel); 293 addMouseListener(new MyMouseAdapter()); 294 addTreeWillExpandListener(new MyTreeWillExpandListener()); 295 addTreeExpansionListener(new MyTreeExpansionListener()); 296 addTreeSelectionListener(new MyTreeSelectionListener()); 297 setCellRenderer(new MyTreeCellRenderer()); 298 popupEnabled_ = true; 299 dndEnabled_ = true; 300 multipleRolesEnabled_ = false; 301 buildDnD(); 302 setToolTipText(""); 303 } 304 305 setShowsRootHandles(true); 306 collapseRow(0); 307 expandRow(0); 308 309 setRootVisible(false); 310 } 311 312 315 public DynamicTree() { 316 ContextContainerFactory ccf = new InitialContextContainerFactory(null); 317 build(ccf.create(), true); 318 } 319 320 324 public DynamicTree(ContextContainer initialContext) { 325 build(initialContext, true); 326 } 327 328 334 340 protected DefaultMutableTreeNode addObject( 341 DefaultMutableTreeNode parent, 342 ExtendedEntry child) { 343 if (custom != null) { 344 Context context = custom.getContext(child.getValue()); 345 if (context != null) { 346 child.setOWValue(context); 347 child.setWrappedObject(((Wrapper) context).getWrapped()); 348 } 349 } 350 DefaultMutableTreeNode childNode = new DefaultMutableTreeNode (child); 351 if (parent == null) { 352 parent = rootNode; 353 } 354 treeModel.insertNodeInto(childNode, parent, parent.getChildCount()); 355 356 return childNode; 357 } 358 359 366 protected String pathToString(DefaultMutableTreeNode node) { 367 StringBuffer sf = new StringBuffer (); 368 Object [] o = node.getUserObjectPath(); 369 for (int i = 0; i < o.length; i++) { 370 sf.append(((Entry) o[i]).getName().toString()); 371 if (i != o.length - 1) 372 sf.append("/"); 373 } 374 return sf.toString(); 375 } 376 377 382 protected Entry objectToEntry(Object o) { 383 if (o != null) { 384 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) o; 385 return (Entry) node.getUserObject(); 386 } 387 return null; 388 } 389 390 394 protected TreePath getPath(Point p){ 395 if(p!=null){ 396 return getPathForLocation((int) p.getX(), (int) p.getY()); 397 } 398 return null; 399 } 400 401 405 protected void showMenu(Point p) { 406 TreePath path = getPath(p); 407 if (path != null) { 408 selectionModel.setSelectionPath(path); 409 Entry entry = objectToEntry(getLastSelectedPathComponent()); 410 if (custom != null) { 411 JPopupMenu menu = custom.getMenu((ExtendedEntry) entry); 412 if (menu != null) { 413 menu.show((Component ) this,(int) (p.getX()),(int) (p.getY())); 414 } 415 } 416 } 417 } 418 419 424 protected TreePath getPathFromInitialContext(String id) { 425 if (id != null) { 426 Enumeration children = rootNode.children(); 427 while (children.hasMoreElements()) { 428 DefaultMutableTreeNode childNode = (DefaultMutableTreeNode ) children.nextElement(); 429 Entry entry = (Entry) childNode.getUserObject(); 430 if (entry.getName().toString().equals(id)) 431 return new TreePath (childNode.getPath()); 432 } 433 } 434 return null; 435 } 436 437 440 protected void createGraphicalInitialContext() { 441 graphicalInitialContext_.clear(); 443 if(custom!=null){ 444 Entry[] entries = custom.getRoot(); 446 for (int i = 0 ; i < entries.length ; i++){ 447 InitialEntry initialEntry = null; 448 try{ 449 initialEntry = (InitialEntry)entries[i]; 450 } catch (ClassCastException e) { 451 initialEntry = new InitialEntry(entries[i].getName().toString(), entries[i].getValue(), 0); 452 } 453 addToInitialContext(initialEntry.getName().toString(), initialEntry.getValue(), initialEntry.getLevel()); 454 } 455 } 456 InitialEntry entryToAdd = null; 458 for(int i=0 ; i<initialContext_.size() ; i++) { 459 entryToAdd = (InitialEntry)initialContext_.get(i); 460 addToInitialContext(entryToAdd.getName().toString(), entryToAdd.getValue(), entryToAdd.getLevel()); 461 } 462 463 } 464 465 471 protected void openTree(TreePath root, int level) { 472 if (level > 0) { 473 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) root.getLastPathComponent(); 474 Enumeration children = node.children(); 475 while (children.hasMoreElements()) { 476 DefaultMutableTreeNode childNode = (DefaultMutableTreeNode ) children.nextElement(); 477 TreePath p = new TreePath (childNode.getPath()); 478 expandPath(p); 479 openTree(p, level - 1); 480 } 481 } 485 } 486 487 490 protected void refreshPanel(){ 491 if(viewPanel_!=null){ 492 Panel currentPanel = viewPanel_.getViewPanel(); 494 if(currentPanel!=null && currentTreeView_!=null) 495 currentPanel.unselected(currentTreeView_); 496 Entry entry = null; 497 Object object = getLastSelectedPathComponent(); 498 String newSelectedNode = null; 499 if (object != null) { 500 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) object; 501 if (!node.equals(rootNode)) { 502 newSelectedNode = pathToString(node); 503 } 504 entry = (Entry) node.getUserObject(); 505 } 506 if (newSelectedNode==null || newSelectedNode.equals("")) { 507 viewPanel_.setViewPanel(blankPanel_); 508 } else if (selectedNode_ != null && newSelectedNode != null && !selectedNode_.equals(newSelectedNode)) { 509 selectedNode_ = newSelectedNode; 511 Panel panel = null; 513 Table table = null; 514 if (entry != null && custom != null) { 515 516 panel = custom.getPanel((ExtendedEntry) entry); 517 if (panel != null) { 518 currentTreeView_ = new DefaultTreeView(this); 519 panel.selected(currentTreeView_); 520 viewPanel_.setViewPanel(panel); 521 } 522 523 550 } 551 if (entry == null || custom == null || panel == null) { 552 viewPanel_.setViewPanel(blankPanel_); 553 } 554 } 555 } 556 } 557 558 561 protected void refreshStatusBar(){ 562 if(statusBar_!=null){ 563 Entry entry = null; 564 Object object = getLastSelectedPathComponent(); 565 if (object != null) { 566 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) object; 567 entry = (Entry) node.getUserObject(); 568 } 569 if (entry != null && custom != null) { 570 Info info = custom.getInfo((ExtendedEntry) entry); 571 if (info != null) { 572 statusBar_.setText(info.getInfo(new DefaultTreeView(this))); 573 } else { 574 statusBar_.setText(""); 575 } 576 } else { 577 statusBar_.setText(""); 578 } 579 } 580 } 581 582 585 protected void refreshActionMenu(){ 586 if(menuBar_!=null && actionMenu_ != null){ 587 Entry entry = null; 588 Object object = getLastSelectedPathComponent(); 589 if (object != null) { 590 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) object; 591 entry = (Entry) node.getUserObject(); 592 } 593 if (entry != null && custom != null) { 594 currentJPopupMenu_ = custom.getMenu((ExtendedEntry) entry); 595 actionMenu_.removeAll(); 596 Component [] componentList = currentJPopupMenu_.getComponents(); 597 if(componentList.length>0){ 598 actionMenu_.setEnabled(true); 599 for(int i=0; i<componentList.length;i++){ 600 if(JPopupMenu.Separator .class.isAssignableFrom(componentList[i].getClass())) 601 actionMenu_.addSeparator(); 602 else 603 actionMenu_.add((JMenuItem )componentList[i]); 604 } 605 } else { 606 actionMenu_.setEnabled(false); 607 } 608 actionMenu_.revalidate(); 609 } 610 } 611 } 612 613 protected void refreshToolBar(){ 614 if(toolBar_!=null){ 615 if(nbElementsToRemove_>0){ 617 int nbComp = toolBar_.getComponentCount(); 618 for(int i = nbComp - 1 ; i >= nbComp - nbElementsToRemove_ ; i--) 619 toolBar_.remove(i); 620 nbElementsToRemove_ = 0; 621 } 622 623 Entry entry = null; 625 Object object = getLastSelectedPathComponent(); 626 if (object != null) { 627 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) object; 628 entry = (Entry) node.getUserObject(); 629 } 630 if (entry != null && custom != null) { 631 currentJPopupMenu_ = custom.getMenu((ExtendedEntry) entry); 632 Component [] componentList = currentJPopupMenu_.getComponents(); 633 if(componentList.length>0){ 634 boolean isFirst = true; 635 boolean iconDisplayed = false; 636 for(int i=0; i<componentList.length;i++){ 637 if(!JPopupMenu.Separator .class.isAssignableFrom(componentList[i].getClass())){ 638 if(((DefaultAction)((JMenuItem )componentList[i]).getAction()).isUserIcon()){ 639 if(isFirst){ 640 isFirst = false; 641 if(toolBar_.getComponentCount()>0){ 642 toolBar_.addSeparator(); 643 nbElementsToRemove_++; 644 } 645 } 646 toolBar_.add(((JMenuItem )componentList[i]).getAction()); 647 iconDisplayed = true; 648 nbElementsToRemove_++; 649 } 650 } else if(iconDisplayed){ 651 toolBar_.addSeparator(); 652 iconDisplayed = false; 653 nbElementsToRemove_++; 654 } 655 } 656 } 657 } 658 if(toolBar_.getComponentCount()==0){ 659 toolBar_.setVisible(false); 660 } else{ 661 toolBar_.setVisible(false); 662 toolBar_.setVisible(true); 663 } 664 } 665 } 666 667 672 protected boolean hasChild(Context context) { 673 if(context!=null){ 674 Entry[] entries = context.getEntries(); 675 return (entries.length > 0); 676 } 677 return false; 678 } 679 680 687 protected void addToInitialContext(String id, Object object, int level) { 688 if (id != null && object != null) { 689 graphicalInitialContext_.addEntry(id, object); 690 refreshPath(new TreePath (rootNode.getPath())); 691 if(!alreadyInInitialContext_.contains(id)){ 694 if (level > 0) { 695 TreePath path = getPathFromInitialContext(id); 696 if (path != null) { 697 expandPath(path); 698 openTree(path, level - 1); 699 } 700 } 701 alreadyInInitialContext_.add(id); 702 } 703 } 704 } 705 706 710 protected void refreshRoleMenu(Role[] roles){ 711 if(menuBar_!=null){ 712 Arrays.sort(roles, new RoleComparator()); 713 Role[] currentRoles = getCurrentRoles(); 714 if(roleMenu_==null){ 715 roleMenu_ = new JMenu ("Roles"); 716 menuBar_.add(roleMenu_); 717 } 718 roleMenu_.removeAll(); 719 if(roles.length>0){ 720 roleMenu_.setVisible(true); 721 if(multipleRolesEnabled_){ 722 List theRoles = Arrays.asList(currentRoles); 723 for(int i=0; i<roles.length;i++){ 724 if(roles[i].isConcrete()){ 725 JCheckBoxMenuItem cbMenuItem = new JCheckBoxMenuItem (new RoleSelected(roles[i])); 726 cbMenuItem.setSelected(theRoles.contains(roles[i])); 727 roleMenu_.add(cbMenuItem); 728 } 729 } 730 } else { 731 ButtonGroup group = new ButtonGroup (); 732 JRadioButtonMenuItem rbMenuItem = null; 733 for(int i=0; i<roles.length;i++){ 734 if(roles[i].isConcrete()){ 735 rbMenuItem = new JRadioButtonMenuItem (new RoleSelected(roles[i])); 736 if(currentRoles!=null) 737 rbMenuItem.setSelected(currentRoles[0].getId().equals(roles[i].getId())); 738 group.add(rbMenuItem); 739 roleMenu_.add(rbMenuItem); 740 } 741 } 742 } 743 } else { 744 roleMenu_.setVisible(false); 745 } 746 } 747 } 748 749 756 protected void updateInitialPathes(String currentName, String newName){ 757 String prefixe = selectedNode_.substring(0,selectedNode_.lastIndexOf(currentName.toString())); 758 Object [] keys = nodeState_.keySet().toArray(); 759 for(int i=0 ; i<keys.length ; i++) { 760 if(keys[i].toString().startsWith(prefixe+currentName)){ 761 String suffixe = ((String )keys[i]).substring(prefixe.length() + currentName.length()); 762 String currentPath = prefixe + currentName + suffixe; 763 String newPath = prefixe + newName + suffixe; 764 nodeState_.put(newPath,nodeState_.get(currentPath)); 765 nodeState_.remove(currentPath); 766 } 767 } 768 } 769 770 776 public String getToolTipText(MouseEvent evt) { 777 String label = ""; 778 TreePath currentPath = getPathForLocation(evt.getX(), evt.getY()); 779 if(currentPath!=null){ 780 Entry entry = objectToEntry(currentPath.getLastPathComponent()); 781 if(custom != null && entry!=null){ 782 label = custom.getDropLabel((ExtendedEntry) entry, -1); 783 if(label != null && label.length()>0){ 784 label = "<html>Drop action: " + label + "</html>"; 785 } 786 } 787 } 788 return label; 789 } 790 791 797 801 public void setNewBrowserProperty(String [] files) { 802 if (files != null) { 803 XMLParser config = new BrowserXMLParser(this); 804 config.setPropertyFile(files); 805 setNewBrowserProperty((BrowserProperty) config); 806 browserProperty_ = config; 807 } 808 } 809 810 813 public void setNewBrowserProperty(BrowserProperty properties) { 814 if (properties != null) { 815 properties.setContextProperty(contextProperty_); 816 custom = new AdminCustomization(properties, this); 817 } 818 refreshRoleMenu(((RoleManagement)properties).getRoleList()); 819 refreshAll(); 820 } 821 822 826 public void addBrowserProperty(String [] files){ 827 if (files != null) { 828 if(browserProperty_!=null){ 829 browserProperty_.setPropertyFile(files); 830 setNewBrowserProperty((BrowserProperty) browserProperty_); 831 refreshRoleMenu(((RoleManagement)browserProperty_).getRoleList()); 832 } else { 833 setNewBrowserProperty(files); 834 } 835 } 836 refreshAll(); 837 } 838 839 843 public BrowserProperty getBrowserProperty() { 844 return custom.getBrowserProperty(); 845 } 846 847 851 public void setNewContextProperty(String file) { 852 if (file != null && !file.equals("")) { 853 XMLParser config = new ContextXMLParser(); 854 config.setPropertyFile(file); 855 setNewContextProperty((ContextProperty) config); 856 } 857 } 858 859 863 public void setNewContextProperty(ContextProperty properties) { 864 if (properties != null) { 865 contextProperty_ = properties; 866 ContextContainerFactory ccf = new InitialContextContainerFactory(properties.getDecoder()); 868 ContextContainer newContext = ccf.create(); 869 if (graphicalInitialContext_ != null) { 870 Entry[] entries = graphicalInitialContext_.getEntries(); 871 for (int i = 0; i < entries.length; i++) { 872 Entry entry = entries[i]; 873 newContext.addEntry(entry.getName().toString(),entry.getValue()); 874 } 875 } 876 build(newContext, false); 877 } 878 } 879 880 884 public ContextProperty getContextProperty() { 885 return contextProperty_; 886 } 887 888 891 public Entry getSelectedEntry() { 892 Object object = getLastSelectedPathComponent(); 893 if (object != null) 894 return objectToEntry(object); 895 return null; 896 } 897 898 901 public Entry getSelectedEntryParent() { 902 Object object = getLastSelectedPathComponent(); 903 if (object != null) 904 return objectToEntry(((DefaultMutableTreeNode ) object).getParent()); 905 return null; 906 } 907 908 911 protected void refreshPath(TreePath treePath){ 912 collapsePath(treePath); 913 expandPath(treePath); 914 } 915 916 919 public void refreshAll() { 920 createGraphicalInitialContext(); 921 TreePath p = new TreePath (rootNode.getPath()); 922 refreshPath(p); 923 selectedNode_ = ""; 924 refreshPanel(); 925 refreshActionMenu(); 926 refreshToolBar(); 927 } 928 929 932 public void refreshSelectedNode() { 933 TreePath path = getSelectionPath(); 934 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) path.getLastPathComponent(); 935 if (isExpanded(path)) { 936 collapsePath(path); 937 expandPath(path); 938 } else if (node.isLeaf()) { 939 fireTreeExpanded(path); 940 } 941 selectedNode_ = ""; 942 refreshPanel(); 943 refreshActionMenu(); 944 refreshToolBar(); 945 } 946 947 951 public Context getInitialContext() { 952 return graphicalInitialContext_; 953 } 954 955 960 public boolean containsEntry(String id) { 961 Entry entry = graphicalInitialContext_.getLocalEntry(id); 962 return entry != null; 963 } 964 965 969 public void setTargetPanel(ViewPanel viewPanel) { 970 viewPanel_ = viewPanel; 971 } 972 973 977 public void setStatusBar(StatusBar statusBar){ 978 statusBar_ = statusBar; 979 } 980 981 985 public void setPopupEnabled(boolean popupEnabled){ 986 popupEnabled_ = popupEnabled; 987 } 988 989 993 public boolean isPopupEnabled(){ 994 return popupEnabled_; 995 } 996 997 1001 public void setDragAndDropEnabled(boolean dndEnabled){ 1002 dndEnabled_ = dndEnabled; 1003 } 1004 1005 1009 public boolean isDragAndDropEnabled(){ 1010 return dndEnabled_; 1011 } 1012 1013 1017 public void setMultipleRolesEnabled(boolean multipleRolesEnabled){ 1018 multipleRolesEnabled_ = multipleRolesEnabled; 1019 } 1020 1021 1025 public boolean isMultipleRolesEnabled(){ 1026 return multipleRolesEnabled_; 1027 } 1028 1029 1033 public AdminCustomization getAdminCustomization(){ 1034 return custom; 1035 } 1036 1037 1041 public void setJMenuBar(JMenuBar jMenuBar) { 1042 menuBar_ = jMenuBar; 1043 actionMenu_ = new JMenu ("Actions"); 1044 actionMenu_.setEnabled(false); 1045 menuBar_.add(actionMenu_); 1046 if(browserProperty_!=null) 1047 refreshRoleMenu(((RoleManagement)browserProperty_).getRoleList()); 1048 } 1049 1050 1054 public void setJToolBar(JToolBar jToolBar){ 1055 toolBar_ = jToolBar; 1056 if(toolBar_!=null && toolBar_.getComponents().length==0) 1057 toolBar_.setVisible(false); 1058 } 1059 1060 1066 1069 public void 1070 refresh(){ 1071 refreshSelectedNode(); 1072 } 1073 1074 1078 public void 1079 close(){ 1080 if(viewPanel_!=null){ 1081 Panel currentPanel = viewPanel_.getViewPanel(); 1082 if(currentPanel!=null && currentTreeView_!=null) 1083 currentPanel.unselected(currentTreeView_); 1084 } 1085 } 1086 1087 1093 public void 1094 addEntry(Object name, Object value){ 1095 addEntry(name,value,0); 1096 } 1097 1098 1105 public void 1106 addEntry(Object name, Object value, int level){ 1107 initialContext_.add(new InitialEntry(name.toString(),value,level)); 1108 addToInitialContext(name.toString(),value,level); 1109 } 1110 1111 1117 public void 1118 renameSelectedNode(String oldName, String newName){ 1119 Object object = getLastSelectedPathComponent(); 1120 if (object != null && oldName!=null && newName!=null) { 1121 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) object; 1122 String selectedNode = pathToString(node); 1123 String prefixe = selectedNode.substring(0,selectedNode.lastIndexOf(oldName)); 1124 if(!selectedNode.equals(prefixe)){ 1125 Object [] keys = nodeState_.keySet().toArray(); 1127 for(int i=0 ; i<keys.length ; i++) { 1128 if(keys[i].toString().startsWith(prefixe+oldName)){ 1129 boolean valid = true; 1130 String oldSuffixe = ((String )keys[i]).substring(prefixe.length() + oldName.length()); 1131 String newSuffixe = oldSuffixe; 1132 if(oldSuffixe!=null && !oldSuffixe.equals("")){ 1133 int index = oldSuffixe.indexOf("/"); 1134 if(index==-1 || index>0) { 1135 valid = false; 1136 } 1137 } 1138 if(valid){ 1139 String oldEntry = prefixe + oldName + oldSuffixe; 1140 String newEntry = prefixe + newName + newSuffixe; 1141 nodeState_.put(newEntry,nodeState_.get(oldEntry)); 1142 nodeState_.remove(oldEntry); 1143 } 1144 } 1145 } 1146 } 1147 } 1148 } 1149 1150 1155 public void 1156 removeEntry(Object name){ 1157 Iterator it = initialContext_.iterator(); 1158 InitialEntry elementToRemove = null; 1159 while (it.hasNext()) { 1160 InitialEntry element = (InitialEntry) it.next(); 1161 if(element.getName().toString().equals(name.toString())) { 1162 elementToRemove = element; 1163 break; 1164 } 1165 } 1166 if(elementToRemove!=null) 1167 initialContext_.remove(elementToRemove); 1168 } 1169 1170 1177 public void 1178 renameInitialEntry(Object currentName, Object newName){ 1179 Iterator it = initialContext_.iterator(); 1180 InitialEntry elementToRemove = null; 1181 while (it.hasNext()) { 1182 InitialEntry element = (InitialEntry) it.next(); 1183 if(element.getName().toString().equals(currentName.toString())) { 1184 element.setName(newName.toString()); 1185 break; 1186 } 1187 } 1188 selectedNode_ = "Root/" + currentName; 1191 updateInitialPathes(currentName.toString(), newName.toString()); 1193 String prefixe = selectedNode_.substring(0,selectedNode_.lastIndexOf(currentName.toString())); 1195 selectedNode_ = prefixe + newName.toString(); 1196 } 1197 1198 1199 1202 public void 1203 clear(){ 1204 initialContext_.clear(); 1205 } 1206 1207 1211 public int 1212 getInitialContextSize(){ 1213 return graphicalInitialContext_.getSize(); 1214 } 1215 1216 1222 public void autoscroll(Point cursorLocn){ 1223 Rectangle rect=getVisibleRect(); 1224 Dimension dim=getSize(); 1225 1226 if(cursorLocn.y>rect.y+rect.height-10) { 1228 rect.y=Math.min(rect.y+10,dim.height-rect.height); 1229 } else if(cursorLocn.y<rect.y+10) { 1230 int i = rect.y+rect.height-10; 1231 rect.y=Math.max(rect.y-10,0); 1232 } 1233 1234 if(cursorLocn.x>rect.x+rect.width-10) { 1236 rect.x=Math.min(rect.x+10,dim.width-rect.width); 1237 } else if(cursorLocn.x<rect.x+10) { 1238 rect.x=Math.max(rect.x-10,0); 1239 } 1240 1241 scrollRectToVisible(rect); 1242 } 1243 1244 public Insets getAutoscrollInsets(){ 1245 Rectangle rect=getVisibleRect(); 1246 Dimension dim=getSize(); 1247 Insets inset=new Insets (rect.y+10,rect.x+10,dim.height-(rect.y+rect.height)+10,dim.width-(rect.x+rect.width)+10); 1248 return inset; 1249 } 1250 1251 1257 1261 final class MyMouseAdapter extends MouseAdapter { 1262 public void mousePressed(MouseEvent e) { 1263 popupLayout(e); 1264 } 1265 public void mouseReleased(MouseEvent e) { 1266 popupLayout(e); 1267 } 1268 private void popupLayout(MouseEvent e) { 1269 if (e.isPopupTrigger()&& popupEnabled_) { 1270 showMenu(e.getPoint()); 1271 } 1272 } 1273 } 1274 1275 1279 final class MyTreeWillExpandListener implements TreeWillExpandListener { 1280 public void treeWillCollapse(TreeExpansionEvent event) { 1281 DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode ) event.getPath().getLastPathComponent(); 1282 Entry e = (Entry) currentNode.getUserObject(); 1283 nodeState_.put(pathToString(currentNode), new GraphicState(false)); 1284 } 1285 1286 public void treeWillExpand(TreeExpansionEvent event) { 1287 DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode ) event.getPath().getLastPathComponent(); 1288 Entry e = (Entry) currentNode.getUserObject(); 1289 Object userObject = e.getValue(); 1290 nodeState_.put(pathToString(currentNode), new GraphicState(true)); 1291 currentNode.removeAllChildren(); 1292 Entry[] entries = ((Context) userObject).getEntries(); 1293 for (int i = 0; i < entries.length; i++) { 1294 Entry entry = entries[i]; 1295 DefaultMutableTreeNode childNode = addObject(currentNode, (ExtendedEntry) entry); 1296 } 1297 treeModel.reload(currentNode); 1298 } 1299 } 1300 1301 1305 final class MyTreeExpansionListener implements TreeExpansionListener { 1306 public void treeCollapsed(TreeExpansionEvent event) { 1307 } 1308 1309 public void treeExpanded(TreeExpansionEvent event) { 1310 DefaultMutableTreeNode node = 1311 (DefaultMutableTreeNode ) (event.getPath()).getLastPathComponent(); 1312 if (!node.isLeaf()) { 1313 Enumeration children = node.children(); 1314 while (children.hasMoreElements()) { 1315 DefaultMutableTreeNode childNode = (DefaultMutableTreeNode ) children.nextElement(); 1316 Entry entry = (Entry) childNode.getUserObject(); 1317 if (nodeState_.containsKey(pathToString(childNode))) { 1318 GraphicState gs = (GraphicState) nodeState_.get(pathToString(childNode)); 1319 if (gs.isExpanded()) { 1320 expandPath(new TreePath (childNode.getPath())); 1321 } 1322 } 1323 1324 if (selectedNode_.equals(pathToString(childNode))) { 1325 setSelectionPath( 1326 new TreePath (treeModel.getPathToRoot(childNode))); 1327 } 1328 } 1329 } 1330 } 1331 } 1332 1333 1337 final class MyTreeSelectionListener implements TreeSelectionListener { 1338 1339 public void valueChanged(TreeSelectionEvent e) { 1340 refreshPanel(); 1341 refreshActionMenu(); 1342 refreshToolBar(); 1343 refreshStatusBar(); 1344 } 1345 } 1346 1347 1352 final class MyTreeCellRenderer extends DefaultTreeCellRenderer { 1353 1354 public Component getTreeCellRendererComponent(JTree tree,Object value,boolean selected,boolean expanded,boolean leaf,int row,boolean hasFocus) { 1355 super.getTreeCellRendererComponent(tree,value,selected,expanded,leaf,row,hasFocus); 1356 1357 Entry entry = objectToEntry(value); 1359 if (custom != null) { 1360 Icon icon = custom.getIcon((ExtendedEntry)entry); 1361 if (icon != null) 1362 setIcon(icon); 1363 } 1364 1365 setText(entry.getName().toString()); 1367 1368 return this; 1370 } 1371 1372 } 1373 1374 1378 final class MyTreeModel extends DefaultTreeModel { 1379 1380 public MyTreeModel(TreeNode root) { 1381 super(root); 1382 } 1383 1384 public boolean isLeaf(Object node) { 1385 Entry entry = objectToEntry(node); 1386 Object o = entry.getValue(); 1387 return !(o instanceof Context); 1389 } 1392 1393 } 1394 1395 1398 protected class GraphicState { 1399 1400 protected boolean isExpanded_; 1401 1402 public GraphicState(boolean isExpanded) { 1403 isExpanded_ = isExpanded; 1404 } 1405 1406 public boolean isExpanded() { 1407 return isExpanded_; 1408 } 1409 1410 public void isEspanded(boolean value) { 1411 isExpanded_ = value; 1412 } 1413 1414 } 1415 1416 1422 1427 public Role[] getCurrentRoles(){ 1428 if(browserProperty_!=null){ 1429 return ((RoleManagement)browserProperty_).getCurrentRoles(); 1430 } 1431 return null; 1432 } 1433 1434 1439 public void setCurrentRole(String [] role_ids){ 1440 if(browserProperty_!=null){ 1441 ((RoleManagement)browserProperty_).setCurrentRole(role_ids); 1442 custom.clear(); 1443 } 1444 if(browserProperty_!=null) 1445 refreshRoleMenu(((RoleManagement)browserProperty_).getRoleList()); 1446 refreshAll(); 1447 } 1448 1449 1454 public void setCurrentRole(Role[] roles){ 1455 if(browserProperty_!=null){ 1456 ((RoleManagement)browserProperty_).setCurrentRole(roles); 1457 custom.clear(); 1458 } 1459 if(browserProperty_!=null) 1460 refreshRoleMenu(((RoleManagement)browserProperty_).getRoleList()); 1461 refreshAll(); 1462 } 1463 1464 1469 public Role[] getRoleList(){ 1470 if(browserProperty_!=null){ 1471 ((RoleManagement)browserProperty_).getRoleList(); 1472 } 1473 return null; 1474 } 1475 1476 1482 1489 protected class TreeDragGestureListener implements DragGestureListener { 1490 1491 1494 public void dragGestureRecognized(DragGestureEvent dge) { 1495 if(dndEnabled_){ 1496 Transferable transferable = new EntryViewTransferable(new EntryView(DynamicTree.this.getSelectedEntry())); 1497 try { 1498 dragSource_.startDrag(dge,DragSource.DefaultLinkDrop, transferable, dragSourceListener_); 1499 }catch(InvalidDnDOperationException idoe) { 1500 System.out.println( idoe ); 1501 } 1502 } 1503 } 1504 } 1505 1506 1514 public class TreeDragSourceListener implements DragSourceListener { 1515 1516 1517 protected int dropAction_; 1518 1519 1532 1533 1538 protected int getDropAction(int gestureModifiers){ 1539 if(gestureModifiers==16) 1540 return DnDConstants.ACTION_MOVE; 1541 else if(gestureModifiers==18) 1542 return DnDConstants.ACTION_COPY; 1543 else if(gestureModifiers==19) 1544 return DnDConstants.ACTION_LINK; 1545 else 1546 return 0; 1547 } 1548 1549 1552 protected void setIcon(DragSourceDragEvent dsde){ 1553 DragSourceContext context = dsde.getDragSourceContext(); 1554 dropAction_ = getDropAction(dsde.getGestureModifiers()); 1555 if (dropAction_ == DnDConstants.ACTION_COPY) { 1556 context.setCursor(DragSource.DefaultCopyDrop); 1557 } else if (dropAction_ == DnDConstants.ACTION_MOVE) { 1558 context.setCursor(DragSource.DefaultMoveDrop); 1559 } else if (dropAction_ == DnDConstants.ACTION_LINK) { 1560 context.setCursor(DragSource.DefaultLinkDrop); 1561 } else { 1562 context.setCursor(DragSource.DefaultLinkNoDrop); 1563 } 1564 } 1565 1566 1569 protected void setForbiddenIcon(DragSourceEvent dse){ 1570 DragSourceContext context = dse.getDragSourceContext(); 1571 if (dropAction_ == DnDConstants.ACTION_COPY) { 1572 context.setCursor(DragSource.DefaultCopyNoDrop); 1573 } else if (dropAction_ == DnDConstants.ACTION_MOVE) { 1574 context.setCursor(DragSource.DefaultMoveNoDrop); 1575 } else if (dropAction_ == DnDConstants.ACTION_LINK) { 1576 context.setCursor(DragSource.DefaultLinkNoDrop); 1577 } else { 1578 context.setCursor(DragSource.DefaultLinkNoDrop); 1579 } 1580 } 1581 1582 1585 public void dragEnter(DragSourceDragEvent dsde) { 1586 setIcon(dsde); 1587 } 1588 1589 1592 public void dragOver(DragSourceDragEvent dsde) { 1593 } 1594 1595 1598 public void dropActionChanged(DragSourceDragEvent dsde) { 1599 setIcon(dsde); 1600 } 1601 1602 1605 public void dragExit(DragSourceEvent dse) { 1606 setForbiddenIcon(dse); 1607 } 1608 1609 1612 public void dragDropEnd(DragSourceDropEvent dsde) { 1613 } 1614 1615 } 1616 1617 1624 protected class TreeDropTargetListener implements DropTargetListener { 1625 1626 1627 protected Point oldPosition_ = new Point (0,0); 1628 1629 1630 protected TreePath oldPath_ = null; 1631 1632 1633 protected boolean actionChanged_ = false; 1634 1635 1638 public void dragEnter(DropTargetDragEvent dtde) { 1639 dtde.acceptDrag(dtde.getDropAction()); 1640 } 1641 1642 1645 public void dragOver(DropTargetDragEvent dtde) { 1646 dtde.acceptDrag(dtde.getDropAction()); 1647 String label = ""; 1648 TreePath path = getPath(dtde.getLocation()); 1649 if(path==null){ 1650 if(statusBar_!=null) 1651 statusBar_.setText(label); 1652 oldPath_ = null; 1653 } else if(oldPath_ == null || (path != null && !path.equals(oldPath_)) || actionChanged_){ 1654 actionChanged_ = false; 1655 oldPath_ = path; 1656 Entry entry = objectToEntry(path.getLastPathComponent()); 1657 if(custom != null){ 1658 label = custom.getDropLabel((ExtendedEntry) entry, dtde.getDropAction()); 1659 if(label == null) 1660 label = ""; 1661 if(statusBar_ != null) 1662 statusBar_.setText(label); 1663 } 1664 } 1665 } 1666 1667 1670 public void dropActionChanged(DropTargetDragEvent dtde) { 1671 dtde.acceptDrag(dtde.getDropAction()); 1672 actionChanged_ = true; 1673 } 1674 1675 1678 public void dragExit(DropTargetEvent dte) { 1679 } 1680 1681 1684 public void drop(DropTargetDropEvent dtde) { 1685 1686 DataFlavor [] df = EntryViewTransferable.flavors_; 1687 DataFlavor chosen = null; 1688 for (int i = 0; i < df.length; i++) { 1689 if(dtde.isDataFlavorSupported(df[i])){ 1690 chosen = df[i]; 1691 break; 1692 } 1693 } 1694 1695 EntryView entryView = null; 1696 try { 1699 entryView = (EntryView)dtde.getTransferable().getTransferData(chosen); 1700 } catch (UnsupportedFlavorException e1) { 1701 e1.printStackTrace(); 1702 } catch (IOException e1) { 1703 e1.printStackTrace(); 1704 } 1705 1706 TreePath path = getPath(dtde.getLocation()); 1707 if (path != null) { 1708 selectionModel.setSelectionPath(path); 1709 Entry entry = objectToEntry(getLastSelectedPathComponent()); 1710 if(custom != null) { 1711 DropAction dropAction = custom.getDropAction((ExtendedEntry) entry, dtde.getDropAction()); 1712 if(dropAction!=null && entryView!=null){ 1713 try{ 1714 dropAction.execute(new DefaultDropTreeView(DynamicTree.this, entryView.getCurrentEntry())); 1715 }catch(Exception e){ 1716 JOptionPane.showMessageDialog(null, e.getMessage(), "Drag & Drop error ! (" + e.getClass().getName() + ")", JOptionPane.ERROR_MESSAGE); 1717 } 1718 DynamicTree.this.refreshAll(); 1719 } 1720 } 1721 } 1722 } 1723 } 1724 1725 private final class RoleComparator implements Comparator { 1726 1727 protected int compare(Role r1, Role r2) { 1728 return r1.getId().compareTo(r2.getId()); 1729 } 1730 1731 public int compare(Object o1, Object o2) { 1732 return compare((Role)o1,(Role)o2); 1733 } 1734 1735 } 1736 1737 private final class RoleSelected extends AbstractAction { 1738 1739 protected Role role_; 1740 1741 public RoleSelected(Role role){ 1742 super(role.getId()); 1743 role_ = role; 1744 } 1745 1746 public void actionPerformed(ActionEvent e) { 1747 if(DynamicTree.this.multipleRolesEnabled_){ 1748 Role[] currentRoles = DynamicTree.this.getCurrentRoles(); 1749 Vector theRoles = new Vector (Arrays.asList(currentRoles)); 1750 if(theRoles.contains(role_)) 1751 theRoles.remove(role_); 1752 else 1753 theRoles.add(role_); 1754 DynamicTree.this.setCurrentRole((Role[])theRoles.toArray(new Role[0])); 1755 } else { 1756 Role currentRole = (DynamicTree.this.getCurrentRoles())[0]; 1757 if(!role_.getId().equals(currentRole.getId())) { 1758 DynamicTree.this.setCurrentRole(new Role[]{role_}); 1759 } 1760 } 1761 refreshAll(); 1762 } 1763 1764 } 1765 1766} 1767 | Popular Tags |