package gui.tree;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
public class FileTreeDragSource implements DragGestureListener,
DragSourceListener {
public FileTreeDragSource(FileTree tree) {
this.tree = tree;
DragSource dragSource = DragSource.getDefaultDragSource();
dragSource.createDefaultDragGestureRecognizer(
tree, DnDConstants.ACTION_COPY_OR_MOVE,
this);
}
public void dragGestureRecognized(DragGestureEvent dge) {
Point location = dge.getDragOrigin();
TreePath dragPath = tree.getPathForLocation(location.x, location.y);
if (dragPath != null && tree.isPathSelected(dragPath)) {
paths = tree.getSelectionPaths();
if (paths != null && paths.length > 0) {
dragFiles = new File[paths.length];
for (int i = 0; i < paths.length; i++) {
String pathName = tree.getPathName(paths[i]);
dragFiles[i] = new File(pathName);
}
Transferable transferable =
new FileListTransferable(dragFiles);
dge.startDrag(null, transferable, this);
}
}
}
public void dragEnter(DragSourceDragEvent dsde) {
DnDUtils.debugPrintln("Drag Source: dragEnter, drop action = "
+ DnDUtils.showActions(dsde.getDropAction()));
}
public void dragOver(DragSourceDragEvent dsde) {
DnDUtils.debugPrintln("Drag Source: dragOver, drop action = "
+ DnDUtils.showActions(dsde.getDropAction()));
}
public void dragExit(DragSourceEvent dse) {
DnDUtils.debugPrintln("Drag Source: dragExit");
}
public void dropActionChanged(DragSourceDragEvent dsde) {
DnDUtils.debugPrintln("Drag Source: dropActionChanged, drop action = "
+ DnDUtils.showActions(dsde.getDropAction()));
}
public void dragDropEnd(DragSourceDropEvent dsde) {
DnDUtils.debugPrintln("Drag Source: drop completed, drop action = "
+ DnDUtils.showActions(dsde.getDropAction())
+ ", success: " + dsde.getDropSuccess());
if (dsde.getDropAction() == DnDConstants.ACTION_MOVE) {
final File[] draggedFiles = dragFiles;
final TreePath[] draggedPaths = paths;
Timer tm = new Timer(200, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
for (int i = 0; i < draggedFiles.length; i++) {
if (draggedFiles[i].exists() == false) {
DefaultMutableTreeNode node =
(DefaultMutableTreeNode) draggedPaths[i].
getLastPathComponent();
((DefaultTreeModel) tree.getModel()).
removeNodeFromParent(node);
}
}
}
});
tm.setRepeats(false);
tm.start();
}
}
public static void main(String[] args) {
JFrame f = new JFrame("Draggable File Tree");
try {
FileTree tree = new FileTree(args[0]);
f.getContentPane().add(new JScrollPane(tree));
FileTreeDragSource dragSource = new FileTreeDragSource(tree);
} catch (Exception e) {
}
f.pack();
f.setVisible(true);
}
protected FileTree tree; protected File[] dragFiles; protected TreePath[] paths; }