74ace1ce0fdbeadb01ce4abc6f66250e415ebd29
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / dnd / DelegatingDragAdapter.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.dnd;
12
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16
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;
22
23 import org.eclipse.jface.util.Assert;
24
25 /**
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.
29  */
30 public class DelegatingDragAdapter implements DragSourceListener {
31
32         private TransferDragSourceListener[] fPossibleListeners;
33         private List fActiveListeners;
34         private TransferDragSourceListener fFinishListener;
35         
36         public DelegatingDragAdapter(TransferDragSourceListener[] listeners) {
37                 setPossibleListeners(listeners);
38         }
39         
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;
44         }
45         
46         /* non Java-doc
47          * @see DragSourceListener
48          */
49         public void dragStart(DragSourceEvent event) {
50                 fFinishListener= null;
51                 boolean saveDoit= event.doit;
52                 Object saveData= event.data;
53                 boolean doIt= false;
54                 List transfers= new ArrayList(fPossibleListeners.length);
55                 fActiveListeners= new ArrayList(fPossibleListeners.length);
56                 
57                 for (int i= 0; i < fPossibleListeners.length; i++) {
58                         TransferDragSourceListener listener= fPossibleListeners[i];
59                         event.doit= saveDoit;
60                         listener.dragStart(event);
61                         if (event.doit) {
62                                 transfers.add(listener.getTransfer());
63                                 fActiveListeners.add(listener);
64                         }
65                         doIt= doIt || event.doit;
66                 }
67                 if (doIt) {
68                         ((DragSource)event.widget).setTransfer((Transfer[])transfers.toArray(new Transfer[transfers.size()]));
69                 }
70                 event.data= saveData;
71                 event.doit= doIt;
72         }
73
74         /* non Java-doc
75          * @see DragSourceListener
76          */
77         public void dragSetData(DragSourceEvent event) {
78                 fFinishListener= getListener(event.dataType);
79                 if (fFinishListener != null)
80                         fFinishListener.dragSetData(event);
81         }
82         
83         /* non Java-doc
84          * @see DragSourceListener
85          */
86         public void dragFinished(DragSourceEvent event) {
87                 try{
88                         if (fFinishListener != null) {
89                                 fFinishListener.dragFinished(event);
90                         } else {
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);
96                         }
97                 } finally{
98                         fFinishListener= null;
99                         fActiveListeners= null;
100                 }       
101         }
102         
103         private TransferDragSourceListener getListener(TransferData type) {
104                 if (type == null)
105                         return null;
106                         
107                 for (Iterator iter= fActiveListeners.iterator(); iter.hasNext();) {
108                         TransferDragSourceListener listener= (TransferDragSourceListener)iter.next();
109                         if (listener.getTransfer().isSupportedType(type)) {
110                                 return listener;
111                         }
112                 }
113                 return null;
114         }       
115 }