APIdia Launch Partner
Top Description Fields Constructors Methods
java.awt.dnd

public Class DragSourceContext

extends Object
implements DragSourceListener, DragSourceMotionListener, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.awt.dnd.DragSourceMotionListener, java.util.EventListener, java.awt.dnd.DragSourceListener
Imports
java.awt.AWTError, .Component, .Cursor, .Image, .Point, .Toolkit, java.awt.datatransfer.DataFlavor, .Transferable, .UnsupportedFlavorException, java.awt.dnd.peer.DragSourceContextPeer, java.io.IOException, .InvalidObjectException, .ObjectInputStream, .ObjectOutputStream, .Serial, .Serializable, java.util.TooManyListenersException, sun.awt.AWTAccessor, .ComponentFactory

The DragSourceContext class is responsible for managing the initiator side of the Drag and Drop protocol. In particular, it is responsible for managing drag event notifications to the DragSourceListeners and DragSourceMotionListeners, and providing the Transferable representing the source data for the drag operation.

Note that the DragSourceContext itself implements the DragSourceListener and DragSourceMotionListener interfaces. This is to allow the platform peer (the DragSourceContextPeer instance) created by the DragSource to notify the DragSourceContext of state changes in the ongoing operation. This allows the DragSourceContext object to interpose itself between the platform and the listeners provided by the initiator of the drag operation.

By default, DragSourceContext sets the cursor as appropriate for the current state of the drag and drop operation. For example, if the user has chosen the move action, and the pointer is over a target that accepts the move action, the default move cursor is shown. When the pointer is over an area that does not accept the transfer, the default "no drop" cursor is shown.

This default handling mechanism is disabled when a custom cursor is set by the setCursor method. When the default handling is disabled, it becomes the responsibility of the developer to keep the cursor up to date, by listening to the DragSource events and calling the setCursor() method. Alternatively, you can provide custom cursor behavior by providing custom implementations of the DragSource and the DragSourceContext classes.

Since
1.2
See Also
DragSourceListener, DragSourceMotionListener, DnDConstants

Field Summary

Modifier and TypeField and Description
protected static final int
CHANGED

An int used by updateCurrentCursor() indicating that the user operation has changed.

private Cursor
cursor

The current drag cursor.

protected static final int
DEFAULT

An int used by updateCurrentCursor() indicating that the Cursor should change to the default (no drop) Cursor.

private static Transferable
protected static final int
ENTER

An int used by updateCurrentCursor() indicating that the Cursor has entered a DropTarget.

private transient DragSourceListener
protected static final int
OVER

An int used by updateCurrentCursor() indicating that the Cursor is over a DropTarget.

private final transient DragSourceContextPeer
private static final long
serialVersionUID

Use serialVersionUID from JDK 1.4 for interoperability.

private int
sourceActions

A bitwise mask of DnDConstants that represents the set of drop actions supported by the drag source for the drag operation associated with this DragSourceContext.

private transient Transferable
private DragGestureEvent
trigger

The event which triggered the start of the drag.

private boolean
useCustomCursor

true if the custom drag cursor is used instead of the default one.

Constructor Summary

AccessConstructor and Description
public
DragSourceContext(DragGestureEvent
the triggering event
trigger
,
Cursor
the initial Cursor for this drag operation or null for the default cursor handling; see class level documentation for more details on the cursor handling mechanism during drag and drop
dragCursor
,
Image
the Image to drag (or null)
dragImage
,
Point
the offset of the image origin from the hotspot at the instant of the triggering event
offset
,
Transferable
the Transferable
t
,
DragSourceListener
the DragSourceListener
dsl
)

Called from DragSource, this constructor creates a new DragSourceContext given the DragSourceContextPeer for this Drag, the DragGestureEvent that triggered the Drag, the initial Cursor to use for the Drag, an (optional) Image to display while the Drag is taking place, the offset of the Image origin from the hotspot at the instant of the triggering event, the Transferable subject data, and the DragSourceListener to use during the Drag and Drop operation.

Method Summary

Modifier and TypeMethod and Description
public synchronized void
addDragSourceListener(DragSourceListener
the DragSourceListener to add. Note that while null is not prohibited, it is not acceptable as a parameter.
dsl
)

Add a DragSourceListener to this DragSourceContext if one has not already been added.

public void
dragDropEnd(DragSourceDropEvent
the DragSourceDropEvent
dsde
)

Implements java.awt.dnd.DragSourceListener.dragDropEnd.

