KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > swt > lib > DynamicTree


1 /*====================================================================
2
3 Objectweb Explorer Framework
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Moroy, Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26 package org.objectweb.util.explorer.swt.lib;
27
28 import java.net.URL JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Vector JavaDoc;
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 /**
87  *
88  *
89  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>,
90  * <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>.
91  *
92  * @version 0.1
93  */

94 public class DynamicTree
95      extends org.eclipse.swt.widgets.Tree
96   implements Tree, BindingController, Explorer
97 {
98
99     //==================================================================
100
//
101
// Internal States.
102
//
103
// ==================================================================
104

105     /** The binding feature to use. */
106     protected BindingFeature bindingFeature_ = null;
107
108     /** The display to use. */
109     protected Display display_ = null;
110     
111     /** The shell to use. */
112     public static Shell shell_ = null;
113
114     /** Registers the state of the node. */
115     protected Map JavaDoc nodeStates_ = null;
116
117     /** The context containing the initial entries. */
118     protected Vector JavaDoc initialContext_ = null;
119
120     /** The menu bar to use. */
121     protected Menu menuBar_ = null;
122
123     /** The action menu item. */
124     protected MenuItem actionMenu_ = null;
125         
126     // ==================================================================
127
//
128
// Constructors.
129
//
130
// ==================================================================
131

132     /**
133      * Calls by the constructor
134      */

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     /**
149      * Default constructor
150      */

151     public DynamicTree() {
152         super(new Shell(), SWT.BORDER);
153         setSize(400,500);
154         
155         bindingFeature_ = new TreeBindingFeature();
156         nodeStates_ = new HashMap JavaDoc();
157         initialContext_ = new Vector JavaDoc();
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     // ==================================================================
169
//
170
// Internal methods.
171
//
172
// ==================================================================
173

174     protected Trace getTrace(){
175         return TraceSystem.get("explorer");
176     }
177
178     /**
179      * Returns the selected entry
180      */

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     /**
190      * Returns the parent of the selected entry
191      */

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     /**
201      * Provides a view on the tree
202      * @return A view on the tree.
203      */

204     protected TreeView getTreeView() {
205         return new DefaultTreeView((Tree)this, getTheSelectedEntry(), getParentEntry());
206     }
207
208     /**
209      * Provides a menu item view.
210      * @return A view on the tree used by menu item.
211      */

212     protected MenuItemTreeView getMenuItemTreeView() {
213         return new DefaultMenuItemTreeView((Tree)this, getTheSelectedEntry(), getParentEntry());
214     }
215
216     /**
217      * Adds the given entry into the tree.
218      * @param entry
219      */

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     /**
229      * Computes if an the given TreeItem is a leaf or not.
230      * A tree item is a leaf if the value of the contained entry is a context
231      * instance or if a context is defined for the value of the contained entry.
232      * This method may modify the value of the treeItem. More precisely, it may
233      * add a wrapper into the contained entry.
234      */

235     protected void completeTreeItem(TreeItem treeItem){
236         Entry entry = (Entry)treeItem.getData();
237         
238         // Computes the icon
239
Image image = (Image)getImageIcon(entry);
240         if(image!=null){
241             treeItem.setImage(image);
242         }
243         
244         // Computes the wrapper
245
if(entry.getWrapper()!=null){
246             // The entry value is a context instance.
247
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     /**
261      * Gets a string representation of the given item.
262      *
263      * @param item The tree item.
264      * @return The path from the root to the given item.
265      *
266      */

267     protected String JavaDoc pathToString(TreeItem item) {
268         if(item!=null){
269             TreeItem current = item;
270             String JavaDoc value = current.getText();
271             while((current = current.getParentItem())!=null){
272                 value = current.getText() + "/" + value;
273             }
274             return value;
275         }
276         return "";
277     }
278         
279     // ==================================================================
280
// Access to Fractal interfaces
281
// ==================================================================
282

283     /**
284      * Provides the PropertyResolver to use.
285      */

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     /**
296      * Provides the descriptionInterpreter to use.
297      */

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     /**
308      * Provides the TextComponent to use.
309      */

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     // ==================================================================
320
// Icon Management
321
// ==================================================================
322

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 JavaDoc object = iconProvider.newIcon(entry.getValue());
328             if(object instanceof Image){
329                 return (Image)object;
330             } else if(object instanceof String JavaDoc) {
331                 return (Image)(new IconFileProvider((String JavaDoc)object)).newIcon(entry.getValue());
332             } else if (object instanceof URL JavaDoc) {
333                 return (Image)(new IconFileProvider((URL JavaDoc)object)).newIcon(entry.getValue());
334             }
335         }
336         return null;
337     }
338     
339     // ==================================================================
340
// Info Management
341
// ==================================================================
342

343     protected void displayInfo(Entry entry){
344         if (getTextComponent() != null){
345             String JavaDoc 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     // ==================================================================
358
// Refresh Management
359
// ==================================================================
360

361     /**
362      * Refreshes all the tree.
363      */

364     protected void refreshAllPathes(){
365         System.out.println("---- BEGIN refreshAllPathes ----");
366         removeAll();
367         Iterator JavaDoc 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     /**
377      * Refreshes the action menu depending on the selected object.
378      * @param entry The selected entry
379      */

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     // ==================================================================
399
//
400
// Public methods for Tree interface.
401
//
402
// ==================================================================
403

404     /* (non-Javadoc)
405      * @see org.objectweb.util.explorer.api.Tree#refreshAll()
406      */

407     public void refreshAll() {
408         refreshAllPathes();
409     }
410     
411     /*
412      * (non-Javadoc)
413      *
414      * @see org.objectweb.util.explorer.api.Tree#addEntry(java.lang.Object,
415      * java.lang.Object)
416      */

417     public void addEntry(Object JavaDoc name, Object JavaDoc 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     /* (non-Javadoc)
426      * @see org.objectweb.util.explorer.api.Tree#addEntry(java.lang.Object, java.lang.Object, int)
427      */

428     public void addEntry(Object JavaDoc name, Object JavaDoc value, int level) {
429         // TODO: Not implemented
430
throw new RuntimeException JavaDoc("NOT IMPLEMENTED YET!");
431     }
432     
433     /* (non-Javadoc)
434      * @see org.objectweb.util.explorer.api.Tree#selectPath(java.lang.String)
435      */

436     public void selectPath(String JavaDoc path) {
437         // TODO: Not implemented
438
throw new RuntimeException JavaDoc("NOT IMPLEMENTED YET!");
439     }
440     
441     /* (non-Javadoc)
442      * @see org.objectweb.util.explorer.api.Tree#clear()
443      */

444     public void clear() {
445         // TODO: Not implemented
446
throw new RuntimeException JavaDoc("NOT IMPLEMENTED YET!");
447     }
448     
449     /* (non-Javadoc)
450      * @see org.objectweb.util.explorer.api.Tree#getInitialContextSize()
451      */

452     public int getInitialContextSize() {
453         // TODO: Not implemented
454
throw new RuntimeException JavaDoc("NOT IMPLEMENTED YET!");
455     }
456     
457     /* (non-Javadoc)
458      * @see org.objectweb.util.explorer.api.Tree#removeEntry(java.lang.Object)
459      */

460     public void removeEntry(Object JavaDoc name) {
461         // TODO: Not implemented
462
throw new RuntimeException JavaDoc("NOT IMPLEMENTED YET!");
463     }
464     
465     /* (non-Javadoc)
466      * @see org.objectweb.util.explorer.api.Tree#renameInitialEntry(java.lang.Object, java.lang.Object)
467      */

468     public void renameInitialEntry(Object JavaDoc currentName, Object JavaDoc newName) {
469         // TODO: Not implemented
470
throw new RuntimeException JavaDoc("NOT IMPLEMENTED YET!");
471     }
472
473     /* (non-Javadoc)
474      * @see org.objectweb.util.explorer.api.Tree#duplicate()
475      */

476     public Component duplicate() {
477         // TODO: Not implemented
478
throw new RuntimeException JavaDoc("NOT IMPLEMENTED YET!");
479     }
480
481     /* (non-Javadoc)
482      * @see org.objectweb.util.explorer.api.Tree#duplicate(boolean)
483      */

484     public Component duplicate(boolean withData) {
485         // TODO: Not implemented
486
throw new RuntimeException JavaDoc("NOT IMPLEMENTED YET!");
487     }
488
489     /* (non-Javadoc)
490      * @see org.objectweb.util.explorer.api.Tree#getSelectedEntry()
491      */

492     public Entry getSelectedEntry() {
493         // TODO: Not implemented
494
throw new RuntimeException JavaDoc("NOT IMPLEMENTED YET!");
495     }
496
497     /* (non-Javadoc)
498      * @see org.objectweb.util.explorer.api.Tree#renameSelectedNode(java.lang.Object, java.lang.Object)
499      */

500     public void renameSelectedNode(Object JavaDoc odlName, Object JavaDoc newName) {
501         // TODO: Not implemented
502
throw new RuntimeException JavaDoc("NOT IMPLEMENTED YET!");
503     }
504
505     /* (non-Javadoc)
506      * @see org.objectweb.util.explorer.api.Tree#close()
507      */

508     public void close() {
509         // TODO: Not implemented
510
throw new RuntimeException JavaDoc("NOT IMPLEMENTED YET!");
511     }
512
513     // ==================================================================
514
//
515
// Public methods for Explorer interface.
516
//
517
// ==================================================================
518

519     /* (non-Javadoc)
520      * @see org.objectweb.util.explorer.swt.api.Explorer#getTree()
521      */

522     public org.eclipse.swt.widgets.Tree getTree(){
523         return this;
524     }
525
526     /**
527      * Fixes the display to use.
528      * @param display The display to use.
529      */

530     public void setDisplay(Display display){
531         display_ = display;
532     }
533     
534     /**
535      * Fixes the shell to use.
536      * @param shell The shell to use.
537      */

538     public void setShell(Shell shell){
539         shell_ = shell;
540     }
541
542     /**
543      * Fixes the menu to use
544      * @param menu The menu to use.
545      */

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     // ==================================================================
556
//
557
// Public methods for BindingController interface.
558
//
559
// ==================================================================
560

561     /*
562      * (non-Javadoc)
563      *
564      * @see org.objectweb.fractal.api.control.BindingController#bindFc(java.lang.String,
565      * java.lang.Object)
566      */

567     public void bindFc(String JavaDoc itfName, Object JavaDoc itfValue)
568             throws NoSuchInterfaceException, IllegalBindingException,
569             IllegalLifeCycleException {
570         bindingFeature_.bindFc(itfName, itfValue);
571     }
572
573     /*
574      * (non-Javadoc)
575      *
576      * @see org.objectweb.fractal.api.control.BindingController#listFc()
577      */

578     public String JavaDoc[] listFc() {
579         return bindingFeature_.listFc();
580     }
581
582     /*
583      * (non-Javadoc)
584      *
585      * @see org.objectweb.fractal.api.control.BindingController#lookupFc(java.lang.String)
586      */

587     public Object JavaDoc lookupFc(String JavaDoc itfName) throws NoSuchInterfaceException {
588         return bindingFeature_.lookupFc(itfName);
589     }
590
591     /*
592      * (non-Javadoc)
593      *
594      * @see org.objectweb.fractal.api.control.BindingController#unbindFc(java.lang.String)
595      */

596     public void unbindFc(String JavaDoc itfName) throws NoSuchInterfaceException,
597             IllegalBindingException, IllegalLifeCycleException {
598         bindingFeature_.unbindFc(itfName);
599     }
600         
601     // ==================================================================
602
//
603
// Internal class for Tree management.
604
//
605
// ==================================================================
606

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             // Removes the children
613
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             // Computes the children
621
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             /*
652             TreeItem currentItem = (TreeItem)e.item;
653             if (currentItem != null) {
654                 System.out.println ("Menu detect");
655             }
656             */

657         }
658     }
659     
660     final class MyMouseDownListener implements Listener {
661         public void handleEvent (Event e) {
662             //Point point = new Point (e.x, e.y);
663
//TreeItem item = getItem (point);
664
//if (item != null) {
665
//System.out.println ("Mouse down");
666
//}
667
}
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         /* (non-Javadoc)
681          * @see org.eclipse.swt.events.TreeListener#treeCollapsed(org.eclipse.swt.events.TreeEvent)
682          */

683         public void treeCollapsed(TreeEvent e) {
684             nodeStates_.put(pathToString((TreeItem)e.item), Boolean.FALSE);
685         }
686         
687         /* (non-Javadoc)
688          * @see org.eclipse.swt.events.TreeListener#treeExpanded(org.eclipse.swt.events.TreeEvent)
689          */

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 JavaDoc childPath = pathToString(child);
698                 if(nodeStates_.containsKey(childPath) && ((Boolean JavaDoc)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     // ==================================================================
713
//
714
// Internal classes for Drag & Drop.
715
//
716
// ==================================================================
717

718     
719     protected class MyDragSourceListener implements DragSourceListener {
720
721         protected Entry dragSourceEntry = null;
722         
723         /* (non-Javadoc)
724          * @see org.eclipse.swt.dnd.DragSourceListener#dragStart(org.eclipse.swt.dnd.DragSourceEvent)
725          */

726         public void dragStart(DragSourceEvent event) {
727             //event.doit = false;
728
dragSourceEntry = getTheSelectedEntry();
729         }
730                 
731         /* (non-Javadoc)
732          * @see org.eclipse.swt.dnd.DragSourceListener#dragFinished(org.eclipse.swt.dnd.DragSourceEvent)
733          */

734         public void dragFinished(DragSourceEvent event) {
735         }
736         
737         /* (non-Javadoc)
738          * @see org.eclipse.swt.dnd.DragSourceListener#dragSetData(org.eclipse.swt.dnd.DragSourceEvent)
739          */

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         /** The currentAction_ */
753         protected boolean actionChanged_ = false;
754         
755         protected String JavaDoc 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 JavaDoc computeActionDesc(Entry entry, String JavaDoc 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         /* (non-Javadoc)
778          * @see org.eclipse.swt.dnd.DropTargetListener#dragEnter(org.eclipse.swt.dnd.DropTargetEvent)
779          */

780         public void dragEnter(DropTargetEvent event) {
781             super.dragEnter(event);
782             dragEntry = getTheSelectedEntry();
783         }
784         
785         /* (non-Javadoc)
786          * @see org.eclipse.swt.dnd.DropTargetListener#dragLeave(org.eclipse.swt.dnd.DropTargetEvent)
787          */

788         public void dragLeave(DropTargetEvent event) {
789             super.dragLeave(event);
790         }
791         
792         /* (non-Javadoc)
793          * @see org.eclipse.swt.dnd.DropTargetListener#dragOperationChanged(org.eclipse.swt.dnd.DropTargetEvent)
794          */

795         public void dragOperationChanged(DropTargetEvent event) {
796             super.dragOperationChanged(event);
797             actionChanged_ = true;
798         }
799         
800         /* (non-Javadoc)
801          * @see org.eclipse.swt.dnd.DropTargetListener#dragOver(org.eclipse.swt.dnd.DropTargetEvent)
802          */

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 JavaDoc value = computeActionDesc((Entry)currentItem.getData(), getDropActionName(event.detail));
816                 if(getTextComponent()!=null){
817                     getTextComponent().setText(value);
818                 }
819             }
820         }
821         
822         /* (non-Javadoc)
823          * @see org.eclipse.swt.dnd.DropTargetListener#drop(org.eclipse.swt.dnd.DropTargetEvent)
824          */

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             //Entry dropEntry = getSelectedEntry();
837
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 JavaDoc 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         /* (non-Javadoc)
867          * @see org.eclipse.swt.dnd.DropTargetListener#dropAccept(org.eclipse.swt.dnd.DropTargetEvent)
868          */

869         public void dropAccept(DropTargetEvent event) {
870             super.dropAccept(event);
871         }
872 }
873 }
874
Popular Tags