public class DelegatingDragAdapter extends Object implements DragSourceListener
DelegatingDragAdapter
is a DragSourceListener
that
maintains and delegates to a set of TransferDragSourceListener
s. Each
TransferDragSourceListener can then be implemented as if it were the
DragSource's
only DragSourceListener.
When a drag is started, a subset of all TransferDragSourceListeners
is generated and stored in a list of active listeners. This subset is
calculated by forwarding DragSourceListener.dragStart(DragSourceEvent)
to
every listener, and checking if the doit
field is left
set to true
.
DragSource
's set of supported Transfer types (DragSource.setTransfer(Transfer[])
) is updated to reflect the Transfer types
corresponding to the active listener subset.
If and when dragSetData(DragSourceEvent)
is called, a single
TransferDragSourceListener
is chosen, and only it is allowed to set the
drag data. The chosen listener is the first listener in the subset of active listeners
whose Transfer supports (Transfer.isSupportedType(TransferData)
) the
dataType
in the DragSourceEvent
.
The following example snippet shows a DelegatingDragAdapter
with two
TransferDragSourceListeners
. One implements drag of text strings,
the other supports file transfer and demonstrates how a listener can be disabled using
the dragStart method.
final TreeViewer viewer = new TreeViewer(shell, SWT.NONE);
DelegatingDragAdapter dragAdapter = new DelegatingDragAdapter();
dragAdapter.addDragSourceListener(new TransferDragSourceListener() {
public Transfer getTransfer() {
return TextTransfer.getInstance();
}
public void dragStart(DragSourceEvent event) {
// always enabled, can control enablement based on selection etc.
}
public void dragSetData(DragSourceEvent event) {
event.data = "Transfer data";
}
public void dragFinished(DragSourceEvent event) {
// no clean-up required
}
});
dragAdapter.addDragSourceListener(new TransferDragSourceListener() {
public Transfer getTransfer() {
return FileTransfer.getInstance();
}
public void dragStart(DragSourceEvent event) {
// enable drag listener if there is a viewer selection
event.doit = !viewer.getSelection().isEmpty();
}
public void dragSetData(DragSourceEvent event) {
File file1 = new File("C:/temp/file1");
File file2 = new File("C:/temp/file2");
event.data = new String[] {file1.getAbsolutePath(), file2.getAbsolutePath()};
}
public void dragFinished(DragSourceEvent event) {
// no clean-up required
}
});
viewer.addDragSupport(DND.DROP_COPY | DND.DROP_MOVE, dragAdapter.getTransfers(), dragAdapter);
Constructor and Description |
---|
DelegatingDragAdapter() |
Modifier and Type | Method and Description |
---|---|
void |
addDragSourceListener(TransferDragSourceListener listener)
Adds the given
TransferDragSourceListener . |
void |
dragFinished(DragSourceEvent event)
The drop has successfully completed.
|
void |
dragSetData(DragSourceEvent event)
The drop data is requested.
|
void |
dragStart(DragSourceEvent event)
A drag operation has started.
|
Transfer[] |
getTransfers()
Returns the
Transfer |
boolean |
isEmpty()
Returns
true if there are no listeners to delegate drag events to. |
void |
removeDragSourceListener(TransferDragSourceListener listener)
Removes the given
TransferDragSourceListener . |
public void addDragSourceListener(TransferDragSourceListener listener)
TransferDragSourceListener
.listener
- the new listenerpublic void dragFinished(DragSourceEvent event)
dragFinished
in interface DragSourceListener
event
- the drag source eventDragSourceListener.dragFinished(DragSourceEvent)
public void dragSetData(DragSourceEvent event)
dragSetData
in interface DragSourceListener
event
- the drag source eventDragSourceListener.dragSetData(DragSourceEvent)
public void dragStart(DragSourceEvent event)
event.doit
to false
if it cannot handle the drag operation. If a listener can
handle the drag, it is added to the list of active listeners.
The drag is aborted if there are no listeners that can handle it.dragStart
in interface DragSourceListener
event
- the drag source eventDragSourceListener.dragStart(DragSourceEvent)
public Transfer[] getTransfers()
Transfers from every TransferDragSourceListener
.
Transfer
spublic boolean isEmpty()
true
if there are no listeners to delegate drag events to.true
if there are no TransferDragSourceListeners
false
otherwise.public void removeDragSourceListener(TransferDragSourceListener listener)
TransferDragSourceListener
.
Listeners should not be removed while a drag and drop operation is in progress.listener
- the TransferDragSourceListener
to remove
Copyright (c) 2000, 2015 Eclipse Contributors and others. All rights reserved.Guidelines for using Eclipse APIs.