KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > dnd > DelegatingDragAdapter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.dnd;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18
19 import org.eclipse.swt.dnd.DragSource;
20 import org.eclipse.swt.dnd.DragSourceEvent;
21 import org.eclipse.swt.dnd.DragSourceListener;
22 import org.eclipse.swt.dnd.Transfer;
23 import org.eclipse.swt.dnd.TransferData;
24
25 import org.eclipse.jface.util.TransferDragSourceListener;
26
27 /**
28  * A delegating drag adapter negotiates between a set of <code>TransferDragSourceListener</code>s
29  * On <code>dragStart</code> the adapter determines the listener to be used for any further
30  * <code>drag*</code> callback.
31  */

32 public class DelegatingDragAdapter implements DragSourceListener {
33
34     private TransferDragSourceListener[] fPossibleListeners;
35     private List JavaDoc fActiveListeners;
36     private TransferDragSourceListener fFinishListener;
37     
38     public DelegatingDragAdapter(TransferDragSourceListener[] listeners) {
39         setPossibleListeners(listeners);
40     }
41     
42     protected void setPossibleListeners(TransferDragSourceListener[] listeners) {
43         Assert.isNotNull(listeners);
44         Assert.isTrue(fActiveListeners == null, "Can only set possible listeners before drag operation has started"); //$NON-NLS-1$
45
fPossibleListeners= listeners;
46     }
47     
48     /* non Java-doc
49      * @see DragSourceListener
50      */

51     public void dragStart(DragSourceEvent event) {
52         fFinishListener= null;
53         boolean saveDoit= event.doit;
54         Object JavaDoc saveData= event.data;
55         boolean doIt= false;
56         List JavaDoc transfers= new ArrayList JavaDoc(fPossibleListeners.length);
57         fActiveListeners= new ArrayList JavaDoc(fPossibleListeners.length);
58         
59         for (int i= 0; i < fPossibleListeners.length; i++) {
60             TransferDragSourceListener listener= fPossibleListeners[i];
61             event.doit= saveDoit;
62             listener.dragStart(event);
63             if (event.doit) {
64                 transfers.add(listener.getTransfer());
65                 fActiveListeners.add(listener);
66             }
67             doIt= doIt || event.doit;
68         }
69         if (doIt) {
70             ((DragSource)event.widget).setTransfer((Transfer[])transfers.toArray(new Transfer[transfers.size()]));
71         }
72         event.data= saveData;
73         event.doit= doIt;
74     }
75
76     /* non Java-doc
77      * @see DragSourceListener
78      */

79     public void dragSetData(DragSourceEvent event) {
80         fFinishListener= getListener(event.dataType);
81         if (fFinishListener != null)
82             fFinishListener.dragSetData(event);
83     }
84     
85     /* non Java-doc
86      * @see DragSourceListener
87      */

88     public void dragFinished(DragSourceEvent event) {
89         try{
90             if (fFinishListener != null) {
91                 fFinishListener.dragFinished(event);
92             } else {
93                 // If the user presses Escape then we get a dragFinished without
94
// getting a dragSetData before.
95
fFinishListener= getListener(event.dataType);
96                 if (fFinishListener != null)
97                     fFinishListener.dragFinished(event);
98             }
99         } finally{
100             fFinishListener= null;
101             fActiveListeners= null;
102         }
103     }
104     
105     private TransferDragSourceListener getListener(TransferData type) {
106         if (type == null)
107             return null;
108             
109         for (Iterator JavaDoc iter= fActiveListeners.iterator(); iter.hasNext();) {
110             TransferDragSourceListener listener= (TransferDragSourceListener)iter.next();
111             if (listener.getTransfer().isSupportedType(type)) {
112                 return listener;
113             }
114         }
115         return null;
116     }
117 }
118
Popular Tags