1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.util;
21
22 import java.awt.Component;
23 import java.awt.dnd.DragGestureEvent;
24 import java.awt.dnd.DragGestureListener;
25 import java.awt.dnd.DragSource;
26 import java.awt.dnd.DragSourceDragEvent;
27 import java.awt.dnd.DragSourceDropEvent;
28 import java.awt.dnd.DragSourceEvent;
29 import java.awt.dnd.DragSourceListener;
30 import java.awt.dnd.InvalidDnDOperationException;
31 import java.io.File;
32
33 import org.apache.log4j.Logger;
34 import org.xnap.gui.FileProvider;
35
36 /***
37 * DragFilesSupport enables dragging of files for a component implementing the
38 * FileProvider interface.
39 *
40 * @see FileProvider
41 */
42 public class DragFilesSupport
43 implements DragSourceListener, DragGestureListener
44 {
45
46
47
48
49 private DragSource dragSource = DragSource.getDefaultDragSource();
50 private FileProvider fp;
51
52 private static Logger logger = Logger.getLogger(DragFilesSupport.class);
53
54
55
56 /***
57 * Constructs a new DragFilesSupport.
58 *
59 * @param c
60 * @param fp
61 */
62 public DragFilesSupport(Component c, FileProvider fp)
63 {
64 this.fp = fp;
65
66
67 }
68
69
70
71 public void dragDropEnd(DragSourceDropEvent event)
72 {
73
74 }
75
76 public void dragEnter(DragSourceDragEvent event)
77 {
78
79 }
80
81 public void dragExit(DragSourceEvent event)
82 {
83
84 }
85
86 public void dragOver(DragSourceDragEvent event)
87 {
88
89 }
90
91 public void dropActionChanged(DragSourceDragEvent event)
92 {
93
94 }
95
96 /***
97 * Called by the {@link java.awt.dnd.DragGestureRecognizer} when a drag gesture is
98 * recognized.
99 */
100 public void dragGestureRecognized(DragGestureEvent event)
101 {
102 File files[] = fp.getFiles();
103
104 if (files == null || files.length == 0)
105 return;
106
107 TransferableFile t = new TransferableFile(files);
108
109 try {
110 dragSource.startDrag(event, DragSource.DefaultMoveDrop, t, this);
111 }
112 catch (InvalidDnDOperationException ie) {
113 logger.debug("drag failed", ie);
114 }
115 }
116 }