Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.eclipse.jface.util
Class DelegatingDropAdapter  view DelegatingDropAdapter download DelegatingDropAdapter.java

java.lang.Object
  extended byorg.eclipse.jface.util.DelegatingDropAdapter
All Implemented Interfaces:
org.eclipse.swt.dnd.DropTargetListener, java.util.EventListener, org.eclipse.swt.internal.SWTEventListener

public class DelegatingDropAdapter
extends java.lang.Object
implements org.eclipse.swt.dnd.DropTargetListener

A DelegatingDropAdapter is a DropTargetListener that maintains and delegates to a set of TransferDropTargetListeners. Each TransferDropTargetListener can then be implemented as if it were the DropTarget's only DropTargetListener.

On dragEnter, dragOperationChanged, dragOver and drop, a current listener is obtained from the set of all TransferDropTargetListeners. The current listener is the first listener to return true for TransferDropTargetListener.isEnabled(DropTargetEvent) 55 . The current listener is forwarded all DropTargetEvents until some other listener becomes the current listener, or the drop terminates.

After adding all TransferDropTargetListeners to the DelegatingDropAdapter the combined set of Transfers should be set in the SWT DropTarget. #getTransfers() provides the set of Transfer types of all TransferDropTargetListeners.

The following example snippet shows a DelegatingDropAdapter with two TransferDropTargetListeners. One supports dropping resources and demonstrates how a listener can be disabled in the isEnabled method. The other listener supports text transfer.

		final TreeViewer viewer = new TreeViewer(shell, SWT.NONE);
 		DelegatingDropAdapter dropAdapter = new DelegatingDropAdapter();
		dropAdapter.addDropTargetListener(new TransferDropTargetListener() {
			public Transfer getTransfer() {
				return ResourceTransfer.getInstance();
			}
			public boolean isEnabled(DropTargetEvent event) {
				// disable drop listener if there is no viewer selection
				if (viewer.getSelection().isEmpty())
					return false;
				return true;
			}
			public void dragEnter(DropTargetEvent event) {}
			public void dragLeave(DropTargetEvent event) {}
			public void dragOperationChanged(DropTargetEvent event) {}
			public void dragOver(DropTargetEvent event) {}
			public void drop(DropTargetEvent event) {
				if (event.data == null)
					return;
				IResource[] resources = (IResource[]) event.data;
				if (event.detail == DND.DROP_COPY) {
					// copy resources
				} else {
					// move resources
				}
					
			}
			public void dropAccept(DropTargetEvent event) {}
		});
		dropAdapter.addDropTargetListener(new TransferDropTargetListener() {
			public Transfer getTransfer() {
				return TextTransfer.getInstance();
			}
			public boolean isEnabled(DropTargetEvent event) {
				return true;
			}
			public void dragEnter(DropTargetEvent event) {}
			public void dragLeave(DropTargetEvent event) {}
			public void dragOperationChanged(DropTargetEvent event) {}
			public void dragOver(DropTargetEvent event) {}
			public void drop(DropTargetEvent event) {
				if (event.data == null)
					return;
				System.out.println(event.data);
			}
			public void dropAccept(DropTargetEvent event) {}
		});		
		viewer.addDropSupport(DND.DROP_COPY | DND.DROP_MOVE, dropAdapter.getTransfers(), dropAdapter);
 

NOTE: This API is experimental and subject to change including removal.

Since:
2.2

Field Summary
private  TransferDropTargetListener currentListener
           
private  java.util.List listeners
           
private  int originalDropType
           
 
Constructor Summary
DelegatingDropAdapter()
           
 
Method Summary
 void addDropTargetListener(TransferDropTargetListener listener)
          Adds the given TransferDropTargetListener.
 void dragEnter(org.eclipse.swt.dnd.DropTargetEvent event)
          The cursor has entered the drop target boundaries.
 void dragLeave(org.eclipse.swt.dnd.DropTargetEvent event)
          The cursor has left the drop target boundaries.
 void dragOperationChanged(org.eclipse.swt.dnd.DropTargetEvent event)
          The operation being performed has changed (usually due to the user changing a drag modifier key while dragging).
 void dragOver(org.eclipse.swt.dnd.DropTargetEvent event)
          The cursor is moving over the drop target.
 void drop(org.eclipse.swt.dnd.DropTargetEvent event)
          Forwards this event to the current listener, if there is one.
 void dropAccept(org.eclipse.swt.dnd.DropTargetEvent event)
          Forwards this event to the current listener if there is one.
