Top Description Fields Constructors Methods
com.sun.jna.platform.dnd

public abstract Class DropHandler

extends Object
implements DropTargetListener
Class Inheritance
All Implemented Interfaces
java.awt.dnd.DropTargetListener, java.util.EventListener
Imports
java.awt.Component, .Point, java.awt.datatransfer.DataFlavor, .Transferable, .UnsupportedFlavorException, java.awt.dnd.DnDConstants, .DropTarget, .DropTargetContext, .DropTargetDragEvent, .DropTargetDropEvent, .DropTargetEvent, .DropTargetListener, java.io.IOException, java.util.Arrays, .HashSet, .List, .Set, java.util.logging.Level, .Logger

Provides simplified drop handling for a component. Usage:

int actions = DnDConstants.MOVE_OR_COPY;
Component component = ...;
DropHandler handler = new DropHandler(component, actions);
The method getDropAction(DropTargetEvent) follows these steps to determine the appropriate action (if any). Override drop(DropTargetDropEvent, int) to handle the drop. You should invoke DropTargetDropEvent#dropComplete as soon as the Transferable data is obtained, to avoid making the DnD operation look suspended.
Author
twall
See Also
DragHandler

Field Summary

Modifier and TypeField and Description
private int
private List<DataFlavor>
private boolean
private DropTarget
private String
private static final Logger
private DropTargetPainter

Constructor Summary

AccessConstructor and Description
public
DropHandler(Component
target component
c
,
int
accepted actions
acceptedActions
)

Create a handler that allows the given set of actions.

public
DropHandler(final Component
The component to receive drops
c
,
int
Allowed actions for drops
acceptedActions
,
DataFlavor[]
Allowed data flavors for drops
acceptedFlavors
)

Enable handling of drops, indicating what actions and flavors are acceptable.

public
DropHandler(final Component
The component to receive drops
c
,
int
Allowed actions for drops
acceptedActions
,
DataFlavor[]
Allowed data flavors for drops
acceptedFlavors
,
DropTargetPainter
Painter to handle drop target feedback
painter
)

Enable handling of drops, indicating what actions and flavors are acceptable, and providing a painter for drop target feedback.

Method Summary

Modifier and TypeMethod and Description
protected int

Returns:

resulting action
acceptOrReject
(DropTargetDragEvent
event
e
)

Accept or reject the drag represented by the given event.

protected boolean

Returns:

whether the drop is supported
canDrop
(DropTargetEvent
event
e
,
int
requested action
action
,
Point
requested drop location
location
)

Indicate whether the given drop action is acceptable at the given location.

private void
public void
dragEnter(DropTargetDragEvent
the DropTargetDragEvent
e
)

Implements java.awt.dnd.DropTargetListener.dragEnter.

Called while a drag operation is ongoing, when the mouse pointer enters the operable part of the drop site for the DropTarget registered with this listener.
public void
dragExit(DropTargetEvent
the DropTargetEvent
e
)

Implements java.awt.dnd.DropTargetListener.dragExit.

Called while a drag operation is ongoing, when the mouse pointer has exited the operable part of the drop site for the DropTarget registered with this listener.
public void
dragOver(DropTargetDragEvent
the DropTargetDragEvent
e
)

Implements java.awt.dnd.DropTargetListener.dragOver.

Called when a drag operation is ongoing, while the mouse pointer is still over the operable part of the drop site for the DropTarget registered with this listener.
public void
drop(DropTargetDropEvent
the DropTargetDropEvent
e
)

Implements java.awt.dnd.DropTargetListener.drop.

Indicates the user has initiated a drop.
protected abstract void
drop(DropTargetDropEvent
event
e
,
int
requested drop type
action
)

Handle an incoming drop with the given action.

public void
dropActionChanged(DropTargetDragEvent
the DropTargetDragEvent
e
)

Implements java.awt.dnd.DropTargetListener.dropActionChanged.

Called if the user has modified the current drop gesture.
protected int

Returns:

effective drop action
getDropAction
(DropTargetEvent e)

Calculate the effective action.

