Java 1.1 adds cut-and-paste capabilities to Java applications through the classes and interfaces of the java.awt.datatransfer package. The DataFlavor class is perhaps the most central of these classes. It represents the type of data to be transferred. Every data flavor consists of a human-readable name and a data type specification. The data type can be specified in one of two ways: with a Java Class object or with a MIME type string. These two different ways of specifying the data type reflect two different ways of transferring the data. When the data type is specified as a class object, objects of that type are transferred using the object serialization mechanism (which is discussed in Chapter 9, Object Serialization). In Example 8.1, for example, the DataFlavor is specified using the Class object for java.util.Vector. This means that data is transferred as a serialized Vector object. It also means that the DataFlavor object has an implicit MIME type of:
application/x-java-serialized-object; class=java.util.Vector
The data type of a DataFlavor can also be specified as a MIME type. In this case, data is transferred through a stream--the recipient of the data receives a Reader stream from which it can read textual data. In this case, the recipient usually has to parse the data according to the rules of the specified MIME type.
The Transferable interface is another important piece of the AWT data transfer picture. This interface specifies methods that must be implemented by any object that wants to make data available for transfer. One of its methods returns an array of all the DataFlavor types it can use to transfer its data. Another method checks whether the Transferable object supports a given method. The most important method, getTransferData(), actually returns the data in a format appropriate for the requested DataFlavor.
While DataFlavor and Transferable provide the underlying infrastructure for data transfer, it is the Clipboard class and ClipboardOwner interface that support the cut-and-paste style of data transfer. A typical cut-and-paste scenario works like this:
Note that untrusted applets are not allowed to work with the system clipboard because there might be sensitive data on it from other applications. This means that applets cannot participate in inter-application cut-and-paste. Instead, an applet must create a private clipboard object that it can use for intra-applet data transfer.
The cut(), copy(), and paste() methods of Example 8.1 implement cut-and-paste functionality for scribbled lines. They rely on the nested SimpleSelection class that implements the Transferable and ClipboardOwner interfaces. Note the definition of a DataFlavor object that serves to specify the type of data transfer. [1]
[1] Although the example application uses the system clipboard, scribbles can only be pasted between windows of the same application, not between separate instances of the application running in separate Java interpreters. In Java 1.1.1, inter-application cut-and-paste only works with the pre-defined DataFlavor.stringFlavor and DataFlavor.textFlavor data flavors. Custom types like the one used in the example do not correctly interface with the system clipboard.
This HTML Help has been published using the chm2web software. |