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.Color;
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Container;
|
---|
8 | import java.awt.datatransfer.DataFlavor;
|
---|
9 | import java.awt.datatransfer.Transferable;
|
---|
10 | import java.awt.datatransfer.UnsupportedFlavorException;
|
---|
11 | import java.awt.dnd.DnDConstants;
|
---|
12 | import java.awt.dnd.DropTarget;
|
---|
13 | import java.awt.dnd.DropTargetDragEvent;
|
---|
14 | import java.awt.dnd.DropTargetDropEvent;
|
---|
15 | import java.awt.dnd.DropTargetEvent;
|
---|
16 | import java.awt.dnd.DropTargetListener;
|
---|
17 | import java.awt.dnd.InvalidDnDOperationException;
|
---|
18 | import java.awt.event.HierarchyEvent;
|
---|
19 | import java.awt.event.HierarchyListener;
|
---|
20 | import java.io.BufferedReader;
|
---|
21 | import java.io.File;
|
---|
22 | import java.io.IOException;
|
---|
23 | import java.io.Reader;
|
---|
24 | import java.net.URI;
|
---|
25 | import java.util.ArrayList;
|
---|
26 | import java.util.Arrays;
|
---|
27 | import java.util.List;
|
---|
28 | import java.util.TooManyListenersException;
|
---|
29 |
|
---|
30 | import javax.swing.BorderFactory;
|
---|
31 | import javax.swing.JComponent;
|
---|
32 | import javax.swing.border.Border;
|
---|
33 |
|
---|
34 | import org.openstreetmap.josm.Main;
|
---|
35 | import org.openstreetmap.josm.actions.OpenFileAction;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * This class makes it easy to drag and drop files from the operating
|
---|
39 | * system to a Java program. Any {@link java.awt.Component} can be
|
---|
40 | * dropped onto, but only {@link javax.swing.JComponent}s will indicate
|
---|
41 | * the drop event with a changed border.
|
---|
42 | * <p>
|
---|
43 | * To use this class, construct a new <tt>FileDrop</tt> by passing
|
---|
44 | * it the target component and a <tt>Listener</tt> to receive notification
|
---|
45 | * when file(s) have been dropped. Here is an example:
|
---|
46 | * <p>
|
---|
47 | * <code>
|
---|
48 | * JPanel myPanel = new JPanel();
|
---|
49 | * new FileDrop( myPanel, new FileDrop.Listener()
|
---|
50 | * { public void filesDropped( java.io.File[] files )
|
---|
51 | * {
|
---|
52 | * // handle file drop
|
---|
53 | * ...
|
---|
54 | * } // end filesDropped
|
---|
55 | * }); // end FileDrop.Listener
|
---|
56 | * </code>
|
---|
57 | * <p>
|
---|
58 | * You can specify the border that will appear when files are being dragged by
|
---|
59 | * calling the constructor with a {@link javax.swing.border.Border}. Only
|
---|
60 | * <tt>JComponent</tt>s will show any indication with a border.
|
---|
61 | * <p>
|
---|
62 | * You can turn on some debugging features by passing a <tt>PrintStream</tt>
|
---|
63 | * object (such as <tt>System.out</tt>) into the full constructor. A <tt>null</tt>
|
---|
64 | * value will result in no extra debugging information being output.
|
---|
65 | * <p>
|
---|
66 | *
|
---|
67 | * <p>I'm releasing this code into the Public Domain. Enjoy.
|
---|
68 | * </p>
|
---|
69 | * <p><em>Original author: Robert Harder, rharder@usa.net</em></p>
|
---|
70 | * <p>2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.</p>
|
---|
71 | *
|
---|
72 | * @author Robert Harder
|
---|
73 | * @author rharder@users.sf.net
|
---|
74 | * @version 1.0.1
|
---|
75 | * @since 1231
|
---|
76 | */
|
---|
77 | public class FileDrop {
|
---|
78 |
|
---|
79 | private Border normalBorder;
|
---|
80 | private DropTargetListener dropListener;
|
---|
81 |
|
---|
82 | /** Discover if the running JVM is modern enough to have drag and drop. */
|
---|
83 | private static Boolean supportsDnD;
|
---|
84 |
|
---|
85 | // Default border color
|
---|
86 | private static Color defaultBorderColor = new Color(0f, 0f, 1f, 0.25f);
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Constructor for JOSM file drop
|
---|
90 | * @param c The drop target
|
---|
91 | */
|
---|
92 | public FileDrop(final Component c) {
|
---|
93 | this(
|
---|
94 | c, // Drop target
|
---|
95 | BorderFactory.createMatteBorder(2, 2, 2, 2, defaultBorderColor), // Drag border
|
---|
96 | true, // Recursive
|
---|
97 | new FileDrop.Listener() {
|
---|
98 | @Override
|
---|
99 | public void filesDropped(File[] files){
|
---|
100 | // start asynchronous loading of files
|
---|
101 | OpenFileAction.OpenFileTask task = new OpenFileAction.OpenFileTask(Arrays.asList(files), null);
|
---|
102 | task.setRecordHistory(true);
|
---|
103 | Main.worker.submit(task);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | );
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Full constructor with a specified border and debugging optionally turned on.
|
---|
111 | * With Debugging turned on, more status messages will be displayed to
|
---|
112 | * <tt>out</tt>. A common way to use this constructor is with
|
---|
113 | * <tt>System.out</tt> or <tt>System.err</tt>. A <tt>null</tt> value for
|
---|
114 | * the parameter <tt>out</tt> will result in no debugging output.
|
---|
115 | *
|
---|
116 | * @param c Component on which files will be dropped.
|
---|
117 | * @param dragBorder Border to use on <tt>JComponent</tt> when dragging occurs.
|
---|
118 | * @param recursive Recursively set children as drop targets.
|
---|
119 | * @param listener Listens for <tt>filesDropped</tt>.
|
---|
120 | */
|
---|
121 | public FileDrop(
|
---|
122 | final Component c,
|
---|
123 | final Border dragBorder,
|
---|
124 | final boolean recursive,
|
---|
125 | final Listener listener) {
|
---|
126 |
|
---|
127 | if (supportsDnD()) {
|
---|
128 | // Make a drop listener
|
---|
129 | dropListener = new DropListener(listener, dragBorder, c);
|
---|
130 |
|
---|
131 | // Make the component (and possibly children) drop targets
|
---|
132 | makeDropTarget(c, recursive);
|
---|
133 | } else {
|
---|
134 | Main.info("FileDrop: Drag and drop is not supported with this JVM");
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | private static synchronized boolean supportsDnD() {
|
---|
139 | if (supportsDnD == null) {
|
---|
140 | boolean support = false;
|
---|
141 | try {
|
---|
142 | Class.forName("java.awt.dnd.DnDConstants");
|
---|
143 | support = true;
|
---|
144 | } catch(Exception e) {
|
---|
145 | support = false;
|
---|
146 | }
|
---|
147 | supportsDnD = support;
|
---|
148 | }
|
---|
149 | return supportsDnD.booleanValue();
|
---|
150 | }
|
---|
151 |
|
---|
152 | // BEGIN 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
|
---|
153 | private static final String ZERO_CHAR_STRING = "" + (char)0;
|
---|
154 | private static File[] createFileArray(BufferedReader bReader) {
|
---|
155 | try {
|
---|
156 | List<File> list = new ArrayList<>();
|
---|
157 | String line = null;
|
---|
158 | while ((line = bReader.readLine()) != null) {
|
---|
159 | try {
|
---|
160 | // kde seems to append a 0 char to the end of the reader
|
---|
161 | if (ZERO_CHAR_STRING.equals(line)) {
|
---|
162 | continue;
|
---|
163 | }
|
---|
164 |
|
---|
165 | File file = new File(new URI(line));
|
---|
166 | list.add(file);
|
---|
167 | } catch (Exception ex) {
|
---|
168 | Main.warn("Error with " + line + ": " + ex.getMessage());
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | return list.toArray(new File[list.size()]);
|
---|
173 | } catch (IOException ex) {
|
---|
174 | Main.warn("FileDrop: IOException");
|
---|
175 | }
|
---|
176 | return new File[0];
|
---|
177 | }
|
---|
178 | // END 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
|
---|
179 |
|
---|
180 | private void makeDropTarget(final Component c, boolean recursive) {
|
---|
181 | // Make drop target
|
---|
182 | final DropTarget dt = new DropTarget();
|
---|
183 | try {
|
---|
184 | dt.addDropTargetListener(dropListener);
|
---|
185 | } catch(TooManyListenersException e) {
|
---|
186 | Main.error(e);
|
---|
187 | Main.warn("FileDrop: Drop will not work due to previous error. Do you have another listener attached?");
|
---|
188 | }
|
---|
189 |
|
---|
190 | // Listen for hierarchy changes and remove the drop target when the parent gets cleared out.
|
---|
191 | c.addHierarchyListener(new HierarchyListener() {
|
---|
192 | @Override
|
---|
193 | public void hierarchyChanged(HierarchyEvent evt) {
|
---|
194 | Main.trace("FileDrop: Hierarchy changed.");
|
---|
195 | Component parent = c.getParent();
|
---|
196 | if (parent == null) {
|
---|
197 | c.setDropTarget(null);
|
---|
198 | Main.trace("FileDrop: Drop target cleared from component.");
|
---|
199 | } else {
|
---|
200 | new DropTarget(c, dropListener);
|
---|
201 | Main.trace("FileDrop: Drop target added to component.");
|
---|
202 | }
|
---|
203 | }
|
---|
204 | });
|
---|
205 | if (c.getParent() != null) {
|
---|
206 | new DropTarget(c, dropListener);
|
---|
207 | }
|
---|
208 |
|
---|
209 | if (recursive && (c instanceof Container)) {
|
---|
210 | // Get the container
|
---|
211 | Container cont = (Container) c;
|
---|
212 |
|
---|
213 | // Get it's components
|
---|
214 | Component[] comps = cont.getComponents();
|
---|
215 |
|
---|
216 | // Set it's components as listeners also
|
---|
217 | for (Component comp : comps) {
|
---|
218 | makeDropTarget(comp, recursive);
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | /** Determine if the dragged data is a file list. */
|
---|
224 | private boolean isDragOk(final DropTargetDragEvent evt) {
|
---|
225 | boolean ok = false;
|
---|
226 |
|
---|
227 | // Get data flavors being dragged
|
---|
228 | DataFlavor[] flavors = evt.getCurrentDataFlavors();
|
---|
229 |
|
---|
230 | // See if any of the flavors are a file list
|
---|
231 | int i = 0;
|
---|
232 | while(!ok && i < flavors.length) {
|
---|
233 | // BEGIN 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
|
---|
234 | // Is the flavor a file list?
|
---|
235 | final DataFlavor curFlavor = flavors[i];
|
---|
236 | if (curFlavor.equals(DataFlavor.javaFileListFlavor) ||
|
---|
237 | curFlavor.isRepresentationClassReader()) {
|
---|
238 | ok = true;
|
---|
239 | }
|
---|
240 | // END 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
|
---|
241 | i++;
|
---|
242 | }
|
---|
243 |
|
---|
244 | // show data flavors
|
---|
245 | if (flavors.length == 0) {
|
---|
246 | Main.trace("FileDrop: no data flavors.");
|
---|
247 | }
|
---|
248 | for (i = 0; i < flavors.length; i++) {
|
---|
249 | Main.trace(flavors[i].toString());
|
---|
250 | }
|
---|
251 |
|
---|
252 | return ok;
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Removes the drag-and-drop hooks from the component and optionally
|
---|
257 | * from the all children. You should call this if you add and remove
|
---|
258 | * components after you've set up the drag-and-drop.
|
---|
259 | * This will recursively unregister all components contained within
|
---|
260 | * <var>c</var> if <var>c</var> is a {@link java.awt.Container}.
|
---|
261 | *
|
---|
262 | * @param c The component to unregister as a drop target
|
---|
263 | * @return {@code true} if at least one item has been removed, {@code false} otherwise
|
---|
264 | */
|
---|
265 | public static boolean remove(Component c) {
|
---|
266 | return remove(c, true);
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Removes the drag-and-drop hooks from the component and optionally
|
---|
271 | * from the all children. You should call this if you add and remove
|
---|
272 | * components after you've set up the drag-and-drop.
|
---|
273 | *
|
---|
274 | * @param c The component to unregister
|
---|
275 | * @param recursive Recursively unregister components within a container
|
---|
276 | * @return {@code true} if at least one item has been removed, {@code false} otherwise
|
---|
277 | */
|
---|
278 | public static boolean remove(Component c, boolean recursive) {
|
---|
279 | // Make sure we support dnd.
|
---|
280 | if (supportsDnD()) {
|
---|
281 | Main.trace("FileDrop: Removing drag-and-drop hooks.");
|
---|
282 | c.setDropTarget(null);
|
---|
283 | if (recursive && (c instanceof Container)) {
|
---|
284 | for (Component comp : ((Container) c).getComponents()) {
|
---|
285 | remove(comp, recursive);
|
---|
286 | }
|
---|
287 | return true;
|
---|
288 | } else
|
---|
289 | return false;
|
---|
290 | } else
|
---|
291 | return false;
|
---|
292 | }
|
---|
293 |
|
---|
294 | /* ******** I N N E R I N T E R F A C E L I S T E N E R ******** */
|
---|
295 |
|
---|
296 | private final class DropListener implements DropTargetListener {
|
---|
297 | private final Listener listener;
|
---|
298 | private final Border dragBorder;
|
---|
299 | private final Component c;
|
---|
300 |
|
---|
301 | private DropListener(Listener listener, Border dragBorder, Component c) {
|
---|
302 | this.listener = listener;
|
---|
303 | this.dragBorder = dragBorder;
|
---|
304 | this.c = c;
|
---|
305 | }
|
---|
306 |
|
---|
307 | @Override
|
---|
308 | public void dragEnter(DropTargetDragEvent evt) {
|
---|
309 | Main.trace("FileDrop: dragEnter event.");
|
---|
310 |
|
---|
311 | // Is this an acceptable drag event?
|
---|
312 | if (isDragOk(evt)) {
|
---|
313 | // If it's a Swing component, set its border
|
---|
314 | if (c instanceof JComponent) {
|
---|
315 | JComponent jc = (JComponent) c;
|
---|
316 | normalBorder = jc.getBorder();
|
---|
317 | Main.trace("FileDrop: normal border saved.");
|
---|
318 | jc.setBorder(dragBorder);
|
---|
319 | Main.trace("FileDrop: drag border set.");
|
---|
320 | }
|
---|
321 |
|
---|
322 | // Acknowledge that it's okay to enter
|
---|
323 | evt.acceptDrag(DnDConstants.ACTION_COPY);
|
---|
324 | Main.trace("FileDrop: event accepted.");
|
---|
325 | } else {
|
---|
326 | // Reject the drag event
|
---|
327 | evt.rejectDrag();
|
---|
328 | Main.trace("FileDrop: event rejected.");
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | @Override
|
---|
333 | public void dragOver(DropTargetDragEvent evt) {
|
---|
334 | // This is called continually as long as the mouse is over the drag target.
|
---|
335 | }
|
---|
336 |
|
---|
337 | @Override
|
---|
338 | public void drop(DropTargetDropEvent evt) {
|
---|
339 | Main.trace("FileDrop: drop event.");
|
---|
340 | try {
|
---|
341 | // Get whatever was dropped
|
---|
342 | Transferable tr = evt.getTransferable();
|
---|
343 |
|
---|
344 | // Is it a file list?
|
---|
345 | if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
|
---|
346 |
|
---|
347 | // Say we'll take it.
|
---|
348 | evt.acceptDrop(DnDConstants.ACTION_COPY);
|
---|
349 | Main.trace("FileDrop: file list accepted.");
|
---|
350 |
|
---|
351 | // Get a useful list
|
---|
352 | List<?> fileList = (List<?>)tr.getTransferData(DataFlavor.javaFileListFlavor);
|
---|
353 |
|
---|
354 | // Convert list to array
|
---|
355 | final File[] files = fileList.toArray(new File[fileList.size()]);
|
---|
356 |
|
---|
357 | // Alert listener to drop.
|
---|
358 | if (listener != null) {
|
---|
359 | listener.filesDropped(files);
|
---|
360 | }
|
---|
361 |
|
---|
362 | // Mark that drop is completed.
|
---|
363 | evt.getDropTargetContext().dropComplete(true);
|
---|
364 | Main.trace("FileDrop: drop complete.");
|
---|
365 | } else {
|
---|
366 | // this section will check for a reader flavor.
|
---|
367 | // Thanks, Nathan!
|
---|
368 | // BEGIN 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
|
---|
369 | DataFlavor[] flavors = tr.getTransferDataFlavors();
|
---|
370 | boolean handled = false;
|
---|
371 | for (DataFlavor flavor : flavors) {
|
---|
372 | if (flavor.isRepresentationClassReader()) {
|
---|
373 | // Say we'll take it.
|
---|
374 | evt.acceptDrop(DnDConstants.ACTION_COPY);
|
---|
375 | Main.trace("FileDrop: reader accepted.");
|
---|
376 |
|
---|
377 | Reader reader = flavor.getReaderForText(tr);
|
---|
378 |
|
---|
379 | BufferedReader br = new BufferedReader(reader);
|
---|
380 |
|
---|
381 | if (listener != null) {
|
---|
382 | listener.filesDropped(createFileArray(br));
|
---|
383 | }
|
---|
384 |
|
---|
385 | // Mark that drop is completed.
|
---|
386 | evt.getDropTargetContext().dropComplete(true);
|
---|
387 | Main.trace("FileDrop: drop complete.");
|
---|
388 | handled = true;
|
---|
389 | break;
|
---|
390 | }
|
---|
391 | }
|
---|
392 | if (!handled) {
|
---|
393 | Main.trace("FileDrop: not a file list or reader - abort.");
|
---|
394 | evt.rejectDrop();
|
---|
395 | }
|
---|
396 | // END 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
|
---|
397 | }
|
---|
398 | } catch (IOException | UnsupportedFlavorException e) {
|
---|
399 | Main.warn("FileDrop: "+e.getClass().getSimpleName()+" - abort:");
|
---|
400 | Main.error(e);
|
---|
401 | try {
|
---|
402 | evt.rejectDrop();
|
---|
403 | } catch (InvalidDnDOperationException ex) {
|
---|
404 | // Catch InvalidDnDOperationException to fix #11259
|
---|
405 | Main.error(ex);
|
---|
406 | }
|
---|
407 | } finally {
|
---|
408 | // If it's a Swing component, reset its border
|
---|
409 | if (c instanceof JComponent) {
|
---|
410 | JComponent jc = (JComponent) c;
|
---|
411 | jc.setBorder(normalBorder);
|
---|
412 | Main.debug("FileDrop: normal border restored.");
|
---|
413 | }
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 | @Override
|
---|
418 | public void dragExit(DropTargetEvent evt) {
|
---|
419 | Main.debug("FileDrop: dragExit event.");
|
---|
420 | // If it's a Swing component, reset its border
|
---|
421 | if (c instanceof JComponent) {
|
---|
422 | JComponent jc = (JComponent) c;
|
---|
423 | jc.setBorder(normalBorder);
|
---|
424 | Main.debug("FileDrop: normal border restored.");
|
---|
425 | }
|
---|
426 | }
|
---|
427 |
|
---|
428 | @Override
|
---|
429 | public void dropActionChanged(DropTargetDragEvent evt) {
|
---|
430 | Main.debug("FileDrop: dropActionChanged event.");
|
---|
431 | // Is this an acceptable drag event?
|
---|
432 | if (isDragOk(evt)) {
|
---|
433 | evt.acceptDrag(DnDConstants.ACTION_COPY);
|
---|
434 | Main.debug("FileDrop: event accepted.");
|
---|
435 | } else {
|
---|
436 | evt.rejectDrag();
|
---|
437 | Main.debug("FileDrop: event rejected.");
|
---|
438 | }
|
---|
439 | }
|
---|
440 | }
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * Implement this inner interface to listen for when files are dropped. For example
|
---|
444 | * your class declaration may begin like this:
|
---|
445 | * <code>
|
---|
446 | * public class MyClass implements FileDrop.Listener
|
---|
447 | * ...
|
---|
448 | * public void filesDropped( java.io.File[] files )
|
---|
449 | * {
|
---|
450 | * ...
|
---|
451 | * } // end filesDropped
|
---|
452 | * ...
|
---|
453 | * </code>
|
---|
454 | */
|
---|
455 | public static interface Listener {
|
---|
456 |
|
---|
457 | /**
|
---|
458 | * This method is called when files have been successfully dropped.
|
---|
459 | *
|
---|
460 | * @param files An array of <tt>File</tt>s that were dropped.
|
---|
461 | */
|
---|
462 | public abstract void filesDropped(File[] files);
|
---|
463 | }
|
---|
464 |
|
---|
465 | /* ******** I N N E R C L A S S ******** */
|
---|
466 |
|
---|
467 | /**
|
---|
468 | * At last an easy way to encapsulate your custom objects for dragging and dropping
|
---|
469 | * in your Java programs!
|
---|
470 | * When you need to create a {@link java.awt.datatransfer.Transferable} object,
|
---|
471 | * use this class to wrap your object.
|
---|
472 | * For example:
|
---|
473 | * <pre><code>
|
---|
474 | * ...
|
---|
475 | * MyCoolClass myObj = new MyCoolClass();
|
---|
476 | * Transferable xfer = new TransferableObject( myObj );
|
---|
477 | * ...
|
---|
478 | * </code></pre>
|
---|
479 | * Or if you need to know when the data was actually dropped, like when you're
|
---|
480 | * moving data out of a list, say, you can use the {@link TransferableObject.Fetcher}
|
---|
481 | * inner class to return your object Just in Time.
|
---|
482 | * For example:
|
---|
483 | * <pre><code>
|
---|
484 | * ...
|
---|
485 | * final MyCoolClass myObj = new MyCoolClass();
|
---|
486 | *
|
---|
487 | * TransferableObject.Fetcher fetcher = new TransferableObject.Fetcher()
|
---|
488 | * { public Object getObject(){ return myObj; }
|
---|
489 | * }; // end fetcher
|
---|
490 | *
|
---|
491 | * Transferable xfer = new TransferableObject( fetcher );
|
---|
492 | * ...
|
---|
493 | * </code></pre>
|
---|
494 | *
|
---|
495 | * The {@link java.awt.datatransfer.DataFlavor} associated with
|
---|
496 | * {@link TransferableObject} has the representation class
|
---|
497 | * <tt>net.iharder.dnd.TransferableObject.class</tt> and MIME type
|
---|
498 | * <tt>application/x-net.iharder.dnd.TransferableObject</tt>.
|
---|
499 | * This data flavor is accessible via the static
|
---|
500 | * {@link #DATA_FLAVOR} property.
|
---|
501 | *
|
---|
502 | *
|
---|
503 | * <p>I'm releasing this code into the Public Domain. Enjoy.</p>
|
---|
504 | *
|
---|
505 | * @author Robert Harder
|
---|
506 | * @author rob@iharder.net
|
---|
507 | * @version 1.2
|
---|
508 | */
|
---|
509 | public static class TransferableObject implements Transferable {
|
---|
510 |
|
---|
511 | /**
|
---|
512 | * The MIME type for {@link #DATA_FLAVOR} is
|
---|
513 | * <tt>application/x-net.iharder.dnd.TransferableObject</tt>.
|
---|
514 | */
|
---|
515 | public static final String MIME_TYPE = "application/x-net.iharder.dnd.TransferableObject";
|
---|
516 |
|
---|
517 | /**
|
---|
518 | * The default {@link java.awt.datatransfer.DataFlavor} for
|
---|
519 | * {@link TransferableObject} has the representation class
|
---|
520 | * <tt>net.iharder.dnd.TransferableObject.class</tt>
|
---|
521 | * and the MIME type
|
---|
522 | * <tt>application/x-net.iharder.dnd.TransferableObject</tt>.
|
---|
523 | */
|
---|
524 | public static final DataFlavor DATA_FLAVOR =
|
---|
525 | new DataFlavor(TransferableObject.class, MIME_TYPE);
|
---|
526 |
|
---|
527 | private Fetcher fetcher;
|
---|
528 | private Object data;
|
---|
529 |
|
---|
530 | private DataFlavor customFlavor;
|
---|
531 |
|
---|
532 | /**
|
---|
533 | * Creates a new {@link TransferableObject} that wraps <var>data</var>.
|
---|
534 | * Along with the {@link #DATA_FLAVOR} associated with this class,
|
---|
535 | * this creates a custom data flavor with a representation class
|
---|
536 | * determined from <code>data.getClass()</code> and the MIME type
|
---|
537 | * <tt>application/x-net.iharder.dnd.TransferableObject</tt>.
|
---|
538 | *
|
---|
539 | * @param data The data to transfer
|
---|
540 | */
|
---|
541 | public TransferableObject(Object data) {
|
---|
542 | this.data = data;
|
---|
543 | this.customFlavor = new DataFlavor(data.getClass(), MIME_TYPE);
|
---|
544 | }
|
---|
545 |
|
---|
546 | /**
|
---|
547 | * Creates a new {@link TransferableObject} that will return the
|
---|
548 | * object that is returned by <var>fetcher</var>.
|
---|
549 | * No custom data flavor is set other than the default
|
---|
550 | * {@link #DATA_FLAVOR}.
|
---|
551 | *
|
---|
552 | * @param fetcher The {@link Fetcher} that will return the data object
|
---|
553 | * @see Fetcher
|
---|
554 | */
|
---|
555 | public TransferableObject(Fetcher fetcher) {
|
---|
556 | this.fetcher = fetcher;
|
---|
557 | }
|
---|
558 |
|
---|
559 | /**
|
---|
560 | * Creates a new {@link TransferableObject} that will return the
|
---|
561 | * object that is returned by <var>fetcher</var>.
|
---|
562 | * Along with the {@link #DATA_FLAVOR} associated with this class,
|
---|
563 | * this creates a custom data flavor with a representation class <var>dataClass</var>
|
---|
564 | * and the MIME type
|
---|
565 | * <tt>application/x-net.iharder.dnd.TransferableObject</tt>.
|
---|
566 | *
|
---|
567 | * @param dataClass The {@link java.lang.Class} to use in the custom data flavor
|
---|
568 | * @param fetcher The {@link Fetcher} that will return the data object
|
---|
569 | * @see Fetcher
|
---|
570 | */
|
---|
571 | public TransferableObject(Class<?> dataClass, Fetcher fetcher) {
|
---|
572 | this.fetcher = fetcher;
|
---|
573 | this.customFlavor = new DataFlavor(dataClass, MIME_TYPE);
|
---|
574 | }
|
---|
575 |
|
---|
576 | /**
|
---|
577 | * Returns the custom {@link java.awt.datatransfer.DataFlavor} associated
|
---|
578 | * with the encapsulated object or <tt>null</tt> if the {@link Fetcher}
|
---|
579 | * constructor was used without passing a {@link java.lang.Class}.
|
---|
580 | *
|
---|
581 | * @return The custom data flavor for the encapsulated object
|
---|
582 | */
|
---|
583 | public DataFlavor getCustomDataFlavor() {
|
---|
584 | return customFlavor;
|
---|
585 | }
|
---|
586 |
|
---|
587 | /* ******** T R A N S F E R A B L E M E T H O D S ******** */
|
---|
588 |
|
---|
589 | /**
|
---|
590 | * Returns a two- or three-element array containing first
|
---|
591 | * the custom data flavor, if one was created in the constructors,
|
---|
592 | * second the default {@link #DATA_FLAVOR} associated with
|
---|
593 | * {@link TransferableObject}, and third the
|
---|
594 | * {@link java.awt.datatransfer.DataFlavor#stringFlavor}.
|
---|
595 | *
|
---|
596 | * @return An array of supported data flavors
|
---|
597 | */
|
---|
598 | @Override
|
---|
599 | public DataFlavor[] getTransferDataFlavors() {
|
---|
600 | if (customFlavor != null)
|
---|
601 | return new DataFlavor[] {
|
---|
602 | customFlavor,
|
---|
603 | DATA_FLAVOR,
|
---|
604 | DataFlavor.stringFlavor};
|
---|
605 | else
|
---|
606 | return new DataFlavor[] {
|
---|
607 | DATA_FLAVOR,
|
---|
608 | DataFlavor.stringFlavor};
|
---|
609 | }
|
---|
610 |
|
---|
611 | /**
|
---|
612 | * Returns the data encapsulated in this {@link TransferableObject}.
|
---|
613 | * If the {@link Fetcher} constructor was used, then this is when
|
---|
614 | * the {@link Fetcher#getObject getObject()} method will be called.
|
---|
615 | * If the requested data flavor is not supported, then the
|
---|
616 | * {@link Fetcher#getObject getObject()} method will not be called.
|
---|
617 | *
|
---|
618 | * @param flavor The data flavor for the data to return
|
---|
619 | * @return The dropped data
|
---|
620 | */
|
---|
621 | @Override
|
---|
622 | public Object getTransferData(DataFlavor flavor)
|
---|
623 | throws UnsupportedFlavorException, IOException {
|
---|
624 | // Native object
|
---|
625 | if (flavor.equals(DATA_FLAVOR))
|
---|
626 | return fetcher == null ? data : fetcher.getObject();
|
---|
627 |
|
---|
628 | // String
|
---|
629 | if (flavor.equals(DataFlavor.stringFlavor))
|
---|
630 | return fetcher == null ? data.toString() : fetcher.getObject().toString();
|
---|
631 |
|
---|
632 | // We can't do anything else
|
---|
633 | throw new UnsupportedFlavorException(flavor);
|
---|
634 | }
|
---|
635 |
|
---|
636 | /**
|
---|
637 | * Returns <tt>true</tt> if <var>flavor</var> is one of the supported
|
---|
638 | * flavors. Flavors are supported using the <code>equals(...)</code> method.
|
---|
639 | *
|
---|
640 | * @param flavor The data flavor to check
|
---|
641 | * @return Whether or not the flavor is supported
|
---|
642 | */
|
---|
643 | @Override
|
---|
644 | public boolean isDataFlavorSupported(DataFlavor flavor) {
|
---|
645 | // Native object
|
---|
646 | if (flavor.equals(DATA_FLAVOR))
|
---|
647 | return true;
|
---|
648 |
|
---|
649 | // String
|
---|
650 | if (flavor.equals(DataFlavor.stringFlavor))
|
---|
651 | return true;
|
---|
652 |
|
---|
653 | // We can't do anything else
|
---|
654 | return false;
|
---|
655 | }
|
---|
656 |
|
---|
657 | /* ******** I N N E R I N T E R F A C E F E T C H E R ******** */
|
---|
658 |
|
---|
659 | /**
|
---|
660 | * Instead of passing your data directly to the {@link TransferableObject}
|
---|
661 | * constructor, you may want to know exactly when your data was received
|
---|
662 | * in case you need to remove it from its source (or do anyting else to it).
|
---|
663 | * When the {@link #getTransferData getTransferData(...)} method is called
|
---|
664 | * on the {@link TransferableObject}, the {@link Fetcher}'s
|
---|
665 | * {@link #getObject getObject()} method will be called.
|
---|
666 | *
|
---|
667 | * @author Robert Harder
|
---|
668 | */
|
---|
669 | public static interface Fetcher {
|
---|
670 | /**
|
---|
671 | * Return the object being encapsulated in the
|
---|
672 | * {@link TransferableObject}.
|
---|
673 | *
|
---|
674 | * @return The dropped object
|
---|
675 | */
|
---|
676 | public abstract Object getObject();
|
---|
677 | }
|
---|
678 | }
|
---|
679 | }
|
---|