Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Drang And Drop Arbitrary Objects
Drang And Drop Arbitrary Objects [message #444868] Thu, 21 October 2004 19:20 Go to next message
Preston Lee is currently offline Preston LeeFriend
Messages: 25
Registered: July 2009
Junior Member
I have a DragSourceListener that is attempting to use event.data of a
TextTransfer to pass an arbitrary object to a DropTargetListener. The
problem is that when the DropTarget.drop(DropTargetEvent) method tries to
retrive the Object from event.data, it is always null.

"DND in SWT" on eclipse.org mentions that TextTransfer expects event.data
to be a String, but the javadoc comment just says "a field for
application use", so I'm not sure if what I'm doing is supposed to
work.

What is the correct way to pass an arbitrary object from a
DragSourceListener to a DropTargetListener?

Preston
Re: Drang And Drop Arbitrary Objects [message #444869 is a reply to message #444868] Thu, 21 October 2004 20:15 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
The correct way to transfer your own data is to write your Transfer
subclass. See:

http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet79.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup

"Preston Lee" <plee@unicon.net> wrote in message
news:pan.2004.10.21.19.20.49.866454@unicon.net...
>I have a DragSourceListener that is attempting to use event.data of a
> TextTransfer to pass an arbitrary object to a DropTargetListener. The
> problem is that when the DropTarget.drop(DropTargetEvent) method tries to
> retrive the Object from event.data, it is always null.
>
> "DND in SWT" on eclipse.org mentions that TextTransfer expects event.data
> to be a String, but the javadoc comment just says "a field for
> application use", so I'm not sure if what I'm doing is supposed to
> work.
>
> What is the correct way to pass an arbitrary object from a
> DragSourceListener to a DropTargetListener?
>
> Preston
>
Re: Drang And Drop Arbitrary Objects [message #444953 is a reply to message #444869] Thu, 21 October 2004 23:02 Go to previous messageGo to next message
Preston Lee is currently offline Preston LeeFriend
Messages: 25
Registered: July 2009
Junior Member
Veronika,

Thanks for the response.

I've subclassed ByteArrayTransfer but am still getting null event.data
when my DropTargetListener's drop method is called. I also looked at the
native to/from Java methods in TextTransfer, but they just added to the
confusion, so I stuck with Snippet79.java. Here are the relevant methods
from my ByteArrayTransfer implementation that is intended to drag and drop
a List..

----BEGIN CODE----

public void javaToNative(final Object object, final TransferData pTransferData) {
if (object != null && object instanceof List
&& isSupportedType(pTransferData)) {
byte[] buffer = (byte[]) super.nativeToJava(pTransferData);
if (buffer != null) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream writeOut = new ObjectOutputStream(out);
writeOut.writeObject(object);
writeOut.close();
super.javaToNative(buffer, pTransferData);
//pTransferData.result = 1; //is this needed?
} catch (IOException e) {
}
}
}
}


public Object nativeToJava(final TransferData pTransferData) {
List list = null;
if (isSupportedType(pTransferData)) {
byte[] buffer = (byte[]) super.nativeToJava(pTransferData);
if (buffer != null) {
ByteArrayInputStream in = new ByteArrayInputStream(buffer);
ObjectInputStream readIn;
try {
readIn = new ObjectInputStream(in);
list = (List) readIn.readObject();
readIn.close();
//is this necessary?
//pTransferData.result = 1;
} catch (final IOException e) {
R2Log.logError("D'oh! I/O error in data transfer.", e);
} catch (final ClassNotFoundException e) {
R2Log.logError("D'oh! I/O error in data transfer.", e);
}
}
}
return list;
}

----END CODE----

Thanks for your help!

Preston


On Thu, 21 Oct 2004 16:15:28 -0400, Veronika Irvine wrote:

> The correct way to transfer your own data is to write your Transfer
> subclass. See:
>
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet79.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup
>
> "Preston Lee" <plee@unicon.net> wrote in message
> news:pan.2004.10.21.19.20.49.866454@unicon.net...
>>I have a DragSourceListener that is attempting to use event.data of a
>> TextTransfer to pass an arbitrary object to a DropTargetListener. The
>> problem is that when the DropTarget.drop(DropTargetEvent) method tries to
>> retrive the Object from event.data, it is always null.
>>
>> "DND in SWT" on eclipse.org mentions that TextTransfer expects event.data
>> to be a String, but the javadoc comment just says "a field for
>> application use", so I'm not sure if what I'm doing is supposed to
>> work.
>>
>> What is the correct way to pass an arbitrary object from a
>> DragSourceListener to a DropTargetListener?
>>
>> Preston
>>
Re: Drang And Drop Arbitrary Objects [message #444957 is a reply to message #444953] Fri, 22 October 2004 01:13 Go to previous messageGo to next message
Nicole is currently offline NicoleFriend
Messages: 26
Registered: July 2009
Junior Member
Hi,

Maybe you can show us the code from your DragSourceAdapter as well? Thanks.

Regards,
Nicole


Preston Lee wrote:

> Veronika,

> Thanks for the response.

> I've subclassed ByteArrayTransfer but am still getting null event.data
> when my DropTargetListener's drop method is called. I also looked at the
> native to/from Java methods in TextTransfer, but they just added to the
> confusion, so I stuck with Snippet79.java. Here are the relevant methods
> from my ByteArrayTransfer implementation that is intended to drag and drop
> a List..

> ----BEGIN CODE----

> public void javaToNative(final Object object, final TransferData
pTransferData) {
> if (object != null && object instanceof List
> && isSupportedType(pTransferData)) {
> byte[] buffer = (byte[]) super.nativeToJava(pTransferData);
> if (buffer != null) {
> try {
> ByteArrayOutputStream out = new ByteArrayOutputStream();
> ObjectOutputStream writeOut = new ObjectOutputStream(out);
> writeOut.writeObject(object);
> writeOut.close();
> super.javaToNative(buffer, pTransferData);
> //pTransferData.result = 1; //is this needed?
> } catch (IOException e) {
> }
> }
> }
> }


> public Object nativeToJava(final TransferData pTransferData) {
> List list = null;
> if (isSupportedType(pTransferData)) {
> byte[] buffer = (byte[]) super.nativeToJava(pTransferData);
> if (buffer != null) {
> ByteArrayInputStream in = new ByteArrayInputStream(buffer);
> ObjectInputStream readIn;
> try {
> readIn = new ObjectInputStream(in);
> list = (List) readIn.readObject();
> readIn.close();
> //is this necessary?
> //pTransferData.result = 1;
> } catch (final IOException e) {
> R2Log.logError("D'oh! I/O error in data transfer.", e);
> } catch (final ClassNotFoundException e) {
> R2Log.logError("D'oh! I/O error in data transfer.", e);
> }
> }
> }
> return list;
> }

> ----END CODE----

> Thanks for your help!

> Preston


> On Thu, 21 Oct 2004 16:15:28 -0400, Veronika Irvine wrote:

> > The correct way to transfer your own data is to write your Transfer
> > subclass. See:
> >
> >
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet79.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup
> >
> > "Preston Lee" <plee@unicon.net> wrote in message
> > news:pan.2004.10.21.19.20.49.866454@unicon.net...
> >>I have a DragSourceListener that is attempting to use event.data of a
> >> TextTransfer to pass an arbitrary object to a DropTargetListener. The
> >> problem is that when the DropTarget.drop(DropTargetEvent) method tries to
> >> retrive the Object from event.data, it is always null.
> >>
> >> "DND in SWT" on eclipse.org mentions that TextTransfer expects event.data
> >> to be a String, but the javadoc comment just says "a field for
> >> application use", so I'm not sure if what I'm doing is supposed to
> >> work.
> >>
> >> What is the correct way to pass an arbitrary object from a
> >> DragSourceListener to a DropTargetListener?
> >>
> >> Preston
> >>
Re: Drag And Drop Arbitrary Objects [message #444966 is a reply to message #444957] Fri, 22 October 2004 15:43 Go to previous messageGo to next message
Preston Lee is currently offline Preston LeeFriend
Messages: 25
Registered: July 2009
Junior Member
Sure. All I'm doing in the DragSourceListener is determine what I need to
pass and stick it on to pEvent.data. The DropTargetListener *is* getting
called: I just can't get payload data off it.

Preston


(Edited to remove irrelevant details.)
----TO ADD DRAG SUPPORT----

public static void addDragMetadataSupport(final TableViewer pViewer) {
if (pViewer == null) {
return;
}
pViewer.addDragSupport(DND.DROP_COPY, new Transfer[] { MyCustomTransfer
.getInstance() }, new DragSourceListener() {

public void dragSetData(DragSourceEvent pEvent) {
// .. unrelated stuff here ..
pEvent.data = somethingSerializable;
}

// ..other empty methods..
}
}

----TO ADD DROP SUPPORT---

public static DropTarget addDropMetadataSupport(final Table pTable) {
Transfer[] types = new Transfer[] { MyCustomTransfer.getInstance() };
DropTarget dt = new DropTarget(pTable, DND.DROP_COPY | DND.DROP_DEFAULT);
dt.setTransfer(types);
dt.addDropListener(new MyCustomDropTargetListener(pTable, pType));
return dt;
}

----MyCustomDropTargetAdapter----


public class MetadataTableDropTargetListener extends DropTargetAdapter {

// ..unrelated stuff..

public void dragEnter(final DropTargetEvent event) {
System.out.println("ENTER");
this.feedback = event.feedback;
if (event.detail == DND.DROP_DEFAULT) {
event.detail = (event.operations & DND.DROP_COPY) != 0 ? DND.DROP_COPY
: DND.DROP_NONE;
}
for (int i = 0, n = event.dataTypes.length; i < n; i++) {
if (MyCustomTransfer.getInstance().isSupportedType(event.dataTy pes[i]))
{
event.currentDataType = event.dataTypes[i];
}
}
}

public void drop(DropTargetEvent pEvent) {
// try to get pEvent.data
Object obj = pEvent.data // <-- always null!!!
// what follows isn't relevant because the incoming data is null
}

}

----CODE END----


On Fri, 22 Oct 2004 01:13:15 +0000, Nicole wrote:

> Hi,
>
> Maybe you can show us the code from your DragSourceAdapter as well? Thanks.
>
> Regards,
> Nicole
>
>
> Preston Lee wrote:
>
>> Veronika,
>
>> Thanks for the response.
>
>> I've subclassed ByteArrayTransfer but am still getting null event.data
>> when my DropTargetListener's drop method is called. I also looked at the
>> native to/from Java methods in TextTransfer, but they just added to the
>> confusion, so I stuck with Snippet79.java. Here are the relevant methods
>> from my ByteArrayTransfer implementation that is intended to drag and drop
>> a List..
>
>> ----BEGIN CODE----
>
>> public void javaToNative(final Object object, final TransferData
> pTransferData) {
>> if (object != null && object instanceof List
>> && isSupportedType(pTransferData)) {
>> byte[] buffer = (byte[]) super.nativeToJava(pTransferData);
>> if (buffer != null) {
>> try {
>> ByteArrayOutputStream out = new ByteArrayOutputStream();
>> ObjectOutputStream writeOut = new ObjectOutputStream(out);
>> writeOut.writeObject(object);
>> writeOut.close();
>> super.javaToNative(buffer, pTransferData);
>> //pTransferData.result = 1; //is this needed?
>> } catch (IOException e) {
>> }
>> }
>> }
>> }
>
>
>> public Object nativeToJava(final TransferData pTransferData) {
>> List list = null;
>> if (isSupportedType(pTransferData)) {
>> byte[] buffer = (byte[]) super.nativeToJava(pTransferData);
>> if (buffer != null) {
>> ByteArrayInputStream in = new ByteArrayInputStream(buffer);
>> ObjectInputStream readIn;
>> try {
>> readIn = new ObjectInputStream(in);
>> list = (List) readIn.readObject();
>> readIn.close();
>> //is this necessary?
>> //pTransferData.result = 1;
>> } catch (final IOException e) {
>> R2Log.logError("D'oh! I/O error in data transfer.", e);
>> } catch (final ClassNotFoundException e) {
>> R2Log.logError("D'oh! I/O error in data transfer.", e);
>> }
>> }
>> }
>> return list;
>> }
>
>> ----END CODE----
>
>> Thanks for your help!
>
>> Preston
>
>
>> On Thu, 21 Oct 2004 16:15:28 -0400, Veronika Irvine wrote:
>
>> > The correct way to transfer your own data is to write your Transfer
>> > subclass. See:
>> >
>> >
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet79.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup
>> >
>> > "Preston Lee" <plee@unicon.net> wrote in message
>> > news:pan.2004.10.21.19.20.49.866454@unicon.net...
>> >>I have a DragSourceListener that is attempting to use event.data of a
>> >> TextTransfer to pass an arbitrary object to a DropTargetListener. The
>> >> problem is that when the DropTarget.drop(DropTargetEvent) method tries to
>> >> retrive the Object from event.data, it is always null.
>> >>
>> >> "DND in SWT" on eclipse.org mentions that TextTransfer expects event.data
>> >> to be a String, but the javadoc comment just says "a field for
>> >> application use", so I'm not sure if what I'm doing is supposed to
>> >> work.
>> >>
>> >> What is the correct way to pass an arbitrary object from a
>> >> DragSourceListener to a DropTargetListener?
>> >>
>> >> Preston
>> >>
Re: Drang And Drop Arbitrary Objects [message #444969 is a reply to message #444953] Fri, 22 October 2004 17:38 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
My guess is you are failing on "object instanceof List" in javaToNative. Be
sure to fully qualify the name List because SWT has a List class, AWT has a
List class, java.util has a List class - all of which extend Object.

If the object you set into event.data in setDragData is not of the same type
expected by MyTransferType, then the event.data field in the drop listener
will be null.

"Preston Lee" <plee@unicon.net> wrote in message
news:pan.2004.10.21.23.02.01.192126@unicon.net...
> Veronika,
>
> Thanks for the response.
>
> I've subclassed ByteArrayTransfer but am still getting null event.data
> when my DropTargetListener's drop method is called. I also looked at the
> native to/from Java methods in TextTransfer, but they just added to the
> confusion, so I stuck with Snippet79.java. Here are the relevant methods
> from my ByteArrayTransfer implementation that is intended to drag and drop
> a List..
>
> ----BEGIN CODE----
>
> public void javaToNative(final Object object, final TransferData
> pTransferData) {
> if (object != null && object instanceof List
> && isSupportedType(pTransferData)) {
> byte[] buffer = (byte[]) super.nativeToJava(pTransferData);
> if (buffer != null) {
> try {
> ByteArrayOutputStream out = new ByteArrayOutputStream();
> ObjectOutputStream writeOut = new ObjectOutputStream(out);
> writeOut.writeObject(object);
> writeOut.close();
> super.javaToNative(buffer, pTransferData);
> //pTransferData.result = 1; //is this needed?
> } catch (IOException e) {
> }
> }
> }
> }
>
>
> public Object nativeToJava(final TransferData pTransferData) {
> List list = null;
> if (isSupportedType(pTransferData)) {
> byte[] buffer = (byte[]) super.nativeToJava(pTransferData);
> if (buffer != null) {
> ByteArrayInputStream in = new ByteArrayInputStream(buffer);
> ObjectInputStream readIn;
> try {
> readIn = new ObjectInputStream(in);
> list = (List) readIn.readObject();
> readIn.close();
> //is this necessary?
> //pTransferData.result = 1;
> } catch (final IOException e) {
> R2Log.logError("D'oh! I/O error in data transfer.", e);
> } catch (final ClassNotFoundException e) {
> R2Log.logError("D'oh! I/O error in data transfer.", e);
> }
> }
> }
> return list;
> }
>
> ----END CODE----
>
> Thanks for your help!
>
> Preston
>
>
> On Thu, 21 Oct 2004 16:15:28 -0400, Veronika Irvine wrote:
>
>> The correct way to transfer your own data is to write your Transfer
>> subclass. See:
>>
>> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet79.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup
>>
>> "Preston Lee" <plee@unicon.net> wrote in message
>> news:pan.2004.10.21.19.20.49.866454@unicon.net...
>>>I have a DragSourceListener that is attempting to use event.data of a
>>> TextTransfer to pass an arbitrary object to a DropTargetListener. The
>>> problem is that when the DropTarget.drop(DropTargetEvent) method tries
>>> to
>>> retrive the Object from event.data, it is always null.
>>>
>>> "DND in SWT" on eclipse.org mentions that TextTransfer expects
>>> event.data
>>> to be a String, but the javadoc comment just says "a field for
>>> application use", so I'm not sure if what I'm doing is supposed to
>>> work.
>>>
>>> What is the correct way to pass an arbitrary object from a
>>> DragSourceListener to a DropTargetListener?
>>>
>>> Preston
>>>
>
Re: Drang And Drop Arbitrary Objects [message #444972 is a reply to message #444969] Fri, 22 October 2004 17:58 Go to previous message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
Sorry. Ignore my previous post. I just took a better look at your code.

Here is the problem: javaToNative can not call nativeToJava to get the
byte[] for of the data

You need to be able to convert List to and from a byte array. Calling
super.nativeToJava requires that transferData contains the data in a native
format - you have not provided that. You do not have a native format of the
data in javaToNative - that is exactly the purpose of javaToNative.

You need to be more specific about the type of data you are transferring.
It has to be something you can represent in bytes. List is too general.
You need to know what the List contains and how it can be converted to bytes
and vice versa.

"Veronika Irvine" <veronika_irvine@oti.com> wrote in message
news:clbg83$dpn$1@eclipse.org...
> My guess is you are failing on "object instanceof List" in javaToNative.
> Be sure to fully qualify the name List because SWT has a List class, AWT
> has a List class, java.util has a List class - all of which extend Object.
>
> If the object you set into event.data in setDragData is not of the same
> type expected by MyTransferType, then the event.data field in the drop
> listener will be null.
>
> "Preston Lee" <plee@unicon.net> wrote in message
> news:pan.2004.10.21.23.02.01.192126@unicon.net...
>> Veronika,
>>
>> Thanks for the response.
>>
>> I've subclassed ByteArrayTransfer but am still getting null event.data
>> when my DropTargetListener's drop method is called. I also looked at the
>> native to/from Java methods in TextTransfer, but they just added to the
>> confusion, so I stuck with Snippet79.java. Here are the relevant methods
>> from my ByteArrayTransfer implementation that is intended to drag and
>> drop
>> a List..
>>
>> ----BEGIN CODE----
>>
>> public void javaToNative(final Object object, final TransferData
>> pTransferData) {
>> if (object != null && object instanceof List
>> && isSupportedType(pTransferData)) {
>> byte[] buffer = (byte[]) super.nativeToJava(pTransferData);
>> if (buffer != null) {
>> try {
>> ByteArrayOutputStream out = new ByteArrayOutputStream();
>> ObjectOutputStream writeOut = new ObjectOutputStream(out);
>> writeOut.writeObject(object);
>> writeOut.close();
>> super.javaToNative(buffer, pTransferData);
>> //pTransferData.result = 1; //is this needed?
>> } catch (IOException e) {
>> }
>> }
>> }
>> }
>>
>>
>> public Object nativeToJava(final TransferData pTransferData) {
>> List list = null;
>> if (isSupportedType(pTransferData)) {
>> byte[] buffer = (byte[]) super.nativeToJava(pTransferData);
>> if (buffer != null) {
>> ByteArrayInputStream in = new ByteArrayInputStream(buffer);
>> ObjectInputStream readIn;
>> try {
>> readIn = new ObjectInputStream(in);
>> list = (List) readIn.readObject();
>> readIn.close();
>> //is this necessary?
>> //pTransferData.result = 1;
>> } catch (final IOException e) {
>> R2Log.logError("D'oh! I/O error in data transfer.", e);
>> } catch (final ClassNotFoundException e) {
>> R2Log.logError("D'oh! I/O error in data transfer.", e);
>> }
>> }
>> }
>> return list;
>> }
>>
>> ----END CODE----
>>
>> Thanks for your help!
>>
>> Preston
>>
>>
>> On Thu, 21 Oct 2004 16:15:28 -0400, Veronika Irvine wrote:
>>
>>> The correct way to transfer your own data is to write your Transfer
>>> subclass. See:
>>>
>>> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet79.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup
>>>
>>> "Preston Lee" <plee@unicon.net> wrote in message
>>> news:pan.2004.10.21.19.20.49.866454@unicon.net...
>>>>I have a DragSourceListener that is attempting to use event.data of a
>>>> TextTransfer to pass an arbitrary object to a DropTargetListener. The
>>>> problem is that when the DropTarget.drop(DropTargetEvent) method tries
>>>> to
>>>> retrive the Object from event.data, it is always null.
>>>>
>>>> "DND in SWT" on eclipse.org mentions that TextTransfer expects
>>>> event.data
>>>> to be a String, but the javadoc comment just says "a field for
>>>> application use", so I'm not sure if what I'm doing is supposed to
>>>> work.
>>>>
>>>> What is the correct way to pass an arbitrary object from a
>>>> DragSourceListener to a DropTargetListener?
>>>>
>>>> Preston
>>>>
>>
>
>
Previous Topic:how to fix the size of Excel OLE object
Next Topic:Drag and Drop validation
Goto Forum:
  


Current Time: Fri Apr 19 00:00:50 GMT 2024

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

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

Back to the top