ここで本当に助けが必要です...
でドラッグアンドドロップイベントに取り組んでいます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
(上のもの)は、本物のコピーのようなものです。source
デバッグしていると、の ID が実際の ID と同じではない ことがわかります。
しかし、support
(のパラメータKineticsTransferHandler.canImport(TransferHandler.TransferSupport)
)にJtree
は、構造を含むものがあります。これは良いものです。
だから、私が考えているのは、 の構造体へのアクセスに問題があるgetTransferData
ということです。シリアライゼーションに問題がある可能性があります。構造体にアクセスすると、構造体がgetTransferData
逆シリアル化されるため、そのクローンのようになります。
どのように修正すればよいか、何か考えはありますか?