com.sun.jna.examples.dnd

Class DragHandler

public abstract class DragHandler extends Object implements DragSourceListener, DragSourceMotionListener, DragGestureListener

Provides simplified drag handling for a component. Usage:

 int actions = DnDConstants.MOVE_OR_COPY;
 Component component = ...;
 DragHandler handler = new DragHandler(component, actions);
 
NOTE: Fundamentally, the active action is determined by the drop handler in DropTargetDragEvent#acceptDrag, but often client code simply relies on DropTargetDragEvent#getDropAction.
Field Summary
protected static intCOPY
Convenience to reference DnDConstants#ACTION_COPY.
static intCOPY_MASK
Modifier mask for a user-requested copy.
static floatDEFAULT_GHOST_ALPHA
Default transparency for ghosting.
static intKEY_MASK
Modifier mask for any user-requested action.
protected static intLINK
Convenience to reference DnDConstants#ACTION_LINK.
static intLINK_MASK
Modifier mask for a user-requested link.
static DimensionMAX_GHOST_SIZE
Default maximum size for ghosted images.
protected static intMOVE
Convenience to reference DnDConstants#ACTION_MOVE.
static intMOVE_MASK
Modifier mask for a user-requested move.
protected static intNONE
Convenience to reference DnDConstants#ACTION_NONE.
static booleanOSX
static intUNKNOWN_MODIFIERS
DragHandler returns this value when the current modifiers state is unknown.
static TransferableUNKNOWN_TRANSFERABLE
DragHandler returns this value when the current Transferable is unknown.
Constructor Summary
protected DragHandler(Component dragSource, int actions)
Enable drags from the given component, supporting the actions in the given action mask.
Method Summary
static StringactionString(int action)
protected intadjustDropAction(DragSourceEvent ev)
Pick a different drop action if the target doesn't support the current one and there are no modifiers.
protected booleancanDrag(DragGestureEvent e)
Override to control whether a drag is started.
protected ImagecreateDragImage(GraphicsConfiguration gc, Icon icon)
Create an image from the given icon.
voiddragDropEnd(DragSourceDropEvent e)
voiddragEnter(DragSourceDragEvent e)
voiddragExit(DragSourceEvent e)
voiddragGestureRecognized(DragGestureEvent e)
Called when a user drag gesture is recognized.
voiddragMouseMoved(DragSourceDragEvent e)
voiddragOver(DragSourceDragEvent e)
protected voiddragStarted(DragGestureEvent e)
Override to perform any decoration of the target at the start of a drag, if desired.
voiddropActionChanged(DragSourceDragEvent e)
protected intgetAcceptableDropAction(int targetActions)
Returns the first available action supported by source and target.
protected CursorgetCursorForAction(int actualAction)
protected IcongetDragIcon(DragGestureEvent e, Point srcOffset)
Override this to provide a custom image.
protected intgetDropAction(DragSourceEvent ev)
Get the currently requested drop action.
static intgetModifiers()
Used to communicate modifier state to DropHandler.
static TransferablegetTransferable(DropTargetEvent e)
Used to communicate the current Transferable during a drag, if available.
protected abstract TransferablegetTransferable(DragGestureEvent e)
Override to provide an appropriate Transferable representing the data being dragged.
protected IconscaleDragIcon(Icon icon, Point imageOffset)
Reduce the size of the given drag icon, if appropriate.
protected voidsetModifiers(int mods)
Update the modifiers hint.
protected voidupdateCursor(DragSourceEvent ev)

Field Detail

COPY

protected static final int COPY
Convenience to reference DnDConstants#ACTION_COPY.

COPY_MASK

static final int COPY_MASK
Modifier mask for a user-requested copy.

DEFAULT_GHOST_ALPHA

public static final float DEFAULT_GHOST_ALPHA
Default transparency for ghosting.

KEY_MASK

static final int KEY_MASK
Modifier mask for any user-requested action.

LINK

protected static final int LINK
Convenience to reference DnDConstants#ACTION_LINK.

