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

public abstract Class DragHandler

extends Object
implements DragSourceListener, DragSourceMotionListener, DragGestureListener
Class Inheritance
All Implemented Interfaces
java.awt.dnd.DragGestureListener, java.util.EventListener, java.awt.dnd.DragSourceMotionListener, java.awt.dnd.DragSourceListener
Imports
com.sun.jna.Platform, java.awt.AlphaComposite, .Component, .Cursor, .Dimension, .Graphics2D, .GraphicsConfiguration, .Image, .Point, .Transparency, java.awt.datatransfer.Transferable, java.awt.dnd.DnDConstants, .DragGestureEvent, .DragGestureListener, .DragSource, .DragSourceContext, .DragSourceDragEvent, .DragSourceDropEvent, .DragSourceEvent, .DragSourceListener, .DragSourceMotionListener, .DropTargetDragEvent, .DropTargetDropEvent, .DropTargetEvent, .InvalidDnDOperationException, java.awt.event.InputEvent, java.awt.image.BufferedImage, java.util.logging.Level, .Logger, javax.swing.Icon, .JColorChooser, .JFileChooser, .JList, .JTable, .JTree, javax.swing.text.JTextComponent

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

Modifier and TypeField and Description
protected static final int
COPY

Convenience to reference DnDConstants#ACTION_COPY.

pack-priv static final int
COPY_MASK

Modifier mask for a user-requested copy.

public static final float
DEFAULT_GHOST_ALPHA

Default transparency for ghosting.

private Component
private boolean
private GhostedDragImage
private float
private Point
pack-priv static final int
KEY_MASK

Modifier mask for any user-requested action.

private String
protected static final int
LINK

Convenience to reference DnDConstants#ACTION_LINK.

private static final Logger
public static final Dimension
MAX_GHOST_SIZE

Default maximum size for ghosted images.

private Dimension
private static int
protected static final int
MOVE

Convenience to reference DnDConstants#ACTION_MOVE.

pack-priv static final int
MOVE_MASK

Modifier mask for a user-requested move.

private boolean
protected static final int
NONE

Convenience to reference DnDConstants#ACTION_NONE.

pack-priv static final boolean
private int
private static Transferable
public static final int
UNKNOWN_MODIFIERS

getModifiers returns this value when the current modifiers state is unknown.

public static final Transferable
UNKNOWN_TRANSFERABLE

getTransferable returns this value when the current Transferable is unknown.

Constructor Summary

AccessConstructor and Description
protected
DragHandler(Component
source of the drag.
dragSource
,
int
actions which should be supported.
actions
)

Enable drags from the given component, supporting the actions in the given action mask.

Method Summary

Modifier and TypeMethod and Description
pack-priv static String
actionString(int action)

protected int

Returns:

effective drop action
adjustDropAction
(DragSourceEvent
event
ev
)

Pick a different drop action if the target doesn't support the current one and there are no modifiers.

protected boolean

Returns:

Whether to allow a drag
canDrag
(DragGestureEvent
event
e
)

Override to control whether a drag is started.

protected Image

Returns:

image based on the given icon.
createDragImage
(GraphicsConfiguration
current graphics configuration.
gc
,
Icon
Icon on which to base the drag image.
icon
)

Create an image from the given icon.

private void
private void
public void
dragDropEnd(DragSourceDropEvent
the DragSourceDropEvent
e
)

Implements java.awt.dnd.DragSourceListener.dragDropEnd.

This method is invoked to signify that the Drag and Drop operation is complete.
public void
dragEnter(DragSourceDragEvent
the DragSourceDragEvent
e
)

Implements java.awt.dnd.DragSourceListener.dragEnter.

Called as the cursor's hotspot enters a platform-dependent drop site.
public void
dragExit(DragSourceEvent
the DragSourceEvent
e
)

Implements java.awt.dnd.DragSourceListener.dragExit.

Called as the cursor's hotspot exits a platform-dependent drop site.
public void
dragGestureRecognized(DragGestureEvent
event
e
)

Implements java.awt.dnd.DragGestureListener.dragGestureRecognized.

Called when a user drag gesture is recognized.
public void
dragMouseMoved(DragSourceDragEvent
the DragSourceDragEvent
e
)

Implements java.awt.dnd.DragSourceMotionListener.dragMouseMoved.

Called whenever the mouse is moved during a drag operation.
public void
dragOver(DragSourceDragEvent
the DragSourceDragEvent
e
)

Implements java.awt.dnd.DragSourceListener.dragOver.

Called as the cursor's hotspot moves over a platform-dependent drop site.
protected void
dragStarted(DragGestureEvent
event
e
)

Override to perform any decoration of the target at the start of a drag, if desired.

public void
dropActionChanged(DragSourceDragEvent
the DragSourceDragEvent
e
)

Implements java.awt.dnd.DragSourceListener.dropActionChanged.

Called when the user has modified the drop gesture.
protected int

Returns:

subset of actions supported based on the input
getAcceptableDropAction
(int
current actions requested
targetActions
)