protected int
getDropAction(DropTargetEvent e, int currentAction, int sourceActions, int acceptedActions)

protected int

Returns:

currently acceptable actions.
getDropActionsForFlavors
(DataFlavor[]
currently available flavors
dataFlavors
)

Indicate the actions available for the given list of data flavors.

protected DropTarget
public boolean

Returns:

Whether this drop target is active.
isActive
()

protected boolean

Returns:

whether any of the given flavors are supported
isSupported
(DataFlavor[]
list of transfer flavors to check
flavors
)

Return whether any of the flavors in the given list are accepted.

protected boolean

Returns:

whether any modifiers are active.
modifiersActive
(int
requested action.
dropAction
)

Returns whether there are key modifiers active , or false if they can't be determined.

protected void
paintDropTarget(DropTargetEvent
The drop target event
e
,
int
The action for the drop
action
,
Point
The intended drop location, or null if there is none
location
)

Update the appearance of the target component.

public void
setActive(boolean
whether this handler should accept drops.
active
)

Set whether this handler (and thus its drop target) will accept any drops.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

acceptedActionsback to summary
private int acceptedActions
acceptedFlavorsback to summary
private List<DataFlavor> acceptedFlavors
activeback to summary
private boolean active
dropTargetback to summary
private DropTarget dropTarget
lastActionback to summary
private String lastAction
LOGback to summary
private static final Logger LOG
painterback to summary
private DropTargetPainter painter

Constructor Detail

DropHandlerback to summary
public DropHandler(Component c, int acceptedActions)

Create a handler that allows the given set of actions. If using this constructor, you will need to override isSupported to indicate which data flavors are allowed.

Parameters
c:Component

target component

acceptedActions:int

accepted actions

DropHandlerback to summary
public DropHandler(final Component c, int acceptedActions, DataFlavor[] acceptedFlavors)

Enable handling of drops, indicating what actions and flavors are acceptable.

Parameters
c:Component

The component to receive drops

acceptedActions:int

Allowed actions for drops

acceptedFlavors:DataFlavor[]

Allowed data flavors for drops

