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
20 * <code>TransferDropTargetListener</code>s On <code>dragEnter</code> the
21 * adapter determines the listener to be used for any further <code>drag*</code>
24 public class DelegatingDropAdapter implements DropTargetListener {
26 private TransferDropTargetListener[] fListeners;
28 private TransferDropTargetListener fChosenListener;
31 * Creates a new delegating drop adapter.
34 * an array of potential listeners
36 public DelegatingDropAdapter(TransferDropTargetListener[] listeners) {
37 fListeners = listeners;
38 Assert.isNotNull(listeners);
44 * @see DropTargetListener
46 public void dragEnter(DropTargetEvent event) {
47 fChosenListener = null;
48 event.currentDataType = selectPreferredListener(event.dataTypes);
49 if (fChosenListener != null)
50 fChosenListener.dragEnter(event);
56 * @see DropTargetListener
58 public void dragLeave(DropTargetEvent event) {
59 if (fChosenListener != null)
60 fChosenListener.dragLeave(event);
66 * @see DropTargetListener
68 public void dragOperationChanged(DropTargetEvent event) {
69 if (fChosenListener != null)
70 fChosenListener.dragOperationChanged(event);
76 * @see DropTargetListener
78 public void dragOver(DropTargetEvent event) {
79 if (fChosenListener != null)
80 fChosenListener.dragOver(event);
86 * @see DropTargetListener
88 public void drop(DropTargetEvent event) {
89 if (fChosenListener != null)
90 fChosenListener.drop(event);
91 fChosenListener = null;
97 * @see DropTargetListener
99 public void dropAccept(DropTargetEvent event) {
100 if (fChosenListener != null)
101 fChosenListener.dropAccept(event);
104 private TransferData selectPreferredListener(TransferData[] dataTypes) {
105 for (int i = 0; i < fListeners.length; i++) {
106 TransferData data = computeTransferData(dataTypes, fListeners[i]);
113 private TransferData computeTransferData(TransferData[] dataTypes,
114 TransferDropTargetListener listener) {
115 for (int i = 0; i < dataTypes.length; i++) {
116 if (listener.getTransfer().isSupportedType(dataTypes[i])) {
117 fChosenListener = listener;