java - 数据传输中反序列化后不是同一个对象

标签 java serialization drag-and-drop jtree data-transfer

我真的需要一些帮助......

我正在 Jtree 中处理拖放事件。
我创建了一个 TransferHandler 来管理拖放。

来源:KineticsTransferHandler.java

package tree;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.tree.TreePath;

import datastructures.base.Acquisition;
import datastructures.base.Kinetics;
import datastructures.base.Location;

public class KineticsTransferHandler extends TransferHandler{
    private static final long serialVersionUID = -5653477841078614666L;

    final public static DataFlavor ACQUISITION_NODE = new DataFlavor(Acquisition.class, "Acquisition Node");

    static DataFlavor flavors[] = { ACQUISITION_NODE };

    @Override
    public int getSourceActions(JComponent c) {
        return MOVE;
    }

    @Override
    protected Transferable createTransferable(JComponent c) {
        JTree tree = (JTree) c;
        TreePath path = tree.getSelectionPath();

        System.out.println(tree.getSelectionPath().toString());

        if (path != null) {
            Object o = path.getLastPathComponent();
            if(o instanceof Acquisition) {
                return new AcquisitionTransferable((Acquisition)o);
            }
        }
        return null;
    }

    @Override
    protected void exportDone(JComponent source, Transferable data, int action) {
        if(action != NONE) {
            JTree tree = (JTree) source;
            StudyTreeModel model = (StudyTreeModel)tree.getModel();
            model.printStudy();

            tree.updateUI();
        }
    }

    @Override
    public boolean canImport(TransferHandler.TransferSupport support) {
        boolean canImport = false;
        if (support.isDrop()) {
            Acquisition source = null;

            if (support.isDataFlavorSupported(ACQUISITION_NODE)) {
                try {
                    source = (Acquisition) support.getTransferable().getTransferData(ACQUISITION_NODE);
                } catch (UnsupportedFlavorException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                if(source != null) {
                    JTree.DropLocation dropLocation = (JTree.DropLocation)support.getDropLocation();
                    Object dest = dropLocation.getPath().getLastPathComponent();            
                    canImport = sameLocation(source, dest);
                }
            }
        }
        return canImport;
    }

    /*Verifies that the source and the dest are in the same Location*/
    private boolean sameLocation(Acquisition source, Object dest) {
        /*...
        A method to check if the source has the same Location than the dest.
        ...*/
    }

    @Override
    public boolean importData(TransferHandler.TransferSupport support) {
        boolean importData = false;
        if (canImport(support)) {
            Acquisition source = null;

            if (support.isDataFlavorSupported(ACQUISITION_NODE)) {
                try {
                    source = (Acquisition) support.getTransferable().getTransferData(ACQUISITION_NODE);
                    ((StudyTree)support.getComponent()).gettr
                } catch (UnsupportedFlavorException e) {
                    e.printStackTrace();
                    return false;
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }

                JTree.DropLocation dropLocation = (JTree.DropLocation)support.getDropLocation();
                Object dest = dropLocation.getPath().getLastPathComponent();

                int childIndex = dropLocation.getChildIndex();
                if (sameLocation(source, dest)) {// dest and source get the same Location
                /*...
                Management of the drop according to the dest.
                ...*/
            }
        }
        return importData;
    }

    public class AcquisitionTransferable implements Transferable {
        Acquisition acquisition;

        public AcquisitionTransferable(Acquisition s) {
            acquisition = s;
        }

        @Override
        public Object getTransferData(DataFlavor flavor)
                throws UnsupportedFlavorException {
            if (!isDataFlavorSupported(flavor))
                throw new UnsupportedFlavorException(flavor);
            return acquisition;
        }

        @Override
        public DataFlavor[] getTransferDataFlavors() {
            return flavors;
        }

        @Override
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return ACQUISITION_NODE.equals(flavor);
        }
    }
}  

它使用 Transferable 进行数据传输,我将其称为 AcquisitionTransferable(正如您在最后看到的那样)。

我的问题来自这部分

来源:KineticsTransferHandler.canImport(TransferHandler.TransferSupport)

source = (Acquisition) support.getTransferable().getTransferData(ACQUISITION_NODE);

最后,我在源代码(上面的结构)中得到的结构就像真实结构的副本。当我调试时,我可以看到source的ID与真实的ID不一样。

但是在support(KineticsTransferHandler.canImport(TransferHandler.TransferSupport)的参数)中,我有我的Jtree,其中包含结构,这是好的。

所以,我的想法是,getTransferData中的结构体访问有问题,可能是序列化的问题。当我访问我的结构时,getTransferData 会反序列化该结构,这就是为什么我就像它的克隆一样。

你知道我应该如何解决它吗?

最佳答案

您需要使用 javaJVMLocalObjectMimeType 定义 DataFlavor,以表示传输的数据驻留在本地 JVM 中。在您的情况下,DataFlavor 定义应如下所示:

final public static DataFlavor ACQUISITION_NODE = 
    new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType+"; class=\"" +Acquisition.class.getCanonicalName()+"\"" 
    ,"Acquisition Node");

检查另外两个 Java 对象 MIME 类型 here还有。

关于java - 数据传输中反序列化后不是同一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11505704/

相关文章:

angular - 使用 Angular Material CDK 拖放自定义静态组件

java - Jersey 客户端 REST API 生成 http 403 错误

java - 在 JSP Eclipse 中禁用格式化程序

java - 库层次结构的 Maven 依赖问题

python - Django DRY 模型/表单/序列化程序验证

javascript - 在 HTML5 Canvas 中拖放多个对象

java - OCPJP 8 什么是使用 NIO2 的符号链接(symbolic link)

c# - 无法反序列化当前的 JSON 数组(例如 [1,2,3])

jquery序列化问题

android - Android 9 Pie 中的拖放崩溃