See Also
isSupported
DropHandlerback to summary
public DropHandler(final 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.

Parameters
c:Component

The component to receive drops

acceptedActions:int

Allowed actions for drops

acceptedFlavors:DataFlavor[]

Allowed data flavors for drops

painter:DropTargetPainter

Painter to handle drop target feedback

See Also
paintDropTarget

Method Detail

acceptOrRejectback to summary
protected int acceptOrReject(DropTargetDragEvent e)

Accept or reject the drag represented by the given event. Returns the action determined by getDropAction(DropTargetEvent).

Parameters
e:DropTargetDragEvent

event

Returns:int

resulting action

canDropback to summary
protected boolean canDrop(DropTargetEvent e, int action, Point location)

Indicate whether the given drop action is acceptable at the given location. This method is the last check performed by getDropAction(DropTargetEvent). You may override this method to refuse drops on certain areas within the drop target component. The default always returns true.

Parameters
e:DropTargetEvent

event

action:int

requested action

location:Point

requested drop location

Returns:boolean

whether the drop is supported

describeback to summary
private void describe(String type, DropTargetEvent e)
dragEnterback to summary
public void dragEnter(DropTargetDragEvent e)

Implements java.awt.dnd.DropTargetListener.dragEnter.

Doc from java.awt.dnd.DropTargetListener.dragEnter.

Called while a drag operation is ongoing, when the mouse pointer enters the operable part of the drop site for the DropTarget registered with this listener.

Parameters
e:DropTargetDragEvent

the DropTargetDragEvent

Annotations
@Override
dragExitback to summary
public void dragExit(DropTargetEvent e)

Implements java.awt.dnd.DropTargetListener.dragExit.

Doc from java.awt.dnd.DropTargetListener.dragExit.

Called while a drag operation is ongoing, when the mouse pointer has exited the operable part of the drop site for the DropTarget registered with this listener.

Parameters
e:DropTargetEvent

the DropTargetEvent

Annotations
@Override
dragOverback to summary
public void dragOver(DropTargetDragEvent e)

Implements java.awt.dnd.DropTargetListener.dragOver.

Doc from java.awt.dnd.DropTargetListener.dragOver.

Called when a drag operation is ongoing, while the mouse pointer is still over the operable part of the drop site for the DropTarget registered with this listener.

Parameters
e:DropTargetDragEvent

the DropTargetDragEvent

Annotations
@Override
dropback to summary
public void drop(DropTargetDropEvent e)

Implements java.awt.dnd.DropTargetListener.drop.

Indicates the user has initiated a drop. The default performs all standard drop validity checking and handling, then invokes drop(DropTargetDropEvent, int) if the drop looks acceptable.

Parameters
e:DropTargetDropEvent

Doc from java.awt.dnd.DropTargetListener.drop.

the DropTargetDropEvent

Annotations
@Override
dropback to summary
protected abstract void drop(DropTargetDropEvent e, int action) throws UnsupportedFlavorException, IOException

Handle an incoming drop with the given action. The action passed in might be different from DropTargetDropEvent#getDropAction, for instance, if there are no modifiers and the default action is not supported. Calling DropTargetDropEvent#dropComplete 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.

Parameters
e:DropTargetDropEvent

event

action:int

requested drop type

Exceptions
UnsupportedFlavorException:
dropped item has no supported flavors
IOException:
data access failure
dropActionChangedback to summary
public void dropActionChanged(DropTargetDragEvent e)

Implements java.awt.dnd.DropTargetListener.dropActionChanged.

Doc from java.awt.dnd.DropTargetListener.dropActionChanged.

Called if the user has modified the current drop gesture.

Parameters
e:DropTargetDragEvent

the DropTargetDragEvent

Annotations
@Override
getDropActionback to summary
protected int getDropAction(DropTargetEvent e)

Calculate the effective action. The default implementation checks whether any DataFlavors 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.

Parameters
e:DropTargetEvent

DropTargetEvent

Returns:int

effective drop action

See Also
isSupported(DataFlavor[]), getDropActionsForFlavors, getDropAction(DropTargetEvent, int, int, int), canDrop(DropTargetEvent, int, Point)
getDropActionback to summary
protected int getDropAction(DropTargetEvent e, int currentAction, int sourceActions, int acceptedActions)
getDropActionsForFlavorsback to summary
protected int getDropActionsForFlavors(DataFlavor[] dataFlavors)

Indicate the actions available for the given list of data flavors. Override this method if the acceptable drop actions depend on the currently available DataFlavor. The default returns the accepted actions passed into the constructor.

Parameters
dataFlavors:DataFlavor[]

currently available flavors

Returns:int

currently acceptable actions.

See Also
getDropAction(DropTargetEvent, int, int, int), canDrop(DropTargetEvent, int, Point)
getDropTargetback to summary
protected DropTarget getDropTarget()
isActiveback to summary
public boolean isActive()
Returns:boolean

Whether this drop target is active.

isSupportedback to summary
protected boolean isSupported(DataFlavor[] flavors)

Return whether any of the flavors in the given list are accepted. The list is compared against the accepted list provided in the constructor.

Parameters
flavors:DataFlavor[]

list of transfer flavors to check

Returns:boolean

whether any of the given flavors are supported

modifiersActiveback to summary
protected boolean modifiersActive(int dropAction)

Returns whether there are key modifiers active , or false if they can't be determined. We use the DragHandler hint, if available, or fall back to whether the drop action is other than the default (move).

Parameters
dropAction:int

requested action.

Returns:boolean

whether any modifiers are active.

paintDropTargetback to summary
protected void paintDropTarget(DropTargetEvent e, int action, Point location)

Update the appearance of the target component. Normally the decoration should be painted only if the event is an instance of 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.

Parameters
e:DropTargetEvent

The drop target event

action:int

The action for the drop

location:Point

The intended drop location, or null if there is none

setActiveback to summary
public void setActive(boolean active)

Set whether this handler (and thus its drop target) will accept any drops.

Parameters
active:boolean

whether this handler should accept drops.