DragSource defines the source object for a drag and drop transfer.
IMPORTANT: 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("");
}
});
DragSourcedefines the source object for a drag and drop transfer.IMPORTANT: 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
DragSourceListenerand 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(""); } });