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;
14 //import org.eclipse.jface.text.Assert;
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.swt.dnd.DropTargetEvent;
17 import org.eclipse.swt.dnd.DropTargetListener;
18 import org.eclipse.swt.dnd.TransferData;
21 * A delegating drop adapter negotiates between a set of
22 * <code>TransferDropTargetListener</code>s On <code>dragEnter</code> the
23 * adapter determines the listener to be used for any further <code>drag*</code>
26 public class DelegatingDropAdapter implements DropTargetListener {
28 private TransferDropTargetListener[] fListeners;
30 private TransferDropTargetListener fChosenListener;
33 * Creates a new delegating drop adapter.
36 * an array of potential listeners
38 public DelegatingDropAdapter(TransferDropTargetListener[] listeners) {
39 fListeners = listeners;
40 Assert.isNotNull(listeners);
46 * @see DropTargetListener
48 public void dragEnter(DropTargetEvent event) {
49 fChosenListener = null;
50 event.currentDataType = selectPreferredListener(event.dataTypes);
51 if (fChosenListener != null)
52 fChosenListener.dragEnter(event);
58 * @see DropTargetListener
60 public void dragLeave(DropTargetEvent event) {
61 if (fChosenListener != null)
62 fChosenListener.dragLeave(event);
68 * @see DropTargetListener
70 public void dragOperationChanged(DropTargetEvent event) {
71 if (fChosenListener != null)
72 fChosenListener.dragOperationChanged(event);
78 * @see DropTargetListener
80 public void dragOver(DropTargetEvent event) {
81 if (fChosenListener != null)
82 fChosenListener.dragOver(event);
88 * @see DropTargetListener
90 public void drop(DropTargetEvent event) {
91 if (fChosenListener != null)
92 fChosenListener.drop(event);
93 fChosenListener = null;
99 * @see DropTargetListener
101 public void dropAccept(DropTargetEvent event) {
102 if (fChosenListener != null)
103 fChosenListener.dropAccept(event);
106 private TransferData selectPreferredListener(TransferData[] dataTypes) {
107 for (int i = 0; i < fListeners.length; i++) {
108 TransferData data = computeTransferData(dataTypes, fListeners[i]);
115 private TransferData computeTransferData(TransferData[] dataTypes,
116 TransferDropTargetListener listener) {
117 for (int i = 0; i < dataTypes.length; i++) {
118 if (listener.getTransfer().isSupportedType(dataTypes[i])) {
119 fChosenListener = listener;