Package ca.cgjennings.ui.dnd
Class FileDrop
- java.lang.Object
-
- ca.cgjennings.ui.dnd.FileDrop
-
public class FileDrop extends java.lang.Object
A support class that allows interface components to easily accept files being dropped on them from other components or from the platform.- Since:
- 1.61
- Author:
- Adapted from public domain code by Robert Harder and Nathan Blomquist
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FileDrop.DropEvent
Event that is provided to listeners when files are dropped on a target.static interface
FileDrop.DropListener
Listener that may be registered to receiveFileDrop.DropEvent
s when files are dropped on a target component.static interface
FileDrop.Listener
A simpler listener that may be used as an alternative toFileDrop.DropListener
.
-
Constructor Summary
Constructors Constructor Description FileDrop(java.awt.Component component, FileDrop.Listener listener)
Creates a newFileDrop
on the component.FileDrop(java.awt.Component component, javax.swing.JComponent borderOwner, boolean attachToDescendants)
Creates a newFileDrop
on the component.FileDrop(java.awt.Component component, javax.swing.JComponent borderOwner, boolean attachToDescendants, FileDrop.Listener listener)
Creates a newFileDrop
on the component.FileDrop(java.awt.Component component, javax.swing.JComponent borderOwner, FileDrop.Listener listener)
Creates a newFileDrop
on the component.FileDrop(javax.swing.JComponent borderOwner, java.awt.Component[] components)
Creates a newFileDrop
on the specified components.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.io.FileFilter
getFileFilter()
Returns the file filter for this drop handler.static FileDrop
of(javax.swing.JComponent borderOwner, java.awt.Component... components)
Creates and returns a newFileDrop
on the specified components.static void
remove(java.awt.Component component, boolean recursive)
Removes all drag-and-drop support from a component and optionally from its descendants.void
setFileFilter(java.io.FileFilter fileFilter)
Sets the file filter for this drop handler.void
setListener(FileDrop.DropListener complexListener)
Sets the drop listener to receive detailed information about dropped files.void
setListener(FileDrop.Listener simpleListener)
Sets the drop listener to a simple listener that receives a non-null, non-empty array of files.
-
-
-
Constructor Detail
-
FileDrop
public FileDrop(java.awt.Component component, javax.swing.JComponent borderOwner, boolean attachToDescendants)
Creates a newFileDrop
on the component. If the component is aJComponent
, aDropBorder
will appear when dragging files over it.- Parameters:
component
- the component that will accept filesborderOwner
- if non-null
, a component whose border will change to indicate that files are being dragged over the componentattachToDescendants
- iftrue
, then all of the descendants of the component will also listen for file drops
-
FileDrop
public FileDrop(java.awt.Component component, FileDrop.Listener listener)
Creates a newFileDrop
on the component. If the component is aJComponent
, aDropBorder
will appear when dragging files over it.- Parameters:
component
- the component that will accept fileslistener
- the optional simple listener that will be notified when files are dropped
-
FileDrop
public FileDrop(java.awt.Component component, javax.swing.JComponent borderOwner, FileDrop.Listener listener)
Creates a newFileDrop
on the component. IfborderOwner
is notnull
, aDropBorder
will appear when dragging files over the component.- Parameters:
component
- the component that will accept filesborderOwner
- if non-null
, a component whose border will change to indicate that files are being dragged over the componentlistener
- the optional simple listener that will be notified when files are dropped
-
FileDrop
public FileDrop(java.awt.Component component, javax.swing.JComponent borderOwner, boolean attachToDescendants, FileDrop.Listener listener)
Creates a newFileDrop
on the component. If the component is aJComponent
, aDropBorder
will appear when dragging files over it.- Parameters:
component
- the component that will accept filesborderOwner
- if non-null
, a component whose border will change to indicate that files are being dragged over the componentattachToDescendants
- iftrue
, then all of the descendants of the component will also listen for file dropslistener
- the optional simple listener that will be notified when files are dropped
-
FileDrop
public FileDrop(javax.swing.JComponent borderOwner, java.awt.Component[] components)
Creates a newFileDrop
on the specified components. The drop will initially have no listener attached.- Parameters:
borderOwner
- if non-null
, a component whose border will change to indicate that files are being dragged over the componentcomponents
- the components that will accept files- See Also:
setListener(ca.cgjennings.ui.dnd.FileDrop.Listener)
-
-
Method Detail
-
of
public static FileDrop of(javax.swing.JComponent borderOwner, java.awt.Component... components)
Creates and returns a newFileDrop
on the specified components. The returnedFileDrop
will have no listener attached, but is can be set using builder-style method chaining.The method was added to deal with two issues: First, when this form was provided as a constrctor, it broke scripts as the script engine could not distinguish which overload to choose. Second, it avoids some compiler warnings as a common patern is to create a
FileDrop
in a constructor without storing a reference to it.- Parameters:
borderOwner
- if non-null
, a component whose border will change to indicate that files are being dragged over the componentcomponents
- the components that will accept files- See Also:
setListener(ca.cgjennings.ui.dnd.FileDrop.Listener)
-
setListener
public final void setListener(FileDrop.Listener simpleListener)
Sets the drop listener to a simple listener that receives a non-null, non-empty array of files. Only one listener may be attached to theFileDrop
.- Parameters:
simpleListener
- the listener, or null to stop receiving events
-
setListener
public void setListener(FileDrop.DropListener complexListener)
Sets the drop listener to receive detailed information about dropped files. Only one listener may be attached to theFileDrop
.- Parameters:
complexListener
- the listener, or null to stop receiving events
-
setFileFilter
public void setFileFilter(java.io.FileFilter fileFilter)
Sets the file filter for this drop handler. When set, dropped files will be filtered before calling the drop listener.- Parameters:
fileFilter
- the filter to apply, ornull
to accept all files
-
getFileFilter
public java.io.FileFilter getFileFilter()
Returns the file filter for this drop handler.- Returns:
- the file filter for this drop handler, or
null
-
remove
public static void remove(java.awt.Component component, boolean recursive)
Removes all drag-and-drop support from a component and optionally from its descendants.- Parameters:
component
- the top-level component to modifyrecursive
- iftrue
, apply recursively to all descendants
-
-