[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: SWT vs SWING/AWT Drag and drop

Ok, I have figured out the last peice!  Full Swing to SWT custom data 
type Drag & Drop.

It took a bit of reverse engineering.

=====================================
The Problem:
============================
The problem I was having was replicating the TYPE_ID  on my SWT Transfer 
Object, so that it would match the type generated by the OS when 
dragging and dropping from a Swing component (with a custom DataFlavor 
defined).

Code-wise, I had a Swing DataFlavor:

> DataFlavor customFlavor = new DataFlavor (CustomClass.class,
>                                           "A Custom Object");

And I had a custom SWT Transfer:

>public final CustomTransfer extends ByteArrayTransfer{  
>     private static final String TYPE_NAME = "A Custom Object";
>     private static final int TYPE_ID = registerType (TYPE_NAME );
>
>     protected int[] getTypeIds() {
>        return new int[] { TYPE_ID };
>     }
>}

The problem is that the super class (ByteArrayTransfer) calls --

> public boolean isSupportedType(TransferData transferData) {..}

 to see if my Transfer is compatible with the incoming D&D object.  
However, TYPE_ID that was created by calling registerType() did not 
match the transferData.type.  So... this made it impossible to say that 
my SWT Transfer was compatible with the Swing DataFlavor.

So, I tried to find exactly what the DataFlavor looked like, and what 
transferData.type Id it became.

I thought it might be:
> customFlavor.getMimeType();

which was:
> application/x-java-serialized-object; class=com.mydomain.CustomClass

Still when calling 
> registerType("application/x-java-serialized-object; 
class=com.mydomain.CustomClass")

 the resulting TYPE_ID did not match what was being generated in 
transferData.type.

=====================================
Solution:
=====================================
At run time, I tried calling:
>        int maxSize = 128;
>        TCHAR buffer = new TCHAR(0, maxSize);
>        int size = COM.GetClipboardFormatName(49948, buffer, maxSize);
>        System.out.println("size=" + size + ", buffer= " + buffer);

(where 49948 is the ID that was passed in the transferData DnD object).

Out comes...
size=107, buffer= JAVA_DATAFLAVOR:application/x-java-serialized-object; 
class=com.mydomain.CustomClass

...!!!  It's the DataFlavor generated MIME type String, with a prefix!

So, I changed my SWT Transfer:

>public final CustomTransfer extends ByteArrayTransfer{  
>     private static final String TYPE_NAME = "JAVA_DATAFLAVOR:" + 
>                                         customFlavor .getMimeType();
>     private static final int TYPE_ID = registerType (TYPE_NAME );
>
>     protected int[] getTypeIds() {
>        return new int[] { TYPE_ID };
>     }
>}


Now, the TYPE_ID matches the exact transferData.type ID created through 
the SWT drag and drop mechanism! And it is programmatic (only guaranteed 
to work in the same JVM though; but that is exactly what I want in my 
use case).

Thanks for all the help!
Sheldon Warkentin

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

duongn@xxxxxxxxxx (Duong Nguyen) wrote in
news:633dda0156d16daf7877cc5299a81f3d$1@xxxxxxxxxxxxxxx: 

> I don't know SWING/AWT implementation so I'm not sure how they
> generate the string to register with the OS.
> 
> Perhaps you can use the constructor DataFlavor(String) and use the
> same string for SWT. Or you can call getMimeType() after the
> DataFlavor is created to see what string is being used by SWING/AWT.
> 
> If you get it working, please report back. I'm curious what it is.
> 
> Duong
> 
> Sheldon Warkentin wrote:
> 
>> Thanks Duong!
> 
>> This almost entirely works.  I am only having an issue with the 
>> generated Type Id in the SWT custom transfer class.
> 
>> Re:
>>>     protected static DataFlavor customFlavor = new DataFlavor
>>> (CustomClass.class,
>>>         "A Custom Object");
> 
>> and:
>>>     private static final String MYTYPENAME = "A Custom Object";
>>>     private static final int MYTYPEID = registerType (MYTYPENAME);
> 
> .........................
> 
>> Do I need MYTYPENAME to look something like, "Mime type/text... 
>> class=custom.class"?
> 
>> With this resolved - I think I will be fully satisfied with the 
>> solution!
> 
>> Thank you,
>> Sheldon
> 
> 
>