Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » DnD of POJOs(How do I implement DnD of POJOs)
DnD of POJOs [message #899975] Fri, 03 August 2012 10:04 Go to next message
Thomas Klöber is currently offline Thomas KlöberFriend
Messages: 10
Registered: June 2012
Junior Member
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
Re: DnD of POJOs [message #899984 is a reply to message #899975] Fri, 03 August 2012 10:37 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
You need to create your own MemoryTransfer.

> /**
> * 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
>
Re: DnD of POJOs [message #899997 is a reply to message #899984] Fri, 03 August 2012 11:38 Go to previous messageGo to next message
Thomas Klöber is currently offline Thomas KlöberFriend
Messages: 10
Registered: June 2012
Junior Member
Tom,

thanks for your answer. But I don't quite understand what you do in your nativeToJava() and javaToNative() methods...

Thomas
Re: DnD of POJOs [message #900000 is a reply to message #899997] Fri, 03 August 2012 11:47 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
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
Re: DnD of POJOs [message #900008 is a reply to message #899984] Fri, 03 August 2012 12:21 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 2012-08-03 12:37, Tom Schindl wrote:
> You need to create your own MemoryTransfer.
>
>> /**
>> * 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;
>> }
>>
>> }
>> }

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
Re: DnD of POJOs [message #900010 is a reply to message #900008] Fri, 03 August 2012 12:38 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
[....]

> 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 ;-)

Tom
Re: DnD of POJOs [message #900016 is a reply to message #900000] Fri, 03 August 2012 13:20 Go to previous messageGo to next message
Thomas Klöber is currently offline Thomas KlöberFriend
Messages: 10
Registered: June 2012
Junior Member
Thanks, I think I got the idea.

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.

Thanks,

Thomas
Re: DnD of POJOs [message #900029 is a reply to message #900010] Fri, 03 August 2012 14:09 Go to previous message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
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.

Thanks again,

- Daniel Krügler
Previous Topic:ViewerFilter Minimize Flickering Where To Call Table.setRedraw(false)
Next Topic:DnD Question
Goto Forum:
  


Current Time: Tue Mar 19 02:01:17 GMT 2024

Powered by FUDForum. Page generated in 0.02134 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top