Eclipse插件开发 - 在多个插件中的组件之间拖放

时间:2013-03-21 03:53:18

标签: java eclipse eclipse-plugin

我开发了两个插件plugin1plugin2。在plugin1中,有一个名为pluginOneView的视图,而在plugin2中有另一个名为pluginTwoView的视图。所以我的要求是pluginTwoView上的可拖动组件很少,我应该可以将它拖到pluginOneView。目前我正在开发拖放功能,我的代码是(pluginView2DragSource

DragSource ds = new DragSource(btn, DND.DROP_MOVE); //btn is a draggable component
    ds.setTransfer(new Transfer[] { TextTransfer.getInstance() });

    ds.addDragListener(new DragSourceAdapter() {
             // There are dragStart and other methods here
        }
}

但我的问题在于DropTarget方法:

DropTarget target = new DropTarget(component, dtl);

此处在component的位置我需要将目标添加为pluginOneView(在另一个视图中)。我的问题是如何在工作区中获取该视图的组件对象,以便我可以将其作为DropTarget方法的参数传递?

我试图获得

PlatformUI.getWorkbench().getViewRegistry().find("targetId");

但它返回IViewDescriptor类型,我需要component类型。任何人都可以帮助我吗?因为我是Eclipse插件开发的新手。

1 个答案:

答案 0 :(得分:1)

我觉得你误解了如何使用DropTarget。您不需要知道要拖动的插件。您还使用了TextTransfer,但我假设您要拖动Java对象而不是字符串。因此,我做了一个小例子,展示了如何在多个视图之间拖动对象(可能在不同的插件中)。在我的示例中,传输了ISomeClass类型的Object。为了传输它,ISomeClass必须是可序列化的。我提供了自己的TransferType处理传输的SomeClassTransfer类。请注意,两个视图插件都需要访问ISomeClass和SomeClassTransfer定义。完成此任务的最简单方法是创建一个包含这些类的第三个插件。两个视图插件都可以保存对第三个插件的引用。

<强> SomeClassTransfer:

public class SomeClassTransfer extends ByteArrayTransfer {
    private final static String[] typeNames;
    private final static int[] typeIds;
    private final static SomeClassTransfer instance;

    static {
        String typeName = "SomeClassTransfer";
        int id = registerType(typeName);
        typeNames = new String[] { typeName };
        typeIds = new int[] { id };
        instance = new SomeClassTransfer();
    }

    public static SomeClassTransfer getInstance() {
        return instance;
    }

    private SomeClassTransfer() {
    }

    @Override
    protected int[] getTypeIds() {
        return typeIds;
    }

    @Override
    protected String[] getTypeNames() {
        return typeNames;
    }

    @Override
    protected void javaToNative(Object object, TransferData transferData) {
        if (object instanceof ISomeClass) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutput out = null;
            try {
                out = new ObjectOutputStream(bos);
                out.writeObject(object);
                byte[] objectBytes = bos.toByteArray();
                object = objectBytes;
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    out.close();
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        super.javaToNative(object, transferData);
    }

    @Override
    protected ISomeClass nativeToJava(TransferData transferData) {
        ISomeClass someClass = null;

        byte[] objectBytes = (byte[]) super.nativeToJava(transferData);
        ByteArrayInputStream bis = new ByteArrayInputStream(objectBytes);
        ObjectInput in = null;
        try {
            in = new ObjectInputStream(bis);
            Object o = in.readObject();
            if (o instanceof ISomeClass) {
                someClass = (ISomeClass) o;
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return someClass;
    }
}

来源视图:

    int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;
    Transfer[] types = new Transfer[] { SomeClassTransfer.getInstance() };
    DragSource source = new DragSource(tableViewer.getControl(), operations);
    source.setTransfer(types);

    source.addDragListener(new DragSourceListener() {
        @Override
        public void dragStart(DragSourceEvent event) {
            if (tableViewer.getSelection().isEmpty()) {
                // do not start drag
                event.doit = false;
            }
        }

        @Override
        public void dragSetData(DragSourceEvent event) {
            if (SomeClassTransfer.getInstance().isSupportedType(
                    event.dataType)) {
                event.data = ((IStructuredSelection) tableViewer
                        .getSelection()).getFirstElement();
            }
        }

        @Override
        public void dragFinished(DragSourceEvent event) {
            // A Move operation has been performed so remove the data
            // from the source
            if (event.detail == DND.DROP_MOVE) {
                tableViewer.remove(((IStructuredSelection) tableViewer
                        .getSelection()).getFirstElement());
            }
        }
    });

目的地视图:

    int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;
    Transfer[] types = new Transfer[] { SomeClassTransfer.getInstance() };
    DropTarget target = new DropTarget(tableViewer.getControl(), operations);
    target.setTransfer(types);

    target.addDropListener(new DropTargetListener() {
        @Override
        public void dragEnter(DropTargetEvent event) {
        }

        @Override
        public void dragOver(DropTargetEvent event) {
        }

        @Override
        public void dragLeave(DropTargetEvent event) {
        }

        @Override
        public void dragOperationChanged(DropTargetEvent event) {
        }

        @Override
        public void dropAccept(DropTargetEvent event) {
        }

        @Override
        public void drop(DropTargetEvent event) {
            if (event.data == null) {
                // no data to copy, indicate failure in event.detail
                event.detail = DND.DROP_NONE;
                return;
            }
            // data copied to viewer
            tableViewer.add((ISomeClass) event.data);
        }
    });

此代码使您可以在两个完全独立的视图之间拖动数据。