Add Drag and drop to GMF Editor

 

*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.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值