Project

General

Profile

Download (9.07 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2003, 2010 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.ArrayList;
14
import java.util.LinkedHashSet;
15
import java.util.List;
16
import java.util.Set;
17

    
18
import org.eclipse.core.runtime.SafeRunner;
19
import org.eclipse.jface.util.LocalSelectionTransfer;
20
import org.eclipse.jface.viewers.ISelection;
21
import org.eclipse.jface.viewers.ISelectionProvider;
22
import org.eclipse.jface.viewers.IStructuredSelection;
23
import org.eclipse.swt.dnd.DragSource;
24
import org.eclipse.swt.dnd.DragSourceAdapter;
25
import org.eclipse.swt.dnd.DragSourceEvent;
26
import org.eclipse.swt.dnd.Transfer;
27
import org.eclipse.swt.widgets.Control;
28
import org.eclipse.ui.internal.navigator.NavigatorSafeRunnable;
29
import org.eclipse.ui.internal.navigator.Policy;
30
import org.eclipse.ui.internal.navigator.dnd.NavigatorPluginDropAction;
31
import org.eclipse.ui.part.PluginTransfer;
32

    
33
/**
34
 * 
35
 * Provides an implementation of {@link DragSourceAdapter} which uses the
36
 * extensions provided by the associated {@link INavigatorContentService}.
37
 * 
38
 * <p>
39
 * Clients should not need to create an instance of this class unless they are
40
 * creating their own custom viewer. Otherwise, {@link CommonViewer} configures
41
 * its drag adapter automatically.
42
 * </p> 
43
 * 
44
 * @see INavigatorDnDService
45
 * @see CommonDragAdapterAssistant
46
 * @see CommonDropAdapter
47
 * @see CommonDropAdapterAssistant
48
 * @see CommonViewer
49
 * @since 3.2
50
 * 
51
 */
52
public final class CommonDragAdapter extends DragSourceAdapter {
53

    
54
	private final INavigatorContentService contentService;
55

    
56
	private final ISelectionProvider provider;
57

    
58
	private CommonDragAdapterAssistant setDataAssistant;
59
	
60
	private List assistantsToUse;
61
	
62
	/**
63
	 * Create a DragAdapter that drives the configuration of the drag data.
64
	 * 
65
	 * @param aContentService
66
	 *            The content service this Drag Adapter is associated with
67
	 * @param aProvider
68
	 *            The provider that can give the current selection from the
69
	 *            appropriate viewer.
70
	 */
71
	public CommonDragAdapter(INavigatorContentService aContentService,
72
			ISelectionProvider aProvider) {
73
		super();
74
		contentService = aContentService;
75
		provider = aProvider;
76
		assistantsToUse = new ArrayList();
77
	}
78

    
79
	/**
80
	 * 
81
	 * @return An array of supported Drag Transfer types. The list contains [
82
	 *         {@link LocalSelectionTransfer#getTransfer()},
83
	 *         {@link PluginTransfer#getInstance()}] in addition to any
84
	 *         supported types contributed by the
85
	 *         {@link CommonDragAdapterAssistant assistants}.
86
	 * @see CommonDragAdapterAssistant
87
	 * @see LocalSelectionTransfer
88
	 * @see PluginTransfer
89
	 */
90
	public Transfer[] getSupportedDragTransfers() {
91
		CommonDragAdapterAssistant[] assistants = contentService
92
				.getDnDService().getCommonDragAssistants();
93

    
94
		Set supportedTypes = new LinkedHashSet();
95
		supportedTypes.add(PluginTransfer.getInstance());
96
		supportedTypes.add(LocalSelectionTransfer.getTransfer());
97
		Transfer[] transferTypes = null;
98
		for (int i = 0; i < assistants.length; i++) {
99
			transferTypes = assistants[i].getSupportedTransferTypes();
100
			for (int j = 0; j < transferTypes.length; j++) {
101
				if (transferTypes[j] != null) {
102
					supportedTypes.add(transferTypes[j]);
103
				}
104
			}
105
		}
106
		
107
		Transfer[] transfers = (Transfer[]) supportedTypes
108
				.toArray(new Transfer[supportedTypes.size()]);
109
		return transfers;
110
	}
111

    
112
	/*
113
	 * (non-Javadoc)
114
	 * 
115
	 * @see org.eclipse.swt.dnd.DragSourceAdapter#dragStart(org.eclipse.swt.dnd.DragSourceEvent)
116
	 */
117
	public void dragStart(final DragSourceEvent event) {
118
		if (Policy.DEBUG_DND) {
119
			System.out.println("CommonDragAdapter.dragStart (begin): " + event); //$NON-NLS-1$
120
		}
121
		SafeRunner.run(new NavigatorSafeRunnable() {
122
			public void run() throws Exception {
123
				DragSource dragSource = (DragSource) event.widget;
124
				if (Policy.DEBUG_DND) {
125
					System.out.println("CommonDragAdapter.dragStart source: " + dragSource); //$NON-NLS-1$
126
				}
127
				Control control = dragSource.getControl();
128
				if (control == control.getDisplay().getFocusControl()) {
129
					ISelection selection = provider.getSelection();
130
					assistantsToUse.clear();
131

    
132
					if (!selection.isEmpty()) {
133
						LocalSelectionTransfer.getTransfer().setSelection(selection);
134

    
135
						boolean doIt = false;
136
						INavigatorDnDService dndService = contentService.getDnDService();
137
						CommonDragAdapterAssistant[] assistants = dndService
138
								.getCommonDragAssistants();
139
						if (assistants.length == 0)
140
							doIt = true;
141
						for (int i = 0; i < assistants.length; i++) {
142
							if (Policy.DEBUG_DND) {
143
								System.out
144
										.println("CommonDragAdapter.dragStart assistant: " + assistants[i]); //$NON-NLS-1$
145
							}
146
							event.doit = true;
147
							assistants[i].dragStart(event, (IStructuredSelection) selection);
148
							doIt |= event.doit;
149
							if (event.doit) {
150
								if (Policy.DEBUG_DND) {
151
									System.out
152
											.println("CommonDragAdapter.dragStart assistant - event.doit == true"); //$NON-NLS-1$
153
								}
154
								assistantsToUse.add(assistants[i]);
155
							}
156
						}
157

    
158
						event.doit = doIt;
159
					} else {
160
						event.doit = false;
161
					}
162
				} else {
163
					event.doit = false;
164
				}
165
			}
166
		});
167

    
168
		if (Policy.DEBUG_DND) {
169
			System.out.println("CommonDragAdapter.dragStart (end): doit=" + event.doit); //$NON-NLS-1$
170
		}
171
	}
172

    
173
	/*
174
	 * (non-Javadoc)
175
	 * 
176
	 * @see org.eclipse.swt.dnd.DragSourceAdapter#dragSetData(org.eclipse.swt.dnd.DragSourceEvent)
177
	 */
178
	public void dragSetData(final DragSourceEvent event) {
179

    
180
		final ISelection selection = LocalSelectionTransfer.getTransfer()
181
				.getSelection();
182

    
183
		if (Policy.DEBUG_DND) {
184
			System.out
185
					.println("CommonDragAdapter.dragSetData: event" + event + " selection=" + selection); //$NON-NLS-1$ //$NON-NLS-2$
186
		}
187

    
188
		if (LocalSelectionTransfer.getTransfer()
189
				.isSupportedType(event.dataType)) {
190
			event.data = selection;
191

    
192
			if (Policy.DEBUG_DND) {
193
				System.out
194
						.println("CommonDragAdapter.dragSetData set LocalSelectionTransfer: " + event.data); //$NON-NLS-1$
195
			}
196
		} else if (PluginTransfer.getInstance().isSupportedType(event.dataType)) {
197
			event.data = NavigatorPluginDropAction
198
					.createTransferData(contentService);
199
			if (Policy.DEBUG_DND) {
200
				System.out
201
						.println("CommonDragAdapter.dragSetData set PluginTransfer: " + event.data); //$NON-NLS-1$
202
			}
203
		} else if (selection instanceof IStructuredSelection) {
204
			if (Policy.DEBUG_DND) {
205
				System.out
206
						.println("CommonDragAdapter.dragSetData looking for assistants"); //$NON-NLS-1$
207
			}
208

    
209
			for (int i = 0, len = assistantsToUse.size(); i < len; i++) {
210
				final CommonDragAdapterAssistant assistant = (CommonDragAdapterAssistant) assistantsToUse.get(i); 
211
				if (Policy.DEBUG_DND) {
212
					System.out
213
							.println("CommonDragAdapter.dragSetData assistant: " + assistant); //$NON-NLS-1$
214
				}
215

    
216
				Transfer[] supportedTransferTypes = assistant
217
						.getSupportedTransferTypes();
218
				final boolean[] getOut = new boolean[1];
219
				for (int j = 0; j < supportedTransferTypes.length; j++) {
220
					if (supportedTransferTypes[j].isSupportedType(event.dataType)) {
221
						SafeRunner.run(new NavigatorSafeRunnable() {
222
							public void run() throws Exception {
223
								if (Policy.DEBUG_DND) {
224
									System.out
225
											.println("CommonDragAdapter.dragSetData supported xfer type"); //$NON-NLS-1$
226
								}
227
								if (assistant.setDragData(event, (IStructuredSelection) selection)) {
228
									if (Policy.DEBUG_DND) {
229
										System.out
230
												.println("CommonDragAdapter.dragSetData set data " + event.data); //$NON-NLS-1$
231
									}
232
									setDataAssistant = assistant;
233
									getOut[0] = true;
234
								}
235
							}
236
						});
237
						if (getOut[0])
238
							return;
239
					}
240
				}
241
			}
242

    
243
			if (Policy.DEBUG_DND) {
244
				System.out
245
						.println("CommonDragAdapter.dragSetData FAILED no assistant handled it"); //$NON-NLS-1$
246
			}
247
			event.doit = false;
248

    
249
		} else {
250
			if (Policy.DEBUG_DND) {
251
				System.out
252
						.println("CommonDragAdapter.dragSetData FAILED can't identify transfer type"); //$NON-NLS-1$
253
			}
254
			event.doit = false;
255
		}
256
	}
257
	 
258
	/*
259
	 * (non-Javadoc)
260
	 * 
261
	 * @see org.eclipse.swt.dnd.DragSourceAdapter#dragFinished(org.eclipse.swt.dnd.DragSourceEvent)
262
	 */
263
	public void dragFinished(DragSourceEvent event) {
264

    
265
		if (Policy.DEBUG_DND) {
266
			System.out.println("CommonDragAdapter.dragFinished(): " + event); //$NON-NLS-1$
267
		}
268

    
269
		ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
270

    
271
		if (event.doit && selection instanceof IStructuredSelection && setDataAssistant != null)
272
			setDataAssistant.dragFinished(event, (IStructuredSelection) selection);
273
			
274
		setDataAssistant = null;
275

    
276
		LocalSelectionTransfer.getTransfer().setSelection(null);
277

    
278
		// TODO Handle clean up if drop target was outside of workbench
279
		// if (event.doit != false) {
280
		//			
281
		// }
282
	}
283

    
284
}
(2-2/49)