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.swt.dnd.DragSource;
18 import org.eclipse.swt.dnd.DragSourceEvent;
19 import org.eclipse.swt.dnd.DragSourceListener;
20 import org.eclipse.swt.dnd.Transfer;
21 import org.eclipse.swt.dnd.TransferData;
23 import org.eclipse.jface.util.Assert;
26 * A delegating drag adapter negotiates between a set of <code>TransferDragSourceListener</code>s
27 * On <code>dragStart</code> the adapter determines the listener to be used for any further
28 * <code>drag*</code> callback.
30 public class DelegatingDragAdapter implements DragSourceListener {
32 private TransferDragSourceListener[] fPossibleListeners;
33 private List fActiveListeners;
34 private TransferDragSourceListener fFinishListener;
36 public DelegatingDragAdapter(TransferDragSourceListener[] listeners) {
37 setPossibleListeners(listeners);
40 protected void setPossibleListeners(TransferDragSourceListener[] listeners) {
41 Assert.isNotNull(listeners);
42 Assert.isTrue(fActiveListeners == null, "Can only set possible listeners before drag operation has started"); //$NON-NLS-1$
43 fPossibleListeners= listeners;
47 * @see DragSourceListener
49 public void dragStart(DragSourceEvent event) {
50 fFinishListener= null;
51 boolean saveDoit= event.doit;
52 Object saveData= event.data;
54 List transfers= new ArrayList(fPossibleListeners.length);
55 fActiveListeners= new ArrayList(fPossibleListeners.length);
57 for (int i= 0; i < fPossibleListeners.length; i++) {
58 TransferDragSourceListener listener= fPossibleListeners[i];
60 listener.dragStart(event);
62 transfers.add(listener.getTransfer());
63 fActiveListeners.add(listener);
65 doIt= doIt || event.doit;
68 ((DragSource)event.widget).setTransfer((Transfer[])transfers.toArray(new Transfer[transfers.size()]));
75 * @see DragSourceListener
77 public void dragSetData(DragSourceEvent event) {
78 fFinishListener= getListener(event.dataType);
79 if (fFinishListener != null)
80 fFinishListener.dragSetData(event);
84 * @see DragSourceListener
86 public void dragFinished(DragSourceEvent event) {
88 if (fFinishListener != null) {
89 fFinishListener.dragFinished(event);
91 // If the user presses Escape then we get a dragFinished without
92 // getting a dragSetData before.
93 fFinishListener= getListener(event.dataType);
94 if (fFinishListener != null)
95 fFinishListener.dragFinished(event);
98 fFinishListener= null;
99 fActiveListeners= null;
103 private TransferDragSourceListener getListener(TransferData type) {
107 for (Iterator iter= fActiveListeners.iterator(); iter.hasNext();) {
108 TransferDragSourceListener listener= (TransferDragSourceListener)iter.next();
109 if (listener.getTransfer().isSupportedType(type)) {