14.5 A Transferable Shape
In order to support data transfer of custom data types, we
need to implement the
Transferable interface. Example 14-4 does that for the PolyLine
class (a java.awt.Shape implementation) defined in
Example 12-16.
Transferable is a simple interface to implement,
and this example is short. Note that the example also defines a
DataFlavor for transferring
PolyLine objects.
Example 14-4. TransferablePolyLine.java
package je3.datatransfer;
import java.awt.datatransfer.*;
import je3.graphics.PolyLine;
/**
* This class implements the Transferable interface for PolyLine objects.
* It also defines a DataFlavor used to describe this data type.
*/
public class TransferablePolyLine implements Transferable {
public static DataFlavor FLAVOR= new DataFlavor(PolyLine.class,"PolyLine");
static DataFlavor[ ] FLAVORS = new DataFlavor[ ] { FLAVOR };
PolyLine line; // This is the PolyLine we wrap.
public TransferablePolyLine(PolyLine line) { this.line = line; }
/** Return the supported flavor */
public DataFlavor[ ] getTransferDataFlavors( ) { return FLAVORS; }
/** Check for the one flavor we support */
public boolean isDataFlavorSupported(DataFlavor f){return f.equals(FLAVOR);}
/** Return the wrapped PolyLine, if the flavor is right */
public Object getTransferData(DataFlavor f)
throws UnsupportedFlavorException
{
if (!f.equals(FLAVOR)) throw new UnsupportedFlavorException(f);
return line;
}
}
 |