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 java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
17 import org.eclipse.jface.util.Assert;
18 import org.eclipse.swt.dnd.DragSource;
19 import org.eclipse.swt.dnd.DragSourceEvent;
20 import org.eclipse.swt.dnd.DragSourceListener;
21 import org.eclipse.swt.dnd.Transfer;
22 import org.eclipse.swt.dnd.TransferData;
25 * A delegating drag adapter negotiates between a set of <code>TransferDragSourceListener</code>s
26 * On <code>dragStart</code> the adapter determines the listener to be used for any further
27 * <code>drag*</code> callback.
29 public class DelegatingDragAdapter implements DragSourceListener {
31 private TransferDragSourceListener[] fPossibleListeners;
32 private List fActiveListeners;
33 private TransferDragSourceListener fFinishListener;
35 public DelegatingDragAdapter(TransferDragSourceListener[] listeners) {
36 setPossibleListeners(listeners);
39 protected void setPossibleListeners(TransferDragSourceListener[] listeners) {
40 Assert.isNotNull(listeners);
41 Assert.isTrue(fActiveListeners == null, "Can only set possible listeners before drag operation has started"); //$NON-NLS-1$
42 fPossibleListeners= listeners;
46 * @see DragSourceListener
48 public void dragStart(DragSourceEvent event) {
49 fFinishListener= null;
50 boolean saveDoit= event.doit;
51 Object saveData= event.data;
53 List transfers= new ArrayList(fPossibleListeners.length);
54 fActiveListeners= new ArrayList(fPossibleListeners.length);
56 for (int i= 0; i < fPossibleListeners.length; i++) {
57 TransferDragSourceListener listener= fPossibleListeners[i];
59 listener.dragStart(event);
61 transfers.add(listener.getTransfer());
62 fActiveListeners.add(listener);
64 doIt= doIt || event.doit;
67 ((DragSource)event.widget).setTransfer((Transfer[])transfers.toArray(new Transfer[transfers.size()]));
74 * @see DragSourceListener
76 public void dragSetData(DragSourceEvent event) {
77 fFinishListener= getListener(event.dataType);
78 if (fFinishListener != null)
79 fFinishListener.dragSetData(event);
83 * @see DragSourceListener
85 public void dragFinished(DragSourceEvent event) {
87 if (fFinishListener != null) {
88 fFinishListener.dragFinished(event);
90 // If the user presses Escape then we get a dragFinished without
91 // getting a dragSetData before.
92 fFinishListener= getListener(event.dataType);
93 if (fFinishListener != null)
94 fFinishListener.dragFinished(event);
97 fFinishListener= null;
98 fActiveListeners= null;
102 private TransferDragSourceListener getListener(TransferData type) {
106 for (Iterator iter= fActiveListeners.iterator(); iter.hasNext();) {
107 TransferDragSourceListener listener= (TransferDragSourceListener)iter.next();
108 if (listener.getTransfer().isSupportedType(type)) {