1) Fixed issue #347: Syntax highlight doesn't like apostrophe in heredoc.
[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 //incastrix
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;
25
26 /**
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>
30  * callback.
31  */
32 public class DelegatingDragAdapter implements DragSourceListener {
33
34         private TransferDragSourceListener[] fPossibleListeners;
35
36         private List fActiveListeners;
37
38         private TransferDragSourceListener fFinishListener;
39
40         public DelegatingDragAdapter(TransferDragSourceListener[] listeners) {
41                 setPossibleListeners(listeners);
42         }
43
44         protected void setPossibleListeners(TransferDragSourceListener[] listeners) {
45                 Assert.isNotNull(listeners);
46                 Assert
47                                 .isTrue(fActiveListeners == null,
48                                                 "Can only set possible listeners before drag operation has started"); //$NON-NLS-1$
49                 fPossibleListeners = listeners;
50         }
51
52         /*
53          * non Java-doc
54          * 
55          * @see DragSourceListener
56          */
57         public void dragStart(DragSourceEvent event) {
58                 fFinishListener = null;
59                 boolean saveDoit = event.doit;
60                 Object saveData = event.data;
61                 boolean doIt = false;
62                 List transfers = new ArrayList(fPossibleListeners.length);
63                 fActiveListeners = new ArrayList(fPossibleListeners.length);
64
65                 for (int i = 0; i < fPossibleListeners.length; i++) {
66                         TransferDragSourceListener listener = fPossibleListeners[i];
67                         event.doit = saveDoit;
68                         listener.dragStart(event);
69                         if (event.doit) {
70                                 transfers.add(listener.getTransfer());
71                                 fActiveListeners.add(listener);
72                         }
73                         doIt = doIt || event.doit;
74                 }
75                 if (doIt) {
76                         ((DragSource) event.widget).setTransfer((Transfer[]) transfers
77                                         .toArray(new Transfer[transfers.size()]));
78                 }
79                 event.data = saveData;
80                 event.doit = doIt;
81         }
82
83         /*
84          * non Java-doc
85          * 
86          * @see DragSourceListener
87          */
88         public void dragSetData(DragSourceEvent event) {
89                 fFinishListener = getListener(event.dataType);
90                 if (fFinishListener != null)
91                         fFinishListener.dragSetData(event);
92         }
93
94         /*
95          * non Java-doc
96          * 
97          * @see DragSourceListener
98          */
99         public void dragFinished(DragSourceEvent event) {
100                 try {
101                         if (fFinishListener != null) {
102                                 fFinishListener.dragFinished(event);
103                         } else {
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);
109                         }
110                 } finally {
111                         fFinishListener = null;
112                         fActiveListeners = null;
113                 }
114         }
115
116         private TransferDragSourceListener getListener(TransferData type) {
117                 if (type == null)
118                         return null;
119
120                 for (Iterator iter = fActiveListeners.iterator(); iter.hasNext();) {
121                         TransferDragSourceListener listener = (TransferDragSourceListener) iter
122                                         .next();
123                         if (listener.getTransfer().isSupportedType(type)) {
124                                 return listener;
125                         }
126                 }
127                 return null;
128         }
129 }