
Example 6−1: TransferableColor.java (continued)
/** Check whether a specified DataFlavor is available */
public boolean isDataFlavorSupported(DataFlavor flavor) {
if (flavor.equals(colorFlavor) ||
flavor.equals(DataFlavor.stringFlavor) ||
flavor.equals(DataFlavor.plainTextFlavor)) return true;
return false;
}
/**
* Transfer the data. Given a specified DataFlavor, return an Object
* appropriate for that flavor. Throw UnsupportedFlavorException if we
* don't support the requested flavor.
*/
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException
{
if (flavor.equals(colorFlavor)) return color;
else if (flavor.equals(DataFlavor.stringFlavor)) return color.toString();
else if (flavor.equals(DataFlavor.plainTextFlavor))
return new ByteArrayInputStream(color.toString().getBytes("Unicode"));
else throw new UnsupportedFlavorException(flavor);
}
}
Cut-and-Paste
In addition to the data transfer framework classes, the java.awt.datatransfer
package also defines the Clipboard class and the ClipboardOwner inter face, which
implement data transfer with the cut-and-paste metaphor.
A typical cut-and-paste transfer works as follows:
•Inthe initiating application, the user types Ctrl-C or Ctrl-X or in some other
way tells the application that he wants to copy or cut some data.
• The application takes the selected data and encapsulates it in an appropriate
Transferable object. The next step ...