I need to implement DnD in TreeViewer and need some help with the Transfer Objects.
Here is what I have:
a TreeViewer showing different Java Objects as parents and children
Here is what I need:
drag a tree node and drop it on to some other tree node
The only examples I could find where dealing with simple text strings being dragged and dropped.
But how do I support my custom objects? I don't need any native platform support, clipboard support or such like. So the ByteArrayTransfer I came across seems to be somewhat over the top.
Are there any other examples how to dnd POJOs?
> /**
> * Transfer information in memory (only useable to drag/copy stuff inside the
> * same JVM-Instance)
> */
> public class MemoryTransfer extends ByteArrayTransfer {
> private static final String TYPE_NAME = "memory-format"; //$NON-NLS-1$
>
> private static final int TYPE_ID = registerType(TYPE_NAME);
>
> private long startTime;
>
> private Object object;
>
>
> /**
> * @return The one and only instance
> */
> public static MemoryTransfer getInstance() {
> MemoryTransfer instance = (MemoryTransfer) Display.getCurrent().getData( MemoryTransfer.class.getName() + ".MEMORY_TRANSFER"); //$NON-NLS-1$
> if( instance == null ) {
> instance = new MemoryTransfer();
> Display.getCurrent().setData( MemoryTransfer.class.getName() + ".MEMORY_TRANSFER", instance); //$NON-NLS-1$
> }
> return instance;
> }
>
> private MemoryTransfer() {
> super();
> }
>
> @Override
> protected int[] getTypeIds() {
> return new int[] { TYPE_ID };
> }
>
> @Override
> public String[] getTypeNames() {
> return new String[] { TYPE_NAME };
> }
>
> @Override
> public void javaToNative(Object object, TransferData transferData) {
> startTime = System.currentTimeMillis();
> this.object = object;
> if (transferData != null) {
> super.javaToNative(String.valueOf(startTime).getBytes(),
> transferData);
> }
> }
>
> @Override
> public Object nativeToJava(TransferData transferData) {
> byte[] bytes = (byte[]) super.nativeToJava(transferData);
> if (bytes == null)
> return null;
>
> try {
> long startTime = Long.parseLong(new String(bytes));
> return this.startTime == startTime ? object : null;
> } catch (NumberFormatException exception) {
> return null;
> }
>
> }
> }
Tom
Am 03.08.12 12:04, schrieb Thomas Klöber:
> Folkse's,
>
> I need to implement DnD in TreeViewer and need some help with the
> Transfer Objects.
> Here is what I have:
>
> a TreeViewer showing different Java Objects as parents and children
>
> Here is what I need:
>
> drag a tree node and drop it on to some other tree node
>
> The only examples I could find where dealing with simple text strings
> being dragged and dropped.
> But how do I support my custom objects? I don't need any native platform
> support, clipboard support or such like. So the ByteArrayTransfer I came
> across seems to be somewhat over the top.
> Are there any other examples how to dnd POJOs?
>
> Any help/ideas are appreciated,
>
> Thomas
>
Instead of creating a ByteTransfer you use this one.
Tom
Am 03.08.12 13:38, schrieb Thomas Klöber:
> Tom,
>
> thanks for your answer. But I don't quite understand what you do in your
> nativeToJava() and javaToNative() methods...
>
> Thomas
> Hi Tom,
>
> I wonder: Is their some notable difference between MemoryTransfer and
> org.eclipse.emf.edit.ui.dnd.LocalTransfer?
>
> Thanks & Greetings form Bremen,
>
> Daniel Krügler
>
>
No - beside the fact that you don't add an emf-dependency on your
project - in fact this is a copy of the emf version ;-)
Now I have an follow-on, if I may.
I have fairly complicated drop validation, ie depending on the dragged object type it may or may not be allowed to drop on certain nodes in my tree.
Looking at the validateDrop() method I can't seem to get the dragged objects (I set the dragged data in dragStart() as part of the DragSourceEvent; dragSetData() seems to be called too late).
What would be the correct/proper/best way of getting the information into my validateDrop() method.
On 2012-08-03 14:38, Tom Schindl wrote:
> [....]
>
>> Hi Tom,
>>
>> I wonder: Is their some notable difference between MemoryTransfer and
>> org.eclipse.emf.edit.ui.dnd.LocalTransfer?
>>
>> Thanks & Greetings form Bremen,
>>
>> Daniel Krügler
>>
>
> No - beside the fact that you don't add an emf-dependency on your
> project - in fact this is a copy of the emf version ;-)
Thanks Tom. I think that MemoryTransfer is fundamental enough to have it
without need to dependent on emf. I would much appreciate to have it
publicly available such that not everyone needs to duplicate the
org.eclipse.emf.edit.ui.dnd.LocalTransfer implementation.