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.swt.dnd.DropTargetEvent;
14 import org.eclipse.swt.dnd.DropTargetListener;
15 import org.eclipse.swt.dnd.TransferData;
17 import org.eclipse.jface.util.Assert;
20 * A delegating drop adapter negotiates between a set of <code>TransferDropTargetListener</code>s
21 * On <code>dragEnter</code> the adapter determines the listener to be used for any further
22 * <code>drag*</code> callback.
24 public class DelegatingDropAdapter implements DropTargetListener {
26 private TransferDropTargetListener[] fListeners;
28 private TransferDropTargetListener fChosenListener;
31 * Creates a new delegating drop adapter.
33 * @param listeners an array of potential listeners
35 public DelegatingDropAdapter(TransferDropTargetListener[] listeners) {
36 fListeners= listeners;
37 Assert.isNotNull(listeners);
41 * @see DropTargetListener
43 public void dragEnter(DropTargetEvent event) {
44 fChosenListener= null;
45 event.currentDataType= selectPreferredListener(event.dataTypes);
46 if (fChosenListener != null)
47 fChosenListener.dragEnter(event);
51 * @see DropTargetListener
53 public void dragLeave(DropTargetEvent event){
54 if (fChosenListener != null)
55 fChosenListener.dragLeave(event);
59 * @see DropTargetListener
61 public void dragOperationChanged(DropTargetEvent event){
62 if (fChosenListener != null)
63 fChosenListener.dragOperationChanged(event);
67 * @see DropTargetListener
69 public void dragOver(DropTargetEvent event){
70 if (fChosenListener != null)
71 fChosenListener.dragOver(event);
75 * @see DropTargetListener
77 public void drop(DropTargetEvent event){
78 if (fChosenListener != null)
79 fChosenListener.drop(event);
80 fChosenListener= null;
84 * @see DropTargetListener
86 public void dropAccept(DropTargetEvent event){
87 if (fChosenListener != null)
88 fChosenListener.dropAccept(event);
91 private TransferData selectPreferredListener(TransferData[] dataTypes) {
92 for (int i= 0; i < fListeners.length; i++) {
93 TransferData data= computeTransferData(dataTypes, fListeners[i]);
100 private TransferData computeTransferData(TransferData[] dataTypes, TransferDropTargetListener listener) {
101 for (int i= 0; i < dataTypes.length; i++) {
102 if (listener.getTransfer().isSupportedType(dataTypes[i])) {
103 fChosenListener= listener;