I have developed a two plugins, plugin1
and plugin2
. In plugin1
there is a one view called pluginOneView
and in plugin2
there is another view called pluginTwoView
. So my requirement is there will be few dragabble components on pluginTwoView
and I should be able to drag it to pluginOneView
. Currently I am developing drag and drop for the same, my code is (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
}
}
But my problem lies in DropTarget
method:
DropTarget target = new DropTarget(component, dtl);
Here in the place of component
I need to add target as pluginOneView
(which is in another view). My question is how can I get a component object of that view in the workspace, so that I can pass it as a arugment to DropTarget
method?
I tried of getting
PlatformUI.getWorkbench().getViewRegistry().find("targetId");
But it returns me of IViewDescriptor
type, where as I need of component
type. Can anyone help me in this? Since I am new to Eclipse plugin development.
Well I think you misunderstood how to use a DropTarget. You don't need to know the Plugin you're dragging to. You also used a TextTransfer, but I assume you want to drag Java Objects not Strings. Therefore I made this little example that shows how to drag objects between multiple views (which could be in different plugins). In my example an Object of type ISomeClass is transferred. In order to transfer it, ISomeClass must be serializable. I have provided my own TransferType the SomeClassTransfer class that handles the transfer. Be aware that both view plugins need access to the ISomeClass and SomeClassTransfer definitions. The easiest way to accomplish this is to make a third plugin which holds these classes. Both view plugins could then hold a reference to this third plugin.
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;
}
}
The source view:
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());
}
}
});
The destination view:
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);
}
});
This code enables you to drag data between two completely independent views.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.