Class DragSourceContext
- java.lang.Object
-
- java.awt.dnd.DragSourceContext
-
- All Implemented Interfaces:
- DragSourceListener, DragSourceMotionListener, Serializable, EventListener
public class DragSourceContext extends Object implements DragSourceListener, DragSourceMotionListener, Serializable
TheDragSourceContext
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 theTransferable
representing the source data for the drag operation.Note that the
DragSourceContext
itself implements theDragSourceListener
andDragSourceMotionListener
interfaces. This is to allow the platform peer (theDragSourceContextPeer
instance) created by theDragSource
to notify theDragSourceContext
of state changes in the ongoing operation. This allows theDragSourceContext
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(java.awt.Cursor)
method. When the default handling is disabled, it becomes the responsibility of the developer to keep the cursor up to date, by listening to theDragSource
events and calling thesetCursor()
method. Alternatively, you can provide custom cursor behavior by providing custom implementations of theDragSource
and theDragSourceContext
classes.- Since:
- 1.2
- See Also:
-
DragSourceListener
,DragSourceMotionListener
,DnDConstants
, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field and Description protected static int
CHANGED
Anint
used by updateCurrentCursor() indicating that the user operation has changed.protected static int
DEFAULT
Anint
used by updateCurrentCursor() indicating that theCursor
should change to the default (no drop)Cursor
.protected static int
ENTER
Anint
used by updateCurrentCursor() indicating that theCursor
has entered aDropTarget
.protected static int
OVER
Anint
used by updateCurrentCursor() indicating that theCursor
is over aDropTarget
.
-
Constructor Summary
Constructors Constructor and Description DragSourceContext(java.awt.dnd.peer.DragSourceContextPeer dscp, DragGestureEvent trigger, Cursor dragCursor, Image dragImage, Point offset, Transferable t, DragSourceListener dsl)
Called fromDragSource
, this constructor creates a newDragSourceContext
given theDragSourceContextPeer
for this Drag, theDragGestureEvent
that triggered the Drag, the initialCursor
to use for the Drag, an (optional)Image
to display while the Drag is taking place, the offset of theImage
origin from the hotspot at the instant of the triggering event, theTransferable
subject data, and theDragSourceListener
to use during the Drag and Drop operation.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description void
addDragSourceListener(DragSourceListener dsl)
Add aDragSourceListener
to thisDragSourceContext
if one has not already been added.void
dragDropEnd(DragSourceDropEvent dsde)
CallsdragDropEnd
on theDragSourceListener
s registered with thisDragSourceContext
and with the associatedDragSource
, and passes them the specifiedDragSourceDropEvent
.void
dragEnter(DragSourceDragEvent dsde)
CallsdragEnter
on theDragSourceListener
s registered with thisDragSourceContext
and with the associatedDragSource
, and passes them the specifiedDragSourceDragEvent
.void
dragExit(DragSourceEvent dse)
CallsdragExit
on theDragSourceListener
s registered with thisDragSourceContext
and with the associatedDragSource
, and passes them the specifiedDragSourceEvent
.void
dragMouseMoved(DragSourceDragEvent dsde)
CallsdragMouseMoved
on theDragSourceMotionListener
s registered with theDragSource
associated with thisDragSourceContext
, and them passes the specifiedDragSourceDragEvent
.void
dragOver(DragSourceDragEvent dsde)
CallsdragOver
on theDragSourceListener
s registered with thisDragSourceContext
and with the associatedDragSource
, and passes them the specifiedDragSourceDragEvent
.void
dropActionChanged(DragSourceDragEvent dsde)
CallsdropActionChanged
on theDragSourceListener
s registered with thisDragSourceContext
and with the associatedDragSource
, and passes them the specifiedDragSourceDragEvent
.Component
getComponent()
Returns theComponent
associated with thisDragSourceContext
.Cursor
getCursor()
Returns the current dragCursor
.DragSource
getDragSource()
Returns theDragSource
that instantiated thisDragSourceContext
.int
getSourceActions()
Returns a bitwise mask ofDnDConstants
that represent the set of drop actions supported by the drag source for the drag operation associated with thisDragSourceContext
.Transferable
getTransferable()
Returns theTransferable
associated with thisDragSourceContext
.DragGestureEvent
getTrigger()
Returns theDragGestureEvent
that initially triggered the drag.void
removeDragSourceListener(DragSourceListener dsl)
Removes the specifiedDragSourceListener
from thisDragSourceContext
.void
setCursor(Cursor c)
Sets the cursor for this drag operation to the specifiedCursor
.void
transferablesFlavorsChanged()
Notifies the peer that theTransferable
'sDataFlavor
s have changed.protected 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.
-
-
-
Field Detail
DEFAULT
protected static final int DEFAULT
Anint
used by updateCurrentCursor() indicating that theCursor
should change to the default (no drop)Cursor
.- See Also:
- Constant Field Values
ENTER
protected static final int ENTER
Anint
used by updateCurrentCursor() indicating that theCursor
has entered aDropTarget
.- See Also:
- Constant Field Values
OVER
protected static final int OVER
Anint
used by updateCurrentCursor() indicating that theCursor
is over aDropTarget
.- See Also:
- Constant Field Values
CHANGED
protected static final int CHANGED
Anint
used by updateCurrentCursor() indicating that the user operation has changed.- See Also:
- Constant Field Values
-
Constructor Detail
DragSourceContext
public DragSourceContext(java.awt.dnd.peer.DragSourceContextPeer dscp, DragGestureEvent trigger, Cursor dragCursor, Image dragImage, Point offset, Transferable t, DragSourceListener dsl)
Called fromDragSource
, this constructor creates a newDragSourceContext
given theDragSourceContextPeer
for this Drag, theDragGestureEvent
that triggered the Drag, the initialCursor
to use for the Drag, an (optional)Image
to display while the Drag is taking place, the offset of theImage
origin from the hotspot at the instant of the triggering event, theTransferable
subject data, and theDragSourceListener
to use during the Drag and Drop operation.
IfDragSourceContextPeer
isnull
NullPointerException
is thrown.
IfDragGestureEvent
isnull
NullPointerException
is thrown.
IfCursor
isnull
no exception is thrown and the default drag cursor behavior is activated for this drag operation.
IfImage
isnull
no exception is thrown.
IfImage
is notnull
and the offset isnull
NullPointerException
is thrown.
IfTransferable
isnull
NullPointerException
is thrown.
IfDragSourceListener
isnull
no exception is thrown.- Parameters:
-
dscp
- theDragSourceContextPeer
for this drag -
trigger
- the triggering event -
dragCursor
- the initialCursor
for this drag operation ornull
for the default cursor handling; see class level documentation for more details on the cursor handling mechanism during drag and drop -
dragImage
- theImage
to drag (ornull
) -
offset
- the offset of the image origin from the hotspot at the instant of the triggering event -
t
- theTransferable
-
dsl
- theDragSourceListener
- Throws:
-
IllegalArgumentException
- if theComponent
associated with the trigger event isnull
. -
IllegalArgumentException
- if theDragSource
for the trigger event isnull
. -
IllegalArgumentException
- if the drag action for the trigger event isDnDConstants.ACTION_NONE
. -
IllegalArgumentException
- if the source actions for theDragGestureRecognizer
associated with the trigger event are equal toDnDConstants.ACTION_NONE
. -
NullPointerException
- if dscp, trigger, or t are null, or if dragImage is non-null and offset is null
-
Method Detail
getDragSource
public DragSource getDragSource()
Returns theDragSource
that instantiated thisDragSourceContext
.- Returns:
-
the
DragSource
that instantiated thisDragSourceContext
getComponent
public Component getComponent()
Returns theComponent
associated with thisDragSourceContext
.- Returns:
-
the
Component
that started the drag
getTrigger
public DragGestureEvent getTrigger()
Returns theDragGestureEvent
that initially triggered the drag.- Returns:
- the Event that triggered the drag
getSourceActions
public int getSourceActions()
Returns a bitwise mask ofDnDConstants
that represent the set of drop actions supported by the drag source for the drag operation associated with thisDragSourceContext
.- Returns:
- the drop actions supported by the drag source
setCursor
public void setCursor(Cursor c)
Sets the cursor for this drag operation to the specifiedCursor
. If the specifiedCursor
isnull
, the default drag cursor behavior is activated for this drag operation, otherwise it is deactivated.- Parameters:
-
c
- the initialCursor
for this drag operation, ornull
for the default cursor handling; see class level documentation for more details on the cursor handling during drag and drop
getCursor
public Cursor getCursor()
Returns the current dragCursor
.- Returns:
-
the current drag
Cursor
addDragSourceListener
public void addDragSourceListener(DragSourceListener dsl) throws TooManyListenersException
Add aDragSourceListener
to thisDragSourceContext
if one has not already been added. If aDragSourceListener
already exists, this method throws aTooManyListenersException
.- Parameters:
-
dsl
- theDragSourceListener
to add. Note that whilenull
is not prohibited, it is not acceptable as a parameter. - Throws:
-
TooManyListenersException
- if aDragSourceListener
has already been added
removeDragSourceListener
public void removeDragSourceListener(DragSourceListener dsl)
Removes the specifiedDragSourceListener
from thisDragSourceContext
.- Parameters:
-
dsl
- theDragSourceListener
to remove; note that whilenull
is not prohibited, it is not acceptable as a parameter
transferablesFlavorsChanged
public void transferablesFlavorsChanged()
Notifies the peer that theTransferable
'sDataFlavor
s have changed.
dragEnter
public void dragEnter(DragSourceDragEvent dsde)
CallsdragEnter
on theDragSourceListener
s registered with thisDragSourceContext
and with the associatedDragSource
, and passes them the specifiedDragSourceDragEvent
.- Specified by:
-
dragEnter
in interfaceDragSourceListener
- Parameters:
-
dsde
- theDragSourceDragEvent
dragOver
public void dragOver(DragSourceDragEvent dsde)
CallsdragOver
on theDragSourceListener
s registered with thisDragSourceContext
and with the associatedDragSource
, and passes them the specifiedDragSourceDragEvent
.- Specified by:
-
dragOver
in interfaceDragSourceListener
- Parameters:
-
dsde
- theDragSourceDragEvent
dragExit
public void dragExit(DragSourceEvent dse)
CallsdragExit
on theDragSourceListener
s registered with thisDragSourceContext
and with the associatedDragSource
, and passes them the specifiedDragSourceEvent
.- Specified by:
-
dragExit
in interfaceDragSourceListener
- Parameters:
-
dse
- theDragSourceEvent
dropActionChanged
public void dropActionChanged(DragSourceDragEvent dsde)
CallsdropActionChanged
on theDragSourceListener
s registered with thisDragSourceContext
and with the associatedDragSource
, and passes them the specifiedDragSourceDragEvent
.- Specified by:
-
dropActionChanged
in interfaceDragSourceListener
- Parameters:
-
dsde
- theDragSourceDragEvent
dragDropEnd
public void dragDropEnd(DragSourceDropEvent dsde)
CallsdragDropEnd
on theDragSourceListener
s registered with thisDragSourceContext
and with the associatedDragSource
, and passes them the specifiedDragSourceDropEvent
.- Specified by:
-
dragDropEnd
in interfaceDragSourceListener
- Parameters:
-
dsde
- theDragSourceDropEvent
dragMouseMoved
public void dragMouseMoved(DragSourceDragEvent dsde)
CallsdragMouseMoved
on theDragSourceMotionListener
s registered with theDragSource
associated with thisDragSourceContext
, and them passes the specifiedDragSourceDragEvent
.- Specified by:
-
dragMouseMoved
in interfaceDragSourceMotionListener
- Parameters:
-
dsde
- theDragSourceDragEvent
- Since:
- 1.4
getTransferable
public Transferable getTransferable()
Returns theTransferable
associated with thisDragSourceContext
.- Returns:
-
the
Transferable
updateCurrentCursor
protected 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
- the actions supported by the drag source -
targetAct
- the drop target action -
status
- one of the fieldsDEFAULT
,ENTER
,OVER
,CHANGED
-
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2022, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.