I'm working on an component in which smaller components are stacked. The user should be able to change the order of these components using drag and drop. I've made this work by implementing an TransferHandler that accepts a local reference DataFlavor
(javaJVMLocalObjectMimeType
) of the underlying data models. This works fine.
Now I would like to also run my application a second time and be able to drag on of my components from one application into the other. In this case I want to bundle the necessary data of the drag source into a serializable object to reconstruct the object at the drop application and use a serializable DataFlavor for that. I don't want to use object serialization in both situations.
How do I decide if my drag operation is originated in the same JVM so that I can decide to use the object reference or the serialized version of the data. The official swing DnD documentation mentions that it is possible to mix local and serialization flavors but it doesn't tell how to make best use of this.
Edit
Here's how I create the flavor in my DataModelTransferable
public static DataFlavor localFlavor;
static {
try {
localFlavor = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + ";class=\"" + ArrayList.class.getName() + "\"");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
...
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { localFlavor };
}
And in my TransferHandler
I do this
@Override
public boolean canImport(TransferSupport support) {
return support.isDataFlavorSupported(DataModelTransferable.localFlavor);
}
As I said, this works fine locally, but if I drag from one to another instance the drag is accepted which leads to a java.io.IOException: Owner failed to convert data
on the drop application and a java.io.NotSerializableException: alignment.model.DataModel
on the drag source application. This is ok but the drag shouldn't be accepted on another app in the first place.
I'm using a ArrayList
as I also want to be able to drag several objects at once, fyi.
isDataFlavorSupported
called in mycanImport
returnedtrue
, butgetTransferable.getTransferData
fromimportData
has returnednull
. - Suma