public abstract class DropHandler
extends java.lang.Object
implements java.awt.dnd.DropTargetListener
int actions = DnDConstants.MOVE_OR_COPY;
Component component = ...;
DropHandler handler = new DropHandler(component, actions);
DragHandler
and the user explicitly requests a MOVE operation;
this is indistinguishable from a drag with no modifiers unless we have
access to the key modifiers, which DragHandler
provides).
canDrop(java.awt.dnd.DropTargetEvent, int, java.awt.Point)
.
paintDropTarget(DropTargetEvent, int, Point)
or by providing
a DropTargetPainter
.
getDropAction(DropTargetEvent)
follows these steps to
determine the appropriate action (if any).
isSupported(DataFlavor[])
determines if there are any supported
flavors
getDropActionsForFlavors(DataFlavor[])
reduces the supported
actions based on available flavors. For instance, a text field for file
paths might support DnDConstants.ACTION_COPY_OR_MOVE
on a plain
string, but DnDConstants.ACTION_LINK
might be the only action
supported on a file.
getDropAction(DropTargetEvent, int, int, int)
relax the action
if it's the default, or restrict it for user requested actions.
canDrop(DropTargetEvent, int, Point)
change the action based on
the location in the drop target component, or any other criteria.
drop(DropTargetDropEvent, int)
to handle the drop.
You should invoke DropTargetDropEvent.dropComplete(boolean)
as soon
as the Transferable
data is obtained, to avoid making the DnD
operation look suspended.DragHandler
Constructor and Description |
---|
DropHandler(java.awt.Component c,
int acceptedActions)
Create a handler that allows the given set of actions.
|
DropHandler(java.awt.Component c,
int acceptedActions,
java.awt.datatransfer.DataFlavor[] acceptedFlavors)
Enable handling of drops, indicating what actions and flavors are
acceptable.
|
DropHandler(java.awt.Component c,
int acceptedActions,
java.awt.datatransfer.DataFlavor[] acceptedFlavors,
DropTargetPainter painter)
Enable handling of drops, indicating what actions and flavors are
acceptable, and providing a painter for drop target feedback.
|
Modifier and Type | Method and Description |
---|---|
protected int |
acceptOrReject(java.awt.dnd.DropTargetDragEvent e)
Accept or reject the drag represented by the given event.
|
protected boolean |
canDrop(java.awt.dnd.DropTargetEvent e,
int action,
java.awt.Point location)
Indicate whether the given drop action is acceptable at the given
location.
|
void |
dragEnter(java.awt.dnd.DropTargetDragEvent e) |
void |
dragExit(java.awt.dnd.DropTargetEvent e) |
void |
dragOver(java.awt.dnd.DropTargetDragEvent e) |
void |
drop(java.awt.dnd.DropTargetDropEvent e)
Indicates the user has initiated a drop.
|
protected abstract void |
drop(java.awt.dnd.DropTargetDropEvent e,
int action)
Handle an incoming drop with the given action.
|
void |
dropActionChanged(java.awt.dnd.DropTargetDragEvent e) |
protected int |
getDropAction(java.awt.dnd.DropTargetEvent e)
Calculate the effective action.
|
protected int |
getDropAction(java.awt.dnd.DropTargetEvent e,
int currentAction,
int sourceActions,
int acceptedActions) |
protected int |
getDropActionsForFlavors(java.awt.datatransfer.DataFlavor[] dataFlavors)
Indicate the actions available for the given list of data flavors.
|
protected java.awt.dnd.DropTarget |
getDropTarget() |
boolean |
isActive() |
protected boolean |
isSupported(java.awt.datatransfer.DataFlavor[] flavors)
Return whether any of the flavors in the given list are accepted.
|
protected boolean |
modifiersActive(int dropAction)
Returns whether there are key modifiers active ,
or false if they can't be determined.
|
protected void |
paintDropTarget(java.awt.dnd.DropTargetEvent e,
int action,
java.awt.Point location)
Update the appearance of the target component.
|
void |
setActive(boolean active)
Set whether this handler (and thus its drop target) will accept
any drops.
|
public DropHandler(java.awt.Component c, int acceptedActions)
isSupported(java.awt.datatransfer.DataFlavor[])
to
indicate which data flavors are allowed.c
- target componentacceptedActions
- accepted actionspublic DropHandler(java.awt.Component c, int acceptedActions, java.awt.datatransfer.DataFlavor[] acceptedFlavors)
c
- The component to receive dropsacceptedActions
- Allowed actions for dropsacceptedFlavors
- Allowed data flavors for dropsisSupported(java.awt.datatransfer.DataFlavor[])
public DropHandler(java.awt.Component c, int acceptedActions, java.awt.datatransfer.DataFlavor[] acceptedFlavors, DropTargetPainter painter)
c
- The component to receive dropsacceptedActions
- Allowed actions for dropsacceptedFlavors
- Allowed data flavors for dropspainter
- Painter to handle drop target feedbackpaintDropTarget(java.awt.dnd.DropTargetEvent, int, java.awt.Point)
protected java.awt.dnd.DropTarget getDropTarget()
public boolean isActive()
public void setActive(boolean active)
active
- whether this handler should accept drops.protected int getDropActionsForFlavors(java.awt.datatransfer.DataFlavor[] dataFlavors)
DataFlavor
. The default returns
the accepted actions passed into the constructor.dataFlavors
- currently available flavorsgetDropAction(DropTargetEvent, int, int, int)
,
canDrop(DropTargetEvent, int, Point)
protected int getDropAction(java.awt.dnd.DropTargetEvent e)
DataFlavor
s are supported, and if so,
will change the current action from DnDConstants.ACTION_NONE
to
something in common between the source and destination. Refuse
user-requested actions if they are not supported (rather than silently
accepting a non-user-requested action, which is the Java's DnD default
behavior). The drop action is forced to DnDConstants.ACTION_NONE
if there is no supported data flavor.e
- DropTargetEvent
isSupported(DataFlavor[])
,
getDropActionsForFlavors(java.awt.datatransfer.DataFlavor[])
,
getDropAction(DropTargetEvent, int, int, int)
,
canDrop(DropTargetEvent, int, Point)
protected int getDropAction(java.awt.dnd.DropTargetEvent e, int currentAction, int sourceActions, int acceptedActions)
protected boolean modifiersActive(int dropAction)
dropAction
- requested action.protected int acceptOrReject(java.awt.dnd.DropTargetDragEvent e)
getDropAction(DropTargetEvent)
.e
- eventpublic void dragEnter(java.awt.dnd.DropTargetDragEvent e)
dragEnter
in interface java.awt.dnd.DropTargetListener
public void dragOver(java.awt.dnd.DropTargetDragEvent e)
dragOver
in interface java.awt.dnd.DropTargetListener
public void dragExit(java.awt.dnd.DropTargetEvent e)
dragExit
in interface java.awt.dnd.DropTargetListener
public void dropActionChanged(java.awt.dnd.DropTargetDragEvent e)
dropActionChanged
in interface java.awt.dnd.DropTargetListener
public void drop(java.awt.dnd.DropTargetDropEvent e)
drop(DropTargetDropEvent,int)
if the drop looks acceptable.drop
in interface java.awt.dnd.DropTargetListener
protected boolean isSupported(java.awt.datatransfer.DataFlavor[] flavors)
flavors
- list of transfer flavors to checkprotected void paintDropTarget(java.awt.dnd.DropTargetEvent e, int action, java.awt.Point location)
DropTargetDragEvent
with an action that is not
DragHandler.NONE
. Otherwise the decoration should be removed
or hidden.
For an easy way to highlight the drop target, consider using a single
instance of AbstractComponentDecorator
and moving it
according to the intended drop location.
e
- The drop target eventaction
- The action for the droplocation
- The intended drop location, or null if there is noneprotected boolean canDrop(java.awt.dnd.DropTargetEvent e, int action, java.awt.Point location)
getDropAction(DropTargetEvent)
.
You may override this method to refuse drops on certain areas
within the drop target component. The default always returns true.e
- eventaction
- requested actionlocation
- requested drop locationprotected abstract void drop(java.awt.dnd.DropTargetDropEvent e, int action) throws java.awt.datatransfer.UnsupportedFlavorException, java.io.IOException
DropTargetDropEvent.getDropAction()
,
for instance, if there are no modifiers and the default action is not
supported. Calling DropTargetDropEvent.dropComplete(boolean)
is
recommended as soon as the Transferable
data is obtained; this
allows the drag source to reset the cursor and any drag images which
may be in effect.e
- eventaction
- requested drop typejava.awt.datatransfer.UnsupportedFlavorException
- dropped item has no supported flavorsjava.io.IOException
- data access failure