1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.dnd;
13 import org.eclipse.jface.util.Assert;
14 import org.eclipse.swt.dnd.DropTargetEvent;
15 import org.eclipse.swt.dnd.DropTargetListener;
16 import org.eclipse.swt.dnd.TransferData;
19 * A delegating drop adapter negotiates between a set of <code>TransferDropTargetListener</code>s
20 * On <code>dragEnter</code> the adapter determines the listener to be used for any further
21 * <code>drag*</code> callback.
23 public class DelegatingDropAdapter implements DropTargetListener {
25 private TransferDropTargetListener[] fListeners;
27 private TransferDropTargetListener fChosenListener;
30 * Creates a new delegating drop adapter.
32 * @param listeners an array of potential listeners
34 public DelegatingDropAdapter(TransferDropTargetListener[] listeners) {
35 fListeners= listeners;
36 Assert.isNotNull(listeners);
40 * @see DropTargetListener
42 public void dragEnter(DropTargetEvent event) {
43 fChosenListener= null;
44 event.currentDataType= selectPreferredListener(event.dataTypes);
45 if (fChosenListener != null)
46 fChosenListener.dragEnter(event);
50 * @see DropTargetListener
52 public void dragLeave(DropTargetEvent event){
53 if (fChosenListener != null)
54 fChosenListener.dragLeave(event);
58 * @see DropTargetListener
60 public void dragOperationChanged(DropTargetEvent event){
61 if (fChosenListener != null)
62 fChosenListener.dragOperationChanged(event);
66 * @see DropTargetListener
68 public void dragOver(DropTargetEvent event){
69 if (fChosenListener != null)
70 fChosenListener.dragOver(event);
74 * @see DropTargetListener
76 public void drop(DropTargetEvent event){
77 if (fChosenListener != null)
78 fChosenListener.drop(event);
79 fChosenListener= null;
83 * @see DropTargetListener
85 public void dropAccept(DropTargetEvent event){
86 if (fChosenListener != null)
87 fChosenListener.dropAccept(event);
90 private TransferData selectPreferredListener(TransferData[] dataTypes) {
91 for (int i= 0; i < fListeners.length; i++) {
92 TransferData data= computeTransferData(dataTypes, fListeners[i]);
99 private TransferData computeTransferData(TransferData[] dataTypes, TransferDropTargetListener listener) {
100 for (int i= 0; i < dataTypes.length; i++) {
101 if (listener.getTransfer().isSupportedType(dataTypes[i])) {
102 fChosenListener= listener;