Top Description Fields Constructors Methods
org.eclipse.swt.dnd

public Class DragSource

extends Widget
Class Inheritance
Imports
java.lang.reflect.*, org.eclipse.swt.*, org.eclipse.swt.graphics.*, org.eclipse.swt.internal.*, org.eclipse.swt.internal.dnd.gtk.*, org.eclipse.swt.internal.gtk.*, org.eclipse.swt.internal.gtk3.*, org.eclipse.swt.internal.gtk4.*, org.eclipse.swt.widgets.*

DragSource defines the source object for a drag and drop transfer.

Important Note

This class is not intended to be subclassed.

A drag source is the object which originates a drag and drop operation. For the specified widget, it defines the type of data that is available for dragging and the set of operations that can be performed on that data. The operations can be any bit-wise combination of DND.MOVE, DND.COPY or DND.LINK. The type of data that can be transferred is specified by subclasses of Transfer such as TextTransfer or FileTransfer. The type of data transferred can be a predefined system type or it can be a type defined by the application. For instructions on how to define your own transfer type, refer to ByteArrayTransfer.

You may have several DragSources in an application but you can only have one DragSource per Control. Data dragged from this DragSource can be dropped on a site within this application or it can be dropped on another application such as an external Text editor.

The application supplies the content of the data being transferred by implementing the DragSourceListener and associating it with the DragSource via DragSource#addDragListener.

When a successful move operation occurs, the application is required to take the appropriate action to remove the data from its display and remove any associated operating system resources or internal references. Typically in a move operation, the drop target makes a copy of the data and the drag source deletes the original. However, sometimes copying the data can take a long time (such as copying a large file). Therefore, on some platforms, the drop target may actually move the data in the operating system rather than make a copy. This is usually only done in file transfers. In this case, the drag source is informed in the DragEnd event that a DROP_TARGET_MOVE was performed. It is the responsibility of the drag source at this point to clean up its displayed information. No action needs to be taken on the operating system resources.

The following example shows a Label widget that allows text to be dragged from it.


	// Enable a label as a Drag Source
	Label label = new Label(shell, SWT.NONE);
	// This example will allow text to be dragged
	Transfer[] types = new Transfer[] {TextTransfer.getInstance()};
	// This example will allow the text to be copied or moved to the drop target
	int operations = DND.DROP_MOVE | DND.DROP_COPY;

	DragSource source = new DragSource(label, operations);
	source.setTransfer(types);
	source.addDragListener(new DragSourceListener() {
		public void dragStart(DragSourceEvent e) {
			// Only start the drag if there is actually text in the
			// label - this text will be what is dropped on the target.
			if (label.getText().length() == 0) {
				event.doit = false;
			}
		};
		public void dragSetData(DragSourceEvent event) {
			// A drop has been performed, so provide the data of the
			// requested type.
			// (Checking the type of the requested data is only
			// necessary if the drag source supports more than
			// one data type but is shown here as an example).
			if (TextTransfer.getInstance().isSupportedType(event.dataType)){
				event.data = label.getText();
			}
		}
		public void dragFinished(DragSourceEvent event) {
			// A Move operation has been performed so remove the data
			// from the source
			if (event.detail == DND.DROP_MOVE)
				label.setText("");
		}
	});
 
Styles
DND.DROP_NONE, DND.DROP_COPY, DND.DROP_MOVE, DND.DROP_LINK
Events
DND.DragStart, DND.DragSetData, DND.DragEnd
See Also
Drag and Drop snippets, SWT Example: DNDExample, Sample code and further information

Field Summary

Modifier and TypeField and Description
pack-priv Control
pack-priv Listener
pack-priv static final String
pack-priv static Callback
pack-priv static Callback
pack-priv static Callback
pack-priv DragSourceEffect
pack-priv static Callback
pack-priv static Callback
pack-priv static Callback
pack-priv static Callback
pack-priv boolean
pack-priv long
pack-priv Transfer[]
Inherited from org.eclipse.swt.widgets.Widget:
handle

Constructor Summary

AccessConstructor and Description
public
DragSource(Control
the Control that the user clicks on to initiate the drag
control
,
int
the bitwise OR'ing of allowed operations; this may be a combination of any of DND.DROP_NONE, DND.DROP_COPY, DND.DROP_MOVE, DND.DROP_LINK
style
)

Creates a new DragSource to handle dragging from the specified Control.

Method Summary

Modifier and TypeMethod and Description
public void
addDragListener(DragSourceListener
the listener which should be notified
listener
)

Adds the listener to the collection of listeners who will be notified when a drag and drop operation is in progress, by sending it one of the messages defined in the DragSourceListener interface.

pack-priv boolean
pack-priv static int
checkStyle(int style)

protected void
checkSubclass()

Overrides org.eclipse.swt.widgets.Widget.checkSubclass.

Checks that this class can be subclassed.
pack-priv void
drag(Event dragEvent)

pack-priv static long
DragBegin(long widget, long context)

pack-priv void
dragBegin(long widget, long context)

