I have a program that shows a tree view of an XML file. Using several sources on the Internet, I have Copy / Paste in one instance of a running program. I use the system clipboard. I need, however, to be able to copy node from one instance of the program and paste into another instance of one program.
I tried several different options, all of which led to the same behavior. When pasting from the same application, the clipContent buffer contains a "portable" object with the correct data, along with isLocal set to "true". When I execute a copy and then try to paste from another instance of the same program that starts the clipboard. The content contains “hash” and “taste” values “flavorsToData”, the check for isDataFlavorSupported fails (it never hits my custom class that represents a new taste).
I tried using different values for the requestor object in the getContents () call. Similarly, I tried several different ClipboardOwners to call setContent (). Nothing will change the behavior.
I am very tempted to convert the node, which is copied back to the XML text format, and then pasted back into the DOM model, but probably will not, if possible.
This class is used to define a DataFlavor and a portable object:
import java.awt.datatransfer.*; import org.w3c.dom.Node;
public class NodeCopyPaste implements Transferable {
static public DataFlavor NodeFlavor;
private DataFlavor [] supportedFlavors = {NodeFlavor};
public Node aNode;
public NodeCopyPaste (Node paraNode) {
aNode = paraNode;
try {
NodeFlavor = new DataFlavor (Class.forName ("org.w3c.dom.Node"), "Node");
}
catch (ClassNotFoundException e) {
e.printStackTrace ();
}
}
public synchronized DataFlavor [] getTransferDataFlavors () {
return (supportedFlavors);
}
public boolean isDataFlavorSupported (DataFlavor nodeFlavor) {
return (nodeFlavor.equals (NodeFlavor));
}
public synchronized Object getTransferData (DataFlavor nFlavor) throws UnsupportedFlavorException {
if (nFlavor.equals (NodeFlavor))
return (this);
else
throw new UnsupportedFlavorException (nFlavor);
}
public void lostOwnership (Clipboard parClipboard, Transferable parTransferable) {
}
}
I define the Clipboard object on the main screen of the application, and then attach the copies and paste handlers for the mouse clicks:
During initialization, I assign a system clipboard:
clippy = Toolkit.getDefaultToolkit().getSystemClipboard();
Copy handler
Node copyNode = ((CLIInfo) node.getUserObject()).DOMNode.cloneNode(true);
NodeCopyPaste contents = new NodeCopyPaste(copyNode);
clippy.setContents (contents, null);
Insert handler
Transferable clipboardContent = clippy.getContents (null);
Node clonedNode = null;
if ((clipboardContent != null) &&
(clipboardContent.isDataFlavorSupported (NodeCopyPaste.NodeFlavor))) {
try {
NodeCopyPaste tempNCP = (NodeCopyPaste) clipboardContent.getTransferData (NodeCopyPaste.NodeFlavor);
clonedNode = tempNCP.aNode.cloneNode(true);
}
catch (Exception e) {
e.printStackTrace ();
}
Thank!