com.sun.jna.examples.dnd

Class DropHandler

public abstract class DropHandler extends Object implements DropTargetListener

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 follows these steps to determine the appropriate action (if any). Override DropHandler 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

Constructor Summary
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.
Method Summary
protected intacceptOrReject(DropTargetDragEvent e)
Accept or reject the drag represented by the given event.
protected booleancanDrop(DropTargetEvent e, int action, Point location)
Indicate whether the given drop action is acceptable at the given location.
voiddragEnter(DropTargetDragEvent e)
voiddragExit(DropTargetEvent e)
voiddragOver(DropTargetDragEvent e)
voiddrop(DropTargetDropEvent e)
Indicates the user has initiated a drop.
protected abstract voiddrop(DropTargetDropEvent e, int action)
Handle an incoming drop with the given action.
voiddropActionChanged(DropTargetDragEvent e)
protected intgetDropAction(DropTargetEvent e)
Calculate the effective action.
protected intgetDropAction(DropTargetEvent e, int currentAction, int sourceActions, int acceptedActions)
protected intgetDropActionsForFlavors(DataFlavor[] dataFlavors)
Indicate the actions available for the given list of data flavors.
protected DropTargetgetDropTarget()
booleanisActive()
Whether this drop target is active.
protected booleanisSupported(DataFlavor[] flavors)
Return whether any of the flavors in the given list are accepted.
protected booleanmodifiersActive(int dropAction)
Returns whether there are key modifiers active , or false if they can't be determined.
protected voidpaintDropTarget(DropTargetEvent e, int action, Point location)
Update the appearance of the target component.
voidsetActive(boolean active)
Set whether this handler (and thus its drop target) will accept any drops.

Constructor Detail

DropHandler

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 DropHandler to indicate which data flavors are allowed.

DropHandler

public DropHandler(Component c, int acceptedActions, DataFlavor[] acceptedFlavors)
Enable handling of drops, indicating what actions and flavors are acceptable.

Parameters: c The component to receive drops acceptedActions Allowed actions for drops acceptedFlavors Allowed data flavors for drops

See Also: DropHandler

DropHandler

public 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.

Parameters: c The component to receive drops acceptedActions Allowed actions for drops acceptedFlavors Allowed data flavors for drops painter Painter to handle drop target feedback

See Also: DropHandler

Method Detail

acceptOrReject

protected int acceptOrReject(DropTargetDragEvent e)
Accept or reject the drag represented by the given event. Returns the action determined by getDropAction.

canDrop

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. You may override this method to refuse drops on certain areas within the drop target component. The default always returns true.

dragEnter

public void dragEnter(DropTargetDragEvent e)

dragExit

public void dragExit(DropTargetEvent e)

dragOver

public void dragOver(DropTargetDragEvent e)

drop

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

drop

protected abstract void drop(DropTargetDropEvent e, int action)
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.

dropActionChanged

public void dropActionChanged(DropTargetDragEvent e)

getDropAction

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.

See Also: (DataFlavor[]) DropHandler DropHandler DropHandler

getDropAction

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

getDropActionsForFlavors

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 currently available flavors

See Also: DropHandler DropHandler

getDropTarget

protected DropTarget getDropTarget()

isActive

public boolean isActive()
Whether this drop target is active.

isSupported

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.

modifiersActive

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).

paintDropTarget

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 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 The drop target event action The action for the drop location The intended drop location, or null if there is none

setActive

public void setActive(boolean active)
Set whether this handler (and thus its drop target) will accept any drops.
Copyright © 2007-2009 Timothy Wall. All Rights Reserved.