- All Implemented Interfaces:
- DragSourceListener,- DragSourceMotionListener,- Serializable,- EventListener
public class DragSourceContext extends Object implements DragSourceListener, DragSourceMotionListener, Serializable
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(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 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, Serialized Form
- 
Field SummaryFields Modifier and Type Field Description protected static intCHANGEDAnintused by updateCurrentCursor() indicating that the user operation has changed.protected static intDEFAULTAnintused by updateCurrentCursor() indicating that theCursorshould change to the default (no drop)Cursor.protected static intENTERAnintused by updateCurrentCursor() indicating that theCursorhas entered aDropTarget.protected static intOVERAnintused by updateCurrentCursor() indicating that theCursoris over aDropTarget.
- 
Constructor SummaryConstructors Constructor Description DragSourceContext(DragGestureEvent trigger, Cursor dragCursor, Image dragImage, Point offset, Transferable t, DragSourceListener dsl)Called fromDragSource, this constructor creates a newDragSourceContextgiven theDragSourceContextPeerfor this Drag, theDragGestureEventthat triggered the Drag, the initialCursorto use for the Drag, an (optional)Imageto display while the Drag is taking place, the offset of theImageorigin from the hotspot at the instant of the triggering event, theTransferablesubject data, and theDragSourceListenerto use during the Drag and Drop operation.
- 
Method SummaryModifier and Type Method Description voidaddDragSourceListener(DragSourceListener dsl)Add aDragSourceListenerto thisDragSourceContextif one has not already been added.voiddragDropEnd(DragSourceDropEvent dsde)CallsdragDropEndon theDragSourceListeners registered with thisDragSourceContextand with the associatedDragSource, and passes them the specifiedDragSourceDropEvent.voiddragEnter(DragSourceDragEvent dsde)CallsdragEnteron theDragSourceListeners registered with thisDragSourceContextand with the associatedDragSource, and passes them the specifiedDragSourceDragEvent.voiddragExit(DragSourceEvent dse)CallsdragExiton theDragSourceListeners registered with thisDragSourceContextand with the associatedDragSource, and passes them the specifiedDragSourceEvent.voiddragMouseMoved(DragSourceDragEvent dsde)CallsdragMouseMovedon theDragSourceMotionListeners registered with theDragSourceassociated with thisDragSourceContext, and them passes the specifiedDragSourceDragEvent.voiddragOver(DragSourceDragEvent dsde)CallsdragOveron theDragSourceListeners registered with thisDragSourceContextand with the associatedDragSource, and passes them the specifiedDragSourceDragEvent.voiddropActionChanged(DragSourceDragEvent dsde)CallsdropActionChangedon theDragSourceListeners registered with thisDragSourceContextand with the associatedDragSource, and passes them the specifiedDragSourceDragEvent.ComponentgetComponent()Returns theComponentassociated with thisDragSourceContext.CursorgetCursor()Returns the current custom dragCursor.DragSourcegetDragSource()Returns theDragSourcethat instantiated thisDragSourceContext.intgetSourceActions()Returns a bitwise mask ofDnDConstantsthat represent the set of drop actions supported by the drag source for the drag operation associated with thisDragSourceContext.TransferablegetTransferable()Returns theTransferableassociated with thisDragSourceContext.DragGestureEventgetTrigger()Returns theDragGestureEventthat initially triggered the drag.voidremoveDragSourceListener(DragSourceListener dsl)Removes the specifiedDragSourceListenerfrom thisDragSourceContext.voidsetCursor(Cursor c)Sets the custom cursor for this drag operation to the specifiedCursor.voidtransferablesFlavorsChanged()Notifies the peer that theTransferable'sDataFlavors have changed.protected voidupdateCurrentCursor(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 Details- 
DEFAULTprotected static final int DEFAULTAnintused by updateCurrentCursor() indicating that theCursorshould change to the default (no drop)Cursor.- See Also:
- Constant Field Values
 
- 
ENTERprotected static final int ENTERAnintused by updateCurrentCursor() indicating that theCursorhas entered aDropTarget.- See Also:
- Constant Field Values
 
- 
OVERprotected static final int OVERAnintused by updateCurrentCursor() indicating that theCursoris over aDropTarget.- See Also:
- Constant Field Values
 
- 
CHANGEDprotected static final int CHANGEDAnintused by updateCurrentCursor() indicating that the user operation has changed.- See Also:
- Constant Field Values
 
 
- 
- 
Constructor Details- 
DragSourceContextpublic DragSourceContext(DragGestureEvent trigger, Cursor dragCursor, Image dragImage, Point offset, Transferable t, DragSourceListener dsl)Called fromDragSource, this constructor creates a newDragSourceContextgiven theDragSourceContextPeerfor this Drag, theDragGestureEventthat triggered the Drag, the initialCursorto use for the Drag, an (optional)Imageto display while the Drag is taking place, the offset of theImageorigin from the hotspot at the instant of the triggering event, theTransferablesubject data, and theDragSourceListenerto use during the Drag and Drop operation.
 IfDragSourceContextPeerisnullNullPointerExceptionis thrown.
 IfDragGestureEventisnullNullPointerExceptionis thrown.
 IfCursorisnullno exception is thrown and the default drag cursor behavior is activated for this drag operation.
 IfImageisnullno exception is thrown.
 IfImageis notnulland the offset isnull NullPointerExceptionis thrown.
 IfTransferableisnullNullPointerExceptionis thrown.
 IfDragSourceListenerisnullno exception is thrown.- Parameters:
- trigger- the triggering event
- dragCursor- the initial- Cursorfor this drag operation or- nullfor the default cursor handling; see class level documentation for more details on the cursor handling mechanism during drag and drop
- dragImage- the- Imageto drag (or- null)
- offset- the offset of the image origin from the hotspot at the instant of the triggering event
- t- the- Transferable
- dsl- the- DragSourceListener
- Throws:
- IllegalArgumentException- if the- Componentassociated with the trigger event is- null.
- IllegalArgumentException- if the- DragSourcefor the trigger event is- null.
- IllegalArgumentException- if the drag action for the trigger event is- DnDConstants.ACTION_NONE.
- IllegalArgumentException- if the source actions for the- DragGestureRecognizerassociated 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 Details- 
getDragSourceReturns theDragSourcethat instantiated thisDragSourceContext.- Returns:
- the DragSourcethat instantiated thisDragSourceContext
 
- 
getComponentReturns theComponentassociated with thisDragSourceContext.- Returns:
- the Componentthat started the drag
 
- 
getTriggerReturns theDragGestureEventthat initially triggered the drag.- Returns:
- the Event that triggered the drag
 
- 
getSourceActionspublic int getSourceActions()Returns a bitwise mask ofDnDConstantsthat 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
 
- 
setCursorSets the custom cursor for this drag operation to the specifiedCursor. If the specifiedCursorisnull, the default drag cursor behavior is activated for this drag operation, otherwise it is deactivated.- Parameters:
- c- the initial- Cursorfor this drag operation, or- nullfor the default cursor handling; see class level documentation for more details on the cursor handling during drag and drop
 
- 
getCursorReturns the current custom dragCursor.- Returns:
- the current custom drag Cursor, if it was set otherwise returnsnull.
- See Also:
- setCursor(java.awt.Cursor)
 
- 
addDragSourceListenerAdd aDragSourceListenerto thisDragSourceContextif one has not already been added. If aDragSourceListeneralready exists, this method throws aTooManyListenersException.- Parameters:
- dsl- the- DragSourceListenerto add. Note that while- nullis not prohibited, it is not acceptable as a parameter.
- Throws:
- TooManyListenersException- if a- DragSourceListenerhas already been added
 
- 
removeDragSourceListenerRemoves the specifiedDragSourceListenerfrom thisDragSourceContext.- Parameters:
- dsl- the- DragSourceListenerto remove; note that while- nullis not prohibited, it is not acceptable as a parameter
 
- 
transferablesFlavorsChangedpublic void transferablesFlavorsChanged()Notifies the peer that theTransferable'sDataFlavors have changed.
- 
dragEnterCallsdragEnteron theDragSourceListeners registered with thisDragSourceContextand with the associatedDragSource, and passes them the specifiedDragSourceDragEvent.- Specified by:
- dragEnterin interface- DragSourceListener
- Parameters:
- dsde- the- DragSourceDragEvent
 
- 
dragOverCallsdragOveron theDragSourceListeners registered with thisDragSourceContextand with the associatedDragSource, and passes them the specifiedDragSourceDragEvent.- Specified by:
- dragOverin interface- DragSourceListener
- Parameters:
- dsde- the- DragSourceDragEvent
 
- 
dragExitCallsdragExiton theDragSourceListeners registered with thisDragSourceContextand with the associatedDragSource, and passes them the specifiedDragSourceEvent.- Specified by:
- dragExitin interface- DragSourceListener
- Parameters:
- dse- the- DragSourceEvent
 
- 
dropActionChangedCallsdropActionChangedon theDragSourceListeners registered with thisDragSourceContextand with the associatedDragSource, and passes them the specifiedDragSourceDragEvent.- Specified by:
- dropActionChangedin interface- DragSourceListener
- Parameters:
- dsde- the- DragSourceDragEvent
 
- 
dragDropEndCallsdragDropEndon theDragSourceListeners registered with thisDragSourceContextand with the associatedDragSource, and passes them the specifiedDragSourceDropEvent.- Specified by:
- dragDropEndin interface- DragSourceListener
- Parameters:
- dsde- the- DragSourceDropEvent
 
- 
dragMouseMovedCallsdragMouseMovedon theDragSourceMotionListeners registered with theDragSourceassociated with thisDragSourceContext, and them passes the specifiedDragSourceDragEvent.- Specified by:
- dragMouseMovedin interface- DragSourceMotionListener
- Parameters:
- dsde- the- DragSourceDragEvent
- Since:
- 1.4
 
- 
getTransferableReturns theTransferableassociated with thisDragSourceContext.- Returns:
- the Transferable
 
- 
updateCurrentCursorprotected 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 fields- DEFAULT,- ENTER,- OVER,- CHANGED
 
 
-