1 26 package org.objectweb.util.explorer.swt.lib; 27 28 import java.net.URL ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.Vector ; 33 34 import org.eclipse.swt.SWT; 35 import org.eclipse.swt.dnd.DND; 36 import org.eclipse.swt.dnd.DragSource; 37 import org.eclipse.swt.dnd.DragSourceEvent; 38 import org.eclipse.swt.dnd.DragSourceListener; 39 import org.eclipse.swt.dnd.DropTarget; 40 import org.eclipse.swt.dnd.DropTargetAdapter; 41 import org.eclipse.swt.dnd.DropTargetEvent; 42 import org.eclipse.swt.dnd.Transfer; 43 import org.eclipse.swt.events.TreeEvent; 44 import org.eclipse.swt.events.TreeListener; 45 import org.eclipse.swt.graphics.Image; 46 import org.eclipse.swt.widgets.Display; 47 import org.eclipse.swt.widgets.Event; 48 import org.eclipse.swt.widgets.Listener; 49 import org.eclipse.swt.widgets.Menu; 50 import org.eclipse.swt.widgets.MenuItem; 51 import org.eclipse.swt.widgets.MessageBox; 52 import org.eclipse.swt.widgets.Shell; 53 import org.eclipse.swt.widgets.TreeItem; 54 import org.objectweb.fractal.api.Component; 55 import org.objectweb.fractal.api.NoSuchInterfaceException; 56 import org.objectweb.fractal.api.control.BindingController; 57 import org.objectweb.fractal.api.control.IllegalBindingException; 58 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 59 import org.objectweb.util.explorer.api.Context; 60 import org.objectweb.util.explorer.api.DropAction; 61 import org.objectweb.util.explorer.api.Entry; 62 import org.objectweb.util.explorer.api.IconProvider; 63 import org.objectweb.util.explorer.api.Info; 64 import org.objectweb.util.explorer.api.MenuItemTreeView; 65 import org.objectweb.util.explorer.api.Tree; 66 import org.objectweb.util.explorer.api.TreeView; 67 import org.objectweb.util.explorer.core.common.api.Description; 68 import org.objectweb.util.explorer.core.common.api.ExplorerConstants; 69 import org.objectweb.util.explorer.core.common.api.TextComponent; 70 import org.objectweb.util.explorer.core.common.lib.BindingFeature; 71 import org.objectweb.util.explorer.core.common.lib.DefaultTreeView; 72 import org.objectweb.util.explorer.core.common.lib.TreeBindingFeature; 73 import org.objectweb.util.explorer.core.dnd.api.DnDDescription; 74 import org.objectweb.util.explorer.core.dnd.api.DnDDescriptions; 75 import org.objectweb.util.explorer.core.dnd.lib.DefaultDropTreeView; 76 import org.objectweb.util.explorer.core.menu.api.MenuDescription; 77 import org.objectweb.util.explorer.core.menu.lib.DefaultMenuItemTreeView; 78 import org.objectweb.util.explorer.core.naming.lib.DefaultEntry; 79 import org.objectweb.util.explorer.interpreter.api.DescriptionInterpreter; 80 import org.objectweb.util.explorer.interpreter.lib.swt.IconFileProvider; 81 import org.objectweb.util.explorer.resolver.api.PropertyResolver; 82 import org.objectweb.util.explorer.swt.api.Explorer; 83 import org.objectweb.util.trace.Trace; 84 import org.objectweb.util.trace.TraceSystem; 85 86 94 public class DynamicTree 95 extends org.eclipse.swt.widgets.Tree 96 implements Tree, BindingController, Explorer 97 { 98 99 105 106 protected BindingFeature bindingFeature_ = null; 107 108 109 protected Display display_ = null; 110 111 112 public static Shell shell_ = null; 113 114 115 protected Map nodeStates_ = null; 116 117 118 protected Vector initialContext_ = null; 119 120 121 protected Menu menuBar_ = null; 122 123 124 protected MenuItem actionMenu_ = null; 125 126 132 135 private void initDragAndDrop(){ 136 Transfer[] types = new Transfer[] {EntryTransfer.getInstance()}; 137 int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK; 138 139 final DragSource source = new DragSource (this, operations); 140 source.setTransfer(types); 141 source.addDragListener (new MyDragSourceListener ()); 142 143 DropTarget target = new DropTarget(this, operations); 144 target.setTransfer(types); 145 target.addDropListener (new MyDropTargetAdapter()); 146 } 147 148 151 public DynamicTree() { 152 super(new Shell(), SWT.BORDER); 153 setSize(400,500); 154 155 bindingFeature_ = new TreeBindingFeature(); 156 nodeStates_ = new HashMap (); 157 initialContext_ = new Vector (); 158 159 addListener (SWT.Expand, new MyExpensionListener()); 160 addListener (SWT.MenuDetect, new MyMenuDetectListener()); 161 addListener (SWT.MouseDown, new MyMouseDownListener()); 162 addListener (SWT.Selection, new MySelectionListener()); 163 addTreeListener(new MyTreeListener()); 164 165 initDragAndDrop(); 166 } 167 168 174 protected Trace getTrace(){ 175 return TraceSystem.get("explorer"); 176 } 177 178 181 protected Entry getTheSelectedEntry() { 182 TreeItem[] items = getSelection(); 183 if(items != null && items.length>0){ 184 return (Entry)items[0].getData(); 185 } 186 return null; 187 } 188 189 192 protected Entry getParentEntry() { 193 TreeItem[] items = getSelection(); 194 if(items[0].getParentItem()!=null){ 195 return (Entry)items[0].getParentItem().getData(); 196 } 197 return null; 198 } 199 200 204 protected TreeView getTreeView() { 205 return new DefaultTreeView((Tree)this, getTheSelectedEntry(), getParentEntry()); 206 } 207 208 212 protected MenuItemTreeView getMenuItemTreeView() { 213 return new DefaultMenuItemTreeView((Tree)this, getTheSelectedEntry(), getParentEntry()); 214 } 215 216 220 protected void addInitialEntry(Entry entry){ 221 TreeItem item = new TreeItem(this,0); 222 item.setText(entry.getName().toString()); 223 item.setData(entry); 224 completeTreeItem(item); 225 item.setExpanded(true); 226 } 227 228 235 protected void completeTreeItem(TreeItem treeItem){ 236 Entry entry = (Entry)treeItem.getData(); 237 238 Image image = (Image)getImageIcon(entry); 240 if(image!=null){ 241 treeItem.setImage(image); 242 } 243 244 if(entry.getWrapper()!=null){ 246 new TreeItem(treeItem,0); 248 } else { 249 Description desc = getPropertyResolver().resolve(ExplorerConstants.WRAPPER_PROPERTY, entry); 250 if(desc!=null){ 251 Context childContext = (Context) getDescriptionInterpreter().interprete(desc, null); 252 if (childContext != null) { 253 entry.setWrapper(childContext); 254 new TreeItem(treeItem,0); 255 } 256 } 257 } 258 } 259 260 267 protected String pathToString(TreeItem item) { 268 if(item!=null){ 269 TreeItem current = item; 270 String value = current.getText(); 271 while((current = current.getParentItem())!=null){ 272 value = current.getText() + "/" + value; 273 } 274 return value; 275 } 276 return ""; 277 } 278 279 283 286 protected PropertyResolver getPropertyResolver() { 287 try { 288 return (PropertyResolver) bindingFeature_.lookupFc(PropertyResolver.PROPERTY_RESOLVER); 289 } catch (NoSuchInterfaceException e) { 290 getTrace().warn(PropertyResolver.PROPERTY_RESOLVER + ": interface not found!"); 291 return null; 292 } 293 } 294 295 298 protected DescriptionInterpreter getDescriptionInterpreter() { 299 try { 300 return (DescriptionInterpreter) bindingFeature_.lookupFc(DescriptionInterpreter.DESCRIPTION_INTERPRETER); 301 } catch (NoSuchInterfaceException e) { 302 getTrace().warn(DescriptionInterpreter.DESCRIPTION_INTERPRETER + ": interface not found!"); 303 return null; 304 } 305 } 306 307 310 protected TextComponent getTextComponent(){ 311 try { 312 return (TextComponent) bindingFeature_.lookupFc(TextComponent.TEXT_COMPONENT); 313 } catch (NoSuchInterfaceException e) { 314 getTrace().warn(TextComponent.TEXT_COMPONENT + ": interface not found!"); 315 return null; 316 } 317 } 318 319 323 protected Image getImageIcon(Entry entry){ 324 Description desc = getPropertyResolver().resolve(ExplorerConstants.ICON_PROPERTY, entry); 325 IconProvider iconProvider = (IconProvider) getDescriptionInterpreter().interprete(desc, null); 326 if(iconProvider!=null){ 327 Object object = iconProvider.newIcon(entry.getValue()); 328 if(object instanceof Image){ 329 return (Image)object; 330 } else if(object instanceof String ) { 331 return (Image)(new IconFileProvider((String )object)).newIcon(entry.getValue()); 332 } else if (object instanceof URL ) { 333 return (Image)(new IconFileProvider((URL )object)).newIcon(entry.getValue()); 334 } 335 } 336 return null; 337 } 338 339 343 protected void displayInfo(Entry entry){ 344 if (getTextComponent() != null){ 345 String message = ""; 346 Description desc = getPropertyResolver().resolve(ExplorerConstants.INFO_PROPERTY, entry); 347 if(desc!=null){ 348 Info info = (Info) getDescriptionInterpreter().interprete(desc, null); 349 if (info != null) { 350 message = info.getInfo(getTreeView()); 351 } 352 } 353 getTextComponent().setText(message); 354 } 355 } 356 357 361 364 protected void refreshAllPathes(){ 365 System.out.println("---- BEGIN refreshAllPathes ----"); 366 removeAll(); 367 Iterator it = initialContext_.iterator(); 368 while (it.hasNext()) { 369 Entry entry = (Entry) it.next(); 370 System.out.println("=> Adds " + entry.getName()); 371 addInitialEntry(entry); 372 } 373 System.out.println("---- END refreshAllPathes ----"); 374 } 375 376 380 protected void refreshActionItem(Entry entry){ 381 if(actionMenu_!=null && entry!=null){ 382 Description desc = getPropertyResolver().resolve(ExplorerConstants.MENU_PROPERTY, entry, getParentEntry()); 383 if(desc!=null && ! desc.isEmpty()){ 384 ((MenuDescription)desc).setDescriptionType("bar"); 385 Menu menu = (Menu) getDescriptionInterpreter().interprete(desc, getMenuItemTreeView()); 386 actionMenu_.setMenu(menu); 387 actionMenu_.setEnabled(true); 388 } else { 389 actionMenu_.setEnabled(false); 390 } 391 } 392 } 393 394 protected void refreshActionItem(){ 395 refreshActionItem(getTheSelectedEntry()); 396 } 397 398 404 407 public void refreshAll() { 408 refreshAllPathes(); 409 } 410 411 417 public void addEntry(Object name, Object value) { 418 if(name!=null && value!=null){ 419 Entry entry = new DefaultEntry(name, value); 420 addInitialEntry(entry); 421 initialContext_.add(entry); 422 } 423 } 424 425 428 public void addEntry(Object name, Object value, int level) { 429 throw new RuntimeException ("NOT IMPLEMENTED YET!"); 431 } 432 433 436 public void selectPath(String path) { 437 throw new RuntimeException ("NOT IMPLEMENTED YET!"); 439 } 440 441 444 public void clear() { 445 throw new RuntimeException ("NOT IMPLEMENTED YET!"); 447 } 448 449 452 public int getInitialContextSize() { 453 throw new RuntimeException ("NOT IMPLEMENTED YET!"); 455 } 456 457 460 public void removeEntry(Object name) { 461 throw new RuntimeException ("NOT IMPLEMENTED YET!"); 463 } 464 465 468 public void renameInitialEntry(Object currentName, Object newName) { 469 throw new RuntimeException ("NOT IMPLEMENTED YET!"); 471 } 472 473 476 public Component duplicate() { 477 throw new RuntimeException ("NOT IMPLEMENTED YET!"); 479 } 480 481 484 public Component duplicate(boolean withData) { 485 throw new RuntimeException ("NOT IMPLEMENTED YET!"); 487 } 488 489 492 public Entry getSelectedEntry() { 493 throw new RuntimeException ("NOT IMPLEMENTED YET!"); 495 } 496 497 500 public void renameSelectedNode(Object odlName, Object newName) { 501 throw new RuntimeException ("NOT IMPLEMENTED YET!"); 503 } 504 505 508 public void close() { 509 throw new RuntimeException ("NOT IMPLEMENTED YET!"); 511 } 512 513 519 522 public org.eclipse.swt.widgets.Tree getTree(){ 523 return this; 524 } 525 526 530 public void setDisplay(Display display){ 531 display_ = display; 532 } 533 534 538 public void setShell(Shell shell){ 539 shell_ = shell; 540 } 541 542 546 public void setMenuBar(Menu menu){ 547 if(menu!=null){ 548 menuBar_ = menu; 549 actionMenu_ = new MenuItem(menu, SWT.CASCADE); 550 actionMenu_.setText("Actions"); 551 actionMenu_.setEnabled(false); 552 } 553 } 554 555 561 567 public void bindFc(String itfName, Object itfValue) 568 throws NoSuchInterfaceException, IllegalBindingException, 569 IllegalLifeCycleException { 570 bindingFeature_.bindFc(itfName, itfValue); 571 } 572 573 578 public String [] listFc() { 579 return bindingFeature_.listFc(); 580 } 581 582 587 public Object lookupFc(String itfName) throws NoSuchInterfaceException { 588 return bindingFeature_.lookupFc(itfName); 589 } 590 591 596 public void unbindFc(String itfName) throws NoSuchInterfaceException, 597 IllegalBindingException, IllegalLifeCycleException { 598 bindingFeature_.unbindFc(itfName); 599 } 600 601 607 final class MyExpensionListener implements Listener { 608 public void handleEvent (Event e) { 609 TreeItem currentItem = (TreeItem)e.item; 610 getTrace().debug("Expension listener: " + currentItem.getText()); 611 612 TreeItem[] itemChildren = currentItem.getItems(); 614 for (int i = 0; i < itemChildren.length; i++) { 615 if (itemChildren[i].getData() != null) 616 return; 617 itemChildren[i].dispose(); 618 } 619 620 Entry currentEntry = (Entry)currentItem.getData(); 622 Context context = (Context)currentEntry.getWrapper(); 623 Entry[] children = context.getEntries(currentEntry.getValue()); 624 for (int i = 0; i < children.length; i++) { 625 TreeItem item = new TreeItem(currentItem, 0); 626 item.setText(children[i].getName().toString()); 627 item.setData(children[i]); 628 completeTreeItem(item); 629 } 630 } 631 } 632 633 final class MyMenuDetectListener implements Listener { 634 public void handleEvent (Event e) { 635 Entry entry = getTheSelectedEntry(); 636 if(entry!=null){ 637 Description desc = getPropertyResolver().resolve(ExplorerConstants.MENU_PROPERTY, entry, getParentEntry()); 638 if(desc!=null){ 639 ((MenuDescription)desc).setDescriptionType("popup"); 640 Menu menu = (Menu) getDescriptionInterpreter().interprete(desc, getMenuItemTreeView()); 641 if(menu!=null){ 642 menu.setLocation (e.x, e.y); 643 menu.setVisible (true); 644 while (!menu.isDisposed () && menu.isVisible ()) { 645 if (!display_.readAndDispatch ()) display_.sleep (); 646 } 647 menu.dispose (); 648 } 649 } 650 } 651 657 } 658 } 659 660 final class MyMouseDownListener implements Listener { 661 public void handleEvent (Event e) { 662 } 668 } 669 670 final class MySelectionListener implements Listener { 671 public void handleEvent (Event e) { 672 Entry entry = getTheSelectedEntry(); 673 displayInfo(entry); 674 refreshActionItem(entry); 675 } 676 } 677 678 final class MyTreeListener implements TreeListener { 679 680 683 public void treeCollapsed(TreeEvent e) { 684 nodeStates_.put(pathToString((TreeItem)e.item), Boolean.FALSE); 685 } 686 687 690 public void treeExpanded(TreeEvent e) { 691 TreeItem item = (TreeItem)e.item; 692 getTrace().debug("treeExpanded: " + item.getText()); 693 nodeStates_.put(pathToString(item), Boolean.TRUE); 694 TreeItem[] children = item.getItems(); 695 for(int i = 0 ; i < children.length ; i++){ 696 TreeItem child = children[i]; 697 String childPath = pathToString(child); 698 if(nodeStates_.containsKey(childPath) && ((Boolean )nodeStates_.get(childPath)).booleanValue()){ 699 getTrace().debug("Expand " + child.getText()); 700 TreeItem[] children2 = child.getItems(); 701 for(int j=0;j<children2.length;j++){ 702 children2[j].dispose(); 703 } 704 child.setExpanded(true); 705 setRedraw(true); 706 } 707 } 708 709 } 710 } 711 712 718 719 protected class MyDragSourceListener implements DragSourceListener { 720 721 protected Entry dragSourceEntry = null; 722 723 726 public void dragStart(DragSourceEvent event) { 727 dragSourceEntry = getTheSelectedEntry(); 729 } 730 731 734 public void dragFinished(DragSourceEvent event) { 735 } 736 737 740 public void dragSetData(DragSourceEvent event) { 741 event.data = dragSourceEntry; 742 } 743 744 } 745 746 protected class MyDropTargetAdapter extends DropTargetAdapter { 747 748 protected Entry dragEntry = null; 749 750 protected TreeItem oldItem_ = null; 751 752 753 protected boolean actionChanged_ = false; 754 755 protected String getDropActionName(int dropAction){ 756 if(dropAction==DND.DROP_MOVE) 757 return DnDDescription.MOVE_ACTION; 758 else if(dropAction==DND.DROP_COPY) 759 return DnDDescription.COPY_ACTION; 760 else if(dropAction==DND.DROP_LINK) 761 return DnDDescription.LINK_ACTION; 762 else 763 return ""; 764 } 765 766 protected String computeActionDesc(Entry entry, String dropAction){ 767 DnDDescriptions desc = (DnDDescriptions)getPropertyResolver().resolve(ExplorerConstants.DND_PROPERTY, entry); 768 if(desc!=null){ 769 DnDDescription dndDesc = desc.getDropAction(dropAction); 770 if(dndDesc !=null){ 771 return dndDesc.getLabel(); 772 } 773 } 774 return ""; 775 } 776 777 780 public void dragEnter(DropTargetEvent event) { 781 super.dragEnter(event); 782 dragEntry = getTheSelectedEntry(); 783 } 784 785 788 public void dragLeave(DropTargetEvent event) { 789 super.dragLeave(event); 790 } 791 792 795 public void dragOperationChanged(DropTargetEvent event) { 796 super.dragOperationChanged(event); 797 actionChanged_ = true; 798 } 799 800 803 public void dragOver(DropTargetEvent event) { 804 event.feedback = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL | DND.FEEDBACK_SELECT; 805 TreeItem currentItem = (TreeItem)event.item; 806 807 if(currentItem==null){ 808 oldItem_ = null; 809 if(getTextComponent()!=null){ 810 getTextComponent().setText(""); 811 } 812 } else if(oldItem_ == null || (currentItem != null && !currentItem.equals(oldItem_)) || actionChanged_){ 813 actionChanged_ = false; 814 oldItem_ = currentItem; 815 String value = computeActionDesc((Entry)currentItem.getData(), getDropActionName(event.detail)); 816 if(getTextComponent()!=null){ 817 getTextComponent().setText(value); 818 } 819 } 820 } 821 822 825 public void drop(DropTargetEvent event) { 826 super.drop(event); 827 if (event.data == null) { 828 event.detail = DND.DROP_NONE; 829 return; 830 } 831 832 if(getTextComponent()!=null){ 833 getTextComponent().setText(""); 834 } 835 836 if(event.item!=null){ 838 TreeItem item = (TreeItem)event.item; 839 840 Entry dropEntry = (Entry)item.getData(); 841 842 DnDDescriptions desc = (DnDDescriptions)getPropertyResolver().resolve(ExplorerConstants.DND_PROPERTY, dropEntry); 843 if(desc!=null){ 844 DnDDescription dndDesc = desc.getDropAction(getDropActionName(event.detail)); 845 if(dndDesc !=null){ 846 DropAction dropAction = (DropAction)getDescriptionInterpreter().interprete(dndDesc.getCodeDescription(),null); 847 if(dropAction!=null && dragEntry!=null){ 848 try{ 849 dropAction.execute(new DefaultDropTreeView((Tree)DynamicTree.this, dropEntry, getParentEntry(), dragEntry)); 850 }catch(Exception e){ 851 getTrace().warn(getClass().getName() + ", " + e.getClass().getName() + ": "+ e.getMessage()); 852 MessageBox mb = new MessageBox(DynamicTree.shell_, SWT.ICON_ERROR | SWT.OK); 853 mb.setMessage(e.getClass().getName() + ":\n" + e.getMessage()); 854 mb.setText("Exception (" + event.widget + ")"); 855 mb.open(); 856 } 857 DynamicTree.this.refreshAllPathes(); 858 } 859 } 860 } 861 862 } 863 864 } 865 866 869 public void dropAccept(DropTargetEvent event) { 870 super.dropAccept(event); 871 } 872 } 873 } 874 | Popular Tags |