Top Description Fields Constructors Methods
com.sun.jna.examples.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
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, 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.

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 dragSource, int 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
adjustDropAction(DragSourceEvent ev)

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

protected boolean
canDrag(DragGestureEvent e)

Override to control whether a drag is started.

protected Image
createDragImage(GraphicsConfiguration gc, Icon 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
the DragGestureEvent describing the gesture that has just occurred
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 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
getAcceptableDropAction(int targetActions)

Returns the first available action supported by source and target.

protected Cursor
getCursorForAction(int actualAction)

protected Icon
getDragIcon(DragGestureEvent 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
getDropAction(DragSourceEvent ev)

Get the currently requested drop action.

private Point
pack-priv static int
getModifiers()

Used to communicate modifier state to DropHandler.

public static Transferable
getTransferable(DropTargetEvent e)

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

protected abstract Transferable
getTransferable(DragGestureEvent 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
scaleDragIcon(Icon icon, Point imageOffset)

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

protected void
setModifiers(int mods)

Update the modifiers hint.

protected void
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
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.

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.

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.

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.

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

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

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

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

Doc from java.awt.dnd.DragGestureListener.dragGestureRecognized.

the DragGestureEvent describing the gesture that has just occurred

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

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

dragStartedback to summary
protected void dragStarted(DragGestureEvent e)

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

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

getAcceptableDropActionback to summary
protected int getAcceptableDropAction(int targetActions)

Returns the first available action supported by source and target.

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

getDropActionback to summary
protected int getDropAction(DragSourceEvent ev)

Get the currently requested 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.

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.

getTransferableback to summary
protected abstract Transferable getTransferable(DragGestureEvent e)

Override to provide an appropriate Transferable representing the data 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)

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.

setModifiersback to summary
protected void setModifiers(int mods)

Update the modifiers hint.

updateCursorback to summary
protected void updateCursor(DragSourceEvent ev)