从View拖放到GEF编辑器

时间:2014-02-10 18:42:53

标签: java drag-and-drop eclipse-rcp eclipse-gef eclipse-gmf

我有一个简单的ViewTreeViewer代表POJO。 我需要从视图中拖动TreeItem并将它们拖放到GEF编辑器中(在多页编辑器中)。

当我将项目从查看器拖到编辑器区域时,将在侦听器中调用创建请求,句柄拖动和更新请求方法。 但当我放弃该项目时,handleDrop / drop不会被调用。

我的View包含:

Transfer[] types = new Transfer[]{TextTransfer.getInstance()};
treeViewer.addDragSupport(DND.DROP_MOVE, types, new InstructionDragListener(treeViewer));

DragListener看起来像:

public class InstructionDragListener implements DragSourceListener {

    private Viewer viewer;

    public InstructionDragListener(Viewer viewer) {
        this.viewer = viewer;
    }

    @Override
    public void dragStart(DragSourceEvent event) {
        System.out.println("start");
        event.doit = true;
    }

    @Override
    public void dragSetData(DragSourceEvent event) {
        IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
        Instruction ins=null;

        if (selection.getFirstElement() instanceof Instruction)
            ins = (Instruction) selection.getFirstElement();

        event.data= ins.getID();
        System.out.println("data");
    }

    @Override
    public void dragFinished(DragSourceEvent event) {
        System.out.println("end");
    }
}

编辑器是MultiPageEditor的一部分,看起来像是:

public class MyGraphicalEditor extends GraphicalEditorWithFlyoutPalette {

    public static String ID = "MyEditorID";

    public MyGraphicalEditor() {
        setEditDomain(new DefaultEditDomain(this));
    }

    @Override
    protected void initializeGraphicalViewer() {
        super.initializeGraphicalViewer();
        getGraphicalViewer().setContents(ProjectManager.getInstance().getTestCaseTest());
    }

    @Override
    protected void configureGraphicalViewer() {
        super.configureGraphicalViewer();
        getGraphicalViewer().setEditPartFactory(new TestCaseEditPartFactory());

        getGraphicalViewer().addDropTargetListener(new MyTransferTargetDropListener(getGraphicalViewer()));
    }
}

DropListener看起来像:

public class MyTransferTargetDropLsitener extends AbstractTransferDropTargetListener {

    private MyTransferTagetDropFactory factory = new MyTransferTagetDropFactory();

    public MyTransferTargetDropLsitener(EditPartViewer viewer, Transfer xfer) {
        super(viewer, xfer);
    }


    @Override
    protected Request createTargetRequest() {
        System.out.println("CREATE REQUEST");
        CreateRequest request = new CreateRequest();
        request.setFactory(factory);
        return request;
    }
    protected void handleDragOver() {
        System.out.println("HANDLE DRAG");
        super.handleDragOver();
    }

    @Override
    protected void updateTargetRequest() {
        System.out.println("UPDATE REQUEST");
        System.out.println(getDropLocation().toString());
        ((CreateRequest)getTargetRequest()).setLocation(getDropLocation());
    }

    @Override
    protected void handleDrop() {
        System.out.println("DROP HANDLED");
        super.handleDrop();

    }

    @Override
    public void drop(DropTargetEvent event) {
        System.out.println("DROPPED");
        super.drop(event);
    }
}

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我明白了。

问题是,特定XYPolicy必须安装AbstractGraphicalEditPart