Drag & Drop eclipse Viewer to GMF Editor

1,添加Viewer的Drag support
private void addDragSupport(final TreeViewer viewer) {
		DelegatingDragAdapter dragAdapter = new DelegatingDragAdapter();
		dragAdapter.addDragSourceListener(new TransferDragSourceListener() {
			public Transfer getTransfer() {
				return ResourceTransfer.getInstance();
			}
			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,创建的DROPListener。
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() {
		//need sub
		return null;
	}
}

3,配置Editor的DropListener
protected void configureGraphicalViewer() {		
	super.configureGraphicalViewer();
	IDiagramGraphicalViewer viewer = getDiagramGraphicalViewer();	
	viewer.addDropTargetListener(new GMFDropListener(viewer,
				ResourceTransfer.getInstance()));
}

4,创建处理editPolicy
public class DropObjectEditPolicy extends GraphicalEditPolicy implements
		EditPolicy {
	public Command getCommand(Request request) {
		if (RequestConstants.REQ_DROP_OBJECTS.equals(request.getType())) {
			DropObjectsRequest dndRequest = (DropObjectsRequest) request;
			return getDropCommand(dndRequest);
		}
		return null;
	}

	private Command getDropCommand(DropObjectsRequest dndRequest) {
		//need sub
		return null;
	}

	public boolean understandsRequest(Request request) {
		return RequestConstants.REQ_DROP_OBJECTS.equals(request.getType())
				&& request instanceof DropObjectsRequest;
	}
}

5,为目标EditPart安装editPolicy
installEditPolicy("Drop_Objects_EditPolicy", new DropObjectEditPolicy());

ps:如果是Package Explorer等Viewer,便不需要增加drag support。

你可能感兴趣的:(java,eclipse)