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;
18 //import org.eclipse.jface.text.Assert;
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.swt.dnd.DragSource;
21 import org.eclipse.swt.dnd.DragSourceEvent;
22 import org.eclipse.swt.dnd.DragSourceListener;
23 import org.eclipse.swt.dnd.Transfer;
24 import org.eclipse.swt.dnd.TransferData;
27 * A delegating drag adapter negotiates between a set of
28 * <code>TransferDragSourceListener</code>s On <code>dragStart</code> the
29 * adapter determines the listener to be used for any further <code>drag*</code>
32 public class DelegatingDragAdapter implements DragSourceListener {
34 private TransferDragSourceListener[] fPossibleListeners;
36 private List fActiveListeners;
38 private TransferDragSourceListener fFinishListener;
40 public DelegatingDragAdapter(TransferDragSourceListener[] listeners) {
41 setPossibleListeners(listeners);
44 protected void setPossibleListeners(TransferDragSourceListener[] listeners) {
45 Assert.isNotNull(listeners);
47 .isTrue(fActiveListeners == null,
48 "Can only set possible listeners before drag operation has started"); //$NON-NLS-1$
49 fPossibleListeners = listeners;
55 * @see DragSourceListener
57 public void dragStart(DragSourceEvent event) {
58 fFinishListener = null;
59 boolean saveDoit = event.doit;
60 Object saveData = event.data;
62 List transfers = new ArrayList(fPossibleListeners.length);
63 fActiveListeners = new ArrayList(fPossibleListeners.length);
65 for (int i = 0; i < fPossibleListeners.length; i++) {
66 TransferDragSourceListener listener = fPossibleListeners[i];
67 event.doit = saveDoit;
68 listener.dragStart(event);
70 transfers.add(listener.getTransfer());
71 fActiveListeners.add(listener);
73 doIt = doIt || event.doit;
76 ((DragSource) event.widget).setTransfer((Transfer[]) transfers
77 .toArray(new Transfer[transfers.size()]));
79 event.data = saveData;
86 * @see DragSourceListener
88 public void dragSetData(DragSourceEvent event) {
89 fFinishListener = getListener(event.dataType);
90 if (fFinishListener != null)
91 fFinishListener.dragSetData(event);
97 * @see DragSourceListener
99 public void dragFinished(DragSourceEvent event) {
101 if (fFinishListener != null) {
102 fFinishListener.dragFinished(event);
104 // If the user presses Escape then we get a dragFinished without
105 // getting a dragSetData before.
106 fFinishListener = getListener(event.dataType);
107 if (fFinishListener != null)
108 fFinishListener.dragFinished(event);
111 fFinishListener = null;
112 fActiveListeners = null;
116 private TransferDragSourceListener getListener(TransferData type) {
120 for (Iterator iter = fActiveListeners.iterator(); iter.hasNext();) {
121 TransferDragSourceListener listener = (TransferDragSourceListener) iter
123 if (listener.getTransfer().isSupportedType(type)) {