Bali Share 1.1.18

oracle.bali.share.datatransfer
Class ObjectTransferable

java.lang.Object
  |
  +--oracle.bali.share.datatransfer.ObjectTransferable
All Implemented Interfaces:
java.awt.datatransfer.Transferable

public abstract class ObjectTransferable
extends java.lang.Object
implements java.awt.datatransfer.Transferable

An ObjectTransferable is a basic Transferable implementation that makes it easy to send an object as a Transferable for use in drag-and-drop. It supports requests for data flavors that are superclasses of the provided data, and allows for both lazily-instantiated data and data provided up-front. It also correctly special cases Strings and single-element arrays.

The DataFlavor used by ObjectTransferables can be independently derived from TransferUtils.getDataFlavor().

The most common style of usage is:

    Transferable t = ObjectTransferable.createTransferable(anObject);
 
In this style of usage, the object must be provided up front. In cases where this is impossible or undesirable (e.g., because the object requires a lot of memory to instantiate), this class can be subclassed to lazily request the object's data:

   class DelayedTransferable extends ObjectTransferable
   {
     public DelayedTransferable()
     {
       // Provide the class of the data immediately
       super(SomeClass.class);
     }
 
     protected Object getTransferData()
     {
       // But we only create the object when necessary
       return new SomeClass();
     }
   } 
 

This class is especially useful in conjunction with the CompoundTransferable class (also in this package), which lets you glue multiple transferables together.

See Also:
TransferUtils, CompoundTransferable

Constructor Summary
protected ObjectTransferable(java.lang.Class classObj)
          Creates a lazily-instantiated transferable for the given class.
 
Method Summary
static java.awt.datatransfer.Transferable createTransferable(java.lang.Object object)
          Returns a Transferable implementation that will wrap the given object.
protected abstract  java.lang.Object getTransferData()
          Subclasses must override this method to return the data Object contained by this Transferable.
 java.lang.Object getTransferData(java.awt.datatransfer.DataFlavor flavor)
          Returns an object which represents the data to be transferred.
 java.awt.datatransfer.DataFlavor[] getTransferDataFlavors()
          Returns an array of DataFlavor objects indicating the flavors the data can be provided in.
 boolean isDataFlavorSupported(java.awt.datatransfer.DataFlavor flavor)
          Returns whether or not the specified data flavor is supported for this object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectTransferable

protected ObjectTransferable(java.lang.Class classObj)
Creates a lazily-instantiated transferable for the given class. Clients that wish to provide the data up-front should simply use the static createTransferable() methods; other clients must subclass this class and override getTransferData().
See Also:
createTransferable(java.lang.Object)
Method Detail

createTransferable

public static java.awt.datatransfer.Transferable createTransferable(java.lang.Object object)
Returns a Transferable implementation that will wrap the given object.

Arrays with single elements are special cased to also include a DataFlavor of just the first element.

Strings are special-cased to return instances of StringSelection.

Parameters:
object - the object

getTransferDataFlavors

public java.awt.datatransfer.DataFlavor[] getTransferDataFlavors()
Returns an array of DataFlavor objects indicating the flavors the data can be provided in. The array should be ordered according to preference for providing the data (from most richly descriptive to least descriptive).
Specified by:
getTransferDataFlavors in interface java.awt.datatransfer.Transferable
Returns:
an array of data flavors in which this data can be transferred

isDataFlavorSupported

public boolean isDataFlavorSupported(java.awt.datatransfer.DataFlavor flavor)
Returns whether or not the specified data flavor is supported for this object. Will return true if the requested data flavor is for the same class as the data, or for any superclass of the data.
Specified by:
isDataFlavorSupported in interface java.awt.datatransfer.Transferable
Parameters:
flavor - the requested flavor for the data
Returns:
boolean indicating whether or not the data flavor is supported

getTransferData

public final java.lang.Object getTransferData(java.awt.datatransfer.DataFlavor flavor)
                                       throws java.awt.datatransfer.UnsupportedFlavorException,
                                              java.io.IOException
Returns an object which represents the data to be transferred. The class of the object returned is defined by the representation class of the flavor. Subclasses must override the protected getTransferData() method to return the actual data.
Specified by:
getTransferData in interface java.awt.datatransfer.Transferable
Parameters:
flavor - the requested flavor for the data
Throws:
java.io.IOException - if the data is no longer available in the requested flavor.
java.awt.datatransfer.UnsupportedFlavorException - if the requested data flavor is not supported.

getTransferData

protected abstract java.lang.Object getTransferData()
Subclasses must override this method to return the data Object contained by this Transferable. The returned object must be an instance of the Class provided to the constructor (or a subclass of that class, or, if that Class represented an interface, an object implementing that interface).

Bali Share 1.1.18