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
|
public class DndListener implements DragGestureListener, DragSourceListener, DropTargetListener{
DragSource source;
DropTarget target;
DragGestureRecognizer recognizer;
JTree arbre;
public DndListener (JTree a, JButton trash){
arbre=a;
source = new DragSource();
//target = new DropTarget(trash, this);
target = new DropTarget(arbre, this);
recognizer = source.createDefaultDragGestureRecognizer(arbre,DnDConstants.ACTION_MOVE, this);
}
/*First function called for each drag*/
public void dragGestureRecognized(DragGestureEvent dge) {
TreePath path = arbre.getSelectionPath();
TreeElement te = (TreeElement)path.getLastPathComponent();
if (te instanceof Folder){
//No drag on folder
return;
}
else if ((path == null) || (path.getPathCount() <= 1)) {
// We can't move the root node or an empty selection
return;
}
else{
try{
Transferable totrans = new TransferablePath(path);
System.out.println(path+" -> "+totrans);
//dge.startDrag(DragSource.DefaultMoveDrop, totrans , this);
source.startDrag(dge, DragSource.DefaultMoveDrop, totrans, this);
//arbre.setCursor(DragSource.DefaultMoveDrop);
}
catch(InvalidDnDOperationException idoe) {
System.err.println(idoe);
}
}
}
/*Drag source -> JTree*/
public void dragDropEnd(DragSourceDropEvent dsde) {
/*
//arbre.setCursor(Cursor.getDefaultCursor());
Transferable t = dsde.getDragSourceContext().getTransferable();
System.out.println("Drag Drop End ->"+t);
try{
System.out.println(t.getTransferData(TransferablePath.TREE_PATH_FLAVOR));
}
catch(Exception e){
}
*/
}
public void dragEnter(DragSourceDragEvent dsde) {}
public void dragExit(DragSourceEvent dse) {}
public void dragOver(DragSourceDragEvent dsde) {}
public void dropActionChanged(DragSourceDragEvent dsde) {}
/*Drop target -> TRASH*/
public void dragEnter(DropTargetDragEvent dtde) {
System.out.println("drop enter ");
}
public void dragExit(DropTargetEvent dte) {
System.out.println("drop exit ");
}
public void dragOver(DropTargetDragEvent dtde) {
System.out.println("dragOver");
}
public void drop(DropTargetDropEvent dtde) {
Transferable t = dtde.getTransferable();
System.out.println("drop target event ->"+t);
try{
System.out.println(t.getTransferData(TransferablePath.TREE_PATH_FLAVOR));
}
catch(Exception e){
}
}
public void dropActionChanged(DropTargetDragEvent dtde) {
System.out.println("drop action changed exit");
}
} |
Partager