Drag and Drop from JFace-Viewer to GEF-Editor
4. October 2006
Do you know the palette from GEF?- It is THE Creation Tool for graphical Editors based on GEF. Unfortunately the requirement is often to drag and drop already created objects into the graphical viewer, e.g from a navigation view, or another JFace-Viewer. Therefore you have to implement your own Drag'n Drop mechanism that is also compatible with the EditPart concept and your Commands. Today I want to show you a way how to drag elements from a TableViewer and drop them on my EditParts. Before dropping the elements you have the possibility to hook in your own code for showing an additional wizard/dialog or if you want to manipulate/replace/adapt the dragged model-object. I have written a small manager and that decides when and how a drop is allowed and how to process the dropped data.
Let me tell the truth. It's not as difficult as it sounds.
First Step: Adding Drag-Support
For adding Drag support you can use GEFs TemplateTransfer-Object that holds the viewers selected object.
-
this.viewer.addDragSupport( DND.DROP_COPY,
-
new Transfer[] { TemplateTransfer.getInstance() },
-
new DragSourceAdapter() {
-
}
-
/* (non-Javadoc)
-
* @see org.eclipse.swt.dnd.DragSourceAdapter#dragStart(org.eclipse.swt.dnd.DragSourceEvent)
-
*/
-
DragHelper.sameObject = false;
-
}
-
-
});
Second Step: Adding Drop-Support
Your graphical editor can hold a TransferDropTargetListener, that implements the object transfer between drag source and drop target.
-
/* (non-Javadoc)
-
* @see org.eclipse.gef.ui.parts.GraphicalEditor#initializeGraphicalViewer()
-
*/
-
@Override
-
protected void initializeGraphicalViewer() {
-
EditPartViewer viewer = getGraphicalViewer();
-
viewer.setContents(this.model);
-
viewer.addDropTargetListener(new ExampleDropTargetListener(getGraphicalViewer(),this.model));
-
-
}
Third Step: Implement the Rules
I've written a small and easy extendable layer that decides on the dragged object if a drop is allowed and what other code needs to be executed. For extending the Drag'n Drop Mechanism add a new mapping entry to the class PolicyMappings and implement an IDropPolicy for the object you want to drag'n drop (see the class-diagram).
Demo
View the Demo as Flash-Movie (400 kByte)
Download
Download the GEF Drag'n Drop Example as RCP (Source included - 10 Mbyte)
CVS-Checkout (more info)


Hi:
It’s a really nice example of what can be done with GEF+RCP.
I find it very difficult to do something similar.
I would like to check the code *.java so that I can learn from the example.
Can you send me that to jmurua@qualityobjects.com
Comment by Jon — 28. December 2006 @ 18:47
Hello, I would like to know more about this example, I would like to check the *.java code in order to learn from the example like Jon.
My mail is kasiodus@gmail.com.
Thanks u a lot….
Comment by kasiodus — 23. February 2007 @ 14:43
The easiest way to check the sources is to checkout the project de.spiritlink.gefdnd from cvs. Please see http://www.richclient2.eu/cvs for details. Cheerz
Comment by Tom Seidel — 23. February 2007 @ 14:49
Hi,
Do you know how can I create a wizard when a user drag and drop element from the palette
Comment by Alexandre — 11. January 2008 @ 16:20
Hi,
nice example. My problem differs a bit: Is is possible to drag a element from the GEF editor and drop it in a view (navigator tree)?
Regards
Jan
Comment by Jan — 20. May 2008 @ 14:38
Hi…
Its a good example but i’m not able to access CVS respository.Can u please send me src files.
Thanks and Regards,
Shwetha
Comment by Shwtha — 5. November 2008 @ 10:15
Alhtough the cvs checkout is working for me, the source is integrated in the RCP Download under plugins/de.spiritlink.gefdnd_1.0.0.jar/src
Comment by Tom Seidel — 6. November 2008 @ 17:43
Hi friends,
I want to automate the Drag and drop feature of GEF image in Eclipse
is there any to do it. let me know..
Comment by Nawaz — 29. January 2009 @ 08:58
Hi,
I have to implement the drag & drop for my project. Like in your example, i want to drag an entry from a treeViewer into my GEF editor. I really like to see your .java files (the cvs repository is not accessible).
Thanks a lot
Otilia
Comment by Otilia — 25. March 2009 @ 13:18
hi,
i like your example alot but i want to drag an image to my jface viewer and have the image icon (or an image icon that i create and that is bigger than the tiny cursor icon in your example) getting displayed when it’s dragged. isn’t this what windows does natively when you drag an image? is there any way at all to drag an image in jface or swt? the “guru” here told me it is impossible. is this true? is there any way at all to do this with java?
thanx,
valerie
Comment by valerie froloff — 7. June 2009 @ 05:06