1 28 29 package org.objectweb.util.explorer.swing.lib; 30 31 import java.awt.Component ; 32 import java.awt.Dimension ; 33 import java.awt.Insets ; 34 import java.awt.Point ; 35 import java.awt.Rectangle ; 36 import java.awt.datatransfer.DataFlavor ; 37 import java.awt.datatransfer.Transferable ; 38 import java.awt.datatransfer.UnsupportedFlavorException ; 39 import java.awt.dnd.Autoscroll ; 40 import java.awt.dnd.DnDConstants ; 41 import java.awt.dnd.DragGestureEvent ; 42 import java.awt.dnd.DragGestureListener ; 43 import java.awt.dnd.DragSource ; 44 import java.awt.dnd.DragSourceContext ; 45 import java.awt.dnd.DragSourceDragEvent ; 46 import java.awt.dnd.DragSourceDropEvent ; 47 import java.awt.dnd.DragSourceEvent ; 48 import java.awt.dnd.DragSourceListener ; 49 import java.awt.dnd.DropTarget ; 50 import java.awt.dnd.DropTargetDragEvent ; 51 import java.awt.dnd.DropTargetDropEvent ; 52 import java.awt.dnd.DropTargetEvent ; 53 import java.awt.dnd.DropTargetListener ; 54 import java.awt.dnd.InvalidDnDOperationException ; 55 import java.awt.event.ActionEvent ; 56 import java.awt.event.MouseAdapter ; 57 import java.awt.event.MouseEvent ; 58 import java.io.IOException ; 59 import java.net.URL ; 60 import java.util.Arrays ; 61 import java.util.Enumeration ; 62 import java.util.HashMap ; 63 import java.util.Iterator ; 64 import java.util.List ; 65 import java.util.Map ; 66 import java.util.StringTokenizer ; 67 import java.util.Vector ; 68 69 import javax.swing.AbstractAction ; 70 import javax.swing.ButtonGroup ; 71 import javax.swing.Icon ; 72 import javax.swing.JCheckBoxMenuItem ; 73 import javax.swing.JMenu ; 74 import javax.swing.JMenuBar ; 75 import javax.swing.JMenuItem ; 76 import javax.swing.JOptionPane ; 77 import javax.swing.JPopupMenu ; 78 import javax.swing.JRadioButtonMenuItem ; 79 import javax.swing.JSeparator ; 80 import javax.swing.JToolBar ; 81 import javax.swing.JTree ; 82 import javax.swing.event.TreeExpansionEvent ; 83 import javax.swing.event.TreeExpansionListener ; 84 import javax.swing.event.TreeSelectionEvent ; 85 import javax.swing.event.TreeSelectionListener ; 86 import javax.swing.event.TreeWillExpandListener ; 87 import javax.swing.tree.DefaultMutableTreeNode ; 88 import javax.swing.tree.DefaultTreeCellRenderer ; 89 import javax.swing.tree.DefaultTreeModel ; 90 import javax.swing.tree.DefaultTreeSelectionModel ; 91 import javax.swing.tree.TreeNode ; 92 import javax.swing.tree.TreePath ; 93 import javax.swing.tree.TreeSelectionModel ; 94 95 import org.objectweb.fractal.api.NoSuchInterfaceException; 96 import org.objectweb.fractal.api.control.BindingController; 97 import org.objectweb.fractal.api.control.ContentController; 98 import org.objectweb.fractal.api.control.IllegalBindingException; 99 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 100 import org.objectweb.fractal.api.control.LifeCycleController; 101 import org.objectweb.fractal.api.control.NameController; 102 import org.objectweb.fractal.api.control.SuperController; 103 import org.objectweb.fractal.api.factory.Factory; 104 import org.objectweb.fractal.api.factory.InstantiationException; 105 import org.objectweb.fractal.util.Fractal; 106 import org.objectweb.util.explorer.api.Context; 107 import org.objectweb.util.explorer.api.DropAction; 108 import org.objectweb.util.explorer.api.Entry; 109 import org.objectweb.util.explorer.api.IconProvider; 110 import org.objectweb.util.explorer.api.Info; 111 import org.objectweb.util.explorer.api.MenuItemTreeView; 112 import org.objectweb.util.explorer.api.RootContext; 113 import org.objectweb.util.explorer.api.RootEntry; 114 import org.objectweb.util.explorer.api.Tree; 115 import org.objectweb.util.explorer.api.TreeView; 116 import org.objectweb.util.explorer.context.api.ContextContainerFactory; 117 import org.objectweb.util.explorer.core.common.api.ContextContainer; 118 import org.objectweb.util.explorer.core.common.api.Description; 119 import org.objectweb.util.explorer.core.common.api.ExplorerConstants; 120 import org.objectweb.util.explorer.core.common.api.Synchronization; 121 import org.objectweb.util.explorer.core.common.api.TextComponent; 122 import org.objectweb.util.explorer.core.common.lib.BindingFeature; 123 import org.objectweb.util.explorer.core.common.lib.DefaultTreeView; 124 import org.objectweb.util.explorer.core.common.lib.RootContextContainer; 125 import org.objectweb.util.explorer.core.common.lib.TreeBindingFeature; 126 import org.objectweb.util.explorer.core.dnd.api.DnDDescription; 127 import org.objectweb.util.explorer.core.dnd.api.DnDDescriptions; 128 import org.objectweb.util.explorer.core.dnd.lib.DefaultDropTreeView; 129 import org.objectweb.util.explorer.core.dnd.lib.EntryTransferable; 130 import org.objectweb.util.explorer.core.menu.lib.DefaultMenuItemTreeView; 131 import org.objectweb.util.explorer.core.naming.lib.DefaultEntry; 132 import org.objectweb.util.explorer.core.role.api.RoleManager; 133 import org.objectweb.util.explorer.core.role.api.RoleProvider; 134 import org.objectweb.util.explorer.core.root.lib.DefaultRootEntry; 135 import org.objectweb.util.explorer.interpreter.api.DescriptionInterpreter; 136 import org.objectweb.util.explorer.resolver.api.PropertyResolver; 137 import org.objectweb.util.explorer.swing.api.Explorer; 138 import org.objectweb.util.explorer.swing.icon.IconFileProvider; 139 import org.objectweb.util.explorer.swing.menu.GenericAction; 140 import org.objectweb.util.trace.Trace; 141 import org.objectweb.util.trace.TraceSystem; 142 143 151 public class DynamicTree 152 extends JTree 153 implements Tree , BindingController, LifeCycleController, Explorer, Autoscroll 154 { 155 156 162 165 protected BindingFeature bindingFeature_ = null; 166 167 170 protected DefaultMutableTreeNode rootNode_; 171 172 175 protected DefaultTreeModel treeModel_; 176 177 180 protected List initialContext_; 181 182 186 protected ContextContainer graphicalInitialContext_; 187 188 194 protected List alreadyInInitialContext_ = null; 195 196 199 protected DefaultTreeSelectionModel selectionModel_; 200 201 202 203 protected boolean popupEnabled_ = true; 204 205 208 protected Map nodeStates_ = null; 209 210 213 protected JMenuBar menuBar_ = null; 214 215 218 protected JMenu actionMenu_ = null; 219 220 223 protected JMenu roleMenu_ = null; 224 225 228 protected JToolBar toolBar_ = null; 229 230 231 protected int nbElementsToRemove_ = 0; 232 233 237 protected boolean multipleRoles_ = false; 238 239 241 242 protected boolean dndEnabled_ = true; 243 244 245 protected DragSource dragSource_; 246 247 248 protected DragGestureListener dragGestureListener_; 249 250 251 protected DragSourceListener dragSourceListener_; 252 253 254 protected int dragAction_ = DnDConstants.ACTION_COPY_OR_MOVE 255 | DnDConstants.ACTION_LINK; 256 257 258 protected int acceptableActions_ = DnDConstants.ACTION_COPY_OR_MOVE 259 | DnDConstants.ACTION_LINK; 260 261 262 protected DropTarget dropTarget_; 263 264 265 protected DropTargetListener dropTargetListener_; 266 267 273 276 private void initDragAndDrop(){ 277 dragSource_ = DragSource.getDefaultDragSource(); 279 dragSourceListener_ = new TreeDragSourceListener(); 280 dragGestureListener_ = new TreeDragGestureListener(); 281 dragSource_.createDefaultDragGestureRecognizer(this, dragAction_, dragGestureListener_); 282 283 dropTargetListener_ = new TreeDropTargetListener(); 285 dropTarget_ = new DropTarget (this, acceptableActions_, dropTargetListener_, true); 286 } 287 288 public DynamicTree() { 289 super(); 290 291 bindingFeature_ = new TreeBindingFeature(); 292 293 initialContext_ = new Vector (); 294 alreadyInInitialContext_ = new Vector (); 295 296 nodeStates_ = new HashMap (); 297 299 rootNode_ = new DefaultMutableTreeNode (new DefaultEntry("Root",graphicalInitialContext_)); 300 treeModel_ = new MyTreeModel(rootNode_); 301 setModel(treeModel_); 302 303 selectionModel_ = new DefaultTreeSelectionModel (); 304 selectionModel_.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); 305 setSelectionModel(selectionModel_); 306 addMouseListener(new MyMouseAdapter()); 307 addTreeWillExpandListener(new MyTreeWillExpandListener()); 308 addTreeExpansionListener(new MyTreeExpansionListener()); 309 addTreeSelectionListener(new MyTreeSelectionListener()); 310 setCellRenderer(new MyTreeCellRenderer()); 311 312 initDragAndDrop(); 313 314 setToolTipText(""); 315 setShowsRootHandles(true); 316 collapseRow(0); 317 expandRow(0); 318 setRootVisible(false); 319 } 320 321 327 protected Trace getTrace(){ 328 return TraceSystem.get("explorer"); 329 } 330 331 337 protected Entry objectToEntry(Object o) { 338 if (o != null) { 339 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) o; 340 return (Entry) node.getUserObject(); 341 } 342 return null; 343 } 344 345 352 protected String pathToString(DefaultMutableTreeNode node) { 353 StringBuffer sf = new StringBuffer (); 354 Object [] o = node.getUserObjectPath(); 355 for (int i = 1; i < o.length; i++) { 356 sf.append(((Entry) o[i]).getName().toString()); 357 if (i != o.length - 1) 358 sf.append("/"); 359 } 360 return sf.toString(); 361 } 362 363 370 protected String pathToString(TreePath path) { 371 if(path!=null) 372 return pathToString((DefaultMutableTreeNode )path.getLastPathComponent()); 373 return ""; 374 375 } 376 377 protected DefaultMutableTreeNode getChildren(DefaultMutableTreeNode parent, String name){ 378 Enumeration enumeration = parent.children(); 379 while (enumeration.hasMoreElements()) { 380 DefaultMutableTreeNode childNode = (DefaultMutableTreeNode ) enumeration.nextElement(); 381 Entry entry = (Entry)childNode.getUserObject(); 382 if(name.equals(entry.getName())){ 383 return childNode; 384 } 385 386 } 387 return null; 388 } 389 390 396 protected TreePath stringToPath(String path) { 397 if (path != null && !path.equals("")) { 398 StringTokenizer st = new StringTokenizer (path,"/"); 402 DefaultMutableTreeNode parentNode = rootNode_; 403 DefaultMutableTreeNode childNode = null; 404 while (st.hasMoreTokens()) { 405 String currentPath = st.nextToken(); 406 childNode = getChildren(parentNode, currentPath); 407 if(childNode!=null){ 408 parentNode = childNode; 409 } else { 410 return new TreePath (parentNode.getPath()); 411 } 412 } 413 return new TreePath (childNode.getPath()); 414 } 415 return null; 416 } 417 418 423 protected TreePath getPath(Point p) { 424 if (p != null) { 425 return getPathForLocation((int) p.getX(), (int) p.getY()); 426 } 427 return null; 428 } 429 430 433 protected Object getSelectedObject(){ 434 Entry entry = getSelectedEntry(); 435 if(entry!=null){ 436 return entry.getValue(); 437 } 438 return null; 439 } 440 441 444 protected Entry getParentEntry() { 445 Object object = getLastSelectedPathComponent(); 446 if (object != null) 447 return objectToEntry(((DefaultMutableTreeNode ) object).getParent()); 448 return null; 449 } 450 451 455 protected TreeView getTreeView() { 456 return new DefaultTreeView((Tree )this, getSelectedEntry(), getParentEntry()); 457 } 458 459 463 protected MenuItemTreeView getMenuItemTreeView() { 464 return new DefaultMenuItemTreeView((Tree )this, getSelectedEntry(), getParentEntry()); 465 } 466 467 470 protected void createGraphicalInitialContext() { 471 getGraphicalInitialContext().clear(); 473 Description desc = getPropertyResolver().resolve(ExplorerConstants.ROOT_PROPERTY, null); 475 if(desc!=null && ! desc.isEmpty()){ 476 RootContext rootContext = (RootContext) getDescriptionInterpreter().interprete(desc, null); 477 RootEntry[] entries = rootContext.getEntries(); 478 for (int i = 0; i < entries.length; i++) { 479 addToInitialContext(entries[i]); 480 } 481 } 482 for(int i=0 ; i<initialContext_.size() ; i++) { 484 RootEntry rootEntry = (RootEntry)initialContext_.get(i); 485 RootEntry newRootEntry = new DefaultRootEntry(rootEntry.getName(), rootEntry.getValue(), rootEntry.getLevel()); 488 addToInitialContext(newRootEntry); 489 } 490 } 491 492 499 protected void addToInitialContext(RootEntry entry) { 500 if (entry!=null) { 501 getGraphicalInitialContext().addEntry(entry); 502 refreshRootNode(); 503 String id = entry.getName().toString(); 506 if(!alreadyInInitialContext_.contains(id)){ 507 if (entry.getLevel() > 0) { 508 TreePath path = stringToPath(id); 509 if (path != null) { 510 expandPath(path); 511 openTree(path, entry.getLevel() - 1); 512 } 513 } 514 alreadyInInitialContext_.add(id); 515 } 516 } 517 } 518 519 525 protected void openTree(TreePath root, int level) { 526 if (level > 0) { 527 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) root.getLastPathComponent(); 528 Enumeration enumeration = node.children(); 529 while (enumeration.hasMoreElements()) { 530 DefaultMutableTreeNode childNode = (DefaultMutableTreeNode ) enumeration.nextElement(); 531 TreePath p = new TreePath (childNode.getPath()); 532 expandPath(p); 533 openTree(p, level - 1); 534 } 535 } 539 } 540 541 548 protected void updateInitialPathes(String currentName, String newName){ 549 Object [] keys = nodeStates_.keySet().toArray(); 550 for(int i=0 ; i<keys.length ; i++) { 551 if(keys[i].toString().startsWith(currentName)){ 552 String suffixe = ((String )keys[i]).substring(currentName.length()); 553 String currentPath = currentName + suffixe; 554 String newPath = newName + suffixe; 555 nodeStates_.put(newPath,nodeStates_.get(currentPath)); 556 nodeStates_.remove(currentPath); 557 } 558 } 559 } 560 561 565 568 protected PropertyResolver getPropertyResolver() { 569 try { 570 return (PropertyResolver) bindingFeature_.lookupFc(PropertyResolver.PROPERTY_RESOLVER); 571 } catch (NoSuchInterfaceException e) { 572 getTrace().warn(PropertyResolver.PROPERTY_RESOLVER + ": interface not found!"); 573 return null; 574 } 575 } 576 577 580 protected DescriptionInterpreter getDescriptionInterpreter() { 581 try { 582 return (DescriptionInterpreter) bindingFeature_.lookupFc(DescriptionInterpreter.DESCRIPTION_INTERPRETER); 583 } catch (NoSuchInterfaceException e) { 584 getTrace().warn(DescriptionInterpreter.DESCRIPTION_INTERPRETER + ": interface not found!"); 585 return null; 586 } 587 } 588 589 592 protected RoleManager getRoleManager() { 593 try { 594 return (RoleManager) bindingFeature_.lookupFc(RoleManager.ROLE_MANAGER); 595 } catch (NoSuchInterfaceException e) { 596 getTrace().warn(RoleManager.ROLE_MANAGER + ": interface not found!"); 597 return null; 598 } 599 } 600 601 604 protected RoleProvider getRoleProvider() { 605 try { 606 return (RoleProvider) bindingFeature_.lookupFc(RoleProvider.ROLE_PROVIDER); 607 } catch (NoSuchInterfaceException e) { 608 getTrace().warn(RoleProvider.ROLE_PROVIDER + ": interface not found!"); 609 return null; 610 } 611 } 612 613 616 protected TextComponent getTextComponent(){ 617 try { 618 return (TextComponent) bindingFeature_.lookupFc(TextComponent.TEXT_COMPONENT); 619 } catch (NoSuchInterfaceException e) { 620 getTrace().warn(TextComponent.TEXT_COMPONENT + ": interface not found!"); 621 return null; 622 } 623 } 624 625 628 protected org.objectweb.fractal.api.Component getTreeTemplate(){ 629 try { 630 return (org.objectweb.fractal.api.Component) bindingFeature_.lookupFc(Tree.TREE_TMPL); 631 } catch (NoSuchInterfaceException e) { 632 getTrace().warn(Tree.TREE_TMPL + ": interface not found!"); 633 return null; 634 } 635 } 636 637 640 protected ContextContainerFactory getContextContainerFactory(){ 641 try { 642 return (ContextContainerFactory) bindingFeature_.lookupFc(ContextContainerFactory.CONTEXT_CONTAINER_FACTORY); 643 } catch (NoSuchInterfaceException e) { 644 getTrace().warn(ContextContainerFactory.CONTEXT_CONTAINER_FACTORY + ": interface not found!"); 645 return null; 646 } 647 } 648 649 653 657 protected void dispatchSynchronization(TreeView treeView){ 658 String [] clientIftNames = listFc(); 659 for (int i = 0; i < clientIftNames.length; i++) { 660 if(clientIftNames[i].startsWith(Synchronization.SYNCHRONIZATION)){ 661 try { 662 ((Synchronization)lookupFc(clientIftNames[i])).refresh(treeView); 663 } catch (NoSuchInterfaceException e) { 664 getTrace().warn(e.getMessage()); 665 } catch (Exception e) { 666 getTrace().warn(e.getMessage()); 667 e.printStackTrace(); 668 JOptionPane.showMessageDialog(null, e.getMessage(), "Error! (" + e.getClass().getName() + ")", JOptionPane.ERROR_MESSAGE); 669 } 670 } 671 } 672 } 673 674 protected void dispatchSynchronization(){ 675 dispatchSynchronization(getTreeView()); 676 } 677 678 681 protected void refreshPath(TreePath treePath){ 682 collapsePath(treePath); 683 expandPath(treePath); 684 } 685 686 689 protected void refreshRootNode(){ 690 refreshPath(new TreePath (rootNode_.getPath())); 691 } 692 693 697 protected void refreshActionMenu(Entry entry){ 698 if(actionMenu_!=null && entry!=null){ 699 actionMenu_.removeAll(); 700 Description desc = getPropertyResolver().resolve(ExplorerConstants.MENU_PROPERTY, entry, getParentEntry()); 701 if(desc!=null && ! desc.isEmpty()){ 702 JPopupMenu menu = (JPopupMenu ) getDescriptionInterpreter().interprete(desc, getMenuItemTreeView()); 703 Component [] componentList = menu.getComponents(); 704 for(int i=0; i<componentList.length;i++){ 705 if(JSeparator .class.isAssignableFrom(componentList[i].getClass())){ 706 actionMenu_.addSeparator(); 707 } else { 708 actionMenu_.add((JMenuItem )componentList[i]); 709 } 710 } 711 actionMenu_.setEnabled(true); 712 } else { 714 actionMenu_.setEnabled(false); 715 } 716 } 717 } 718 719 723 protected void refreshToolBar(Entry entry){ 724 if(toolBar_!=null && entry!=null){ 725 if(nbElementsToRemove_>0){ 727 int nbComp = toolBar_.getComponentCount(); 728 for(int i = nbComp - 1 ; i >= nbComp - nbElementsToRemove_ ; i--) 729 toolBar_.remove(i); 730 nbElementsToRemove_ = 0; 731 } 732 733 Description desc = getPropertyResolver().resolve(ExplorerConstants.MENU_PROPERTY, entry, getParentEntry()); 734 if(desc!=null && ! desc.isEmpty()){ 735 boolean iconDisplayed = false; 736 JPopupMenu menu = (JPopupMenu ) getDescriptionInterpreter().interprete(desc, getMenuItemTreeView()); 737 Component [] componentList = menu.getComponents(); 738 for(int i=0; i<componentList.length;i++){ 739 if(!JSeparator .class.isAssignableFrom(componentList[i].getClass())){ 740 if(((GenericAction)((JMenuItem )componentList[i]).getAction()).isUserIcon()){ 741 toolBar_.add(((JMenuItem )componentList[i]).getAction()); 743 nbElementsToRemove_++; 744 iconDisplayed = true; 745 } 746 } else if (iconDisplayed) { 747 toolBar_.addSeparator(); 749 iconDisplayed = false; 750 nbElementsToRemove_++; 751 } 752 } 753 } 754 755 if(toolBar_.getComponentCount()==0){ 756 toolBar_.setVisible(false); 757 } else{ 758 toolBar_.setVisible(false); 759 toolBar_.setVisible(true); 760 } 761 } 762 763 } 764 765 770 protected void refreshActionMenu(){ 771 refreshActionMenu(getSelectedEntry()); 772 } 773 774 779 protected void refreshToolBar(){ 780 refreshToolBar(getSelectedEntry()); 781 } 782 783 786 protected void refreshAllPathes(){ 787 String selectedPath = pathToString(getSelectionPath()); 789 createGraphicalInitialContext(); 790 refreshRootNode(); 792 selectPath(selectedPath); 793 Entry selectedEntry = getSelectedEntry(); 794 refreshActionMenu(selectedEntry); 796 refreshToolBar(selectedEntry); 797 } 798 799 802 protected void refreshRoleMenu(){ 803 if(menuBar_!=null){ 804 String [] roleList = getRoleProvider().getRoleList(true, true); 805 String [] currentRoles = getRoleManager().getCurrentRoleIds(); 806 if(roleMenu_ == null){ 807 roleMenu_ = new JMenu ("Roles"); 808 menuBar_.add(roleMenu_); 809 } 810 roleMenu_.removeAll(); 811 if(roleList.length<=0){ 812 roleMenu_.setVisible(false); 813 } else { 814 roleMenu_.setVisible(true); 815 if(multipleRoles_){ 816 List theRoles = Arrays.asList(currentRoles); 817 for (int i = 0; i < roleList.length; i++) { 818 JCheckBoxMenuItem cbMenuItem = new JCheckBoxMenuItem (new RoleSelected(roleList[i])); 819 cbMenuItem.setSelected(theRoles.contains(roleList[i])); 820 roleMenu_.add(cbMenuItem); 821 } 822 } else { 823 ButtonGroup roleGroup = new ButtonGroup (); 824 JRadioButtonMenuItem rbMenuItem = null; 825 for(int i=0; i<roleList.length;i++){ 826 rbMenuItem = new JRadioButtonMenuItem (new RoleSelected(roleList[i])); 827 if(currentRoles.length>0) 828 rbMenuItem.setSelected(currentRoles[0].equals(roleList[i])); 829 roleGroup.add(rbMenuItem); 830 roleMenu_.add(rbMenuItem); 831 } 832 } 833 } 834 } 835 } 836 837 841 847 protected DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Entry child) { 848 Description desc = getPropertyResolver().resolve(ExplorerConstants.WRAPPER_PROPERTY, child); 849 if(desc!=null){ 850 Context context = (Context ) getDescriptionInterpreter().interprete(desc, getTreeView()); 851 if (context != null) { 852 child.setWrapper(getContextContainerFactory().create(context)); 853 } 854 } 855 DefaultMutableTreeNode childNode = new DefaultMutableTreeNode (child); 856 if (parent == null) { 857 parent = rootNode_; 858 } 859 treeModel_.insertNodeInto(childNode, parent, parent.getChildCount()); 860 return childNode; 861 } 862 863 867 872 protected void showMenu(Point p) { 873 TreePath path = getPath(p); 874 if (path != null) { 875 selectionModel_.setSelectionPath(path); 876 Description desc = getPropertyResolver().resolve(ExplorerConstants.MENU_PROPERTY, getSelectedEntry(), getParentEntry()); 877 JPopupMenu menu = (JPopupMenu ) getDescriptionInterpreter().interprete(desc, getMenuItemTreeView()); 878 if (menu != null) { 879 menu.show((Component ) this, (int) (p.getX()), (int) (p.getY())); 880 } 881 } 882 } 883 884 888 protected Icon getImageIcon(Entry entry){ 889 if(getPropertyResolver()!=null){ 890 Description desc = getPropertyResolver().resolve(ExplorerConstants.ICON_PROPERTY, entry); 891 IconProvider iconProvider = (IconProvider) getDescriptionInterpreter().interprete(desc, getTreeView()); 892 if(iconProvider!=null){ 893 Object object = iconProvider.newIcon(entry.getValue()); 894 if(object instanceof Icon ){ 895 return (Icon )object; 896 } else if(object instanceof String ) { 897 return (Icon )(new IconFileProvider((String )object)).newIcon(entry.getValue()); 898 } else if (object instanceof URL ) { 899 return (Icon )(new IconFileProvider((URL )object)).newIcon(entry.getValue()); 900 } 901 } 902 } 903 return null; 904 } 905 906 910 protected void displayInfo(Entry entry){ 911 if (getTextComponent() != null){ 912 String message = ""; 913 Description desc = getPropertyResolver().resolve(ExplorerConstants.INFO_PROPERTY, entry); 914 if(desc!=null){ 915 Info info = (Info) getDescriptionInterpreter().interprete(desc, null); 916 if (info != null) { 917 message = info.getInfo(getTreeView()); 918 } 919 } 920 getTextComponent().setText(message); 921 } 922 } 923 924 930 933 public void refreshAll() { 934 refreshAllPathes(); 935 } 936 937 943 public void addEntry(Object name, Object value) { 944 addEntry(name, value, 0); 945 } 946 947 953 public void addEntry(Object name, Object value, int level) { 954 RootEntry rootEntry = new DefaultRootEntry(name, value, level); 955 initialContext_.add(rootEntry); 956 addToInitialContext(rootEntry); 957 } 958 959 962 public void 963 selectPath(String path){ 964 TreePath treePath = stringToPath(path); 965 setSelectionPath(treePath); 966 } 967 968 971 public void clear() { 972 initialContext_.clear(); 973 } 974 975 978 public int getInitialContextSize() { 979 return getGraphicalInitialContext().getSize(); 980 } 981 982 985 public void removeEntry(Object name) { 986 Iterator it = initialContext_.iterator(); 987 RootEntry elementToRemove = null; 988 while (it.hasNext()) { 989 RootEntry element = (RootEntry) it.next(); 990 if(element.getName().toString().equals(name.toString())) { 991 elementToRemove = element; 992 break; 993 } 994 } 995 if(elementToRemove!=null) 996 initialContext_.remove(elementToRemove); 997 } 998 999 1002 public void 1003 renameInitialEntry(Object currentName, Object newName){ 1004 Iterator it = initialContext_.iterator(); 1005 while (it.hasNext()) { 1006 RootEntry element = (RootEntry) it.next(); 1007 if(element.getName().toString().equals(currentName.toString())) { 1008 element.setName(newName.toString()); 1009 break; 1010 } 1011 } 1012 updateInitialPathes(currentName.toString(), newName.toString()); 1013 refreshAll(); 1014 selectPath(newName.toString()); 1015 } 1016 1017 1018 1019 1022 public org.objectweb.fractal.api.Component duplicate() { 1023 return duplicate(true); 1024 } 1025 1026 1029 public Entry getSelectedEntry() { 1030 Object object = getLastSelectedPathComponent(); 1031 if (object != null) 1032 return objectToEntry(object); 1033 return null; 1034 } 1035 1036 1039 public org.objectweb.fractal.api.Component duplicate(boolean withData) { 1040 try { 1041 org.objectweb.fractal.api.Component tmpl = getTreeTemplate(); 1043 Factory factory = (Factory) tmpl.getFcInterface("factory"); 1044 org.objectweb.fractal.api.Component instance = factory.newFcInstance(); 1045 1046 NameController nameController = (NameController)instance.getFcInterface("name-controller"); 1048 nameController.setFcName("tree-cpt-" + System.currentTimeMillis()); 1049 1050 org.objectweb.fractal.api.Component treeComponent = (org.objectweb.fractal.api.Component)lookupFc(Tree.TREE_ITSELF); 1052 SuperController sc = (SuperController)treeComponent.getFcInterface("super-controller"); 1053 org.objectweb.fractal.api.Component[] superComponents = sc.getFcSuperComponents(); 1054 if(superComponents !=null && superComponents.length > 0){ 1055 org.objectweb.fractal.api.Component superComponent = superComponents[0]; 1056 ContentController cc = (ContentController)superComponent.getFcInterface("content-controller"); 1057 cc.addFcSubComponent(instance); 1058 } 1059 1060 BindingController bc = (BindingController)instance.getFcInterface("binding-controller"); 1062 bc.bindFc(PropertyResolver.PROPERTY_RESOLVER, bindingFeature_.lookupFc(PropertyResolver.PROPERTY_RESOLVER)); 1063 bc.bindFc(DescriptionInterpreter.DESCRIPTION_INTERPRETER, bindingFeature_.lookupFc(DescriptionInterpreter.DESCRIPTION_INTERPRETER)); 1064 bc.bindFc(ContextContainerFactory.CONTEXT_CONTAINER_FACTORY, bindingFeature_.lookupFc(ContextContainerFactory.CONTEXT_CONTAINER_FACTORY)); 1065 1066 Fractal.getLifeCycleController(instance).startFc(); 1067 if(withData) { 1068 Tree tree = (Tree )instance.getFcInterface(Tree.TREE); 1069 Entry[] entries = getGraphicalInitialContext().getEntries(null); 1070 for (int i = 0; i < entries.length; i++) { 1071 try{ 1072 tree.addEntry(entries[i].getName(), entries[i].getValue(),1); 1073 } catch(Exception e){ 1074 e.printStackTrace(); 1075 } 1076 } 1077 } 1078 return instance; 1079 } catch (NoSuchInterfaceException ex) { 1080 ex.printStackTrace(); 1081 } catch (IllegalLifeCycleException ex) { 1082 ex.printStackTrace(); 1083 } catch (InstantiationException ex) { 1084 ex.printStackTrace(); 1085 } catch (IllegalBindingException ex) { 1086 ex.printStackTrace(); 1087 } catch (Exception ex) { 1088 ex.printStackTrace(); 1089 throw new RuntimeException ("Error during replication!!!"); 1090 } 1091 return null; 1092 } 1093 1094 1097 public void renameSelectedNode(Object oldName, Object newName) { 1098 String selectedPath = pathToString(getSelectionPath()); 1099 String prefixe = selectedPath.substring(0,selectedPath.lastIndexOf(oldName.toString())); 1100 if(!selectedPath.equals(prefixe)){ 1101 updateInitialPathes(prefixe + oldName, prefixe + newName); 1102 refreshAll(); 1103 selectPath(prefixe + newName); 1104 } 1105 } 1106 1107 1110 public void close() { 1111 dispatchSynchronization(null); 1112 } 1113 1114 1120 1125 public JTree getTree() { 1126 return this; 1127 } 1128 1129 1132 public void setMenuBar(JMenuBar menuBar) { 1133 menuBar_ = menuBar; 1134 actionMenu_ = new JMenu ("Actions"); 1135 actionMenu_.setEnabled(false); 1136 menuBar_.add(actionMenu_); 1137 refreshRoleMenu(); 1138 } 1139 1140 1143 public void setToolBar(JToolBar toolBar) { 1144 toolBar_ = toolBar; 1145 if(toolBar_!=null && toolBar_.getComponents().length==0){ 1146 toolBar_.setVisible(false); 1147 } else { 1148 toolBar_.addSeparator(); 1149 } 1150 } 1151 1152 1155 public boolean isMultipleRoles() { 1156 return multipleRoles_; 1157 } 1158 1159 1162 public void setCurrentRoles(String [] roleIds) { 1163 getRoleManager().setCurrentRoleIds(roleIds); 1164 refreshRoleMenu(); 1165 refreshAllPathes(); 1166 } 1167 1168 1171 public void setMultipleRoles(boolean multipleRoles) { 1172 multipleRoles_ = multipleRoles; 1173 refreshRoleMenu(); 1174 } 1175 1176 1179 public boolean isDragAndDropEnabled() { 1180 return dndEnabled_; 1181 } 1182 1185 public boolean isPopupEnabled() { 1186 return popupEnabled_; 1187 } 1188 1191 public void setDragAndDropEnabled(boolean dndEnabled) { 1192 dndEnabled_ = dndEnabled; 1193 } 1194 1197 public void setPopupEnabled(boolean popupEnabled) { 1198 popupEnabled_ = popupEnabled; 1199 } 1200 1201 1207 1213 public void bindFc(String itfName, Object itfValue) 1214 throws NoSuchInterfaceException, IllegalBindingException, 1215 IllegalLifeCycleException { 1216 bindingFeature_.bindFc(itfName, itfValue); 1217 } 1218 1219 1224 public String [] listFc() { 1225 return bindingFeature_.listFc(); 1226 } 1227 1228 1233 public Object lookupFc(String itfName) throws NoSuchInterfaceException { 1234 return bindingFeature_.lookupFc(itfName); 1235 } 1236 1237 1242 public void unbindFc(String itfName) throws NoSuchInterfaceException, 1243 IllegalBindingException, IllegalLifeCycleException { 1244 bindingFeature_.unbindFc(itfName); 1245 } 1246 1247 1253 public String getFcState () { 1254 return null; 1255 } 1256 1257 public void startFc () { 1258 1264 } 1265 1266 public void stopFc () { 1267 } 1269 1270 protected ContextContainer getGraphicalInitialContext(){ 1271 if(graphicalInitialContext_==null){ 1272 graphicalInitialContext_ = getContextContainerFactory().create(new RootContextContainer()); 1273 rootNode_ = new DefaultMutableTreeNode (new DefaultEntry("Root",graphicalInitialContext_)); 1274 treeModel_ = new MyTreeModel(rootNode_); 1275 setModel(treeModel_); 1276 } 1277 return graphicalInitialContext_; 1278 } 1279 1280 1286 1290 final class MyMouseAdapter extends MouseAdapter { 1291 public void mousePressed(MouseEvent e) { 1292 popupLayout(e); 1293 } 1294 1295 public void mouseReleased(MouseEvent e) { 1296 popupLayout(e); 1297 } 1298 1299 private void popupLayout(MouseEvent e) { 1300 if (e.isPopupTrigger() && popupEnabled_) { 1301 showMenu(e.getPoint()); 1302 } 1303 } 1304 } 1305 1306 1310 final class MyTreeWillExpandListener implements TreeWillExpandListener { 1311 public void treeWillCollapse(TreeExpansionEvent event) { 1312 DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode ) event.getPath().getLastPathComponent(); 1313 Entry e = (Entry) currentNode.getUserObject(); 1314 nodeStates_.put(pathToString(currentNode), Boolean.FALSE); 1315 } 1316 1317 public void treeWillExpand(TreeExpansionEvent event) { 1318 DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode ) event.getPath().getLastPathComponent(); 1319 Entry e = (Entry) currentNode.getUserObject(); 1320 nodeStates_.put(pathToString(currentNode), Boolean.TRUE); 1321 currentNode.removeAllChildren(); 1322 Entry[] entries = e.getWrapper().getEntries(e.getValue()); 1323 for (int i = 0; i < entries.length; i++) { 1324 DefaultMutableTreeNode childNode = addObject(currentNode, entries[i]); 1325 } 1326 treeModel_.reload(currentNode); 1327 } 1328 } 1329 1330 1334 final class MyTreeExpansionListener implements TreeExpansionListener { 1335 public void treeCollapsed(TreeExpansionEvent event) { 1336 } 1337 1338 public void treeExpanded(TreeExpansionEvent event) { 1339 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) (event.getPath()).getLastPathComponent(); 1340 if (!node.isLeaf()) { 1341 Enumeration enumeration = node.children(); 1342 while (enumeration.hasMoreElements()) { 1343 DefaultMutableTreeNode childNode = (DefaultMutableTreeNode ) enumeration.nextElement(); 1344 Entry entry = (Entry) childNode.getUserObject(); 1345 String pathName = pathToString(childNode); 1346 if (nodeStates_.containsKey(pathName)) { 1347 if (((Boolean )nodeStates_.get(pathName)).booleanValue()) { 1348 expandPath(new TreePath (childNode.getPath())); 1349 } 1350 } 1351 } 1355 } 1356 } 1357 } 1358 1359 1363 final class MyTreeCellRenderer extends DefaultTreeCellRenderer { 1364 1365 public Component getTreeCellRendererComponent(JTree tree,Object value,boolean selected,boolean expanded,boolean leaf,int row,boolean hasFocus) { 1366 super.getTreeCellRendererComponent(tree,value,selected,expanded,leaf,row,hasFocus); 1367 1368 Entry entry = objectToEntry(value); 1370 Icon icon = getImageIcon(entry); 1371 if(icon!=null){ 1372 setIcon(icon); 1373 } 1374 1375 setText(entry.getName().toString()); 1377 1378 return this; 1379 } 1380 1381 } 1382 1383 1387 final class MyTreeSelectionListener implements TreeSelectionListener { 1388 1389 public void valueChanged(TreeSelectionEvent e) { 1390 1394 Entry selectedEntry = getSelectedEntry(); 1396 displayInfo(selectedEntry); 1397 refreshActionMenu(selectedEntry); 1398 refreshToolBar(selectedEntry); 1399 dispatchSynchronization(); 1400 1401 1406 } 1407 } 1408 1409 1413 final class MyTreeModel extends DefaultTreeModel { 1414 1415 public MyTreeModel(TreeNode root) { 1416 super(root); 1417 } 1418 1419 public boolean isLeaf(Object node) { 1420 Entry entry = objectToEntry(node); 1421 return (entry.getWrapper()==null && !(entry.getValue() instanceof Context )); 1422 } 1423 1424 } 1425 1426 1432 public String getToolTipText(MouseEvent evt) { 1433 StringBuffer label = new StringBuffer (); 1434 TreePath currentPath = getPathForLocation(evt.getX(), evt.getY()); 1435 if(currentPath!=null){ 1436 Entry entry = objectToEntry(currentPath.getLastPathComponent()); 1437 DnDDescriptions desc = (DnDDescriptions)getPropertyResolver().resolve(ExplorerConstants.DND_PROPERTY, entry); 1438 if(desc != null && !desc.isEmpty()){ 1439 String [] labels = desc.getActionsDescription(); 1440 if(labels.length>0){ 1441 label.append("<html>"); 1442 label.append("Drop action:"); 1443 for (int i = 0; i < labels.length; i++) { 1444 label.append("<br> - " + labels[i]); 1445 } 1446 label.append("</html>"); 1447 } 1448 } 1449 } 1450 return label.toString(); 1451 } 1452 1453 1459 public void autoscroll(Point cursorLocn){ 1460 Rectangle rect=getVisibleRect(); 1461 Dimension dim=getSize(); 1462 1463 if(cursorLocn.y>rect.y+rect.height-10) { 1465 rect.y=Math.min(rect.y+10,dim.height-rect.height); 1466 } else if(cursorLocn.y<rect.y+10) { 1467 int i = rect.y+rect.height-10; 1468 rect.y=Math.max(rect.y-10,0); 1469 } 1470 1471 if(cursorLocn.x>rect.x+rect.width-10) { 1473 rect.x=Math.min(rect.x+10,dim.width-rect.width); 1474 } else if(cursorLocn.x<rect.x+10) { 1475 rect.x=Math.max(rect.x-10,0); 1476 } 1477 1478 scrollRectToVisible(rect); 1479 } 1480 1481 public Insets getAutoscrollInsets(){ 1482 Rectangle rect=getVisibleRect(); 1483 Dimension dim=getSize(); 1484 Insets inset=new Insets (rect.y+10,rect.x+10,dim.height-(rect.y+rect.height)+10,dim.width-(rect.x+rect.width)+10); 1485 return inset; 1486 } 1487 1488 1494 1501 protected class TreeDragGestureListener implements DragGestureListener { 1502 1503 1506 public void dragGestureRecognized(DragGestureEvent dge) { 1507 if(dndEnabled_){ 1508 Transferable transferable = new EntryTransferable(DynamicTree.this.getSelectedEntry()); 1509 try { 1510 dragSource_.startDrag(dge,DragSource.DefaultLinkDrop, transferable, dragSourceListener_); 1511 }catch(InvalidDnDOperationException idoe) { 1512 System.out.println( idoe ); 1513 } 1514 } 1515 } 1516 } 1517 1518 1526 public class TreeDragSourceListener implements DragSourceListener { 1527 1528 1529 protected int dropAction_; 1530 1531 1544 1545 1550 protected int getDropAction(int gestureModifiers){ 1551 if(gestureModifiers==16) 1552 return DnDConstants.ACTION_MOVE; 1553 else if(gestureModifiers==18) 1554 return DnDConstants.ACTION_COPY; 1555 else if(gestureModifiers==19) 1556 return DnDConstants.ACTION_LINK; 1557 else 1558 return 0; 1559 } 1560 1561 1564 protected void setIcon(DragSourceDragEvent dsde){ 1565 DragSourceContext context = dsde.getDragSourceContext(); 1566 dropAction_ = getDropAction(dsde.getGestureModifiers()); 1567 if (dropAction_ == DnDConstants.ACTION_COPY) { 1568 context.setCursor(DragSource.DefaultCopyDrop); 1569 } else if (dropAction_ == DnDConstants.ACTION_MOVE) { 1570 context.setCursor(DragSource.DefaultMoveDrop); 1571 } else if (dropAction_ == DnDConstants.ACTION_LINK) { 1572 context.setCursor(DragSource.DefaultLinkDrop); 1573 } else { 1574 context.setCursor(DragSource.DefaultLinkNoDrop); 1575 } 1576 } 1577 1578 1581 protected void setForbiddenIcon(DragSourceEvent dse){ 1582 DragSourceContext context = dse.getDragSourceContext(); 1583 if (dropAction_ == DnDConstants.ACTION_COPY) { 1584 context.setCursor(DragSource.DefaultCopyNoDrop); 1585 } else if (dropAction_ == DnDConstants.ACTION_MOVE) { 1586 context.setCursor(DragSource.DefaultMoveNoDrop); 1587 } else if (dropAction_ == DnDConstants.ACTION_LINK) { 1588 context.setCursor(DragSource.DefaultLinkNoDrop); 1589 } else { 1590 context.setCursor(DragSource.DefaultLinkNoDrop); 1591 } 1592 } 1593 1594 1597 public void dragEnter(DragSourceDragEvent dsde) { 1598 setIcon(dsde); 1599 } 1600 1601 1604 public void dragOver(DragSourceDragEvent dsde) { 1605 } 1606 1607 1610 public void dropActionChanged(DragSourceDragEvent dsde) { 1611 setIcon(dsde); 1612 } 1613 1614 1617 public void dragExit(DragSourceEvent dse) { 1618 setForbiddenIcon(dse); 1619 } 1620 1621 1624 public void dragDropEnd(DragSourceDropEvent dsde) { 1625 } 1626 1627 } 1628 1629 1636 protected class TreeDropTargetListener implements DropTargetListener { 1637 1638 1639 protected TreePath oldPath_ = null; 1640 1641 1642 protected boolean actionChanged_ = false; 1643 1644 protected String getDropActionName(int dropAction){ 1645 if(dropAction==DnDConstants.ACTION_MOVE) 1646 return DnDDescription.MOVE_ACTION; 1647 else if(dropAction==DnDConstants.ACTION_COPY) 1648 return DnDDescription.COPY_ACTION; 1649 else if(dropAction==DnDConstants.ACTION_LINK) 1650 return DnDDescription.LINK_ACTION; 1651 else 1652 return ""; 1653 } 1654 1655 protected String computeActionDesc(Entry entry, String dropAction){ 1656 DnDDescriptions desc = (DnDDescriptions)getPropertyResolver().resolve(ExplorerConstants.DND_PROPERTY, entry); 1657 if(desc!=null){ 1658 DnDDescription dndDesc = desc.getDropAction(dropAction); 1659 if(dndDesc !=null){ 1660 return dndDesc.getLabel(); 1661 } 1662 } 1663 return ""; 1664 } 1665 1666 1669 public void dragEnter(DropTargetDragEvent dtde) { 1670 dtde.acceptDrag(dtde.getDropAction()); 1671 } 1672 1673 1676 public void dragOver(DropTargetDragEvent dtde) { 1677 dtde.acceptDrag(dtde.getDropAction()); 1678 String label = ""; 1679 TreePath path = getPath(dtde.getLocation()); 1680 if(path==null){ 1681 oldPath_ = null; 1682 if(getTextComponent()!=null){ 1683 getTextComponent().setText(""); 1684 } 1685 } else if(oldPath_ == null || (path != null && !path.equals(oldPath_)) || actionChanged_){ 1686 actionChanged_ = false; 1687 oldPath_ = path; 1688 Entry entry = objectToEntry(path.getLastPathComponent()); 1689 String value = computeActionDesc(entry, getDropActionName(dtde.getDropAction())); 1690 if(getTextComponent()!=null){ 1691 getTextComponent().setText(value); 1692 } 1693 } 1694 } 1695 1696 1699 public void dropActionChanged(DropTargetDragEvent dtde) { 1700 dtde.acceptDrag(dtde.getDropAction()); 1701 actionChanged_ = true; 1702 } 1703 1704 1707 public void dragExit(DropTargetEvent dte) { 1708 } 1709 1710 1713 public void drop(DropTargetDropEvent dtde) { 1714 1715 DataFlavor [] df = EntryTransferable.flavors_; 1716 DataFlavor chosen = null; 1717 for (int i = 0; i < df.length; i++) { 1718 if(dtde.isDataFlavorSupported(df[i])){ 1719 chosen = df[i]; 1720 break; 1721 } 1722 } 1723 1724 if(getTextComponent()!=null){ 1725 getTextComponent().setText(""); 1726 } 1727 1728 Entry dragEntry = null; 1729 try { 1732 dragEntry = (Entry)dtde.getTransferable().getTransferData(chosen); 1733 } catch (UnsupportedFlavorException e1) { 1734 getTrace().warn("[" + getClass().getName() + "] UnsupportedFlavorException: " + e1.getMessage()); 1735 } catch (IOException e1) { 1736 e1.printStackTrace(); 1737 } 1738 1739 TreePath path = getPath(dtde.getLocation()); 1740 if (path != null) { 1741 selectionModel.setSelectionPath(path); 1742 Entry dropEntry = objectToEntry(getLastSelectedPathComponent()); 1743 DnDDescriptions desc = (DnDDescriptions)getPropertyResolver().resolve(ExplorerConstants.DND_PROPERTY, dropEntry); 1744 if(desc!=null){ 1745 DnDDescription dndDesc = desc.getDropAction(getDropActionName(dtde.getDropAction())); 1746 if(dndDesc !=null){ 1747 DropAction dropAction = (DropAction)getDescriptionInterpreter().interprete(dndDesc.getCodeDescription(),null); 1748 if(dropAction!=null && dragEntry!=null){ 1749 try{ 1750 dropAction.execute(new DefaultDropTreeView((Tree )DynamicTree.this, dropEntry, getParentEntry(), dragEntry)); 1751 }catch(Exception e){ 1752 JOptionPane.showMessageDialog(null, e.getMessage(), "Drag & Drop error ! (" + e.getClass().getName() + ")", JOptionPane.ERROR_MESSAGE); 1753 } 1754 DynamicTree.this.refreshAllPathes(); 1755 } 1756 } 1757 } 1758 } 1759 1760 } 1761 } 1762 1763 1771 protected final class RoleSelected extends AbstractAction { 1772 1773 protected String roleId_; 1774 1775 public RoleSelected(String roleId){ 1776 super(roleId); 1777 roleId_ = roleId; 1778 } 1779 1780 public void actionPerformed(ActionEvent e) { 1781 if(DynamicTree.this.multipleRoles_){ 1782 String [] currentRoles = getRoleManager().getCurrentRoleIds(); 1783 Vector theRoles = new Vector (Arrays.asList(currentRoles)); 1784 if(theRoles.contains(roleId_)) 1785 theRoles.remove(roleId_); 1786 else 1787 theRoles.add(roleId_); 1788 getRoleManager().setCurrentRoleIds((String [])theRoles.toArray(new String [theRoles.size()])); 1789 } else { 1790 String currentRoleId = getRoleManager().getCurrentRoleIds()[0]; 1791 if(!roleId_.equals(currentRoleId)) { 1792 getRoleManager().setCurrentRoleIds(new String []{roleId_}); 1793 } 1794 } 1795 refreshAllPathes(); 1796 } 1797 1798 } 1799 1800 1801} 1802 1803 | Popular Tags |