*node: Before you read this article, you should have a idea of how use GMF to generator a simple Graphics Editor, and this acticle is base on that.
Drag from Palette: http://wiki.eclipse.org/GMF_Newsgroup_Q_and_A#How_to_enable_drag-n-drop_from_the_palette.3F
Drag from a view: We should do three things, add drag support to the view, add DropTargetListener to the gmf editor, install the DragDropEditPolicy to the editor's root editpart.
1. add drag support of the view
TreeViewer viewer = ..... DelegatingDragAdapter dragAdapter = new DelegatingDragAdapter(); dragAdapter.addDragSourceListener(new TransferDragSourceListener() { public Transfer getTransfer() { return ResourceTransfer.getInstance(); //replace with your own transfer } public void dragStart(DragSourceEvent event) { if (!viewer.getSelection().isEmpty()) { event.doit = true; return; } event.doit = false; } public void dragSetData(DragSourceEvent event) { } public void dragFinished(DragSourceEvent event) { } }); viewer.addDragSupport(DND.DROP_MOVE, dragAdapter.getTransfers(), dragAdapter);
2. add DropTargetListener to gmf editor
first look at our DropTargetListener :
public class GmfDropListener extends DiagramDropTargetListener { public GmfDropListener(EditPartViewer viewer, Transfer xfer) { super(viewer, xfer); } @Override protected void handleDragOver() { updateTargetRequest(); updateTargetEditPart(); showTargetFeedback(); } @Override protected List getObjectsBeingDropped() { List a = new ArrayList<String>(); a.add(new JobImpl()); // Job is the model class of my figure node return a; } }
the method getObjectsBeingDropped() return the source object of we drag.
Second add this listener to the editor.
In the editor class # method: configureGraphicalViewer()
we add this code:
viewer.addDropTargetListener(new GmfDropListener(viewer, ResourceTransfer.getInstance())); //replace with your own transfer
3. install the DragDropEditPolicy to the editor's root editpart.
In my case, the root editpart is called diagram, so in the class DiagramEditPart # method createDefaultEditPolicies() we add code:
installEditPolicy(EditPolicyRoles.DRAG_DROP_ROLE, new DropObjectEditPolicy());
and the DropObjectEditPolicy class is in below:
public class DropObjectEditPolicy extends DiagramDragDropEditPolicy { @Override protected Command createViewsAndArrangeCommand( DropObjectsRequest dropRequest, List viewDescriptors) { CompoundCommand cm = new CompoundCommand(); Iterator elements = dropRequest.getObjects().iterator(); while (elements.hasNext()) { Object obj = elements.next(); if (obj instanceof EObject) { Command cmd = getDropElementCommand((EObject)obj, dropRequest); if (cmd != null) cm.add(cmd); } } return cm; } protected Command getDropElementCommand(EObject element, DropObjectsRequest request) { IElementType type = EmfgeneratorElementTypes.Job_2002; CreateElementRequest cer = new CreateElementRequest(type); cer.setContainer(((View) getHost().getModel()).getElement()); cer.setNewElement(element); ViewAndElementDescriptor viewDescriptor = new ViewAndElementDescriptor( new CreateElementRequestAdapter(cer), Node.class, ((IHintedType) type).getSemanticHint(), EmfgeneratorDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT); CreateViewAndElementRequest req = new CreateViewAndElementRequest( viewDescriptor); req.setLocation(request.getLocation()); Command c = getHost().getCommand(req); return c; }
In this article I use ResourceTransfer, you need to replace it with your own transfer in order to transfer data.
That's all done.