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
26 * <code>TransferDragSourceListener</code>s On <code>dragStart</code> the
27 * adapter determines the listener to be used for any further <code>drag*</code>
30 public class DelegatingDragAdapter implements DragSourceListener {
32 private TransferDragSourceListener[] fPossibleListeners;
34 private List fActiveListeners;
36 private TransferDragSourceListener fFinishListener;
38 public DelegatingDragAdapter(TransferDragSourceListener[] listeners) {
39 setPossibleListeners(listeners);
42 protected void setPossibleListeners(TransferDragSourceListener[] listeners) {
43 Assert.isNotNull(listeners);
45 .isTrue(fActiveListeners == null,
46 "Can only set possible listeners before drag operation has started"); //$NON-NLS-1$
47 fPossibleListeners = listeners;
53 * @see DragSourceListener
55 public void dragStart(DragSourceEvent event) {
56 fFinishListener = null;
57 boolean saveDoit = event.doit;
58 Object saveData = event.data;
60 List transfers = new ArrayList(fPossibleListeners.length);
61 fActiveListeners = new ArrayList(fPossibleListeners.length);
63 for (int i = 0; i < fPossibleListeners.length; i++) {
64 TransferDragSourceListener listener = fPossibleListeners[i];
65 event.doit = saveDoit;
66 listener.dragStart(event);
68 transfers.add(listener.getTransfer());
69 fActiveListeners.add(listener);
71 doIt = doIt || event.doit;
74 ((DragSource) event.widget).setTransfer((Transfer[]) transfers
75 .toArray(new Transfer[transfers.size()]));
77 event.data = saveData;
84 * @see DragSourceListener
86 public void dragSetData(DragSourceEvent event) {
87 fFinishListener = getListener(event.dataType);
88 if (fFinishListener != null)
89 fFinishListener.dragSetData(event);
95 * @see DragSourceListener
97 public void dragFinished(DragSourceEvent event) {
99 if (fFinishListener != null) {
100 fFinishListener.dragFinished(event);
102 // If the user presses Escape then we get a dragFinished without
103 // getting a dragSetData before.
104 fFinishListener = getListener(event.dataType);
105 if (fFinishListener != null)
106 fFinishListener.dragFinished(event);
109 fFinishListener = null;
110 fActiveListeners = null;
114 private TransferDragSourceListener getListener(TransferData type) {
118 for (Iterator iter = fActiveListeners.iterator(); iter.hasNext();) {
119 TransferDragSourceListener listener = (TransferDragSourceListener) iter
121 if (listener.getTransfer().isSupportedType(type)) {