misc
[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.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;
23
24 /**
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.
28  */
29 public class DelegatingDragAdapter implements DragSourceListener {
30
31         private TransferDragSourceListener[] fPossibleListeners;
32         private List fActiveListeners;
33         private TransferDragSourceListener fFinishListener;
34         
35         public DelegatingDragAdapter(TransferDragSourceListener[] listeners) {
36                 setPossibleListeners(listeners);
37         }
38         
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;
43         }
44         
45         /* non Java-doc
46          * @see DragSourceListener
47          */
48         public void dragStart(DragSourceEvent event) {
49                 fFinishListener= null;
50                 boolean saveDoit= event.doit;
51                 Object saveData= event.data;
52                 boolean doIt= false;
53                 List transfers= new ArrayList(fPossibleListeners.length);
54                 fActiveListeners= new ArrayList(fPossibleListeners.length);
55                 
56                 for (int i= 0; i < fPossibleListeners.length; i++) {
57                         TransferDragSourceListener listener= fPossibleListeners[i];
58                         event.doit= saveDoit;
59                         listener.dragStart(event);
60                         if (event.doit) {
61                                 transfers.add(listener.getTransfer());
62                                 fActiveListeners.add(listener);
63                         }
64                         doIt= doIt || event.doit;
65                 }
66                 if (doIt) {
67                         ((DragSource)event.widget).setTransfer((Transfer[])transfers.toArray(new Transfer[transfers.size()]));
68                 }
69                 event.data= saveData;
70                 event.doit= doIt;
71         }
72
73         /* non Java-doc
74          * @see DragSourceListener
75          */
76         public void dragSetData(DragSourceEvent event) {
77                 fFinishListener= getListener(event.dataType);
78                 if (fFinishListener != null)
79                         fFinishListener.dragSetData(event);
80         }
81         
82         /* non Java-doc
83          * @see DragSourceListener
84          */
85         public void dragFinished(DragSourceEvent event) {
86                 try{
87                         if (fFinishListener != null) {
88                                 fFinishListener.dragFinished(event);
89                         } else {
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);
95                         }
96                 } finally{
97                         fFinishListener= null;
98                         fActiveListeners= null;
99                 }       
100         }
101         
102         private TransferDragSourceListener getListener(TransferData type) {
103                 if (type == null)
104                         return null;
105                         
106                 for (Iterator iter= fActiveListeners.iterator(); iter.hasNext();) {
107                         TransferDragSourceListener listener= (TransferDragSourceListener)iter.next();
108                         if (listener.getTransfer().isSupportedType(type)) {
109                                 return listener;
110                         }
111                 }
112                 return null;
113         }       
114 }