org.eclipse.swt.dnd
Class DragSource

java.lang.Object
  |
  +--org.eclipse.swt.widgets.Widget
        |
        +--org.eclipse.swt.dnd.DragSource

public class DragSource
extends Widget

Untamed: 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("");
		}
	});
 
Styles
DND.DROP_NONE, DND.DROP_COPY, DND.DROP_MOVE, DND.DROP_LINK
Events
DND.DragStart, DND.DragSetData, DND.DragEnd


Field Summary
private static int CFSTR_PERFORMEDDROPEFFECT
           
private  Control control
           
private  Listener controlListener
           
private  int dataEffect
           
private static String DRAGSOURCEID
           
private  org.eclipse.swt.internal.ole.win32.COMObject iDataObject
           
private  org.eclipse.swt.internal.ole.win32.COMObject iDropSource
           
private  int refCount
           
private  Transfer[] transferAgents
           
 
Fields inherited from class org.eclipse.swt.widgets.Widget
 
Constructor Summary
DragSource(Control control, int style)
          Enabled: Creates a new DragSource to handle dragging from the specified Control.
 
Method Summary
 void addDragListener(DragSourceListener listener)
          Enabled: 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.
private  int AddRef()
           
(package private) static int checkStyle(int style)
           
protected  void checkSubclass()
          Checks that this class can be subclassed.
private  void createCOMInterfaces()
           
private  void disposeCOMInterfaces()
           
private  void drag()
           
private  int EnumFormatEtc(int dwDirection, int ppenumFormatetc)
           
 Control getControl()
          Enabled: Returns the Control which is registered for this DragSource.
private  int GetData(int pFormatetc, int pmedium)
           
 Display getDisplay()
          Enabled:
 Transfer[] getTransfer()
          Enabled: Returns the list of data types that can be transferred by this DragSource.
private  int GiveFeedback(int dwEffect)
           
private  void onDispose()
           
private  int opToOs(int operation)
           
private  int osToOp(int osOperation)
           
private  int QueryContinueDrag(int fEscapePressed, int grfKeyState)
           
private  int QueryGetData(int pFormatetc)
           
private  int QueryInterface(int riid, int ppvObject)
           
private  int Release()
           
 void removeDragListener(DragSourceListener listener)
          Enabled: Removes the listener from the collection of listeners who will be notified when a drag and drop operation is in progress.
private  int SetData(int pFormatetc, int pmedium, int fRelease)
           
 void setTransfer(Transfer[] transferAgents)
          Enabled: Specifies the list of data types that can be transferred by this DragSource.
 
Methods inherited from class org.eclipse.swt.widgets.Widget
addDisposeListener, addListener, checkWidget, dispose, getData, getData, getStyle, isDisposed, isListening, notifyListeners, removeDisposeListener, removeListener, removeListener, setData, setData, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

iDropSource

private org.eclipse.swt.internal.ole.win32.COMObject iDropSource

iDataObject

private org.eclipse.swt.internal.ole.win32.COMObject iDataObject

refCount

private int refCount

control

private Control control

controlListener

private Listener controlListener

transferAgents

private Transfer[] transferAgents

dataEffect

private int dataEffect

DRAGSOURCEID

private static final String DRAGSOURCEID

CFSTR_PERFORMEDDROPEFFECT

private static int CFSTR_PERFORMEDDROPEFFECT
Constructor Detail

DragSource

public DragSource(Control control,
                  int style)
Enabled: 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 - the Control that the user clicks on to initiate the drag
style - 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
See Also:
Widget.dispose(), checkSubclass(), DND.DROP_NONE, DND.DROP_COPY, DND.DROP_MOVE, DND.DROP_LINK
Method Detail

addDragListener

public void addDragListener(DragSourceListener listener)
Enabled: 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.

Parameters:
listener - the listener which should be notified
See Also:
DragSourceListener, removeDragListener(org.eclipse.swt.dnd.DragSourceListener), DragSourceEvent

AddRef

private int AddRef()

checkStyle

static int checkStyle(int style)

createCOMInterfaces

private void createCOMInterfaces()

checkSubclass

protected void checkSubclass()
Description copied from class: Widget
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: 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.

Overrides:
checkSubclass in class Widget

disposeCOMInterfaces

private void disposeCOMInterfaces()

drag

private void drag()

EnumFormatEtc

private int EnumFormatEtc(int dwDirection,
                          int ppenumFormatetc)

getControl

public Control getControl()
Enabled: Returns the Control which is registered for this DragSource. This is the control that the user clicks in to initiate dragging.

Returns:
the Control which is registered for this DragSource

GetData

private int GetData(int pFormatetc,
                    int pmedium)

getDisplay

public Display getDisplay()
Enabled:

Specified by:
getDisplay in class Widget
Returns:
the receiver's display

getTransfer

public Transfer[] getTransfer()
Enabled: Returns the list of data types that can be transferred by this DragSource.

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

GiveFeedback

private int GiveFeedback(int dwEffect)

QueryContinueDrag

private int QueryContinueDrag(int fEscapePressed,
                              int grfKeyState)

onDispose

private void onDispose()

opToOs

private int opToOs(int operation)

osToOp

private int osToOp(int osOperation)

QueryGetData

private int QueryGetData(int pFormatetc)

QueryInterface

private int QueryInterface(int riid,
                           int ppvObject)

Release

private int Release()

removeDragListener

public void removeDragListener(DragSourceListener listener)
Enabled: Removes the listener from the collection of listeners who will be notified when a drag and drop operation is in progress.

Parameters:
listener - the listener which should be notified
See Also:
DragSourceListener, addDragListener(org.eclipse.swt.dnd.DragSourceListener)

SetData

private int SetData(int pFormatetc,
                    int pmedium,
                    int fRelease)

setTransfer

public void setTransfer(Transfer[] transferAgents)
Enabled: 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 - a list of Transfer objects which define the types of data that can be dragged from this source


comments?