private  TransferDropTargetListener getCurrentListener()
          Returns the listener which currently handles drop events.
private  org.eclipse.swt.dnd.TransferData getSupportedTransferType(org.eclipse.swt.dnd.TransferData[] dataTypes, TransferDropTargetListener listener)
          Returns the transfer data type supported by the given listener.
 org.eclipse.swt.dnd.Transfer[] getTransfers()
          Returns the combined set of Transfer types of all TransferDropTargetListeners.
 boolean isEmpty()
          Returns true if there are no listeners to delegate events to.
 void removeDropTargetListener(TransferDropTargetListener listener)
          Removes the given TransferDropTargetListener.
private  boolean setCurrentListener(TransferDropTargetListener listener, org.eclipse.swt.dnd.DropTargetEvent event)
          Sets the current listener to listener.
private  void updateCurrentListener(org.eclipse.swt.dnd.DropTargetEvent event)
          Updates the current listener to one that can handle the drop.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

listeners

private java.util.List listeners

currentListener

private TransferDropTargetListener currentListener

originalDropType

private int originalDropType
Constructor Detail

DelegatingDropAdapter

public DelegatingDropAdapter()
Method Detail

addDropTargetListener

public void addDropTargetListener(TransferDropTargetListener listener)
Adds the given TransferDropTargetListener.


dragEnter

public void dragEnter(org.eclipse.swt.dnd.DropTargetEvent event)
The cursor has entered the drop target boundaries. The current listener is updated, and #dragEnter() is forwarded to the current listener.

Specified by:
dragEnter in interface org.eclipse.swt.dnd.DropTargetListener

dragLeave

public void dragLeave(org.eclipse.swt.dnd.DropTargetEvent event)
The cursor has left the drop target boundaries. The event is forwarded to the current listener.

Specified by:
dragLeave in interface org.eclipse.swt.dnd.DropTargetListener

dragOperationChanged

public void dragOperationChanged(org.eclipse.swt.dnd.DropTargetEvent event)
The operation being performed has changed (usually due to the user changing a drag modifier key while dragging). Updates the current listener and forwards this event to that listener.

Specified by:
dragOperationChanged in interface org.eclipse.swt.dnd.DropTargetListener

dragOver

public void dragOver(org.eclipse.swt.dnd.DropTargetEvent event)
The cursor is moving over the drop target. Updates the current listener and forwards this event to that listener. If no listener can handle the drag operation the event.detail field is set to DND.DROP_NONE to indicate an invalid drop.

Specified by:
dragOver in interface org.eclipse.swt.dnd.DropTargetListener

drop

public void drop(org.eclipse.swt.dnd.DropTargetEvent event)
Forwards this event to the current listener, if there is one. Sets the current listener to null afterwards.

Specified by:
drop in interface org.eclipse.swt.dnd.DropTargetListener

dropAccept

public void dropAccept(org.eclipse.swt.dnd.DropTargetEvent event)
Forwards this event to the current listener if there is one.

Specified by:
dropAccept in interface org.eclipse.swt.dnd.DropTargetListener

getCurrentListener

private TransferDropTargetListener getCurrentListener()
Returns the listener which currently handles drop events.


getSupportedTransferType

private org.eclipse.swt.dnd.TransferData getSupportedTransferType(org.eclipse.swt.dnd.TransferData[] dataTypes,
                                                                  TransferDropTargetListener listener)
Returns the transfer data type supported by the given listener. Returns null if the listener does not support any of the specified data types.


getTransfers

public org.eclipse.swt.dnd.Transfer[] getTransfers()
Returns the combined set of Transfer types of all TransferDropTargetListeners.


isEmpty

public boolean isEmpty()
Returns true if there are no listeners to delegate events to.


removeDropTargetListener

public void removeDropTargetListener(TransferDropTargetListener listener)
Removes the given TransferDropTargetListener. Listeners should not be removed while a drag and drop operation is in progress.


setCurrentListener

private boolean setCurrentListener(TransferDropTargetListener listener,
                                   org.eclipse.swt.dnd.DropTargetEvent event)
Sets the current listener to listener. Sends the given DropTargetEvent if the current listener changes.


updateCurrentListener

private void updateCurrentListener(org.eclipse.swt.dnd.DropTargetEvent event)
Updates the current listener to one that can handle the drop. There can be many listeners and each listener may be able to handle many TransferData types. The first listener found that can handle a drop of one of the given TransferData types will be selected. If no listener can handle the drag operation the event.detail field is set to DND.DROP_NONE to indicate an invalid drop.