LINK_MASK

static final int LINK_MASK
Modifier mask for a user-requested link.

MAX_GHOST_SIZE

public static final Dimension MAX_GHOST_SIZE
Default maximum size for ghosted images.

MOVE

protected static final int MOVE
Convenience to reference DnDConstants#ACTION_MOVE.

MOVE_MASK

static final int MOVE_MASK
Modifier mask for a user-requested move.

NONE

protected static final int NONE
Convenience to reference DnDConstants#ACTION_NONE.

OSX

static final boolean OSX

UNKNOWN_MODIFIERS

public static final int UNKNOWN_MODIFIERS
DragHandler returns this value when the current modifiers state is unknown.

UNKNOWN_TRANSFERABLE

public static final Transferable UNKNOWN_TRANSFERABLE
DragHandler returns this value when the current Transferable is unknown.

Constructor Detail

DragHandler

protected DragHandler(Component dragSource, int actions)
Enable drags from the given component, supporting the actions in the given action mask.

Method Detail

actionString

static String actionString(int action)

adjustDropAction

protected int adjustDropAction(DragSourceEvent ev)
Pick a different drop action if the target doesn't support the current one and there are no modifiers.

canDrag

protected boolean canDrag(DragGestureEvent e)
Override to control whether a drag is started. The default implementation disallows the drag if the user is applying modifiers and the user-requested action is not supported.

createDragImage

protected Image createDragImage(GraphicsConfiguration gc, Icon icon)
Create an image from the given icon. The image is provided to the native handler if drag images are supported natively.

dragDropEnd

public void dragDropEnd(DragSourceDropEvent e)

dragEnter

public void dragEnter(DragSourceDragEvent e)

dragExit

public void dragExit(DragSourceEvent e)

dragGestureRecognized

public void dragGestureRecognized(DragGestureEvent e)
Called when a user drag gesture is recognized. This method is responsible for initiating the drag operation.

dragMouseMoved

public void dragMouseMoved(DragSourceDragEvent e)

dragOver

public void dragOver(DragSourceDragEvent e)

dragStarted

protected void dragStarted(DragGestureEvent e)
Override to perform any decoration of the target at the start of a drag, if desired.

dropActionChanged

public void dropActionChanged(DragSourceDragEvent e)

getAcceptableDropAction

protected int getAcceptableDropAction(int targetActions)
Returns the first available action supported by source and target.

getCursorForAction

protected Cursor getCursorForAction(int actualAction)

getDragIcon

protected Icon getDragIcon(DragGestureEvent e, Point srcOffset)
Override this to provide a custom image. The Icon returned by this method by default is null, which results in no drag image.

Parameters: srcOffset set this to be the offset from the drag source component's upper left corner to the image's upper left corner. For example, when dragging a row from a list, the offset would be the row's bounding rectangle's (x,y) coordinate.

The default value is (0,0), so if unchanged, the image is will use the same origin as the drag source component.

getDropAction

protected int getDropAction(DragSourceEvent ev)
Get the currently requested drop action.

getModifiers

static int getModifiers()
Used to communicate modifier state to DropHandler. Note that this field will only be accurate when a DragHandler in the same VM started the drag. Otherwise, UNKNOWN_MODIFIERS will be returned.

getTransferable

public static Transferable getTransferable(DropTargetEvent e)
Used to communicate the current Transferable during a drag, if available. Work around absence of access to the data when dragging pre-1.5.

getTransferable

protected abstract Transferable getTransferable(DragGestureEvent e)
Override to provide an appropriate Transferable representing the data being dragged.

scaleDragIcon

protected Icon scaleDragIcon(Icon icon, Point imageOffset)
Reduce the size of the given drag icon, if appropriate. When using a smaller drag icon, we also need to adjust the cursor offset within the icon.

setModifiers

protected void setModifiers(int mods)
Update the modifiers hint.

updateCursor

protected void updateCursor(DragSourceEvent ev)
Copyright © 2007-2009 Timothy Wall. All Rights Reserved.