1

我开发了两个插件,plugin1plugin2. 其中plugin1有一个视图称为pluginOneView,而plugin2另一个视图称为pluginTwoView。所以我的要求是上面的可拖动组件很少pluginTwoView,我应该能够将它拖动到pluginOneView. 目前我正在开发相同的拖放功能,我的代码是 (in pluginView2) for DragSource

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 插件开发的新手。

4

1 回答 1

1

好吧,我认为您误解了如何使用 DropTarget。您不需要知道要拖动到的插件。您还使用了 TextTransfer,但我假设您想要拖动 Java 对象而不是字符串。因此我做了这个小例子,展示了如何在多个视图之间拖动对象(可能在不同的插件中)。在我的示例中,传输了一个 ISomeClass 类型的对象。为了传输它,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);
        }
    });

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

于 2013-03-23T21:59:43.430 回答