fixed bug #1115300, #1115292
[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.swt.dnd.DropTargetEvent;
14 import org.eclipse.swt.dnd.DropTargetListener;
15 import org.eclipse.swt.dnd.TransferData;
16
17 import org.eclipse.jface.util.Assert;
18
19 /**
20  * A delegating drop adapter negotiates between a set of <code>TransferDropTargetListener</code>s
21  * On <code>dragEnter</code> the adapter determines the listener to be used for any further
22  * <code>drag*</code> 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 an array of potential listeners
34          */     
35         public DelegatingDropAdapter(TransferDropTargetListener[] listeners) {
36                 fListeners= listeners;
37                 Assert.isNotNull(listeners);
38         }
39
40         /* non Java-doc
41          * @see DropTargetListener
42          */
43         public void dragEnter(DropTargetEvent event) {
44                 fChosenListener= null;
45                 event.currentDataType= selectPreferredListener(event.dataTypes);
46                 if (fChosenListener != null)
47                         fChosenListener.dragEnter(event);
48         }
49         
50         /* non Java-doc
51          * @see DropTargetListener
52          */
53         public void dragLeave(DropTargetEvent event){
54                 if (fChosenListener != null)
55                         fChosenListener.dragLeave(event);
56         }
57         
58         /* non Java-doc
59          * @see DropTargetListener
60          */
61         public void dragOperationChanged(DropTargetEvent event){
62                 if (fChosenListener != null)
63                         fChosenListener.dragOperationChanged(event);
64         }
65         
66         /* non Java-doc
67          * @see DropTargetListener
68          */
69         public void dragOver(DropTargetEvent event){
70                 if (fChosenListener != null)
71                         fChosenListener.dragOver(event);
72         }
73         
74         /* non Java-doc
75          * @see DropTargetListener
76          */
77         public void drop(DropTargetEvent event){
78                 if (fChosenListener != null)
79                         fChosenListener.drop(event);
80                 fChosenListener= null;  
81         }
82         
83         /* non Java-doc
84          * @see DropTargetListener
85          */
86         public void dropAccept(DropTargetEvent event){
87                 if (fChosenListener != null)
88                         fChosenListener.dropAccept(event);
89         }
90         
91         private TransferData selectPreferredListener(TransferData[] dataTypes) {
92                 for (int i= 0; i < fListeners.length; i++) {
93                         TransferData data= computeTransferData(dataTypes, fListeners[i]);
94                         if (data != null)
95                                 return data;
96                 }
97                 return null;
98         }
99         
100         private TransferData computeTransferData(TransferData[] dataTypes, TransferDropTargetListener listener) {
101                 for (int i= 0; i < dataTypes.length; i++) {
102                         if (listener.getTransfer().isSupportedType(dataTypes[i])) {
103                                 fChosenListener= listener;
104                                 return dataTypes[i];
105                         }
106                 }
107                 return null;
108         }       
109 }