java.lang.Object
furbelow.DropHandler
- All Implemented Interfaces:
DropTargetListener,EventListener
Provides simplified drop handling for a component.
Usage:
int actions = DnDConstants.MOVE_OR_COPY;
Component component = ...;
DropHandler handler = new DropHandler(component, actions);
- Accept drops where the action is the default (i.e. no modifiers) but the intersection of source and target actions is not the default. Doing so allows the source to adjust the cursor appropriately.
- Refuse drops where the user modifiers request an action that is not
supported (this works for all cases except when the drag source is not
a
DragHandlerand the user explicitly requests a MOVE operation; this is indistinguishable from a drag with no modifiers unless we have access to the key modifiers, whichDragHandlerprovides). - Drops may be refused based on data flavor, location, intended drop
action, or any combination of those, by overriding
canDrop(java.awt.dnd.DropTargetEvent, int, java.awt.Point). - Custom decoration of the drop area may be performed in
paintDropTarget(DropTargetEvent, int, Point).
getDropAction(DropTargetEvent) follows these steps to
determine the appropriate action (if any).
isSupported(List)determines if there are any supported flavorsgetDropActionsForFlavor(List)reduces the supported actions based on available flavors. For instance, a text field for file paths might supportDnDConstants.ACTION_COPY_OR_MOVEon a plain string, butDnDConstants.ACTION_LINKmight 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.- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDropHandler(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
Modifier and TypeMethodDescriptionprotected intAccept 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.voidvoidvoidvoidIndicates the user has initiated a drop.protected abstract voiddrop(DropTargetDropEvent e, int action) Handle an incoming drop with the given action.voidprotected intCalculate the effective action.protected intgetDropAction(DropTargetEvent e, int currentAction, int sourceActions, int acceptedActions) protected intgetDropActionsForFlavor(List dataFlavors) Indicate the actions available for the given list of data flavors.protected DropTargetbooleanisActive()Whether this drop target is active.protected booleanisSupported(List 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 Details
-
DropHandler
Create a handler that allows the given set of actions. If using this constructor, you will need to overrideisSupported(java.util.List)to indicate which data flavors are allowed. -
DropHandler
Enable handling of drops, indicating what actions and flavors are acceptable.- Parameters:
c- The component to receive dropsacceptedActions- Allowed actions for dropsacceptedFlavors- Allowed data flavors for drops- See Also:
-
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 dropsacceptedActions- Allowed actions for dropsacceptedFlavors- Allowed data flavors for dropspainter- Painter to handle drop target feedback- See Also:
-
-
Method Details
-
getDropTarget
-
isActive
public boolean isActive()Whether this drop target is active. -
setActive
public void setActive(boolean active) Set whether this handler (and thus its drop target) will accept any drops. -
getDropActionsForFlavor
Indicate the actions available for the given list of data flavors. Override this method if the acceptable drop actions depend on the currently availableDataFlavor. The default returns the accepted actions passed into the constructor.- Parameters:
dataFlavors- currently available flavors- See Also:
-
getDropAction
Calculate the effective action. The default implementation checks whether anyDataFlavors are supported, and if so, will change the current action fromDnDConstants.ACTION_NONEto 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 toDnDConstants.ACTION_NONEif there is no supported data flavor.- See Also:
-
getDropAction
protected int getDropAction(DropTargetEvent e, int currentAction, int sourceActions, int acceptedActions) -
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). -
acceptOrReject
Accept or reject the drag represented by the given event. Returns the action determined bygetDropAction(DropTargetEvent). -
dragEnter
- Specified by:
dragEnterin interfaceDropTargetListener
-
dragOver
- Specified by:
dragOverin interfaceDropTargetListener
-
dragExit
- Specified by:
dragExitin interfaceDropTargetListener
-
dropActionChanged
- Specified by:
dropActionChangedin interfaceDropTargetListener
-
drop
Indicates the user has initiated a drop. The default performs all standard drop validity checking and handling, then invokesdrop(DropTargetDropEvent,int)if the drop looks acceptable.- Specified by:
dropin interfaceDropTargetListener
-
isSupported
Return whether any of the flavors in the given list are accepted. The list is compared against the accepted list provided in the constructor. -
paintDropTarget
Update the appearance of the target component. Normally the decoration should be painted only if the event is an instance ofDropTargetDragEventwith an action that is notDragHandler.NONE. Otherwise the decoration should be removed or hidden.For an easy way to highlight the drop target, consider using a single instance of
AbstractComponentDecoratorand moving it according to the intended drop location.- Parameters:
e- The drop target eventaction- The action for the droplocation- The intended drop location, or null if there is none
-
canDrop
Indicate whether the given drop action is acceptable at the given location. This method is the last check performed bygetDropAction(DropTargetEvent). You may override this method to refuse drops on certain areas within the drop target component. The default always returns true. -
drop
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 fromDropTargetDropEvent.getDropAction(), for instance, if there are no modifiers and the default action is not supported. CallingDropTargetDropEvent.dropComplete(boolean)is recommended as soon as theTransferabledata is obtained; this allows the drag source to reset the cursor and any drag images which may be in effect.
-