pack-priv void
dragBeginGtk4(long source)

pack-priv static void
dragBeginProc(long source, long drag)

pack-priv static long
DragDataDelete(long widget, long context)

pack-priv void
dragDataDelete(long widget, long context)

pack-priv static long
DragEnd(long widget, long context)

pack-priv void
dragEnd(long widget, long context)

pack-priv static void
dragEndProc(long source, long drag, boolean delete_data)

pack-priv static long
DragGetData(long widget, long context, long selection_data, long info, long time)

pack-priv void
dragGetData(long widget, long context, long selection_data, int info, int time)

pack-priv long
pack-priv static long
dragPrepareProc(long source, double x, double y)

pack-priv static DragSource
FindDragSource(long handle)

public Control

Returns:

the Control which is registered for this DragSource
getControl
()

Returns the Control which is registered for this DragSource.

public DragSourceListener[]

Returns:

the listeners who will be notified when a drag and drop operation is in progress
getDragListeners
()

Returns an array of listeners who will be notified when a drag and drop operation is in progress, by sending it one of the messages defined in the DragSourceListener interface.

public DragSourceEffect

Returns:

the drag effect that is registered for this DragSource
getDragSourceEffect
()

Returns the drag effect that is registered for this DragSource.

public Transfer[]

Returns:

the list of data types that can be transferred by this DragSource
getTransfer
()

Returns the list of data types that can be transferred by this DragSource.

pack-priv void
pack-priv int
opToOsOp(int operation)

pack-priv int
osOpToOp(int osOperation)

public void
removeDragListener(DragSourceListener
the listener which should no longer be notified
listener
)

Removes the listener from the collection of listeners who will be notified when a drag and drop operation is in progress.

public void
setDragSourceEffect(DragSourceEffect
the drag effect that is registered for this DragSource
effect
)

Specifies the drag effect for this DragSource.

public void
setTransfer(Transfer...
a list of Transfer objects which define the types of data that can be dragged from this source
transferAgents
)

Specifies the list of data types that can be transferred by this DragSource.

Inherited from org.eclipse.swt.widgets.Widget:
addDisposeListeneraddListenercheckWidgetdisposegetDatagetDatagetDisplaygetListenersgetStyleisAutoDirectionisDisposedisListeningnotifyListenersremoveDisposeListenerremoveListenerremoveListenerreskinsetDatasetDatatoString

Field Detail

controlback to summary
pack-priv Control control
controlListenerback to summary
pack-priv Listener controlListener
DEFAULT_DRAG_SOURCE_EFFECTback to summary
pack-priv static final String DEFAULT_DRAG_SOURCE_EFFECT
DragBeginback to summary
pack-priv static Callback DragBegin
dragBeginProcback to summary
pack-priv static Callback dragBeginProc
DragDataDeleteback to summary
pack-priv static Callback DragDataDelete
dragEffectback to summary
pack-priv DragSourceEffect dragEffect
DragEndback to summary
pack-priv static Callback DragEnd
dragEndProcback to summary
pack-priv static Callback dragEndProc
DragGetDataback to summary
pack-priv static Callback DragGetData
dragPrepareProcback to summary
pack-priv static Callback dragPrepareProc
moveDataback to summary
pack-priv boolean moveData
targetListback to summary
pack-priv long targetList
transferAgentsback to summary
pack-priv Transfer[] transferAgents

Constructor Detail

DragSourceback to summary
public DragSource(Control control, int style)

Creates a new DragSource to handle dragging from the specified Control. Creating an instance of a DragSource may cause system resources to be allocated depending on the platform. It is therefore mandatory that the DragSource instance be disposed when no longer required.

Parameters
control:Control

the Control that the user clicks on to initiate the drag

style:int

the bitwise OR'ing of allowed operations; this may be a combination of any of DND.DROP_NONE, DND.DROP_COPY, DND.DROP_MOVE, DND.DROP_LINK

Exceptions
SWTException:
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent
  • ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass
SWTError:
  • ERROR_CANNOT_INIT_DRAG - unable to initiate drag source; this will occur if more than one drag source is created for a control or if the operating system will not allow the creation of the drag source

NOTE: ERROR_CANNOT_INIT_DRAG should be an SWTException, since it is a recoverable error, but can not be changed due to backward compatibility.

See Also
Widget#dispose, DragSource#checkSubclass, DND#DROP_NONE, DND#DROP_COPY, DND#DROP_MOVE, DND#DROP_LINK

Method Detail

addDragListenerback to summary
public void addDragListener(DragSourceListener listener)

Adds the listener to the collection of listeners who will be notified when a drag and drop operation is in progress, by sending it one of the messages defined in the DragSourceListener interface.

  • dragStart is called when the user has begun the actions required to drag the widget. This event gives the application the chance to decide if a drag should be started.
  • dragSetData is called when the data is required from the drag source.
  • dragFinished is called when the drop has successfully completed (mouse up over a valid target) or has been terminated (such as hitting the ESC key). Perform cleanup such as removing data from the source side on a successful move operation.