Calls dragDropEnd on the DragSourceListeners registered with this DragSourceContext and with the associated DragSource, and passes them the specified DragSourceDropEvent.

public void
dragEnter(DragSourceDragEvent
the DragSourceDragEvent
dsde
)

Implements java.awt.dnd.DragSourceListener.dragEnter.

Calls dragEnter on the DragSourceListeners registered with this DragSourceContext and with the associated DragSource, and passes them the specified DragSourceDragEvent.

public void
dragExit(DragSourceEvent
the DragSourceEvent
dse
)

Implements java.awt.dnd.DragSourceListener.dragExit.

Calls dragExit on the DragSourceListeners registered with this DragSourceContext and with the associated DragSource, and passes them the specified DragSourceEvent.

public void
dragMouseMoved(DragSourceDragEvent
the DragSourceDragEvent
dsde
)

Implements java.awt.dnd.DragSourceMotionListener.dragMouseMoved.

Calls dragMouseMoved on the DragSourceMotionListeners registered with the DragSource associated with this DragSourceContext, and them passes the specified DragSourceDragEvent.

public void
dragOver(DragSourceDragEvent
the DragSourceDragEvent
dsde
)

Implements java.awt.dnd.DragSourceListener.dragOver.

Calls dragOver on the DragSourceListeners registered with this DragSourceContext and with the associated DragSource, and passes them the specified DragSourceDragEvent.

public void
dropActionChanged(DragSourceDragEvent
the DragSourceDragEvent
dsde
)

Implements java.awt.dnd.DragSourceListener.dropActionChanged.

Calls dropActionChanged on the DragSourceListeners registered with this DragSourceContext and with the associated DragSource, and passes them the specified DragSourceDragEvent.

public Component

Returns:

the Component that started the drag
getComponent
()

Returns the Component associated with this DragSourceContext.

public Cursor

Returns:

the current custom drag Cursor, if it was set otherwise returns null.
getCursor
()

Returns the current custom drag Cursor.

public DragSource

Returns:

the DragSource that instantiated this DragSourceContext
getDragSource
()

Returns the DragSource that instantiated this DragSourceContext.

public int

Returns:

the drop actions supported by the drag source
getSourceActions
()

Returns a bitwise mask of DnDConstants that represent the set of drop actions supported by the drag source for the drag operation associated with this DragSourceContext.

public Transferable

Returns:

the Transferable
getTransferable
()

Returns the Transferable associated with this DragSourceContext.

public DragGestureEvent

Returns:

the Event that triggered the drag
getTrigger
()

Returns the DragGestureEvent that initially triggered the drag.

private void
readObject(ObjectInputStream
the ObjectInputStream to read
s
)

Deserializes this DragSourceContext.

public synchronized void
removeDragSourceListener(DragSourceListener
the DragSourceListener to remove; note that while null is not prohibited, it is not acceptable as a parameter
dsl
)

Removes the specified DragSourceListener from this DragSourceContext.

public synchronized void
setCursor(Cursor
the initial Cursor for this drag operation, or null for the default cursor handling; see class level documentation for more details on the cursor handling during drag and drop
c
)

Sets the custom cursor for this drag operation to the specified Cursor.

private void
public void
transferablesFlavorsChanged()

Notifies the peer that the Transferable's DataFlavors have changed.

protected synchronized void
updateCurrentCursor(int
the actions supported by the drag source
sourceAct
,
int
the drop target action
targetAct
,
int
one of the fields DEFAULT, ENTER, OVER, CHANGED
status
)

If the default drag cursor behavior is active, this method sets the default drag cursor for the specified actions supported by the drag source, the drop target action, and status, otherwise this method does nothing.

private void
writeObject(ObjectOutputStream
the ObjectOutputStream to write
s
)

Serializes this DragSourceContext.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CHANGEDback to summary
protected static final int CHANGED

An int used by updateCurrentCursor() indicating that the user operation has changed.

cursorback to summary
private Cursor cursor

The current drag cursor.

DEFAULTback to summary
protected static final int DEFAULT

An int used by updateCurrentCursor() indicating that the Cursor should change to the default (no drop) Cursor.

emptyTransferableback to summary
private static Transferable emptyTransferable
ENTERback to summary
protected static final int ENTER

An int used by updateCurrentCursor() indicating that the Cursor has entered a DropTarget.

listenerback to summary
private transient DragSourceListener listener
OVERback to summary
protected static final int OVER

An int used by updateCurrentCursor() indicating that the Cursor is over a DropTarget.

