KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > navigator > CommonDragAdapter


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.ui.navigator;
12
13 import java.util.LinkedHashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.jface.util.LocalSelectionTransfer;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.ISelectionProvider;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.swt.dnd.DragSource;
21 import org.eclipse.swt.dnd.DragSourceAdapter;
22 import org.eclipse.swt.dnd.DragSourceEvent;
23 import org.eclipse.swt.dnd.Transfer;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.ui.internal.navigator.NavigatorPlugin;
26 import org.eclipse.ui.internal.navigator.dnd.NavigatorPluginDropAction;
27 import org.eclipse.ui.part.PluginTransfer;
28
29 /**
30  *
31  * Provides an implementation of {@link DragSourceAdapter} which uses the
32  * extensions provided by the associated {@link INavigatorContentService}.
33  *
34  * <p>
35  * Clients should not need to create an instance of this class unless they are
36  * creating their own custom viewer. Otherwise, {@link CommonViewer} configures
37  * its drag adapter automatically.
38  * </p>
39  *
40  * @see INavigatorDnDService
41  * @see CommonDragAdapterAssistant
42  * @see CommonDropAdapter
43  * @see CommonDropAdapterAssistant
44  * @see CommonViewer
45  * @since 3.2
46  *
47  */

48 public final class CommonDragAdapter extends DragSourceAdapter {
49
50     private static final boolean DEBUG = false;
51
52     private final INavigatorContentService contentService;
53
54     private final ISelectionProvider provider;
55
56     /**
57      * Create a DragAdapter that drives the configuration of the drag data.
58      *
59      * @param aContentService
60      * The content service this Drag Adapter is associated with
61      * @param aProvider
62      * The provider that can give the current selection from the
63      * appropriate viewer.
64      */

65     public CommonDragAdapter(INavigatorContentService aContentService,
66             ISelectionProvider aProvider) {
67         super();
68         contentService = aContentService;
69         provider = aProvider;
70     }
71
72     /**
73      *
74      * @return An array of supported Drag Transfer types. The list contains [
75      * {@link LocalSelectionTransfer#getTransfer()},
76      * {@link PluginTransfer#getInstance()}] in addition to any
77      * supported types contributed by the
78      * {@link CommonDragAdapterAssistant assistants}.
79      * @see CommonDragAdapterAssistant
80      * @see LocalSelectionTransfer
81      * @see PluginTransfer
82      */

83     public Transfer[] getSupportedDragTransfers() {
84         CommonDragAdapterAssistant[] assistants = contentService
85                 .getDnDService().getCommonDragAssistants();
86
87         Set JavaDoc supportedTypes = new LinkedHashSet JavaDoc();
88         supportedTypes.add(PluginTransfer.getInstance());
89         supportedTypes.add(LocalSelectionTransfer.getTransfer());
90         Transfer[] transferTypes = null;
91         for (int i = 0; i < assistants.length; i++) {
92             transferTypes = assistants[i].getSupportedTransferTypes();
93             for (int j = 0; j < transferTypes.length; j++) {
94                 if (transferTypes[j] != null) {
95                     supportedTypes.add(transferTypes[j]);
96                 }
97             }
98         }
99         
100         Transfer[] transfers = (Transfer[]) supportedTypes
101                 .toArray(new Transfer[supportedTypes.size()]);
102         return transfers;
103     }
104
105     /*
106      * (non-Javadoc)
107      *
108      * @see org.eclipse.swt.dnd.DragSourceAdapter#dragStart(org.eclipse.swt.dnd.DragSourceEvent)
109      */

110     public void dragStart(DragSourceEvent event) {
111         if (DEBUG) {
112             System.out.println("CommonDragAdapter.dragStart (begin): " + event); //$NON-NLS-1$
113
}
114         try {
115             // Workaround for 1GEUS9V
116
DragSource dragSource = (DragSource) event.widget;
117             Control control = dragSource.getControl();
118             if (control == control.getDisplay().getFocusControl()) {
119                 ISelection selection = provider.getSelection();
120                 if (!selection.isEmpty()) {
121                     LocalSelectionTransfer.getTransfer()
122                             .setSelection(selection);
123                     event.doit = true;
124                 } else {
125                     event.doit = false;
126                 }
127             } else {
128                 event.doit = false;
129             }
130         } catch (RuntimeException JavaDoc e) {
131             NavigatorPlugin.logError(0, e.getMessage(), e);
132         }
133
134         if (DEBUG) {
135             System.out
136                     .println("CommonDragAdapter.dragStart (end): doit=" + event.doit); //$NON-NLS-1$
137
}
138     }
139
140     /*
141      * (non-Javadoc)
142      *
143      * @see org.eclipse.swt.dnd.DragSourceAdapter#dragSetData(org.eclipse.swt.dnd.DragSourceEvent)
144      */

145     public void dragSetData(DragSourceEvent event) {
146
147         ISelection selection = LocalSelectionTransfer.getTransfer()
148                 .getSelection();
149
150         if (DEBUG) {
151             System.out
152                     .println("CommonDragAdapter.dragSetData (begin): event" + event + " selection=" + selection); //$NON-NLS-1$ //$NON-NLS-2$
153
}
154
155         if (LocalSelectionTransfer.getTransfer()
156                 .isSupportedType(event.dataType)) {
157             event.data = selection;
158
159             if (DEBUG) {
160                 System.out
161                         .println("CommonDragAdapter.dragSetData set LocalSelectionTransfer"); //$NON-NLS-1$
162
}
163         } else if (PluginTransfer.getInstance().isSupportedType(event.dataType)) {
164             event.data = NavigatorPluginDropAction
165                     .createTransferData(contentService);
166             if (DEBUG) {
167                 System.out
168                         .println("CommonDragAdapter.dragSetData set PluginTransfer"); //$NON-NLS-1$
169
}
170         } else if (selection instanceof IStructuredSelection) {
171             if (DEBUG) {
172                 System.out
173                         .println("CommonDragAdapter.dragSetData looking for assistants"); //$NON-NLS-1$
174
}
175
176             INavigatorDnDService dndService = contentService.getDnDService();
177             CommonDragAdapterAssistant[] assistants = dndService
178                     .getCommonDragAssistants();
179             for (int i = 0; i < assistants.length; i++) {
180
181                 Transfer[] supportedTransferTypes = assistants[i]
182                         .getSupportedTransferTypes();
183                 for (int j = 0; j < supportedTransferTypes.length; j++) {
184                     if (supportedTransferTypes[j]
185                             .isSupportedType(event.dataType)) {
186                         try {
187                             if (DEBUG) {
188                                 System.out
189                                         .println("CommonDragAdapter.dragSetData set assistant transfer type"); //$NON-NLS-1$
190
}
191                             if(assistants[i].setDragData(event,
192                                     (IStructuredSelection) selection)) {
193                                 return;
194                             }
195                         } catch (RuntimeException JavaDoc re) {
196                             NavigatorPlugin.logError(0, re.getMessage(), re);
197                         }
198                     }
199
200                 }
201             }
202
203         } else {
204             event.doit = false;
205         }
206
207         if (DEBUG) {
208             System.out.println("CommonDragAdapter.dragSetData (end): " + event); //$NON-NLS-1$
209
}
210     }
211      
212     /*
213      * (non-Javadoc)
214      *
215      * @see org.eclipse.swt.dnd.DragSourceAdapter#dragFinished(org.eclipse.swt.dnd.DragSourceEvent)
216      */

217     public void dragFinished(DragSourceEvent event) {
218
219         if (DEBUG) {
220             System.out.println("CommonDragAdapter.dragFinished()."); //$NON-NLS-1$
221
}
222
223         LocalSelectionTransfer.getTransfer().setSelection(null);
224
225         // TODO Handle clean up if drop target was outside of workbench
226
// if (event.doit != false) {
227
//
228
// }
229
}
230
231 }
232
Popular Tags