Returns the first available action supported by source and target.

protected Cursor
getCursorForAction(int actualAction)

protected Icon

Returns:

drag icon (defaults to none)
getDragIcon
(DragGestureEvent
event
e
,
Point
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.

srcOffset
)

Override this to provide a custom image.

protected int

Returns:

effective drop action
getDropAction
(DragSourceEvent
event
ev
)

Get the currently requested drop action.

private Point
pack-priv static int

Returns:

Current drag modifiers.
getModifiers
()

Used to communicate modifier state to DropHandler.

public static Transferable

Returns:

Transferable representation of the item being dragged.
getTransferable
(DropTargetEvent
event
e
)

Used to communicate the current Transferable during a drag, if available.

protected abstract Transferable

Returns:

Transferable representation of item being dragged.
getTransferable
(DragGestureEvent
event
e
)

Override to provide an appropriate Transferable representing the data being dragged.

private int
reduce(int actions)

Reduce a multiply-set bit mask to a single bit.

protected Icon

Returns:

Scaled Icon, or the original if there was no change.
scaleDragIcon
(Icon
Icon to be scaled.
icon
,
Point
Modified to account for the new icon's size.
imageOffset
)

Change the size of the given drag icon, if appropriate.

protected void
setModifiers(int
Current modifiers
mods
)

Update the modifiers hint.

protected void
updateCursor(DragSourceEvent
event
ev
)

Hook to update the cursor on various DragSourceEvent updates.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

COPYback to summary
protected static final int COPY

Convenience to reference DnDConstants#ACTION_COPY.

COPY_MASKback to summary
pack-priv static final int COPY_MASK

Modifier mask for a user-requested copy.

DEFAULT_GHOST_ALPHAback to summary
public static final float DEFAULT_GHOST_ALPHA

Default transparency for ghosting.

dragSourceback to summary
private Component dragSource
fixCursorback to summary
private boolean fixCursor
ghostback to summary
private GhostedDragImage ghost
ghostAlphaback to summary
private float ghostAlpha
imageOffsetback to summary
private Point imageOffset
KEY_MASKback to summary
pack-priv static final int KEY_MASK

Modifier mask for any user-requested action.

lastActionback to summary
private String lastAction
LOGback to summary
private static final Logger LOG
MAX_GHOST_SIZEback to summary
public static final Dimension MAX_GHOST_SIZE

Default maximum size for ghosted images.

maxGhostSizeback to summary
private Dimension maxGhostSize
modifiersback to summary
private static int modifiers
MOVEback to summary
protected static final int MOVE

Convenience to reference DnDConstants#ACTION_MOVE.

MOVE_MASKback to summary
pack-priv static final int MOVE_MASK

Modifier mask for a user-requested move.

movedback to summary
private boolean moved
NONEback to summary
protected static final int NONE

Convenience to reference DnDConstants#ACTION_NONE.

OSXback to summary
pack-priv static final boolean OSX
supportedActionsback to summary
private int supportedActions
transferableback to summary
private static Transferable transferable
UNKNOWN_MODIFIERSback to summary
public static final int UNKNOWN_MODIFIERS

getModifiers returns this value when the current modifiers state is unknown.

UNKNOWN_TRANSFERABLEback to summary
public static final Transferable UNKNOWN_TRANSFERABLE

getTransferable returns this value when the current Transferable is unknown.

Constructor Detail

DragHandlerback to summary
protected DragHandler(Component dragSource, int actions)

Enable drags from the given component, supporting the actions in the given action mask.

Parameters
dragSource:Component

source of the drag.

actions:int

actions which should be supported.

Method Detail

actionStringback to summary
pack-priv static String actionString(int action)
adjustDropActionback to summary
protected int adjustDropAction(DragSourceEvent ev)

Pick a different drop action if the target doesn't support the current one and there are no modifiers.

Parameters
ev:DragSourceEvent

event

Returns:int

effective drop action

canDragback to summary
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.

Parameters
e:DragGestureEvent

event

Returns:boolean

Whether to allow a drag

createDragImageback to summary
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.

Parameters
gc:GraphicsConfiguration

current graphics configuration.

icon:Icon

Icon on which to base the drag image.

Returns:Image

image based on the given icon.

describeback to summary
private void describe(String type, DragSourceEvent e)
disableSwingDragSupportback to summary
private void disableSwingDragSupport(Component comp)
dragDropEndback to summary
public void dragDropEnd(DragSourceDropEvent e)

Implements java.awt.dnd.DragSourceListener.dragDropEnd.

Doc from java.awt.dnd.DragSourceListener.dragDropEnd.

This method is invoked to signify that the Drag and Drop operation is complete. The getDropSuccess() method of the DragSourceDropEvent can be used to determine the termination state. The getDropAction() method returns the operation that the drop site selected to apply to the Drop operation. Once this method is complete, the current DragSourceContext and associated resources become invalid.

Parameters
e:DragSourceDropEvent

the DragSourceDropEvent

Annotations
@Override
dragEnterback to summary
public void dragEnter(DragSourceDragEvent e)