peerback to summary
private final transient DragSourceContextPeer peer
serialVersionUIDback to summary
private static final long serialVersionUID

Use serialVersionUID from JDK 1.4 for interoperability.

Annotations
@Serial
sourceActionsback to summary
private int sourceActions

A bitwise mask of DnDConstants that represents the set of drop actions supported by the drag source for the drag operation associated with this DragSourceContext.

transferableback to summary
private transient Transferable transferable
triggerback to summary
private DragGestureEvent trigger

The event which triggered the start of the drag.

useCustomCursorback to summary
private boolean useCustomCursor

true if the custom drag cursor is used instead of the default one.

Constructor Detail

DragSourceContextback to summary
public DragSourceContext(DragGestureEvent trigger, Cursor dragCursor, Image dragImage, Point offset, Transferable t, DragSourceListener dsl)

Called from DragSource, this constructor creates a new DragSourceContext given the DragSourceContextPeer for this Drag, the DragGestureEvent that triggered the Drag, the initial Cursor to use for the Drag, an (optional) Image to display while the Drag is taking place, the offset of the Image origin from the hotspot at the instant of the triggering event, the Transferable subject data, and the DragSourceListener to use during the Drag and Drop operation.
If DragSourceContextPeer is null NullPointerException is thrown.
If DragGestureEvent is null NullPointerException is thrown.
If Cursor is null no exception is thrown and the default drag cursor behavior is activated for this drag operation.
If Image is null no exception is thrown.
If Image is not null and the offset is null NullPointerException is thrown.
If Transferable is null NullPointerException is thrown.
If DragSourceListener is null no exception is thrown.

Parameters
trigger:DragGestureEvent

the triggering event

dragCursor:Cursor

the initial Cursor for this drag operation or null for the default cursor handling; see class level documentation for more details on the cursor handling mechanism during drag and drop

dragImage:Image

the Image to drag (or null)

offset:Point

the offset of the image origin from the hotspot at the instant of the triggering event

t:Transferable

the Transferable

dsl:DragSourceListener

the DragSourceListener

Exceptions
IllegalArgumentException:
  • if the Component associated with the trigger event is null.
  • if the DragSource for the trigger event is null.
  • if the drag action for the trigger event is DnDConstants.ACTION_NONE.
  • if the source actions for the DragGestureRecognizer associated with the trigger event are equal to DnDConstants.ACTION_NONE.
NullPointerException:
if dscp, trigger, or t are null, or if dragImage is non-null and offset is null

Method Detail

addDragSourceListenerback to summary
public synchronized void addDragSourceListener(DragSourceListener dsl) throws TooManyListenersException

Add a DragSourceListener to this DragSourceContext if one has not already been added. If a DragSourceListener already exists, this method throws a TooManyListenersException.

Parameters
dsl:DragSourceListener

the DragSourceListener to add. Note that while null is not prohibited, it is not acceptable as a parameter.

Exceptions
TooManyListenersException:
if a DragSourceListener has already been added
dragDropEndback to summary
public void dragDropEnd(DragSourceDropEvent dsde)

Implements java.awt.dnd.DragSourceListener.dragDropEnd.

Calls dragDropEnd on the DragSourceListeners registered with this DragSourceContext and with the associated DragSource, and passes them the specified DragSourceDropEvent.

Parameters
dsde:DragSourceDropEvent

the DragSourceDropEvent

dragEnterback to summary
public void dragEnter(DragSourceDragEvent dsde)

Implements java.awt.dnd.DragSourceListener.dragEnter.

Calls dragEnter on the DragSourceListeners registered with this DragSourceContext and with the associated DragSource, and passes them the specified DragSourceDragEvent.

Parameters
dsde:DragSourceDragEvent

the DragSourceDragEvent

dragExitback to summary
public void dragExit(DragSourceEvent dse)

Implements java.awt.dnd.DragSourceListener.dragExit.

Calls dragExit on the DragSourceListeners registered with this DragSourceContext and with the associated DragSource, and passes them the specified DragSourceEvent.

Parameters
dse:DragSourceEvent

the DragSourceEvent

dragMouseMovedback to summary
public void dragMouseMoved(DragSourceDragEvent dsde)

Implements java.awt.dnd.DragSourceMotionListener.dragMouseMoved.

Calls dragMouseMoved on the DragSourceMotionListeners registered with the DragSource associated with this DragSourceContext, and them passes the specified DragSourceDragEvent.

Parameters
dsde:DragSourceDragEvent

the DragSourceDragEvent

