在同一程序的两个正在运行的副本之间复制/粘贴 org.w3c.dom.Node

发布于 2024-08-23 21:13:58 字数 2572 浏览 4 评论 0原文

我有一个程序可以显示 XML 文件的树形表示形式。使用许多在线资源,我可以在程序的单个实例中进行复制/粘贴。我正在使用系统剪贴板。但我需要的是能够从程序的一个实例复制节点并粘贴到同一程序的不同实例。

我尝试了许多不同的选项,所有选项都导致相同的行为。当从同一应用程序内粘贴时,clipboardContent 包含一个“可传输”对象,其中包含正确的数据以及 isLocal 设置为“true”。当我执行复制,然后尝试从运行 ClipboardContent 的同一程序的另一个实例进行粘贴时,其中包含“flavorsToData”HashMap 和“flavors”值,对 isDataFlavorSupported 的检查失败(永远不会命中代表新风味的自定义类)。

我尝试在 getContents() 调用中对请求者对象使用不同的值。同样,我为 setContent() 调用尝试了一些不同的 ClipboardOwners。两者似乎都没有以任何方式改变行为。

我非常想将正在复制的节点转换回文本 XML 格式,然后在粘贴时转换回 DOM 模型,但如果可能的话我宁愿不这样做。

此类用于定义 DataFlavor 和可传输对象:

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) {
    } 
}

我从主应用程序屏幕定义一个剪贴板对象,然后绑定鼠标单击的复制和粘贴处理程序:

在初始化期间,我分配系统剪贴板:

    clippy = Toolkit.getDefaultToolkit().getSystemClipboard();

复制处理程序

    Node copyNode = ((CLIInfo) node.getUserObject()).DOMNode.cloneNode(true);
    NodeCopyPaste contents = new NodeCopyPaste(copyNode);
    clippy.setContents (contents, null);

粘贴处理程序

    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 ();
    }

谢谢!

I have a program that shows a tree representation of an XML file. Using a number of sources online I have Copy/Paste within a single instance of the program working. I am using the system Clipboard. What I need though is to be able to copy a node from one instance of the program and paste to a different instance of the same program.

I have tried a number of different options, all resulting in the same behavior. When pasting from within the same application the clipboardContent contains a "transferable" object with the correct data along with an isLocal set to "true". When I perform the copy and then attempt the paste from another instance of the same program running the clipboardContent contains a "flavorsToData" HashMap and "flavors" values, the check for isDataFlavorSupported fails (never hits my custom class that represents the new flavor).

I have tried using different values for the requestor object in the getContents() call. Likewise I have tried a few different ClipboardOwners for the setContent() call. Neither seem to change the behavior in any way.

I am sorely tempted to convert the node that is being copied back into a textual XML format, and then on the paste convert back to the DOM model, but would rather not if possible.

This class is used to define the DataFlavor and transferable 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 a Clipboard object from the main application screen and then tie in copy and paste handlers for the mouse clicks:

During initialization I assign the 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);

Paste 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 ();
    }

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪异←思 2024-08-30 21:13:58

要在进程之间传输剪贴板数据,您需要使用可序列化类作为您风格的表示类。 org.w3c.dom.Node 不扩展 Serialized,因此您的 NodeFlavor 无法复制到另一个进程。

另请注意,您错误地实现了 getTransferData() - 返回的对象应该是表示类的实例。

To transfer clipboard data between processes you need to use a Serializable class as the representation class for your flavor. org.w3c.dom.Node does not extend Serializable, so your NodeFlavor cannot be copied to another process.

Note also that you are implementing getTransferData() incorrectly - the object returned should be an instance of the representation class.

守不住的情 2024-08-30 21:13:58

说到序列化:考虑使用 JAXB 用于编组/解组 XML 数据(可以视为序列化/反序列化)。

Speaking of serialization: consider using JAXB for marshalling/unmarshalling XML data (which can be seen as serializing/deserializing).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文