Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Looking for a generic Transfer implementation for simple D&D
Looking for a generic Transfer implementation for simple D&D [message #461742] Thu, 29 September 2005 08:26 Go to next message
Martin is currently offline MartinFriend
Messages: 35
Registered: July 2009
Member
Hi,

I need to implement drag&drop support. The drag source is a tree viewer
which contains self-defined model objects. The drop target will be some
widget that knows the model class.

I read some docs on SWT/JFace D&D and it seems I have to implement a
subclass of Transfer in order to use the D&D facilities. Now I wonder if
there's no simpler way of doing this: both sender and receiver know the
data type they exchange, but still I have to write a transportation
format that converts my model objects from/to byte[] (assuming I
subclass ByteArrayTransfer)...

Is there no 'generic' Transfer class that can transfer any Java object?
Or (that would be even better) did I miss something and there's a much
simpler solution for this?

Any comments are welcome.
TIA,
Martin
Re: Looking for a generic Transfer implementation for simple D&D [message #461753 is a reply to message #461742] Thu, 29 September 2005 11:02 Go to previous messageGo to next message
Daniel Hirscher is currently offline Daniel HirscherFriend
Messages: 43
Registered: July 2009
Member
Martin,

have a look at EMFs D&D for a good starting point.

--
Daniel
Re: Looking for a generic Transfer implementation for simple D&D [message #461759 is a reply to message #461753] Thu, 29 September 2005 15:12 Go to previous messageGo to next message
Martin is currently offline MartinFriend
Messages: 35
Registered: July 2009
Member
Daniel Hirscher wrote:
> Martin,
>
> have a look at EMFs D&D for a good starting point.
>

Thanks for that hint. Looks very interesting, but I think that adding
EMF to my project wouldn't be reasonable... I already have a working
data model, and it's very simple (a Composite pattern structure with
three classes) so that using a modeling framework seems a bit
over-engineered.

I just want to be able to pass objects from one SWT thingie to another
via D&D. My current implementation uses TextTransfer to pass a unique
string identifying the object; on the drop site, this id is used to get
the original object from a commonly known storage. That works, but I
feel somewhat dirty when looking at the code...

Regards,
Martin
Re: Looking for a generic Transfer implementation for simple D&D [message #461761 is a reply to message #461759] Thu, 29 September 2005 16:20 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
Write your own data transfer that just puts a string in the byte array (same
string you were using for TextTransfer) and locally copies over the object.

The reason TextTransfer is not a good choice is because there are lots of
places where Text data is a valid type and people will drop your string into
strange places.

Here is an example class that you can start with:


public class MyTransfer extends ByteArrayTransfer {

private MyType data;
private static final String MYTYPENAME =
"name_for_my_type_which_is_unique_to_me";
private static final int MYTYPEID = registerType (MYTYPENAME);
private static MyTransfer _instance = new MyTransfer ();

public static MyTransfer getInstance () {
return _instance;
}

public void javaToNative (Object object, TransferData transferData) {
if (!checkMyType(object) || !isSupportedType (transferData)) {
DND.error(DND.ERROR_INVALID_DATA);
}
data = (MyType)object;
byte[] buffer = object.toString().getBytes();
super.javaToNative (buffer, transferData);
}

public Object nativeToJava (TransferData transferData) {
if (isSupportedType (transferData)) {
byte [] buffer = (byte []) super.nativeToJava
(transferData);
String string = new String(buffer);
if (string.equals(data.toString())) return data;
}
return null;
}

protected String [] getTypeNames () {
return new String [] {MYTYPENAME};
}

protected int [] getTypeIds () {
return new int [] {MYTYPEID};
}

boolean checkMyType(Object object) {
return (object != null && object instanceof MyType);
}

protected boolean validate(Object object) {
return checkMyType(object);
}
}


"Martin" <address.starts.after.hyphen-martin.umgeher@joanneum.at> wrote in
message news:dhh0bj$18t$1@news.eclipse.org...
> Daniel Hirscher wrote:
>> Martin,
>>
>> have a look at EMFs D&D for a good starting point.
>>
>
> Thanks for that hint. Looks very interesting, but I think that adding EMF
> to my project wouldn't be reasonable... I already have a working data
> model, and it's very simple (a Composite pattern structure with three
> classes) so that using a modeling framework seems a bit over-engineered.
>
> I just want to be able to pass objects from one SWT thingie to another via
> D&D. My current implementation uses TextTransfer to pass a unique string
> identifying the object; on the drop site, this id is used to get the
> original object from a commonly known storage. That works, but I feel
> somewhat dirty when looking at the code...
>
> Regards,
> Martin
Re: Looking for a generic Transfer implementation for simple D&D [message #461786 is a reply to message #461761] Fri, 30 September 2005 07:47 Go to previous messageGo to next message
Martin is currently offline MartinFriend
Messages: 35
Registered: July 2009
Member
Veronika Irvine wrote:
> Write your own data transfer that just puts a string in the byte array (same
> string you were using for TextTransfer) and locally copies over the object.
>
> The reason TextTransfer is not a good choice is because there are lots of
> places where Text data is a valid type and people will drop your string into
> strange places.
>
> Here is an example class that you can start with:
>
> [ snip ]
>

Thanks, that really helped a lot!

Just as a matter of interest: why do I have to provide something that's
convertable to and from byte[] at all? I just don't get what
javaToNative and nativeToJava are good for if I can store the actual
object I want to transfer in the MyTransfer instance. I experimented a
bit with your code and it seems that those methods have to be called,
but I can ignore their results:

public void javaToNative(Object object, TransferData transferData) {
if (!validate(object) || !isSupportedType(transferData)) {
DND.error(DND.ERROR_INVALID_DATA);
}
data = (MyType)object;
super.javaToNative("whatever".getBytes(), transferData);
}

@Override
public Object nativeToJava(TransferData transferData) {
super.nativeToJava(transferData);
return data;
}

I'd really like to understand SWT D&D a bit better, so any explanation
(e.g. why this works or why it's evil) is welcome.

thx
Martin
Re: Looking for a generic Transfer implementation for simple D&D [message #461808 is a reply to message #461786] Fri, 30 September 2005 14:01 Go to previous message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
Drag and Drop uses the OS to transfer the data. The destination can be in a
completely different application from the source so you can not transfer a
Java object. Also, on many platforms drag and drop will fail if you don't
provide any data at all.

You do not have to create a byte array but if you do not use a byte array
you will have to write platform specific code to create a pointer to your
data in whatever form you choose. The byte array is a convenience format
provided by SWT.

"Martin" <address.starts.after.hyphen-martin.umgeher@joanneum.at> wrote in
message news:dhiqkt$cv6$1@news.eclipse.org...
> Veronika Irvine wrote:
>> Write your own data transfer that just puts a string in the byte array
>> (same string you were using for TextTransfer) and locally copies over the
>> object.
>>
>> The reason TextTransfer is not a good choice is because there are lots of
>> places where Text data is a valid type and people will drop your string
>> into strange places.
>>
>> Here is an example class that you can start with:
>>
>> [ snip ]
>>
>
> Thanks, that really helped a lot!
>
> Just as a matter of interest: why do I have to provide something that's
> convertable to and from byte[] at all? I just don't get what javaToNative
> and nativeToJava are good for if I can store the actual object I want to
> transfer in the MyTransfer instance. I experimented a bit with your code
> and it seems that those methods have to be called, but I can ignore their
> results:
>
> public void javaToNative(Object object, TransferData transferData) {
> if (!validate(object) || !isSupportedType(transferData)) {
> DND.error(DND.ERROR_INVALID_DATA);
> }
> data = (MyType)object;
> super.javaToNative("whatever".getBytes(), transferData);
> }
>
> @Override
> public Object nativeToJava(TransferData transferData) {
> super.nativeToJava(transferData);
> return data;
> }
>
> I'd really like to understand SWT D&D a bit better, so any explanation
> (e.g. why this works or why it's evil) is welcome.
>
> thx
> Martin
Previous Topic:Waiting dialogs with SWT
Next Topic:Is subclass of Button possible?
Goto Forum:
  


Current Time: Mon Oct 23 00:56:25 GMT 2017

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

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