Looking for a generic Transfer implementation for simple D&D [message #461742] |
Thu, 29 September 2005 04:26  |
Eclipse User |
|
|
|
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 #461808 is a reply to message #461786] |
Fri, 30 September 2005 10:01  |
Eclipse User |
|
|
|
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
|
|
|
Powered by
FUDForum. Page generated in 0.05712 seconds