1 | /* code from: http://iharder.sourceforge.net/current/java/filedrop/
|
---|
2 | (public domain) with only very small additions */
|
---|
3 | package org.openstreetmap.josm.gui;
|
---|
4 |
|
---|
5 | import java.awt.datatransfer.DataFlavor;
|
---|
6 | import java.io.BufferedReader;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.PrintStream;
|
---|
10 | import java.io.Reader;
|
---|
11 | import java.util.Arrays;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import javax.swing.BorderFactory;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.actions.OpenFileAction;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * This class makes it easy to drag and drop files from the operating
|
---|
21 | * system to a Java program. Any <tt>java.awt.Component</tt> can be
|
---|
22 | * dropped onto, but only <tt>javax.swing.JComponent</tt>s will indicate
|
---|
23 | * the drop event with a changed border.
|
---|
24 | * <p/>
|
---|
25 | * To use this class, construct a new <tt>FileDrop</tt> by passing
|
---|
26 | * it the target component and a <tt>Listener</tt> to receive notification
|
---|
27 | * when file(s) have been dropped. Here is an example:
|
---|
28 | * <p/>
|
---|
29 | * <code><pre>
|
---|
30 | * JPanel myPanel = new JPanel();
|
---|
31 | * new FileDrop( myPanel, new FileDrop.Listener()
|
---|
32 | * { public void filesDropped( java.io.File[] files )
|
---|
33 | * {
|
---|
34 | * // handle file drop
|
---|
35 | * ...
|
---|
36 | * } // end filesDropped
|
---|
37 | * }); // end FileDrop.Listener
|
---|
38 | * </pre></code>
|
---|
39 | * <p/>
|
---|
40 | * You can specify the border that will appear when files are being dragged by
|
---|
41 | * calling the constructor with a <tt>javax.swing.border.Border</tt>. Only
|
---|
42 | * <tt>JComponent</tt>s will show any indication with a border.
|
---|
43 | * <p/>
|
---|
44 | * You can turn on some debugging features by passing a <tt>PrintStream</tt>
|
---|
45 | * object (such as <tt>System.out</tt>) into the full constructor. A <tt>null</tt>
|
---|
46 | * value will result in no extra debugging information being output.
|
---|
47 | * <p/>
|
---|
48 | *
|
---|
49 | * <p>I'm releasing this code into the Public Domain. Enjoy.
|
---|
50 | * </p>
|
---|
51 | * <p><em>Original author: Robert Harder, rharder@usa.net</em></p>
|
---|
52 | * <p>2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.</p>
|
---|
53 | *
|
---|
54 | * @author Robert Harder
|
---|
55 | * @author rharder@users.sf.net
|
---|
56 | * @version 1.0.1
|
---|
57 | */
|
---|
58 | public class FileDrop
|
---|
59 | {
|
---|
60 | private transient javax.swing.border.Border normalBorder;
|
---|
61 | private transient java.awt.dnd.DropTargetListener dropListener;
|
---|
62 |
|
---|
63 | /** Discover if the running JVM is modern enough to have drag and drop. */
|
---|
64 | private static Boolean supportsDnD;
|
---|
65 |
|
---|
66 | // Default border color
|
---|
67 | private static java.awt.Color defaultBorderColor = new java.awt.Color( 0f, 0f, 1f, 0.25f );
|
---|
68 |
|
---|
69 | /* Constructor for JOSM file drop */
|
---|
70 | public FileDrop(final java.awt.Component c){
|
---|
71 | this(
|
---|
72 | null, // Logging stream
|
---|
73 | c, // Drop target
|
---|
74 | BorderFactory.createMatteBorder( 2, 2, 2, 2, defaultBorderColor ), // Drag border
|
---|
75 | true, // Recursive
|
---|
76 | new FileDrop.Listener(){
|
---|
77 | public void filesDropped( java.io.File[] files ){
|
---|
78 | // start asynchronous loading of files
|
---|
79 | OpenFileAction.OpenFileTask task = new OpenFileAction.OpenFileTask(Arrays.asList(files));
|
---|
80 | Main.worker.submit(task);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | );
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Constructs a {@link FileDrop} with a default light-blue border
|
---|
88 | * and, if <var>c</var> is a {@link java.awt.Container}, recursively
|
---|
89 | * sets all elements contained within as drop targets, though only
|
---|
90 | * the top level container will change borders.
|
---|
91 | *
|
---|
92 | * @param c Component on which files will be dropped.
|
---|
93 | * @param listener Listens for <tt>filesDropped</tt>.
|
---|
94 | * @since 1.0
|
---|
95 | */
|
---|
96 | public FileDrop(
|
---|
97 | final java.awt.Component c,
|
---|
98 | final Listener listener )
|
---|
99 | { this( null, // Logging stream
|
---|
100 | c, // Drop target
|
---|
101 | javax.swing.BorderFactory.createMatteBorder( 2, 2, 2, 2, defaultBorderColor ), // Drag border
|
---|
102 | true, // Recursive
|
---|
103 | listener );
|
---|
104 | } // end constructor
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Constructor with a default border and the option to recursively set drop targets.
|
---|
108 | * If your component is a <tt>java.awt.Container</tt>, then each of its children
|
---|
109 | * components will also listen for drops, though only the parent will change borders.
|
---|
110 | *
|
---|
111 | * @param c Component on which files will be dropped.
|
---|
112 | * @param recursive Recursively set children as drop targets.
|
---|
113 | * @param listener Listens for <tt>filesDropped</tt>.
|
---|
114 | * @since 1.0
|
---|
115 | */
|
---|
116 | public FileDrop(
|
---|
117 | final java.awt.Component c,
|
---|
118 | final boolean recursive,
|
---|
119 | final Listener listener )
|
---|
120 | { this( null, // Logging stream
|
---|
121 | c, // Drop target
|
---|
122 | javax.swing.BorderFactory.createMatteBorder( 2, 2, 2, 2, defaultBorderColor ), // Drag border
|
---|
123 | recursive, // Recursive
|
---|
124 | listener );
|
---|
125 | } // end constructor
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Constructor with a default border and debugging optionally turned on.
|
---|
129 | * With Debugging turned on, more status messages will be displayed to
|
---|
130 | * <tt>out</tt>. A common way to use this constructor is with
|
---|
131 | * <tt>System.out</tt> or <tt>System.err</tt>. A <tt>null</tt> value for
|
---|
132 | * the parameter <tt>out</tt> will result in no debugging output.
|
---|
133 | *
|
---|
134 | * @param out PrintStream to record debugging info or null for no debugging.
|
---|
135 | * @param out
|
---|
136 | * @param c Component on which files will be dropped.
|
---|
137 | * @param listener Listens for <tt>filesDropped</tt>.
|
---|
138 | * @since 1.0
|
---|
139 | */
|
---|
140 | public FileDrop(
|
---|
141 | final java.io.PrintStream out,
|
---|
142 | final java.awt.Component c,
|
---|
143 | final Listener listener )
|
---|
144 | { this( out, // Logging stream
|
---|
145 | c, // Drop target
|
---|
146 | javax.swing.BorderFactory.createMatteBorder( 2, 2, 2, 2, defaultBorderColor ),
|
---|
147 | false, // Recursive
|
---|
148 | listener );
|
---|
149 | } // end constructor
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Constructor with a default border, debugging optionally turned on
|
---|
153 | * and the option to recursively set drop targets.
|
---|
154 | * If your component is a <tt>java.awt.Container</tt>, then each of its children
|
---|
155 | * components will also listen for drops, though only the parent will change borders.
|
---|
156 | * With Debugging turned on, more status messages will be displayed to
|
---|
157 | * <tt>out</tt>. A common way to use this constructor is with
|
---|
158 | * <tt>System.out</tt> or <tt>System.err</tt>. A <tt>null</tt> value for
|
---|
159 | * the parameter <tt>out</tt> will result in no debugging output.
|
---|
160 | *
|
---|
161 | * @param out PrintStream to record debugging info or null for no debugging.
|
---|
162 | * @param out
|
---|
163 | * @param c Component on which files will be dropped.
|
---|
164 | * @param recursive Recursively set children as drop targets.
|
---|
165 | * @param listener Listens for <tt>filesDropped</tt>.
|
---|
166 | * @since 1.0
|
---|
167 | */
|
---|
168 | public FileDrop(
|
---|
169 | final java.io.PrintStream out,
|
---|
170 | final java.awt.Component c,
|
---|
171 | final boolean recursive,
|
---|
172 | final Listener listener)
|
---|
173 | { this( out, // Logging stream
|
---|
174 | c, // Drop target
|
---|
175 | javax.swing.BorderFactory.createMatteBorder( 2, 2, 2, 2, defaultBorderColor ), // Drag border
|
---|
176 | recursive, // Recursive
|
---|
177 | listener );
|
---|
178 | } // end constructor
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Constructor with a specified border
|
---|
182 | *
|
---|
183 | * @param c Component on which files will be dropped.
|
---|
184 | * @param dragBorder Border to use on <tt>JComponent</tt> when dragging occurs.
|
---|
185 | * @param listener Listens for <tt>filesDropped</tt>.
|
---|
186 | * @since 1.0
|
---|
187 | */
|
---|
188 | public FileDrop(
|
---|
189 | final java.awt.Component c,
|
---|
190 | final javax.swing.border.Border dragBorder,
|
---|
191 | final Listener listener)
|
---|
192 | { this(
|
---|
193 | null, // Logging stream
|
---|
194 | c, // Drop target
|
---|
195 | dragBorder, // Drag border
|
---|
196 | false, // Recursive
|
---|
197 | listener );
|
---|
198 | } // end constructor
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Constructor with a specified border and the option to recursively set drop targets.
|
---|
202 | * If your component is a <tt>java.awt.Container</tt>, then each of its children
|
---|
203 | * components will also listen for drops, though only the parent will change borders.
|
---|
204 | *
|
---|
205 | * @param c Component on which files will be dropped.
|
---|
206 | * @param dragBorder Border to use on <tt>JComponent</tt> when dragging occurs.
|
---|
207 | * @param recursive Recursively set children as drop targets.
|
---|
208 | * @param listener Listens for <tt>filesDropped</tt>.
|
---|
209 | * @since 1.0
|
---|
210 | */
|
---|
211 | public FileDrop(
|
---|
212 | final java.awt.Component c,
|
---|
213 | final javax.swing.border.Border dragBorder,
|
---|
214 | final boolean recursive,
|
---|
215 | final Listener listener)
|
---|
216 | { this(
|
---|
217 | null,
|
---|
218 | c,
|
---|
219 | dragBorder,
|
---|
220 | recursive,
|
---|
221 | listener );
|
---|
222 | } // end constructor
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Constructor with a specified border and debugging optionally turned on.
|
---|
226 | * With Debugging turned on, more status messages will be displayed to
|
---|
227 | * <tt>out</tt>. A common way to use this constructor is with
|
---|
228 | * <tt>System.out</tt> or <tt>System.err</tt>. A <tt>null</tt> value for
|
---|
229 | * the parameter <tt>out</tt> will result in no debugging output.
|
---|
230 | *
|
---|
231 | * @param out PrintStream to record debugging info or null for no debugging.
|
---|
232 | * @param c Component on which files will be dropped.
|
---|
233 | * @param dragBorder Border to use on <tt>JComponent</tt> when dragging occurs.
|
---|
234 | * @param listener Listens for <tt>filesDropped</tt>.
|
---|
235 | * @since 1.0
|
---|
236 | */
|
---|
237 | public FileDrop(
|
---|
238 | final java.io.PrintStream out,
|
---|
239 | final java.awt.Component c,
|
---|
240 | final javax.swing.border.Border dragBorder,
|
---|
241 | final Listener listener)
|
---|
242 | { this(
|
---|
243 | out, // Logging stream
|
---|
244 | c, // Drop target
|
---|
245 | dragBorder, // Drag border
|
---|
246 | false, // Recursive
|
---|
247 | listener );
|
---|
248 | } // end constructor
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Full constructor with a specified border and debugging optionally turned on.
|
---|
252 | * With Debugging turned on, more status messages will be displayed to
|
---|
253 | * <tt>out</tt>. A common way to use this constructor is with
|
---|
254 | * <tt>System.out</tt> or <tt>System.err</tt>. A <tt>null</tt> value for
|
---|
255 | * the parameter <tt>out</tt> will result in no debugging output.
|
---|
256 | *
|
---|
257 | * @param out PrintStream to record debugging info or null for no debugging.
|
---|
258 | * @param c Component on which files will be dropped.
|
---|
259 | * @param dragBorder Border to use on <tt>JComponent</tt> when dragging occurs.
|
---|
260 | * @param recursive Recursively set children as drop targets.
|
---|
261 | * @param listener Listens for <tt>filesDropped</tt>.
|
---|
262 | * @since 1.0
|
---|
263 | */
|
---|
264 | public FileDrop(
|
---|
265 | final java.io.PrintStream out,
|
---|
266 | final java.awt.Component c,
|
---|
267 | final javax.swing.border.Border dragBorder,
|
---|
268 | final boolean recursive,
|
---|
269 | final Listener listener)
|
---|
270 | {
|
---|
271 |
|
---|
272 | if( supportsDnD() )
|
---|
273 | { // Make a drop listener
|
---|
274 | dropListener = new java.awt.dnd.DropTargetListener()
|
---|
275 | { public void dragEnter( java.awt.dnd.DropTargetDragEvent evt )
|
---|
276 | { log( out, "FileDrop: dragEnter event." );
|
---|
277 |
|
---|
278 | // Is this an acceptable drag event?
|
---|
279 | if( isDragOk( out, evt ) )
|
---|
280 | {
|
---|
281 | // If it's a Swing component, set its border
|
---|
282 | if( c instanceof javax.swing.JComponent )
|
---|
283 | { javax.swing.JComponent jc = (javax.swing.JComponent) c;
|
---|
284 | normalBorder = jc.getBorder();
|
---|
285 | log( out, "FileDrop: normal border saved." );
|
---|
286 | jc.setBorder( dragBorder );
|
---|
287 | log( out, "FileDrop: drag border set." );
|
---|
288 | } // end if: JComponent
|
---|
289 |
|
---|
290 | // Acknowledge that it's okay to enter
|
---|
291 | //evt.acceptDrag( java.awt.dnd.DnDConstants.ACTION_COPY_OR_MOVE );
|
---|
292 | evt.acceptDrag( java.awt.dnd.DnDConstants.ACTION_COPY );
|
---|
293 | log( out, "FileDrop: event accepted." );
|
---|
294 | } // end if: drag ok
|
---|
295 | else
|
---|
296 | { // Reject the drag event
|
---|
297 | evt.rejectDrag();
|
---|
298 | log( out, "FileDrop: event rejected." );
|
---|
299 | } // end else: drag not ok
|
---|
300 | } // end dragEnter
|
---|
301 |
|
---|
302 | public void dragOver( java.awt.dnd.DropTargetDragEvent evt )
|
---|
303 | { // This is called continually as long as the mouse is
|
---|
304 | // over the drag target.
|
---|
305 | } // end dragOver
|
---|
306 |
|
---|
307 | public void drop( java.awt.dnd.DropTargetDropEvent evt )
|
---|
308 | { log( out, "FileDrop: drop event." );
|
---|
309 | try
|
---|
310 | { // Get whatever was dropped
|
---|
311 | java.awt.datatransfer.Transferable tr = evt.getTransferable();
|
---|
312 |
|
---|
313 | // Is it a file list?
|
---|
314 | if (tr.isDataFlavorSupported (java.awt.datatransfer.DataFlavor.javaFileListFlavor))
|
---|
315 | {
|
---|
316 | // Say we'll take it.
|
---|
317 | //evt.acceptDrop ( java.awt.dnd.DnDConstants.ACTION_COPY_OR_MOVE );
|
---|
318 | evt.acceptDrop ( java.awt.dnd.DnDConstants.ACTION_COPY );
|
---|
319 | log( out, "FileDrop: file list accepted." );
|
---|
320 |
|
---|
321 | // Get a useful list
|
---|
322 | List fileList = (java.util.List)
|
---|
323 | tr.getTransferData(java.awt.datatransfer.DataFlavor.javaFileListFlavor);
|
---|
324 |
|
---|
325 | // Convert list to array
|
---|
326 | final File[] files = (File[]) fileList.toArray();
|
---|
327 |
|
---|
328 | // Alert listener to drop.
|
---|
329 | if( listener != null ) {
|
---|
330 | listener.filesDropped( files );
|
---|
331 | }
|
---|
332 |
|
---|
333 | // Mark that drop is completed.
|
---|
334 | evt.getDropTargetContext().dropComplete(true);
|
---|
335 | log( out, "FileDrop: drop complete." );
|
---|
336 | } // end if: file list
|
---|
337 | else // this section will check for a reader flavor.
|
---|
338 | {
|
---|
339 | // Thanks, Nathan!
|
---|
340 | // BEGIN 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
|
---|
341 | DataFlavor[] flavors = tr.getTransferDataFlavors();
|
---|
342 | boolean handled = false;
|
---|
343 | for (int zz = 0; zz < flavors.length; zz++) {
|
---|
344 | if (flavors[zz].isRepresentationClassReader()) {
|
---|
345 | // Say we'll take it.
|
---|
346 | //evt.acceptDrop ( java.awt.dnd.DnDConstants.ACTION_COPY_OR_MOVE );
|
---|
347 | evt.acceptDrop(java.awt.dnd.DnDConstants.ACTION_COPY);
|
---|
348 | log(out, "FileDrop: reader accepted.");
|
---|
349 |
|
---|
350 | Reader reader = flavors[zz].getReaderForText(tr);
|
---|
351 |
|
---|
352 | BufferedReader br = new BufferedReader(reader);
|
---|
353 |
|
---|
354 | if(listener != null) {
|
---|
355 | listener.filesDropped(createFileArray(br, out));
|
---|
356 | }
|
---|
357 |
|
---|
358 | // Mark that drop is completed.
|
---|
359 | evt.getDropTargetContext().dropComplete(true);
|
---|
360 | log(out, "FileDrop: drop complete.");
|
---|
361 | handled = true;
|
---|
362 | break;
|
---|
363 | }
|
---|
364 | }
|
---|
365 | if(!handled){
|
---|
366 | log( out, "FileDrop: not a file list or reader - abort." );
|
---|
367 | evt.rejectDrop();
|
---|
368 | }
|
---|
369 | // END 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
|
---|
370 | } // end else: not a file list
|
---|
371 | } // end try
|
---|
372 | catch ( java.io.IOException io)
|
---|
373 | { log( out, "FileDrop: IOException - abort:" );
|
---|
374 | io.printStackTrace( out );
|
---|
375 | evt.rejectDrop();
|
---|
376 | } // end catch IOException
|
---|
377 | catch (java.awt.datatransfer.UnsupportedFlavorException ufe)
|
---|
378 | { log( out, "FileDrop: UnsupportedFlavorException - abort:" );
|
---|
379 | ufe.printStackTrace( out );
|
---|
380 | evt.rejectDrop();
|
---|
381 | } // end catch: UnsupportedFlavorException
|
---|
382 | finally
|
---|
383 | {
|
---|
384 | // If it's a Swing component, reset its border
|
---|
385 | if( c instanceof javax.swing.JComponent )
|
---|
386 | { javax.swing.JComponent jc = (javax.swing.JComponent) c;
|
---|
387 | jc.setBorder( normalBorder );
|
---|
388 | log( out, "FileDrop: normal border restored." );
|
---|
389 | } // end if: JComponent
|
---|
390 | } // end finally
|
---|
391 | } // end drop
|
---|
392 |
|
---|
393 | public void dragExit( java.awt.dnd.DropTargetEvent evt )
|
---|
394 | { log( out, "FileDrop: dragExit event." );
|
---|
395 | // If it's a Swing component, reset its border
|
---|
396 | if( c instanceof javax.swing.JComponent )
|
---|
397 | { javax.swing.JComponent jc = (javax.swing.JComponent) c;
|
---|
398 | jc.setBorder( normalBorder );
|
---|
399 | log( out, "FileDrop: normal border restored." );
|
---|
400 | } // end if: JComponent
|
---|
401 | } // end dragExit
|
---|
402 |
|
---|
403 | public void dropActionChanged( java.awt.dnd.DropTargetDragEvent evt )
|
---|
404 | { log( out, "FileDrop: dropActionChanged event." );
|
---|
405 | // Is this an acceptable drag event?
|
---|
406 | if( isDragOk( out, evt ) )
|
---|
407 | { //evt.acceptDrag( java.awt.dnd.DnDConstants.ACTION_COPY_OR_MOVE );
|
---|
408 | evt.acceptDrag( java.awt.dnd.DnDConstants.ACTION_COPY );
|
---|
409 | log( out, "FileDrop: event accepted." );
|
---|
410 | } // end if: drag ok
|
---|
411 | else
|
---|
412 | { evt.rejectDrag();
|
---|
413 | log( out, "FileDrop: event rejected." );
|
---|
414 | } // end else: drag not ok
|
---|
415 | } // end dropActionChanged
|
---|
416 | }; // end DropTargetListener
|
---|
417 |
|
---|
418 | // Make the component (and possibly children) drop targets
|
---|
419 | makeDropTarget( out, c, recursive );
|
---|
420 | } // end if: supports dnd
|
---|
421 | else
|
---|
422 | { log( out, "FileDrop: Drag and drop is not supported with this JVM" );
|
---|
423 | } // end else: does not support DnD
|
---|
424 | } // end constructor
|
---|
425 |
|
---|
426 | private static boolean supportsDnD()
|
---|
427 | { // Static Boolean
|
---|
428 | if( supportsDnD == null )
|
---|
429 | {
|
---|
430 | boolean support = false;
|
---|
431 | try
|
---|
432 | { Class arbitraryDndClass = Class.forName( "java.awt.dnd.DnDConstants" );
|
---|
433 | support = true;
|
---|
434 | } // end try
|
---|
435 | catch( Exception e )
|
---|
436 | { support = false;
|
---|
437 | } // end catch
|
---|
438 | supportsDnD = support;
|
---|
439 | } // end if: first time through
|
---|
440 | return supportsDnD.booleanValue();
|
---|
441 | } // end supportsDnD
|
---|
442 |
|
---|
443 | // BEGIN 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
|
---|
444 | private static String ZERO_CHAR_STRING = "" + (char)0;
|
---|
445 | private static File[] createFileArray(BufferedReader bReader, PrintStream out)
|
---|
446 | {
|
---|
447 | try {
|
---|
448 | java.util.List<File> list = new java.util.ArrayList<File>();
|
---|
449 | java.lang.String line = null;
|
---|
450 | while ((line = bReader.readLine()) != null) {
|
---|
451 | try {
|
---|
452 | // kde seems to append a 0 char to the end of the reader
|
---|
453 | if(ZERO_CHAR_STRING.equals(line)) {
|
---|
454 | continue;
|
---|
455 | }
|
---|
456 |
|
---|
457 | java.io.File file = new java.io.File(new java.net.URI(line));
|
---|
458 | list.add(file);
|
---|
459 | } catch (Exception ex) {
|
---|
460 | log(out, "Error with " + line + ": " + ex.getMessage());
|
---|
461 | }
|
---|
462 | }
|
---|
463 |
|
---|
464 | return list.toArray(new File[list.size()]);
|
---|
465 | } catch (IOException ex) {
|
---|
466 | log(out, "FileDrop: IOException");
|
---|
467 | }
|
---|
468 | return new File[0];
|
---|
469 | }
|
---|
470 | // END 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
|
---|
471 |
|
---|
472 | private void makeDropTarget( final java.io.PrintStream out, final java.awt.Component c, boolean recursive )
|
---|
473 | {
|
---|
474 | // Make drop target
|
---|
475 | final java.awt.dnd.DropTarget dt = new java.awt.dnd.DropTarget();
|
---|
476 | try
|
---|
477 | { dt.addDropTargetListener( dropListener );
|
---|
478 | } // end try
|
---|
479 | catch( java.util.TooManyListenersException e )
|
---|
480 | { e.printStackTrace();
|
---|
481 | log(out, "FileDrop: Drop will not work due to previous error. Do you have another listener attached?" );
|
---|
482 | } // end catch
|
---|
483 |
|
---|
484 | // Listen for hierarchy changes and remove the drop target when the parent gets cleared out.
|
---|
485 | c.addHierarchyListener( new java.awt.event.HierarchyListener()
|
---|
486 | { public void hierarchyChanged( java.awt.event.HierarchyEvent evt )
|
---|
487 | { log( out, "FileDrop: Hierarchy changed." );
|
---|
488 | java.awt.Component parent = c.getParent();
|
---|
489 | if( parent == null )
|
---|
490 | { c.setDropTarget( null );
|
---|
491 | log( out, "FileDrop: Drop target cleared from component." );
|
---|
492 | } // end if: null parent
|
---|
493 | else
|
---|
494 | { new java.awt.dnd.DropTarget(c, dropListener);
|
---|
495 | log( out, "FileDrop: Drop target added to component." );
|
---|
496 | } // end else: parent not null
|
---|
497 | } // end hierarchyChanged
|
---|
498 | }); // end hierarchy listener
|
---|
499 | if( c.getParent() != null ) {
|
---|
500 | new java.awt.dnd.DropTarget(c, dropListener);
|
---|
501 | }
|
---|
502 |
|
---|
503 | if( recursive && (c instanceof java.awt.Container ) )
|
---|
504 | {
|
---|
505 | // Get the container
|
---|
506 | java.awt.Container cont = (java.awt.Container) c;
|
---|
507 |
|
---|
508 | // Get it's components
|
---|
509 | java.awt.Component[] comps = cont.getComponents();
|
---|
510 |
|
---|
511 | // Set it's components as listeners also
|
---|
512 | for( int i = 0; i < comps.length; i++ ) {
|
---|
513 | makeDropTarget( out, comps[i], recursive );
|
---|
514 | }
|
---|
515 | } // end if: recursively set components as listener
|
---|
516 | } // end dropListener
|
---|
517 |
|
---|
518 | /** Determine if the dragged data is a file list. */
|
---|
519 | private boolean isDragOk( final java.io.PrintStream out, final java.awt.dnd.DropTargetDragEvent evt )
|
---|
520 | { boolean ok = false;
|
---|
521 |
|
---|
522 | // Get data flavors being dragged
|
---|
523 | java.awt.datatransfer.DataFlavor[] flavors = evt.getCurrentDataFlavors();
|
---|
524 |
|
---|
525 | // See if any of the flavors are a file list
|
---|
526 | int i = 0;
|
---|
527 | while( !ok && i < flavors.length )
|
---|
528 | {
|
---|
529 | // BEGIN 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
|
---|
530 | // Is the flavor a file list?
|
---|
531 | final DataFlavor curFlavor = flavors[i];
|
---|
532 | if( curFlavor.equals( java.awt.datatransfer.DataFlavor.javaFileListFlavor ) ||
|
---|
533 | curFlavor.isRepresentationClassReader()){
|
---|
534 | ok = true;
|
---|
535 | }
|
---|
536 | // END 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
|
---|
537 | i++;
|
---|
538 | } // end while: through flavors
|
---|
539 |
|
---|
540 | // If logging is enabled, show data flavors
|
---|
541 | if( out != null )
|
---|
542 | { if( flavors.length == 0 ) {
|
---|
543 | log( out, "FileDrop: no data flavors." );
|
---|
544 | }
|
---|
545 | for( i = 0; i < flavors.length; i++ ) {
|
---|
546 | log( out, flavors[i].toString() );
|
---|
547 | }
|
---|
548 | } // end if: logging enabled
|
---|
549 |
|
---|
550 | return ok;
|
---|
551 | } // end isDragOk
|
---|
552 |
|
---|
553 | /** Outputs <tt>message</tt> to <tt>out</tt> if it's not null. */
|
---|
554 | private static void log( java.io.PrintStream out, String message )
|
---|
555 | { // Log message if requested
|
---|
556 | if( out != null ) {
|
---|
557 | out.println( message );
|
---|
558 | }
|
---|
559 | } // end log
|
---|
560 |
|
---|
561 | /**
|
---|
562 | * Removes the drag-and-drop hooks from the component and optionally
|
---|
563 | * from the all children. You should call this if you add and remove
|
---|
564 | * components after you've set up the drag-and-drop.
|
---|
565 | * This will recursively unregister all components contained within
|
---|
566 | * <var>c</var> if <var>c</var> is a {@link java.awt.Container}.
|
---|
567 | *
|
---|
568 | * @param c The component to unregister as a drop target
|
---|
569 | * @since 1.0
|
---|
570 | */
|
---|
571 | public static boolean remove( java.awt.Component c)
|
---|
572 | { return remove( null, c, true );
|
---|
573 | } // end remove
|
---|
574 |
|
---|
575 | /**
|
---|
576 | * Removes the drag-and-drop hooks from the component and optionally
|
---|
577 | * from the all children. You should call this if you add and remove
|
---|
578 | * components after you've set up the drag-and-drop.
|
---|
579 | *
|
---|
580 | * @param out Optional {@link java.io.PrintStream} for logging drag and drop messages
|
---|
581 | * @param c The component to unregister
|
---|
582 | * @param recursive Recursively unregister components within a container
|
---|
583 | * @since 1.0
|
---|
584 | */
|
---|
585 | public static boolean remove( java.io.PrintStream out, java.awt.Component c, boolean recursive )
|
---|
586 | { // Make sure we support dnd.
|
---|
587 | if( supportsDnD() )
|
---|
588 | { log( out, "FileDrop: Removing drag-and-drop hooks." );
|
---|
589 | c.setDropTarget( null );
|
---|
590 | if( recursive && ( c instanceof java.awt.Container ) )
|
---|
591 | { java.awt.Component[] comps = ((java.awt.Container)c).getComponents();
|
---|
592 | for( int i = 0; i < comps.length; i++ ) {
|
---|
593 | remove( out, comps[i], recursive );
|
---|
594 | }
|
---|
595 | return true;
|
---|
596 | } // end if: recursive
|
---|
597 | else return false;
|
---|
598 | } // end if: supports DnD
|
---|
599 | else return false;
|
---|
600 | } // end remove
|
---|
601 |
|
---|
602 | /* ******** I N N E R I N T E R F A C E L I S T E N E R ******** */
|
---|
603 |
|
---|
604 | /**
|
---|
605 | * Implement this inner interface to listen for when files are dropped. For example
|
---|
606 | * your class declaration may begin like this:
|
---|
607 | * <code><pre>
|
---|
608 | * public class MyClass implements FileDrop.Listener
|
---|
609 | * ...
|
---|
610 | * public void filesDropped( java.io.File[] files )
|
---|
611 | * {
|
---|
612 | * ...
|
---|
613 | * } // end filesDropped
|
---|
614 | * ...
|
---|
615 | * </pre></code>
|
---|
616 | *
|
---|
617 | * @since 1.1
|
---|
618 | */
|
---|
619 | public static interface Listener {
|
---|
620 |
|
---|
621 | /**
|
---|
622 | * This method is called when files have been successfully dropped.
|
---|
623 | *
|
---|
624 | * @param files An array of <tt>File</tt>s that were dropped.
|
---|
625 | * @since 1.0
|
---|
626 | */
|
---|
627 | public abstract void filesDropped( java.io.File[] files );
|
---|
628 |
|
---|
629 | } // end inner-interface Listener
|
---|
630 |
|
---|
631 | /* ******** I N N E R C L A S S ******** */
|
---|
632 |
|
---|
633 | /**
|
---|
634 | * This is the event that is passed to the
|
---|
635 | * {@link FileDropListener#filesDropped filesDropped(...)} method in
|
---|
636 | * your {@link FileDropListener} when files are dropped onto
|
---|
637 | * a registered drop target.
|
---|
638 | *
|
---|
639 | * <p>I'm releasing this code into the Public Domain. Enjoy.</p>
|
---|
640 | *
|
---|
641 | * @author Robert Harder
|
---|
642 | * @author rob@iharder.net
|
---|
643 | * @version 1.2
|
---|
644 | */
|
---|
645 | public static class Event extends java.util.EventObject {
|
---|
646 |
|
---|
647 | private java.io.File[] files;
|
---|
648 |
|
---|
649 | /**
|
---|
650 | * Constructs an {@link Event} with the array
|
---|
651 | * of files that were dropped and the
|
---|
652 | * {@link FileDrop} that initiated the event.
|
---|
653 | *
|
---|
654 | * @param files The array of files that were dropped
|
---|
655 | * @source The event source
|
---|
656 | * @since 1.1
|
---|
657 | */
|
---|
658 | public Event( java.io.File[] files, Object source ) {
|
---|
659 | super( source );
|
---|
660 | this.files = files;
|
---|
661 | } // end constructor
|
---|
662 |
|
---|
663 | /**
|
---|
664 | * Returns an array of files that were dropped on a
|
---|
665 | * registered drop target.
|
---|
666 | *
|
---|
667 | * @return array of files that were dropped
|
---|
668 | * @since 1.1
|
---|
669 | */
|
---|
670 | public java.io.File[] getFiles() {
|
---|
671 | return files;
|
---|
672 | } // end getFiles
|
---|
673 |
|
---|
674 | } // end inner class Event
|
---|
675 |
|
---|
676 | /* ******** I N N E R C L A S S ******** */
|
---|
677 |
|
---|
678 | /**
|
---|
679 | * At last an easy way to encapsulate your custom objects for dragging and dropping
|
---|
680 | * in your Java programs!
|
---|
681 | * When you need to create a {@link java.awt.datatransfer.Transferable} object,
|
---|
682 | * use this class to wrap your object.
|
---|
683 | * For example:
|
---|
684 | * <pre><code>
|
---|
685 | * ...
|
---|
686 | * MyCoolClass myObj = new MyCoolClass();
|
---|
687 | * Transferable xfer = new TransferableObject( myObj );
|
---|
688 | * ...
|
---|
689 | * </code></pre>
|
---|
690 | * Or if you need to know when the data was actually dropped, like when you're
|
---|
691 | * moving data out of a list, say, you can use the {@link TransferableObject.Fetcher}
|
---|
692 | * inner class to return your object Just in Time.
|
---|
693 | * For example:
|
---|
694 | * <pre><code>
|
---|
695 | * ...
|
---|
696 | * final MyCoolClass myObj = new MyCoolClass();
|
---|
697 | *
|
---|
698 | * TransferableObject.Fetcher fetcher = new TransferableObject.Fetcher()
|
---|
699 | * { public Object getObject(){ return myObj; }
|
---|
700 | * }; // end fetcher
|
---|
701 | *
|
---|
702 | * Transferable xfer = new TransferableObject( fetcher );
|
---|
703 | * ...
|
---|
704 | * </code></pre>
|
---|
705 | *
|
---|
706 | * The {@link java.awt.datatransfer.DataFlavor} associated with
|
---|
707 | * {@link TransferableObject} has the representation class
|
---|
708 | * <tt>net.iharder.dnd.TransferableObject.class</tt> and MIME type
|
---|
709 | * <tt>application/x-net.iharder.dnd.TransferableObject</tt>.
|
---|
710 | * This data flavor is accessible via the static
|
---|
711 | * {@link #DATA_FLAVOR} property.
|
---|
712 | *
|
---|
713 | *
|
---|
714 | * <p>I'm releasing this code into the Public Domain. Enjoy.</p>
|
---|
715 | *
|
---|
716 | * @author Robert Harder
|
---|
717 | * @author rob@iharder.net
|
---|
718 | * @version 1.2
|
---|
719 | */
|
---|
720 | public static class TransferableObject implements java.awt.datatransfer.Transferable
|
---|
721 | {
|
---|
722 | /**
|
---|
723 | * The MIME type for {@link #DATA_FLAVOR} is
|
---|
724 | * <tt>application/x-net.iharder.dnd.TransferableObject</tt>.
|
---|
725 | *
|
---|
726 | * @since 1.1
|
---|
727 | */
|
---|
728 | public final static String MIME_TYPE = "application/x-net.iharder.dnd.TransferableObject";
|
---|
729 |
|
---|
730 | /**
|
---|
731 | * The default {@link java.awt.datatransfer.DataFlavor} for
|
---|
732 | * {@link TransferableObject} has the representation class
|
---|
733 | * <tt>net.iharder.dnd.TransferableObject.class</tt>
|
---|
734 | * and the MIME type
|
---|
735 | * <tt>application/x-net.iharder.dnd.TransferableObject</tt>.
|
---|
736 | *
|
---|
737 | * @since 1.1
|
---|
738 | */
|
---|
739 | public final static java.awt.datatransfer.DataFlavor DATA_FLAVOR =
|
---|
740 | new java.awt.datatransfer.DataFlavor( FileDrop.TransferableObject.class, MIME_TYPE );
|
---|
741 |
|
---|
742 | private Fetcher fetcher;
|
---|
743 | private Object data;
|
---|
744 |
|
---|
745 | private java.awt.datatransfer.DataFlavor customFlavor;
|
---|
746 |
|
---|
747 | /**
|
---|
748 | * Creates a new {@link TransferableObject} that wraps <var>data</var>.
|
---|
749 | * Along with the {@link #DATA_FLAVOR} associated with this class,
|
---|
750 | * this creates a custom data flavor with a representation class
|
---|
751 | * determined from <code>data.getClass()</code> and the MIME type
|
---|
752 | * <tt>application/x-net.iharder.dnd.TransferableObject</tt>.
|
---|
753 | *
|
---|
754 | * @param data The data to transfer
|
---|
755 | * @since 1.1
|
---|
756 | */
|
---|
757 | public TransferableObject( Object data )
|
---|
758 | { this.data = data;
|
---|
759 | this.customFlavor = new java.awt.datatransfer.DataFlavor( data.getClass(), MIME_TYPE );
|
---|
760 | } // end constructor
|
---|
761 |
|
---|
762 | /**
|
---|
763 | * Creates a new {@link TransferableObject} that will return the
|
---|
764 | * object that is returned by <var>fetcher</var>.
|
---|
765 | * No custom data flavor is set other than the default
|
---|
766 | * {@link #DATA_FLAVOR}.
|
---|
767 | *
|
---|
768 | * @see Fetcher
|
---|
769 | * @param fetcher The {@link Fetcher} that will return the data object
|
---|
770 | * @since 1.1
|
---|
771 | */
|
---|
772 | public TransferableObject( Fetcher fetcher )
|
---|
773 | { this.fetcher = fetcher;
|
---|
774 | } // end constructor
|
---|
775 |
|
---|
776 | /**
|
---|
777 | * Creates a new {@link TransferableObject} that will return the
|
---|
778 | * object that is returned by <var>fetcher</var>.
|
---|
779 | * Along with the {@link #DATA_FLAVOR} associated with this class,
|
---|
780 | * this creates a custom data flavor with a representation class <var>dataClass</var>
|
---|
781 | * and the MIME type
|
---|
782 | * <tt>application/x-net.iharder.dnd.TransferableObject</tt>.
|
---|
783 | *
|
---|
784 | * @see Fetcher
|
---|
785 | * @param dataClass The {@link java.lang.Class} to use in the custom data flavor
|
---|
786 | * @param fetcher The {@link Fetcher} that will return the data object
|
---|
787 | * @since 1.1
|
---|
788 | */
|
---|
789 | public TransferableObject( Class dataClass, Fetcher fetcher )
|
---|
790 | { this.fetcher = fetcher;
|
---|
791 | this.customFlavor = new java.awt.datatransfer.DataFlavor( dataClass, MIME_TYPE );
|
---|
792 | } // end constructor
|
---|
793 |
|
---|
794 | /**
|
---|
795 | * Returns the custom {@link java.awt.datatransfer.DataFlavor} associated
|
---|
796 | * with the encapsulated object or <tt>null</tt> if the {@link Fetcher}
|
---|
797 | * constructor was used without passing a {@link java.lang.Class}.
|
---|
798 | *
|
---|
799 | * @return The custom data flavor for the encapsulated object
|
---|
800 | * @since 1.1
|
---|
801 | */
|
---|
802 | public java.awt.datatransfer.DataFlavor getCustomDataFlavor()
|
---|
803 | { return customFlavor;
|
---|
804 | } // end getCustomDataFlavor
|
---|
805 |
|
---|
806 | /* ******** T R A N S F E R A B L E M E T H O D S ******** */
|
---|
807 |
|
---|
808 | /**
|
---|
809 | * Returns a two- or three-element array containing first
|
---|
810 | * the custom data flavor, if one was created in the constructors,
|
---|
811 | * second the default {@link #DATA_FLAVOR} associated with
|
---|
812 | * {@link TransferableObject}, and third the
|
---|
813 | * {@link java.awt.datatransfer.DataFlavor.stringFlavor}.
|
---|
814 | *
|
---|
815 | * @return An array of supported data flavors
|
---|
816 | * @since 1.1
|
---|
817 | */
|
---|
818 | public java.awt.datatransfer.DataFlavor[] getTransferDataFlavors()
|
---|
819 | {
|
---|
820 | if( customFlavor != null )
|
---|
821 | return new java.awt.datatransfer.DataFlavor[]
|
---|
822 | { customFlavor,
|
---|
823 | DATA_FLAVOR,
|
---|
824 | java.awt.datatransfer.DataFlavor.stringFlavor
|
---|
825 | }; // end flavors array
|
---|
826 | else
|
---|
827 | return new java.awt.datatransfer.DataFlavor[]
|
---|
828 | { DATA_FLAVOR,
|
---|
829 | java.awt.datatransfer.DataFlavor.stringFlavor
|
---|
830 | }; // end flavors array
|
---|
831 | } // end getTransferDataFlavors
|
---|
832 |
|
---|
833 | /**
|
---|
834 | * Returns the data encapsulated in this {@link TransferableObject}.
|
---|
835 | * If the {@link Fetcher} constructor was used, then this is when
|
---|
836 | * the {@link Fetcher#getObject getObject()} method will be called.
|
---|
837 | * If the requested data flavor is not supported, then the
|
---|
838 | * {@link Fetcher#getObject getObject()} method will not be called.
|
---|
839 | *
|
---|
840 | * @param flavor The data flavor for the data to return
|
---|
841 | * @return The dropped data
|
---|
842 | * @since 1.1
|
---|
843 | */
|
---|
844 | public Object getTransferData( java.awt.datatransfer.DataFlavor flavor )
|
---|
845 | throws java.awt.datatransfer.UnsupportedFlavorException, java.io.IOException
|
---|
846 | {
|
---|
847 | // Native object
|
---|
848 | if( flavor.equals( DATA_FLAVOR ) )
|
---|
849 | return fetcher == null ? data : fetcher.getObject();
|
---|
850 |
|
---|
851 | // String
|
---|
852 | if( flavor.equals( java.awt.datatransfer.DataFlavor.stringFlavor ) )
|
---|
853 | return fetcher == null ? data.toString() : fetcher.getObject().toString();
|
---|
854 |
|
---|
855 | // We can't do anything else
|
---|
856 | throw new java.awt.datatransfer.UnsupportedFlavorException(flavor);
|
---|
857 | } // end getTransferData
|
---|
858 |
|
---|
859 | /**
|
---|
860 | * Returns <tt>true</tt> if <var>flavor</var> is one of the supported
|
---|
861 | * flavors. Flavors are supported using the <code>equals(...)</code> method.
|
---|
862 | *
|
---|
863 | * @param flavor The data flavor to check
|
---|
864 | * @return Whether or not the flavor is supported
|
---|
865 | * @since 1.1
|
---|
866 | */
|
---|
867 | public boolean isDataFlavorSupported( java.awt.datatransfer.DataFlavor flavor )
|
---|
868 | {
|
---|
869 | // Native object
|
---|
870 | if( flavor.equals( DATA_FLAVOR ) )
|
---|
871 | return true;
|
---|
872 |
|
---|
873 | // String
|
---|
874 | if( flavor.equals( java.awt.datatransfer.DataFlavor.stringFlavor ) )
|
---|
875 | return true;
|
---|
876 |
|
---|
877 | // We can't do anything else
|
---|
878 | return false;
|
---|
879 | } // end isDataFlavorSupported
|
---|
880 |
|
---|
881 | /* ******** I N N E R I N T E R F A C E F E T C H E R ******** */
|
---|
882 |
|
---|
883 | /**
|
---|
884 | * Instead of passing your data directly to the {@link TransferableObject}
|
---|
885 | * constructor, you may want to know exactly when your data was received
|
---|
886 | * in case you need to remove it from its source (or do anyting else to it).
|
---|
887 | * When the {@link #getTransferData getTransferData(...)} method is called
|
---|
888 | * on the {@link TransferableObject}, the {@link Fetcher}'s
|
---|
889 | * {@link #getObject getObject()} method will be called.
|
---|
890 | *
|
---|
891 | * @author Robert Harder
|
---|
892 | * @copyright 2001
|
---|
893 | * @version 1.1
|
---|
894 | * @since 1.1
|
---|
895 | */
|
---|
896 | public static interface Fetcher
|
---|
897 | {
|
---|
898 | /**
|
---|
899 | * Return the object being encapsulated in the
|
---|
900 | * {@link TransferableObject}.
|
---|
901 | *
|
---|
902 | * @return The dropped object
|
---|
903 | * @since 1.1
|
---|
904 | */
|
---|
905 | public abstract Object getObject();
|
---|
906 | } // end inner interface Fetcher
|
---|
907 |
|
---|
908 | } // end class TransferableObject
|
---|
909 |
|
---|
910 | } // end class FileDrop
|
---|