Applying pteague's patch (re #685)
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / dnd / DelegatingDropAdapter.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 org.eclipse.jface.util.Assert;
14 import org.eclipse.swt.dnd.DropTargetEvent;
15 import org.eclipse.swt.dnd.DropTargetListener;
16 import org.eclipse.swt.dnd.TransferData;
17
18 /**
19  * A delegating drop adapter negotiates between a set of
20  * <code>TransferDropTargetListener</code>s On <code>dragEnter</code> the
21  * adapter determines the listener to be used for any further <code>drag*</code>
22  * callback.
23  */
24 public class DelegatingDropAdapter implements DropTargetListener {
25
26         private TransferDropTargetListener[] fListeners;
27
28         private TransferDropTargetListener fChosenListener;
29
30         /**
31          * Creates a new delegating drop adapter.
32          * 
33          * @param listeners
34          *            an array of potential listeners
35          */
36         public DelegatingDropAdapter(TransferDropTargetListener[] listeners) {
37                 fListeners = listeners;
38                 Assert.isNotNull(listeners);
39         }
40
41         /*
42          * non Java-doc
43          * 
44          * @see DropTargetListener
45          */
46         public void dragEnter(DropTargetEvent event) {
47                 fChosenListener = null;
48                 event.currentDataType = selectPreferredListener(event.dataTypes);
49                 if (fChosenListener != null)
50                         fChosenListener.dragEnter(event);
51         }
52
53         /*
54          * non Java-doc
55          * 
56          * @see DropTargetListener
57          */
58         public void dragLeave(DropTargetEvent event) {
59                 if (fChosenListener != null)
60                         fChosenListener.dragLeave(event);
61         }
62
63         /*
64          * non Java-doc
65          * 
66          * @see DropTargetListener
67          */
68         public void dragOperationChanged(DropTargetEvent event) {
69                 if (fChosenListener != null)
70                         fChosenListener.dragOperationChanged(event);
71         }
72
73         /*
74          * non Java-doc
75          * 
76          * @see DropTargetListener
77          */
78         public void dragOver(DropTargetEvent event) {
79                 if (fChosenListener != null)
80                         fChosenListener.dragOver(event);
81         }
82
83         /*
84          * non Java-doc
85          * 
86          * @see DropTargetListener
87          */
88         public void drop(DropTargetEvent event) {
89                 if (fChosenListener != null)
90                         fChosenListener.drop(event);
91                 fChosenListener = null;
92         }
93
94         /*
95          * non Java-doc
96          * 
97          * @see DropTargetListener
98          */
99         public void dropAccept(DropTargetEvent event) {
100                 if (fChosenListener != null)
101                         fChosenListener.dropAccept(event);
102         }
103
104         private TransferData selectPreferredListener(TransferData[] dataTypes) {
105                 for (int i = 0; i < fListeners.length; i++) {
106                         TransferData data = computeTransferData(dataTypes, fListeners[i]);
107                         if (data != null)
108                                 return data;
109                 }
110                 return null;
111         }
112
113         private TransferData computeTransferData(TransferData[] dataTypes,
114                         TransferDropTargetListener listener) {
115                 for (int i = 0; i < dataTypes.length; i++) {
116                         if (listener.getTransfer().isSupportedType(dataTypes[i])) {
117                                 fChosenListener = listener;
118                                 return dataTypes[i];
119                         }
120                 }
121                 return null;
122         }
123 }