/Users/lyon/j4p/src/gui/tree/FileTreeDragSource.java
|
1 package gui.tree;
2
3 import javax.swing.*;
4 import javax.swing.tree.DefaultMutableTreeNode;
5 import javax.swing.tree.DefaultTreeModel;
6 import javax.swing.tree.TreePath;
7 import java.awt.*;
8 import java.awt.datatransfer.Transferable;
9 import java.awt.dnd.*;
10 import java.awt.event.ActionEvent;
11 import java.awt.event.ActionListener;
12 import java.io.File;
13
14 public class FileTreeDragSource implements DragGestureListener,
15 DragSourceListener {
16 public FileTreeDragSource(FileTree tree) {
17 this.tree = tree;
18
19 // Use the default DragSource
20 DragSource dragSource = DragSource.getDefaultDragSource();
21
22 // Create a DragGestureRecognizer and
23 // register as the listener
24 dragSource.createDefaultDragGestureRecognizer(
25 tree, DnDConstants.ACTION_COPY_OR_MOVE,
26 this);
27 }
28
29 // Implementation of DragGestureListener interface.
30 public void dragGestureRecognized(DragGestureEvent dge) {
31 // Get the gui.mouse.m2.mouse location and convert it to
32 // a location within the gui.tree.
33 Point location = dge.getDragOrigin();
34 TreePath dragPath = tree.getPathForLocation(location.x, location.y);
35 if (dragPath != null && tree.isPathSelected(dragPath)) {
36 // Get the list of selected files and create a Transferable
37 // The list of files and the is saved for use when
38 // the drop completes.
39 paths = tree.getSelectionPaths();
40 if (paths != null && paths.length > 0) {
41 dragFiles = new File[paths.length];
42 for (int i = 0; i < paths.length; i++) {
43 String pathName = tree.getPathName(paths[i]);
44 dragFiles[i] = new File(pathName);
45 }
46
47 Transferable transferable =
48 new FileListTransferable(dragFiles);
49 dge.startDrag(null, transferable, this);
50 }
51 }
52 }
53
54 // Implementation of DragSourceListener interface
55 public void dragEnter(DragSourceDragEvent dsde) {
56 DnDUtils.debugPrintln("Drag Source: dragEnter, drop action = "
57 + DnDUtils.showActions(dsde.getDropAction()));
58 }
59
60 public void dragOver(DragSourceDragEvent dsde) {
61 DnDUtils.debugPrintln("Drag Source: dragOver, drop action = "
62 + DnDUtils.showActions(dsde.getDropAction()));
63 }
64
65 public void dragExit(DragSourceEvent dse) {
66 DnDUtils.debugPrintln("Drag Source: dragExit");
67 }
68
69 public void dropActionChanged(DragSourceDragEvent dsde) {
70 DnDUtils.debugPrintln("Drag Source: dropActionChanged, drop action = "
71 + DnDUtils.showActions(dsde.getDropAction()));
72 }
73
74 public void dragDropEnd(DragSourceDropEvent dsde) {
75 DnDUtils.debugPrintln("Drag Source: drop completed, drop action = "
76 + DnDUtils.showActions(dsde.getDropAction())
77 + ", success: " + dsde.getDropSuccess());
78 // If the drop action was ACTION_MOVE,
79 // the gui.tree might need to be updated.
80 if (dsde.getDropAction() == DnDConstants.ACTION_MOVE) {
81 final File[] draggedFiles = dragFiles;
82 final TreePath[] draggedPaths = paths;
83
84
85 Timer tm = new Timer(200, new ActionListener() {
86 public void actionPerformed(ActionEvent evt) {
87 // Check whether each of the dragged files exists.
88 // If it does not, we need to remove the node
89 // that represents it from the gui.tree.
90 for (int i = 0; i < draggedFiles.length; i++) {
91 if (draggedFiles[i].exists() == false) {
92 // Remove this node
93 DefaultMutableTreeNode node =
94 (DefaultMutableTreeNode) draggedPaths[i].
95 getLastPathComponent();
96 ((DefaultTreeModel) tree.getModel()).
97 removeNodeFromParent(node);
98 }
99 }
100 }
101 });
102 tm.setRepeats(false);
103 tm.start();
104 }
105 }
106
107 public static void main(String[] args) {
108 JFrame f = new JFrame("Draggable File Tree");
109 try {
110 FileTree tree = new FileTree(args[0]);
111 f.getContentPane().add(new JScrollPane(tree));
112
113 // Attach the drag source
114 FileTreeDragSource dragSource = new FileTreeDragSource(tree);
115 } catch (Exception e) {
116 }
117 f.pack();
118 f.setVisible(true);
119 }
120
121 protected FileTree tree; // The associated gui.tree
122 protected File[] dragFiles; // Dragged files
123 protected TreePath[] paths; // Dragged paths
124 }
125
126