好吧,我认为您误解了如何使用 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);
}
});
此代码使您能够在两个完全独立的视图之间拖动数据。