Added some examples for context menu on Outline
[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 <code>TransferDropTargetListener</code>s
20  * On <code>dragEnter</code> the adapter determines the listener to be used for any further
21  * <code>drag*</code> callback.
22  */
23 public class DelegatingDropAdapter implements DropTargetListener {
24
25         private TransferDropTargetListener[] fListeners;
26         
27         private TransferDropTargetListener fChosenListener;
28
29         /**
30          * Creates a new delegating drop adapter.
31          * 
32          * @param listeners an array of potential listeners
33          */     
34         public DelegatingDropAdapter(TransferDropTargetListener[] listeners) {
35                 fListeners= listeners;
36                 Assert.isNotNull(listeners);
37         }
38
39         /* non Java-doc
40          * @see DropTargetListener
41          */
42         public void dragEnter(DropTargetEvent event) {
43                 fChosenListener= null;
44                 event.currentDataType= selectPreferredListener(event.dataTypes);
45                 if (fChosenListener != null)
46                         fChosenListener.dragEnter(event);
47         }
48         
49         /* non Java-doc
50          * @see DropTargetListener
51          */
52         public void dragLeave(DropTargetEvent event){
53                 if (fChosenListener != null)
54                         fChosenListener.dragLeave(event);
55         }
56         
57         /* non Java-doc
58          * @see DropTargetListener
59          */
60         public void dragOperationChanged(DropTargetEvent event){
61                 if (fChosenListener != null)
62                         fChosenListener.dragOperationChanged(event);
63         }
64         
65         /* non Java-doc
66          * @see DropTargetListener
67          */
68         public void dragOver(DropTargetEvent event){
69                 if (fChosenListener != null)
70                         fChosenListener.dragOver(event);
71         }
72         
73         /* non Java-doc
74          * @see DropTargetListener
75          */
76         public void drop(DropTargetEvent event){
77                 if (fChosenListener != null)
78                         fChosenListener.drop(event);
79                 fChosenListener= null;  
80         }
81         
82         /* non Java-doc
83          * @see DropTargetListener
84          */
85         public void dropAccept(DropTargetEvent event){
86                 if (fChosenListener != null)
87                         fChosenListener.dropAccept(event);
88         }
89         
90         private TransferData selectPreferredListener(TransferData[] dataTypes) {
91                 for (int i= 0; i < fListeners.length; i++) {
92                         TransferData data= computeTransferData(dataTypes, fListeners[i]);
93                         if (data != null)
94                                 return data;
95                 }
96                 return null;
97         }
98         
99         private TransferData computeTransferData(TransferData[] dataTypes, TransferDropTargetListener listener) {
100                 for (int i= 0; i < dataTypes.length; i++) {
101                         if (listener.getTransfer().isSupportedType(dataTypes[i])) {
102                                 fChosenListener= listener;
103                                 return dataTypes[i];
104                         }
105                 }
106                 return null;
107         }       
108 }