Java Reference
In-Depth Information
Example 13−3: ScribbleCutAndPaste.java (continued)
* current scribble after copying it to the clipboard
**/
public void cut() {
copy();
clear();
}
/**
* The user invokes this method through the popup menu.
* First, ask for the Transferable contents of the system clipboard.
* Then ask that Transferable object for the scribble data it represents.
* Try using both data flavors supported by the Scribble class.
* If it doesn't work, beep to tell the user it failed.
**/
public void paste() {
Clipboard c = this.getToolkit().getSystemClipboard(); // Get clipboard
Transferable t = c.getContents(this);
// Get its contents
// Now try to get a Scribble object from the transferrable
Scribble pastedScribble = null;
try {
pastedScribble =
(Scribble)t.getTransferData(Scribble.scribbleDataFlavor);
}
catch (Exception e) { // UnsupportedFlavor, NullPointer, etc.
// If that didn't work, try asking for a string instead.
try {
String s = (String)t.getTransferData(DataFlavor.stringFlavor);
// We got a string, so try converting it to a Scribble
pastedScribble = Scribble.parse(s);
}
catch (Exception e2) { // UnsupportedFlavor, NumberFormat, etc.
// If we couldn't get and parse a string, give up
this.getToolkit().beep(); // Tell the user the paste failed
return;
}
}
// If we get here, we've retrieved a Scribble object from the clipboard
// Add it to the current scribble, and ask to be redrawn
scribble.append(pastedScribble);
repaint();
}
/**
* This method implements the ClipboardOwner interface. We specify a
* ClipboardOwner when we copy a Scribble to the clipboard. This method
* will be invoked when something else is copied to the clipboard, and
* bumps our data off the clipboard. When this method is invoked we no
* longer have to maintain our copied Scribble object, since it is no
* longer available to be pasted. Often, a component will highlight a
* selected object while it is on the clipboard, and will use this method
* to un-highlight the object when it is no longer on the clipboard.
**/
public void lostOwnership(Clipboard c, Transferable t) {
selection = null;
}
Search WWH ::




Custom Search