|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.openstreetmap.josm.gui.FileDrop
public class FileDrop
This class makes it easy to drag and drop files from the operating system to a Java program. Any java.awt.Component can be dropped onto, but only javax.swing.JComponents will indicate the drop event with a changed border.
To use this class, construct a new FileDrop by passing it the target component and a Listener to receive notification when file(s) have been dropped. Here is an example:
JPanel myPanel = new JPanel();
new FileDrop( myPanel, new FileDrop.Listener()
{ public void filesDropped( java.io.File[] files )
{
// handle file drop
...
} // end filesDropped
}); // end FileDrop.Listener
You can specify the border that will appear when files are being dragged by
calling the constructor with a javax.swing.border.Border. Only
JComponents will show any indication with a border.
You can turn on some debugging features by passing a PrintStream
object (such as System.out) into the full constructor. A null
value will result in no extra debugging information being output.
I'm releasing this code into the Public Domain. Enjoy.
Original author: Robert Harder, rharder@usa.net
2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
Nested Class Summary | |
---|---|
static class |
FileDrop.Event
This is the event that is passed to the filesDropped(...) method in
your FileDropListener when files are dropped onto
a registered drop target. |
static interface |
FileDrop.Listener
Implement this inner interface to listen for when files are dropped. |
static class |
FileDrop.TransferableObject
At last an easy way to encapsulate your custom objects for dragging and dropping in your Java programs! When you need to create a Transferable object,
use this class to wrap your object. |
Field Summary | |
---|---|
private static java.awt.Color |
defaultBorderColor
|
private java.awt.dnd.DropTargetListener |
dropListener
|
private javax.swing.border.Border |
normalBorder
|
private static java.lang.Boolean |
supportsDnD
Discover if the running JVM is modern enough to have drag and drop. |
private static java.lang.String |
ZERO_CHAR_STRING
|
Constructor Summary | |
---|---|
FileDrop(java.awt.Component c)
|
|
FileDrop(java.awt.Component c,
boolean recursive,
FileDrop.Listener listener)
Constructor with a default border and the option to recursively set drop targets. |
|
FileDrop(java.awt.Component c,
javax.swing.border.Border dragBorder,
boolean recursive,
FileDrop.Listener listener)
Constructor with a specified border and the option to recursively set drop targets. |
|
FileDrop(java.awt.Component c,
javax.swing.border.Border dragBorder,
FileDrop.Listener listener)
Constructor with a specified border |
|
FileDrop(java.awt.Component c,
FileDrop.Listener listener)
Constructs a FileDrop with a default light-blue border
and, if c is a Container , recursively
sets all elements contained within as drop targets, though only
the top level container will change borders. |
|
FileDrop(java.io.PrintStream out,
java.awt.Component c,
boolean recursive,
FileDrop.Listener listener)
Constructor with a default border, debugging optionally turned on and the option to recursively set drop targets. |
|
FileDrop(java.io.PrintStream out,
java.awt.Component c,
javax.swing.border.Border dragBorder,
boolean recursive,
FileDrop.Listener listener)
Full constructor with a specified border and debugging optionally turned on. |
|
FileDrop(java.io.PrintStream out,
java.awt.Component c,
javax.swing.border.Border dragBorder,
FileDrop.Listener listener)
Constructor with a specified border and debugging optionally turned on. |
|
FileDrop(java.io.PrintStream out,
java.awt.Component c,
FileDrop.Listener listener)
Constructor with a default border and debugging optionally turned on. |
Method Summary | |
---|---|
private static java.io.File[] |
createFileArray(java.io.BufferedReader bReader,
java.io.PrintStream out)
|
private boolean |
isDragOk(java.io.PrintStream out,
java.awt.dnd.DropTargetDragEvent evt)
Determine if the dragged data is a file list. |
private static void |
log(java.io.PrintStream out,
java.lang.String message)
Outputs message to out if it's not null. |
private void |
makeDropTarget(java.io.PrintStream out,
java.awt.Component c,
boolean recursive)
|
static boolean |
remove(java.awt.Component c)
Removes the drag-and-drop hooks from the component and optionally from the all children. |
static boolean |
remove(java.io.PrintStream out,
java.awt.Component c,
boolean recursive)
Removes the drag-and-drop hooks from the component and optionally from the all children. |
private static boolean |
supportsDnD()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private transient javax.swing.border.Border normalBorder
private transient java.awt.dnd.DropTargetListener dropListener
private static java.lang.Boolean supportsDnD
private static java.awt.Color defaultBorderColor
private static java.lang.String ZERO_CHAR_STRING
Constructor Detail |
---|
public FileDrop(java.awt.Component c)
public FileDrop(java.awt.Component c, FileDrop.Listener listener)
FileDrop
with a default light-blue border
and, if c is a Container
, recursively
sets all elements contained within as drop targets, though only
the top level container will change borders.
c
- Component on which files will be dropped.listener
- Listens for filesDropped.public FileDrop(java.awt.Component c, boolean recursive, FileDrop.Listener listener)
c
- Component on which files will be dropped.recursive
- Recursively set children as drop targets.listener
- Listens for filesDropped.public FileDrop(java.io.PrintStream out, java.awt.Component c, FileDrop.Listener listener)
out
- PrintStream to record debugging info or null for no debugging.c
- Component on which files will be dropped.listener
- Listens for filesDropped.public FileDrop(java.io.PrintStream out, java.awt.Component c, boolean recursive, FileDrop.Listener listener)
out
- PrintStream to record debugging info or null for no debugging.c
- Component on which files will be dropped.recursive
- Recursively set children as drop targets.listener
- Listens for filesDropped.public FileDrop(java.awt.Component c, javax.swing.border.Border dragBorder, FileDrop.Listener listener)
c
- Component on which files will be dropped.dragBorder
- Border to use on JComponent when dragging occurs.listener
- Listens for filesDropped.public FileDrop(java.awt.Component c, javax.swing.border.Border dragBorder, boolean recursive, FileDrop.Listener listener)
c
- Component on which files will be dropped.dragBorder
- Border to use on JComponent when dragging occurs.recursive
- Recursively set children as drop targets.listener
- Listens for filesDropped.public FileDrop(java.io.PrintStream out, java.awt.Component c, javax.swing.border.Border dragBorder, FileDrop.Listener listener)
out
- PrintStream to record debugging info or null for no debugging.c
- Component on which files will be dropped.dragBorder
- Border to use on JComponent when dragging occurs.listener
- Listens for filesDropped.public FileDrop(java.io.PrintStream out, java.awt.Component c, javax.swing.border.Border dragBorder, boolean recursive, FileDrop.Listener listener)
out
- PrintStream to record debugging info or null for no debugging.c
- Component on which files will be dropped.dragBorder
- Border to use on JComponent when dragging occurs.recursive
- Recursively set children as drop targets.listener
- Listens for filesDropped.Method Detail |
---|
private static boolean supportsDnD()
private static java.io.File[] createFileArray(java.io.BufferedReader bReader, java.io.PrintStream out)
private void makeDropTarget(java.io.PrintStream out, java.awt.Component c, boolean recursive)
private boolean isDragOk(java.io.PrintStream out, java.awt.dnd.DropTargetDragEvent evt)
private static void log(java.io.PrintStream out, java.lang.String message)
public static boolean remove(java.awt.Component c)
Container
.
c
- The component to unregister as a drop targetpublic static boolean remove(java.io.PrintStream out, java.awt.Component c, boolean recursive)
out
- Optional PrintStream
for logging drag and drop messagesc
- The component to unregisterrecursive
- Recursively unregister components within a container
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |