Bonjour,
J'ai vraiment besoin d'aide par ici...
Je travaille sur du drag and drop sur un JTree.
J'ai créé un TransferHandler pour gérer le drag and drop.
Source : KineticsTransferHandler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| package tree;
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);
}
}
} |
Elle utilise un Transferable pour le data-transfer que j'ai appelé AcquisitionTransferable (déclaré à la fin).
Mon problème vient d'ici :
Source : KineticsTransferHandler.canImport(TransferHandler.TransferSupport) (l.51)
source = (Acquisition) support.getTransferable().getTransferData(ACQUISITION_NODE);
La structure que j'ai à la fin dan source (celui au-dessus) est comme une copie de l'originale. Quand je debugg sous Eclipse, je peux constaté que l'ID de source n'est pas la même que l'originale.
Alors que dans support (paramètre de KineticsTransferHandler.canImport(TransferHandler.TransferSupport)), J'ai mon Jtree qui contient la structure et qui, de plus, est la bonne.
Ce que je pense, c'est qu'il y a un problème lors de l'accès à la structure dans getTransferData, c'est, peut-être, un problème avec la serialization. Quand j'accède à ma structure getTransferData deserialize la structure et c'est pourquoi j'ai comme un clone de celle-ci.
Est-ce que vous avez la moindre idée de comment résoudre mon problème?
Partager