DragSource

<code>DragSource</code> defines the source object for a drag and drop transfer.

<p>IMPORTANT: This class is <em>not</em> intended to be subclassed.</p>

<p>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 <code>ByteArrayTransfer</code>.</p>

<p>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.</p>

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

<p>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.</p>

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

<code><pre> // 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() is 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 is DND.DROP_MOVE) label.setText(""); } }); </pre></code>

More...

Constructors

this
this(Control control, int style)

Creates a new <code>DragSource</code> to handle dragging from the specified <code>Control</code>. 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.

Members

Functions

addDragListener
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 <code>DragSourceListener</code> interface.

getControl
Control getControl()

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

getDragListeners
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 <code>DragSourceListener</code> interface.

getDragSourceEffect
DragSourceEffect getDragSourceEffect()

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

getTransfer
Transfer[] getTransfer()

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

removeDragListener
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.

setDragSourceEffect
void setDragSourceEffect(DragSourceEffect effect)

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

setTransfer
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.

Inherited Members

From Widget

handle
GtkWidget* handle;

the handle to the OS resource (Warning: This field is platform dependent) <p> <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT public API. It is marked public only so that it can be shared within the packages provided by SWT. It is not available on all platforms and should never be accessed from application code. </p>

addListener
void addListener(int eventType, Listener listener)

Adds the listener to the collection of listeners who will be notified when an event of the given type occurs. When the event does occur in the widget, the listener is notified by sending it the <code>handleEvent()</code> message. The event type is one of the event constants defined in class <code>SWT</code>.

addDisposeListener
void addDisposeListener(DisposeListener listener)

Adds the listener to the collection of listeners who will be notified when the widget is disposed. When the widget is disposed, the listener is notified by sending it the <code>widgetDisposed()</code> message.

checkParent
void checkParent(Widget parent)

Throws an exception if the specified widget can not be used as a parent for the receiver.

checkSubclass
void checkSubclass()

Checks that this class can be subclassed. <p> The SWT class library is intended to be subclassed only at specific, controlled points (most notably, <code>Composite</code> and <code>Canvas</code> when implementing new widgets). This method enforces this rule unless it is overridden. </p><p> <em>IMPORTANT:</em> 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. </p><p> 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. </p>

checkWidget
void checkWidget()

Throws an <code>SWTException</code> if the receiver can not be accessed by the caller. This may include both checks on the state of the receiver and more generally on the entire execution context. This method <em>should</em> be called by widget implementors to enforce the standard SWT invariants. <p> Currently, it is an error to invoke any method (other than <code>isDisposed()</code>) on a widget that has had its <code>dispose()</code> method called. It is also an error to call widget methods from any thread that is different from the thread that created the widget. </p><p> In future releases of SWT, there may be more or fewer error checks and exceptions may be thrown for different reasons. </p>

dispose
void dispose()

Disposes of the operating system resources associated with the receiver and all its descendants. After this method has been invoked, the receiver and all descendants will answer <code>true</code> when sent the message <code>isDisposed()</code>. Any internal connections between the widgets in the tree will have been removed to facilitate garbage collection. <p> NOTE: This method is not called recursively on the descendants of the receiver. This means that, widget implementers can not detect when a widget is being disposed of by re-implementing this method, but should instead listen for the <code>Dispose</code> event. </p>

getData
Object getData()

Returns the application defined widget data associated with the receiver, or null if it has not been set. The <em>widget data</em> is a single, unnamed field that is stored with every widget. <p> Applications may put arbitrary objects in this field. If the object stored in the widget data needs to be notified when the widget is disposed of, it is the application's responsibility to hook the Dispose event on the widget and do so. </p>

getData
Object getData(String key)

Returns the application defined property of the receiver with the specified name, or null if it has not been set. <p> Applications may have associated arbitrary objects with the receiver in this fashion. If the objects stored in the properties need to be notified when the widget is disposed of, it is the application's responsibility to hook the Dispose event on the widget and do so. </p>

getDisplay
Display getDisplay()

Returns the <code>Display</code> that is associated with the receiver. <p> A widget's display is either provided when it is created (for example, top level <code>Shell</code>s) or is the same as its parent's display. </p>

getListeners
Listener[] getListeners(int eventType)

Returns an array of listeners who will be notified when an event of the given type occurs. The event type is one of the event constants defined in class <code>SWT</code>.

getStyle
int getStyle()

Returns the receiver's style information. <p> Note that the value which is returned by this method <em>may not match</em> the value which was provided to the constructor when the receiver was created. This can occur when the underlying operating system does not support a particular combination of requested styles. For example, if the platform widget used to implement a particular SWT widget always has scroll bars, the result of calling this method would always have the <code>SWT.H_SCROLL</code> and <code>SWT.V_SCROLL</code> bits set. </p>

isDisposed
bool isDisposed()

Returns <code>true</code> if the widget has been disposed, and <code>false</code> otherwise. <p> This method gets the dispose state for the widget. When a widget has been disposed, it is an error to invoke any other method using the widget. </p>

isListening
bool isListening(int eventType)

Returns <code>true</code> if there are any listeners for the specified event type associated with the receiver, and <code>false</code> otherwise. The event type is one of the event constants defined in class <code>SWT</code>.

notifyListeners
void notifyListeners(int eventType, Event event)

Notifies all of the receiver's listeners for events of the given type that one such event has occurred by invoking their <code>handleEvent()</code> method. The event type is one of the event constants defined in class <code>SWT</code>.

removeListener
void removeListener(int eventType, Listener handler)

Removes the listener from the collection of listeners who will be notified when an event of the given type occurs. The event type is one of the event constants defined in class <code>SWT</code>.

removeListener
void removeListener(int eventType, SWTEventListener handler)

Removes the listener from the collection of listeners who will be notified when an event of the given type occurs. <p> <b>IMPORTANT:</b> This method is <em>not</em> part of the SWT public API. It is marked public only so that it can be shared within the packages provided by SWT. It should never be referenced from application code. </p>

removeDisposeListener
void removeDisposeListener(DisposeListener listener)

Removes the listener from the collection of listeners who will be notified when the widget is disposed.

setData
void setData(Object data)

Sets the application defined widget data associated with the receiver to be the argument. The <em>widget data</em> is a single, unnamed field that is stored with every widget. <p> Applications may put arbitrary objects in this field. If the object stored in the widget data needs to be notified when the widget is disposed of, it is the application's responsibility to hook the Dispose event on the widget and do so. </p>

setData
void setData(String key, Object value)

Sets the application defined property of the receiver with the specified name to the given value. <p> Applications may associate arbitrary objects with the receiver in this fashion. If the objects stored in the properties need to be notified when the widget is disposed of, it is the application's responsibility to hook the Dispose event on the widget and do so. </p>

toString
String toString()

Returns a string containing a concise, human-readable description of the receiver.

Detailed Description

<dl> <dt><b>Styles</b></dt> <dd>DND.DROP_NONE, DND.DROP_COPY, DND.DROP_MOVE, DND.DROP_LINK</dd> <dt><b>Events</b></dt> <dd>DND.DragStart, DND.DragSetData, DND.DragEnd</dd> </dl>

@see <a href="http://www.eclipse.org/swt/snippets/#dnd">Drag and Drop snippets</a> @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: DNDExample</a> @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>

Meta