Parameters
listener:DragSourceListener

the listener which should be notified

Exceptions
IllegalArgumentException:
  • ERROR_NULL_ARGUMENT - if the listener is null
SWTException:
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
See Also
DragSourceListener, getDragListeners, removeDragListener, DragSourceEvent
canBeginDragback to summary
pack-priv boolean canBeginDrag()
checkStyleback to summary
pack-priv static int checkStyle(int style)
checkSubclassback to summary
protected void checkSubclass()

Overrides org.eclipse.swt.widgets.Widget.checkSubclass.

Doc from org.eclipse.swt.widgets.Widget.checkSubclass.

Checks that this class can be subclassed.

The SWT class library is intended to be subclassed only at specific, controlled points (most notably, Composite and Canvas when implementing new widgets). This method enforces this rule unless it is overridden.

Important Note

By providing an implementation of this method that allows a subclass of a class which does not normally allow subclassing to be created, the implementer agrees to be fully responsible for the fact that any such subclass will likely fail between SWT releases and will be strongly platform specific. No support is provided for user-written classes which are implemented in this fashion.

The ability to subclass outside of the allowed SWT classes is intended purely to enable those not on the SWT development team to implement patches in order to get around specific limitations in advance of when those limitations can be addressed by the team. Subclassing should not be attempted without an intimate and detailed understanding of the hierarchy.

Annotations
@Override
dragback to summary
pack-priv void drag(Event dragEvent)
DragBeginback to summary
pack-priv static long DragBegin(long widget, long context)
dragBeginback to summary
pack-priv void dragBegin(long widget, long context)
dragBeginGtk4back to summary
pack-priv void dragBeginGtk4(long source)
dragBeginProcback to summary
pack-priv static void dragBeginProc(long source, long drag)
DragDataDeleteback to summary
pack-priv static long DragDataDelete(long widget, long context)
dragDataDeleteback to summary
pack-priv void dragDataDelete(long widget, long context)
DragEndback to summary
pack-priv static long DragEnd(long widget, long context)
dragEndback to summary
pack-priv void dragEnd(long widget, long context)
dragEndProcback to summary
pack-priv static void dragEndProc(long source, long drag, boolean delete_data)
DragGetDataback to summary
pack-priv static long DragGetData(long widget, long context, long selection_data, long info, long time)
dragGetDataback to summary
pack-priv void dragGetData(long widget, long context, long selection_data, int info, int time)
dragPrepareback to summary
pack-priv long dragPrepare()
dragPrepareProcback to summary
pack-priv static long dragPrepareProc(long source, double x, double y)
FindDragSourceback to summary
pack-priv static DragSource FindDragSource(long handle)
getControlback to summary
public Control getControl()

Returns the Control which is registered for this DragSource. This is the control that the user clicks in to initiate dragging.

Returns:Control

the Control which is registered for this DragSource

getDragListenersback to summary
public DragSourceListener[] getDragListeners()

Returns an array of listeners who will be notified when a drag and drop operation is in progress, by sending it one of the messages defined in the DragSourceListener interface.

Returns:DragSourceListener[]

the listeners who will be notified when a drag and drop operation is in progress

Exceptions
SWTException:
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
Since
3.4
See Also
DragSourceListener, addDragListener, removeDragListener, DragSourceEvent
getDragSourceEffectback to summary
public DragSourceEffect getDragSourceEffect()

Returns the drag effect that is registered for this DragSource. This drag effect will be used during a drag and drop operation.

Returns:DragSourceEffect

the drag effect that is registered for this DragSource

Since
3.3
getTransferback to summary
public Transfer[] getTransfer()

Returns the list of data types that can be transferred by this DragSource.

Returns:Transfer[]

the list of data types that can be transferred by this DragSource

onDisposeback to summary
pack-priv void onDispose()
opToOsOpback to summary
pack-priv int opToOsOp(int operation)
osOpToOpback to summary
pack-priv int osOpToOp(int osOperation)
removeDragListenerback to summary
public void removeDragListener(DragSourceListener listener)

Removes the listener from the collection of listeners who will be notified when a drag and drop operation is in progress.

Parameters
listener:DragSourceListener

the listener which should no longer be notified

Exceptions
IllegalArgumentException:
  • ERROR_NULL_ARGUMENT - if the listener is null
SWTException:
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
See Also
DragSourceListener, addDragListener, getDragListeners
setDragSourceEffectback to summary
public void setDragSourceEffect(DragSourceEffect effect)

Specifies the drag effect for this DragSource. This drag effect will be used during a drag and drop operation.

Parameters
effect:DragSourceEffect

the drag effect that is registered for this DragSource

Since
3.3
setTransferback to summary
public void setTransfer(Transfer... transferAgents)

Specifies the list of data types that can be transferred by this DragSource. The application must be able to provide data to match each of these types when a successful drop has occurred.

Parameters
transferAgents:Transfer[]

a list of Transfer objects which define the types of data that can be dragged from this source