Since
1.4
dragOverback to summary
public void dragOver(DragSourceDragEvent dsde)

Implements java.awt.dnd.DragSourceListener.dragOver.

Calls dragOver on the DragSourceListeners registered with this DragSourceContext and with the associated DragSource, and passes them the specified DragSourceDragEvent.

Parameters
dsde:DragSourceDragEvent

the DragSourceDragEvent

dropActionChangedback to summary
public void dropActionChanged(DragSourceDragEvent dsde)

Implements java.awt.dnd.DragSourceListener.dropActionChanged.

Calls dropActionChanged on the DragSourceListeners registered with this DragSourceContext and with the associated DragSource, and passes them the specified DragSourceDragEvent.

Parameters
dsde:DragSourceDragEvent

the DragSourceDragEvent

getComponentback to summary
public Component getComponent()

Returns the Component associated with this DragSourceContext.

Returns:Component

the Component that started the drag

getCursorback to summary
public Cursor getCursor()

Returns the current custom drag Cursor.

Returns:Cursor

the current custom drag Cursor, if it was set otherwise returns null.

See Also
setCursor
getDragSourceback to summary
public DragSource getDragSource()

Returns the DragSource that instantiated this DragSourceContext.

Returns:DragSource

the DragSource that instantiated this DragSourceContext

getSourceActionsback to summary
public int getSourceActions()

Returns a bitwise mask of DnDConstants that represent the set of drop actions supported by the drag source for the drag operation associated with this DragSourceContext.

Returns:int

the drop actions supported by the drag source

getTransferableback to summary
public Transferable getTransferable()

Returns the Transferable associated with this DragSourceContext.

Returns:Transferable

the Transferable

getTriggerback to summary
public DragGestureEvent getTrigger()

Returns the DragGestureEvent that initially triggered the drag.

Returns:DragGestureEvent

the Event that triggered the drag

readObjectback to summary
private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException

Deserializes this DragSourceContext. This method first performs default deserialization for all non-transient fields. This object's Transferable and DragSourceListener are then deserialized as well by using the next two objects in the stream. If the resulting Transferable is null, this object's Transferable is set to a dummy Transferable which supports no DataFlavors.

Parameters
s:ObjectInputStream

the ObjectInputStream to read

Annotations
@Serial
Exceptions
ClassNotFoundException:
if the class of a serialized object could not be found
IOException:
if an I/O error occurs
Since
1.4
removeDragSourceListenerback to summary
public synchronized void removeDragSourceListener(DragSourceListener dsl)

Removes the specified DragSourceListener from this DragSourceContext.

Parameters
dsl:DragSourceListener

the DragSourceListener to remove; note that while null is not prohibited, it is not acceptable as a parameter

setCursorback to summary
public synchronized void setCursor(Cursor c)

Sets the custom cursor for this drag operation to the specified Cursor. If the specified Cursor is null, the default drag cursor behavior is activated for this drag operation, otherwise it is deactivated.

Parameters
c:Cursor

the initial Cursor for this drag operation, or null for the default cursor handling; see class level documentation for more details on the cursor handling during drag and drop

setCursorImplback to summary
private void setCursorImpl(Cursor c)
transferablesFlavorsChangedback to summary
public void transferablesFlavorsChanged()

Notifies the peer that the Transferable's DataFlavors have changed.

updateCurrentCursorback to summary
protected synchronized void updateCurrentCursor(int sourceAct, int targetAct, int status)

If the default drag cursor behavior is active, this method sets the default drag cursor for the specified actions supported by the drag source, the drop target action, and status, otherwise this method does nothing.

Parameters
sourceAct:int

the actions supported by the drag source

targetAct:int

the drop target action

status:int

one of the fields DEFAULT, ENTER, OVER, CHANGED

Annotations
@SuppressWarnings:fallthrough
writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException

Serializes this DragSourceContext. This method first performs default serialization. Next, this object's Transferable is written out if and only if it can be serialized. If not, null is written instead. In this case, a DragSourceContext created from the resulting deserialized stream will contain a dummy Transferable which supports no DataFlavors. Finally, this object's DragSourceListener is written out if and only if it can be serialized. If not, null is written instead.

Parameters
s:ObjectOutputStream

the ObjectOutputStream to write

Annotations
@Serial
Exceptions
IOException:
if an I/O error occurs
Since
1.4
Serial data
The default serializable fields, in alphabetical order, followed by either a Transferable instance, or null, followed by either a DragSourceListener instance, or null.