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