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
| public class JTreeDropTarget implements DropTargetListener {
private static final String TEMP_FILE = "src/tracking/text_temp.txt";
protected JTree jTree;
protected DropTarget dropTarget;
private ClusterixGui clusterixGui;
public JTreeDropTarget(JTree jTree, ClusterixGui clusterixGui) {
this.jTree = jTree;
this.clusterixGui = clusterixGui;
dropTarget = new DropTarget(jTree, DnDConstants.ACTION_COPY_OR_MOVE, this, jTree.isEnabled(), null);
}
public void dragEnter(DropTargetDragEvent dtde) {
}
public void dragOver(DropTargetDragEvent dtde) {
}
public void dropActionChanged(DropTargetDragEvent dtde) {
}
public void dragExit(DropTargetEvent dte) {
}
public void drop(DropTargetDropEvent dtde) {
DnDUtils.debugPrintln("DropTarget drop, drop action = " + DnDUtils.showActions(dtde.getDropAction()));
// Check the drop action
if ((dtde.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0) {
// Accept the drop and get the transfer data
dtde.acceptDrop(dtde.getDropAction());
Transferable transferable = dtde.getTransferable();
boolean dropSucceeded = false;
try {
jTree.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
// Save the user's selections
// saveTreeSelection();
dropSucceeded = dropFile(dtde.getDropAction(), transferable, dtde.getLocation());
DnDUtils.debugPrintln("Drop completed, success: " + dropSucceeded);
} catch (Exception e) {
DnDUtils.debugPrintln("Exception while handling drop " + e);
} finally {
jTree.setCursor(Cursor.getDefaultCursor());
// Restore the user's selections
//restoreTreeSelection();
dtde.dropComplete(dropSucceeded);
}
} else {
DnDUtils.debugPrintln("Drop target rejected drop");
dtde.dropComplete(false);
}
}
protected boolean dropFile(int action, Transferable transferable, Point location) throws IOException, UnsupportedFlavorException, MalformedURLException {
//Your action
return true;
}
} |
Partager