- java.lang.Object
- 
- java.awt.dnd.DragSourceContext
 
- 
- All Implemented Interfaces:
- DragSourceListener,- DragSourceMotionListener,- Serializable,- EventListener
 
 public class DragSourceContext extends Object implements DragSourceListener, DragSourceMotionListener, Serializable TheDragSourceContextclass 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 theTransferablerepresenting the source data for the drag operation.Note that the DragSourceContextitself implements theDragSourceListenerandDragSourceMotionListenerinterfaces. This is to allow the platform peer (theDragSourceContextPeerinstance) created by theDragSourceto notify theDragSourceContextof state changes in the ongoing operation. This allows theDragSourceContextobject to interpose itself between the platform and the listeners provided by the initiator of the drag operation.By default, DragSourceContextsets 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 theDragSourceevents and calling thesetCursor()method. Alternatively, you can provide custom cursor behavior by providing custom implementations of theDragSourceand theDragSourceContextclasses.- 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 SummaryAll Methods Instance Methods Concrete Methods Modifier 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 Detail- 
DEFAULTprotected static final int DEFAULT Anintused by updateCurrentCursor() indicating that theCursorshould change to the default (no drop)Cursor.- See Also:
- Constant Field Values
 
 - 
ENTERprotected static final int ENTER Anintused by updateCurrentCursor() indicating that theCursorhas entered aDropTarget.- See Also:
- Constant Field Values
 
 - 
OVERprotected static final int OVER Anintused by updateCurrentCursor() indicating that theCursoris over aDropTarget.- See Also:
- Constant Field Values
 
 - 
CHANGEDprotected static final int CHANGED Anintused by updateCurrentCursor() indicating that the user operation has changed.- See Also:
- Constant Field Values
 
 
- 
 - 
Constructor Detail- 
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 Detail- 
getDragSourcepublic DragSource getDragSource() Returns theDragSourcethat instantiated thisDragSourceContext.- Returns:
- the DragSourcethat instantiated thisDragSourceContext
 
 - 
getComponentpublic Component getComponent() Returns theComponentassociated with thisDragSourceContext.- Returns:
- the Componentthat started the drag
 
 - 
getTriggerpublic DragGestureEvent getTrigger() Returns 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
 
 - 
setCursorpublic void setCursor(Cursor c) Sets 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
 
 - 
getCursorpublic Cursor getCursor() Returns the current custom dragCursor.- Returns:
- the current custom drag Cursor, if it was set otherwise returnsnull.
- See Also:
- setCursor(java.awt.Cursor)
 
 - 
addDragSourceListenerpublic void addDragSourceListener(DragSourceListener dsl) throws TooManyListenersException Add 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
 
 - 
removeDragSourceListenerpublic void removeDragSourceListener(DragSourceListener dsl) Removes 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.
 - 
dragEnterpublic void dragEnter(DragSourceDragEvent dsde) CallsdragEnteron theDragSourceListeners registered with thisDragSourceContextand with the associatedDragSource, and passes them the specifiedDragSourceDragEvent.- Specified by:
- dragEnterin interface- DragSourceListener
- Parameters:
- dsde- the- DragSourceDragEvent
 
 - 
dragOverpublic void dragOver(DragSourceDragEvent dsde) CallsdragOveron theDragSourceListeners registered with thisDragSourceContextand with the associatedDragSource, and passes them the specifiedDragSourceDragEvent.- Specified by:
- dragOverin interface- DragSourceListener
- Parameters:
- dsde- the- DragSourceDragEvent
 
 - 
dragExitpublic void dragExit(DragSourceEvent dse) CallsdragExiton theDragSourceListeners registered with thisDragSourceContextand with the associatedDragSource, and passes them the specifiedDragSourceEvent.- Specified by:
- dragExitin interface- DragSourceListener
- Parameters:
- dse- the- DragSourceEvent
 
 - 
dropActionChangedpublic void dropActionChanged(DragSourceDragEvent dsde) CallsdropActionChangedon theDragSourceListeners registered with thisDragSourceContextand with the associatedDragSource, and passes them the specifiedDragSourceDragEvent.- Specified by:
- dropActionChangedin interface- DragSourceListener
- Parameters:
- dsde- the- DragSourceDragEvent
 
 - 
dragDropEndpublic void dragDropEnd(DragSourceDropEvent dsde) CallsdragDropEndon theDragSourceListeners registered with thisDragSourceContextand with the associatedDragSource, and passes them the specifiedDragSourceDropEvent.- Specified by:
- dragDropEndin interface- DragSourceListener
- Parameters:
- dsde- the- DragSourceDropEvent
 
 - 
dragMouseMovedpublic void dragMouseMoved(DragSourceDragEvent dsde) CallsdragMouseMovedon theDragSourceMotionListeners registered with theDragSourceassociated with thisDragSourceContext, and them passes the specifiedDragSourceDragEvent.- Specified by:
- dragMouseMovedin interface- DragSourceMotionListener
- Parameters:
- dsde- the- DragSourceDragEvent
- Since:
- 1.4
 
 - 
getTransferablepublic Transferable getTransferable() Returns 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
 
 
- 
 
-