public abstract class DropHandler extends Object implements 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(Component c,
int acceptedActions)
Create a handler that allows the given set of actions.
|
DropHandler(Component c,
int acceptedActions,
DataFlavor[] acceptedFlavors)
Enable handling of drops, indicating what actions and flavors are
acceptable.
|
DropHandler(Component c,
int acceptedActions,
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(DropTargetDragEvent e)
Accept or reject the drag represented by the given event.
|
protected boolean |
canDrop(DropTargetEvent e,
int action,
Point location)
Indicate whether the given drop action is acceptable at the given
location.
|
void |
dragEnter(DropTargetDragEvent e) |
void |
dragExit(DropTargetEvent e) |
void |
dragOver(DropTargetDragEvent e) |
void |
drop(DropTargetDropEvent e)
Indicates the user has initiated a drop.
|
protected abstract void |
drop(DropTargetDropEvent e,
int action)
Handle an incoming drop with the given action.
|
void |
dropActionChanged(DropTargetDragEvent e) |
protected int |
getDropAction(DropTargetEvent e)
Calculate the effective action.
|
protected int |
getDropAction(DropTargetEvent e,
int currentAction,
int sourceActions,
int acceptedActions) |
protected int |
getDropActionsForFlavors(DataFlavor[] dataFlavors)
Indicate the actions available for the given list of data flavors.
|
protected DropTarget |
getDropTarget() |
boolean |
isActive() |
protected boolean |
isSupported(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(DropTargetEvent e,
int action,
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(Component c, int acceptedActions)
isSupported(java.awt.datatransfer.DataFlavor[])
to
indicate which data flavors are allowed.c
- target componentacceptedActions
- accepted actionspublic DropHandler(Component c, int acceptedActions, DataFlavor[] acceptedFlavors)
c
- The component to receive dropsacceptedActions
- Allowed actions for dropsacceptedFlavors
- Allowed data flavors for dropsisSupported(java.awt.datatransfer.DataFlavor[])
public DropHandler(Component c, int acceptedActions, 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 DropTarget getDropTarget()
public boolean isActive()
public void setActive(boolean active)
active
- whether this handler should accept drops.protected int getDropActionsForFlavors(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(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(DropTargetEvent e, int currentAction, int sourceActions, int acceptedActions)
protected boolean modifiersActive(int dropAction)
dropAction
- requested action.protected int acceptOrReject(DropTargetDragEvent e)
getDropAction(DropTargetEvent)
.e
- eventpublic void dragEnter(DropTargetDragEvent e)
dragEnter
in interface DropTargetListener
public void dragOver(DropTargetDragEvent e)
dragOver
in interface DropTargetListener
public void dragExit(DropTargetEvent e)
dragExit
in interface DropTargetListener
public void dropActionChanged(DropTargetDragEvent e)
dropActionChanged
in interface DropTargetListener
public void drop(DropTargetDropEvent e)
drop(DropTargetDropEvent,int)
if the drop looks acceptable.drop
in interface DropTargetListener
protected boolean isSupported(DataFlavor[] flavors)
flavors
- list of transfer flavors to checkprotected void paintDropTarget(DropTargetEvent e, int action, 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(DropTargetEvent e, int action, 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(DropTargetDropEvent e, int action) throws UnsupportedFlavorException, 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 typeUnsupportedFlavorException
- dropped item has no supported flavorsIOException
- data access failure