de.guibuilder.framework
Class FileDrop.TransferableObject

java.lang.Object
  extended by de.guibuilder.framework.FileDrop.TransferableObject
All Implemented Interfaces:
java.awt.datatransfer.Transferable
Enclosing class:
FileDrop

public static class FileDrop.TransferableObject
extends java.lang.Object
implements java.awt.datatransfer.Transferable

At last an easy way to encapsulate your custom objects for dragging and dropping in your Java programs! When you need to create a Transferable object, use this class to wrap your object. For example:


      ...
      MyCoolClass myObj = new MyCoolClass();
      Transferable xfer = new TransferableObject( myObj );
      ...
 
Or if you need to know when the data was actually dropped, like when you're moving data out of a list, say, you can use the FileDrop.TransferableObject.Fetcher inner class to return your object Just in Time. For example:

      ...
      final MyCoolClass myObj = new MyCoolClass();

      TransferableObject.Fetcher fetcher = new TransferableObject.Fetcher()
      {   public Object getObject(){ return myObj; }
      }; // end fetcher

      Transferable xfer = new TransferableObject( fetcher );
      ...
 
The DataFlavor associated with FileDrop.TransferableObject has the representation class net.iharder.dnd.TransferableObject.class and MIME type application/x-net.iharder.dnd.TransferableObject. This data flavor is accessible via the static DATA_FLAVOR property.

I'm releasing this code into the Public Domain. Enjoy.

Version:
1.2
Author:
Robert Harder, rob@iharder.net

Nested Class Summary
static interface FileDrop.TransferableObject.Fetcher
          Instead of passing your data directly to the FileDrop.TransferableObject constructor, you may want to know exactly when your data was received in case you need to remove it from its source (or do anyting else to it).
 
Field Summary
static java.awt.datatransfer.DataFlavor DATA_FLAVOR
          The default DataFlavor for FileDrop.TransferableObject has the representation class net.iharder.dnd.TransferableObject.class and the MIME type application/x-net.iharder.dnd.TransferableObject.
static java.lang.String MIME_TYPE
          The MIME type for DATA_FLAVOR is application/x-net.iharder.dnd.TransferableObject.
 
Constructor Summary
FileDrop.TransferableObject(java.lang.Class dataClass, FileDrop.TransferableObject.Fetcher fetcher)
          Creates a new FileDrop.TransferableObject that will return the object that is returned by fetcher.
FileDrop.TransferableObject(FileDrop.TransferableObject.Fetcher fetcher)
          Creates a new FileDrop.TransferableObject that will return the object that is returned by fetcher.
FileDrop.TransferableObject(java.lang.Object data)
          Creates a new FileDrop.TransferableObject that wraps data.
 
Method Summary
 java.awt.datatransfer.DataFlavor getCustomDataFlavor()
          Returns the custom DataFlavor associated with the encapsulated object or null if the FileDrop.TransferableObject.Fetcher constructor was used without passing a Class.
 java.lang.Object getTransferData(java.awt.datatransfer.DataFlavor flavor)
          Returns the data encapsulated in this FileDrop.TransferableObject.
 java.awt.datatransfer.DataFlavor[] getTransferDataFlavors()
          Returns a two- or three-element array containing first the custom data flavor, if one was created in the constructors, second the default DATA_FLAVOR associated with FileDrop.TransferableObject, and third the java.awt.datatransfer.DataFlavor.stringFlavor.
 boolean isDataFlavorSupported(java.awt.datatransfer.DataFlavor flavor)
          Returns true if flavor is one of the supported flavors.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MIME_TYPE

public static final java.lang.String MIME_TYPE
The MIME type for DATA_FLAVOR is application/x-net.iharder.dnd.TransferableObject.

Since:
1.1
See Also:
Constant Field Values

DATA_FLAVOR

public static final java.awt.datatransfer.DataFlavor DATA_FLAVOR
The default DataFlavor for FileDrop.TransferableObject has the representation class net.iharder.dnd.TransferableObject.class and the MIME type application/x-net.iharder.dnd.TransferableObject.

Since:
1.1
Constructor Detail

FileDrop.TransferableObject

public FileDrop.TransferableObject(java.lang.Object data)
Creates a new FileDrop.TransferableObject that wraps data. Along with the DATA_FLAVOR associated with this class, this creates a custom data flavor with a representation class determined from data.getClass() and the MIME type application/x-net.iharder.dnd.TransferableObject.

Parameters:
data - The data to transfer
Since:
1.1

FileDrop.TransferableObject

public FileDrop.TransferableObject(FileDrop.TransferableObject.Fetcher fetcher)
Creates a new FileDrop.TransferableObject that will return the object that is returned by fetcher. No custom data flavor is set other than the default DATA_FLAVOR.

Parameters:
fetcher - The FileDrop.TransferableObject.Fetcher that will return the data object
Since:
1.1
See Also:
FileDrop.TransferableObject.Fetcher

FileDrop.TransferableObject

public FileDrop.TransferableObject(java.lang.Class dataClass,
                                   FileDrop.TransferableObject.Fetcher fetcher)
Creates a new FileDrop.TransferableObject that will return the object that is returned by fetcher. Along with the DATA_FLAVOR associated with this class, this creates a custom data flavor with a representation class dataClass and the MIME type application/x-net.iharder.dnd.TransferableObject.

Parameters:
dataClass - The Class to use in the custom data flavor
fetcher - The FileDrop.TransferableObject.Fetcher that will return the data object
Since:
1.1
See Also:
FileDrop.TransferableObject.Fetcher
Method Detail

getCustomDataFlavor

public java.awt.datatransfer.DataFlavor getCustomDataFlavor()
Returns the custom DataFlavor associated with the encapsulated object or null if the FileDrop.TransferableObject.Fetcher constructor was used without passing a Class.

Returns:
The custom data flavor for the encapsulated object
Since:
1.1

getTransferDataFlavors

public java.awt.datatransfer.DataFlavor[] getTransferDataFlavors()
Returns a two- or three-element array containing first the custom data flavor, if one was created in the constructors, second the default DATA_FLAVOR associated with FileDrop.TransferableObject, and third the java.awt.datatransfer.DataFlavor.stringFlavor.

Specified by:
getTransferDataFlavors in interface java.awt.datatransfer.Transferable
Returns:
An array of supported data flavors
Since:
1.1

getTransferData

public java.lang.Object getTransferData(java.awt.datatransfer.DataFlavor flavor)
                                 throws java.awt.datatransfer.UnsupportedFlavorException,
                                        java.io.IOException
Returns the data encapsulated in this FileDrop.TransferableObject. If the FileDrop.TransferableObject.Fetcher constructor was used, then this is when the getObject() method will be called. If the requested data flavor is not supported, then the getObject() method will not be called.

Specified by:
getTransferData in interface java.awt.datatransfer.Transferable
Parameters:
flavor - The data flavor for the data to return
Returns:
The dropped data
Throws:
java.awt.datatransfer.UnsupportedFlavorException
java.io.IOException
Since:
1.1

isDataFlavorSupported

public boolean isDataFlavorSupported(java.awt.datatransfer.DataFlavor flavor)
Returns true if flavor is one of the supported flavors. Flavors are supported using the equals(...) method.

Specified by:
isDataFlavorSupported in interface java.awt.datatransfer.Transferable
Parameters:
flavor - The data flavor to check
Returns:
Whether or not the flavor is supported
Since:
1.1