Clipboard class methods above allow you to
name the clipboard, send information to it, or get information from it.
Accessing the system clipboard or creating a local clipboard is
different and requires a bit more discussion. To access the system
clipboard, assign a reference from the system clipboard to the
Clipboard class, such as:
Clipboard clipboard = getToolkit ().getSystemClipboard ();
On the other hand, to create a local clipboard you only need to create a
Clipboard object with the name that you want to assign to
it, for example:
Clipboard clipboard = new Clipboard ("My first clipboard");
Accessing the system clipboard or creating a local clipboard is
different but straightforward.
The ClipboardOwner interface
Because Java is a multiplatform language, and because operating
systems behave differently toward clipboards, the authors of the Java
language had to come up with a mechanism to deal with subtle differences.
This is the reason for the presence of the ClipboardOwner
interface. Its sole function is to inform the owner of the clipboard
when his or her data is being overwritten by someone else. It can also
signal an application when to release a resource associated with the
data.
In a real application, the lostOwnership method could be
used to set a flag that informs your application about the availability
of the data in the clipboard. Microsoft Word, while not written in
Java, is a good example of this mechanism at work in an application.
Whenever you put something in the clipboard within Word and then quit,
a dialog box appears informing you that data is in the clipboard. You
will then be asked if you want to leave the data in the clipboard.
Implementing the ClipboardOwner interface is relatively
straightforward because there is only one method to implement. This
method will cause your program to relinquish ownership of the
clipboard.
The DataFlavor class
The DataFlavor class is used to represent the
type of an object. You're not limited to one data flavor (or
type) per object. And, like us, your objects can have multiple
personalities! For example, an image class can be represented as a Java
class or as an array of bits (GIF, JPEG, and so on). In reality, a
DataFlavor class is a wrapper to a MIME type. The MIME
standard is extensive, hence there are virtually no limits to the data
that can be transferred to the clipboard. (A discussion on the MIME
standard is out of the scope of this article, but you can find
additional information in the JavaWorld Resources section.
As an example of a data flavor, you will find that the
StringSelection class has two flavors based on MIME
types. On implementation is "application/x-java-serialized-object",
and the second is "text/plain; charset=unicode". In fact, this
implementation is telling us that we can retrieve text from the
clipboard as a String class
(application/x-java-serialized-object) or as plain text
(text/plain; charset=unicode).
There are two ways to create a DataFlavor. You can write:
public DataFlavor (representationClass, String humanRepresentationName)
This constructor will create a new data flavor that represents a Java
class. The returned DataFlavor will have
representationClass = representationClass and a
mimeType = application/x-java-serialized-object. As an
example, the following would create a DataFlavor for the
java.awt.Button:
DataFlavor (Class.forName ("java.awt.Button"), "AWT Button");
Now, this second constructor
public DataFlavor (String mimeType, String humanRepresentationName)
will construct a DataFlavor using a
MimeType. The returned DataFlavor will be
based on the MimeType. If the MimeType is
application/x-java-serialized-object, then the result will
be the same as if you called the previous constructor. Nonetheless,
the returned DataFlavor will be representationClass= InputStream and mimeType =mimeType. As an example, the
following call would create a plain-text flavor:
public DataFlavor ("text/plain; charset=unicode", "Unicode");
The following table shows the methods of the DataFlavor
class.
DataFlavor class
| Methods | Description |
boolean equals
(DataFlavor) | Test if the DataFlavor supplied is equal to
the DataFlavor represented by this class |
String getHumanPresentableName
() | Return the human representable name for the format
that this DataFlavor represents |
void setHumanPresentableName
(String) | Set the human representation name for this
DataFlavor |
String getMimeType () | Get
the MIME type string represented by this DataFlavor |
Class getRepresentationClass
() | Return the Class that represents this class |
The Transferable interface
The Transferable interface must be implemented by all
classes that you want to send to the clipboard, hence the Clipboard
class will only understand classes that have been wrapped by the
Transferable interface. The Transferable
interface is comprised of three methods:
Transferable interface
| Methods | Description |
DataFlavor getTransferDataFlavor
() | Return an array of DataFlavor that represents the
object |
boolean isDataFlavorSupported
(DataFlavor) | Test if the DataFlavor supplied is
supported |
Object getTransferData
(DataFlavor) | Return the object represented by the
supplied DataFlavor |
This concludes our tour of all the classes involved in handling the
clipboard. We have seen that in order to access the clipboard we must
either create a Clipboard object or obtain a reference to
the system clipboard. Because the clipboard only accepts objects of type
Transferable, the object that you want to send to the
clipboard must implement this interface. Finally, all objects in the
clipboard have flavors that are represented by the
DataFlavor class, which in reality is a wrapper to MIME
types.
In the next sections, we will put into practice what we have learned.
The recipe for clipboard utilization
How these various classes access the clipboard can be confusing.
Fortunately, there is a simple recipe, which involves the following
steps:
Step 1. Create a class called xxxxSelection. Here, xxx
should name the type represented by this flavor. For example,
ImageSelection would be a good name for an image flavor.
This naming convention is merely a suggestion, of course. I'm following
the established convention of use with the StringSelection
provided in the JDK, but you can name this class anything you want.
It's important to remember that this object must implement the
Transferable and ClipboardOwner interfaces.
If you are planning to transfer text, the StringSelection
class should be used instead.
Step 2. Define a class to access the clipboard. To
access a local clipboard, use the following call: Clipboard
clipboard = new Clipboard ("name"). To access the peer operating
system clipboard, use this call instead: Clipboard clipboard =
getToolkit ().getSystemClipboard ().
Step 3. Set the content of the clipboard. To do this,
use the setContent method in the Clipboard
class, where the first parameter is an object that implements a
Transferable (xxxxSelection class created in
Step 1), and the second parameter is a reference to the class calling
this method.
Step 4. Get the content of the clipboard. Use the
getContent method in the Clipboard class.
This method will return a class of type Transferable.
Step 5. Implement a 'cut operation'. To do this, you
must manually erase the data once it is copied to the clipboard. Java
provides no implementation of a cut operation.
Conclusion
In this tip, we have seen that in order to use the clipboard we need a
Clipboard object. Also, data sent to the clipboard must
implement the Transferable interface. And all data in the
clipboard must have one or more flavors that are defined by the
DataFlavor class.
Related stories:
|
Latest Headlines
Today on CNN
|
Related IDG.net stories:
Note: Pages will open in a new browser window
Related sites:
External sites are not endorsed by CNN Interactive. |