Implements java.awt.dnd.DragSourceListener.dragEnter.

Doc from java.awt.dnd.DragSourceListener.dragEnter.

Called as the cursor's hotspot enters a platform-dependent drop site. This method is invoked when all the following conditions are true:

  • The cursor's hotspot enters the operable part of a platform- dependent drop site.
  • The drop site is active.
  • The drop site accepts the drag.
Parameters
e:DragSourceDragEvent

the DragSourceDragEvent

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

Implements java.awt.dnd.DragSourceListener.dragExit.

Doc from java.awt.dnd.DragSourceListener.dragExit.

Called as the cursor's hotspot exits a platform-dependent drop site. This method is invoked when any of the following conditions are true:

  • The cursor's hotspot no longer intersects the operable part of the drop site associated with the previous dragEnter() invocation.
OR
  • The drop site associated with the previous dragEnter() invocation is no longer active.
OR
  • The drop site associated with the previous dragEnter() invocation has rejected the drag.
Parameters
e:DragSourceEvent

the DragSourceEvent

Annotations
@Override
dragGestureRecognizedback to summary
public void dragGestureRecognized(DragGestureEvent e)

Implements java.awt.dnd.DragGestureListener.dragGestureRecognized.

Called when a user drag gesture is recognized. This method is responsible for initiating the drag operation.

Parameters
e:DragGestureEvent

event

Annotations
@Override
dragMouseMovedback to summary
public void dragMouseMoved(DragSourceDragEvent e)

Implements java.awt.dnd.DragSourceMotionListener.dragMouseMoved.

Doc from java.awt.dnd.DragSourceMotionListener.dragMouseMoved.

Called whenever the mouse is moved during a drag operation.

Parameters
e:DragSourceDragEvent

the DragSourceDragEvent

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

Implements java.awt.dnd.DragSourceListener.dragOver.

Doc from java.awt.dnd.DragSourceListener.dragOver.

Called as the cursor's hotspot moves over a platform-dependent drop site. This method is invoked when all the following conditions are true:

  • The cursor's hotspot has moved, but still intersects the operable part of the drop site associated with the previous dragEnter() invocation.
  • The drop site is still active.
  • The drop site accepts the drag.
Parameters
e:DragSourceDragEvent

the DragSourceDragEvent

Annotations
@Override
dragStartedback to summary
protected void dragStarted(DragGestureEvent e)

Override to perform any decoration of the target at the start of a drag, if desired.

Parameters
e:DragGestureEvent

event

dropActionChangedback to summary
public void dropActionChanged(DragSourceDragEvent e)

Implements java.awt.dnd.DragSourceListener.dropActionChanged.

Doc from java.awt.dnd.DragSourceListener.dropActionChanged.

Called when the user has modified the drop gesture. This method is invoked when the state of the input device(s) that the user is interacting with changes. Such devices are typically the mouse buttons or keyboard modifiers that the user is interacting with.

Parameters
e:DragSourceDragEvent

the DragSourceDragEvent

Annotations
@Override
getAcceptableDropActionback to summary
protected int getAcceptableDropAction(int targetActions)

Returns the first available action supported by source and target.

Parameters
targetActions:int

current actions requested

Returns:int

subset of actions supported based on the input

getCursorForActionback to summary
protected Cursor getCursorForAction(int actualAction)
getDragIconback to summary
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
e:DragGestureEvent

event

srcOffset:Point

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.

Returns:Icon

drag icon (defaults to none)

getDropActionback to summary
protected int getDropAction(DragSourceEvent ev)

Get the currently requested drop action.

Parameters
ev:DragSourceEvent

event

Returns:int

effective drop action

getImageLocationback to summary
private Point getImageLocation(Point where)
getModifiersback to summary
pack-priv 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.

Returns:int

Current drag modifiers.

getTransferableback to summary
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.

Parameters
e:DropTargetEvent

event

Returns:Transferable

Transferable representation of the item being dragged.

getTransferableback to summary
protected abstract Transferable getTransferable(DragGestureEvent e)

Override to provide an appropriate Transferable representing the data being dragged.

Parameters
e:DragGestureEvent

event

Returns:Transferable

Transferable representation of item being dragged.

reduceback to summary
private int reduce(int actions)

Reduce a multiply-set bit mask to a single bit.

scaleDragIconback to summary
protected Icon scaleDragIcon(Icon icon, Point imageOffset)

Change the size of the given drag icon, if appropriate. When using a differently-sized drag icon, we also need to adjust the cursor offset within the icon.

Parameters
icon:Icon

Icon to be scaled.

imageOffset:Point

Modified to account for the new icon's size.

Returns:Icon

Scaled Icon, or the original if there was no change.

setModifiersback to summary
protected void setModifiers(int mods)

Update the modifiers hint.

Parameters
mods:int

Current modifiers

updateCursorback to summary
protected void updateCursor(DragSourceEvent ev)

Hook to update the cursor on various DragSourceEvent updates.

Parameters
ev:DragSourceEvent

event