Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9ba4385ee819ba08261432c3d47b959484680193 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*******************************************************************************
 * Copyright (c) 2012, 2013 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.ui.navigator.dnd;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.runtime.callback.Callback;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistableNodeProperties;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IURIPersistenceService;
import org.eclipse.tcf.te.runtime.services.ServiceManager;
import org.eclipse.tcf.te.tcf.core.peers.Peer;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.ILocatorModelLookupService;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.ILocatorModelRefreshService;
import org.eclipse.tcf.te.tcf.locator.model.Model;
import org.eclipse.tcf.te.ui.views.Managers;
import org.eclipse.tcf.te.ui.views.interfaces.ICategory;
import org.eclipse.tcf.te.ui.views.interfaces.IRoot;
import org.eclipse.tcf.te.ui.views.interfaces.IUIConstants;
import org.eclipse.tcf.te.ui.views.interfaces.categories.ICategorizable;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.navigator.CommonDropAdapter;
import org.eclipse.ui.navigator.CommonNavigator;

/**
 * Common DND operation implementations.
 */
public class CommonDnD {

	/**
	 * If the current selection is draggable.
	 *
	 * @param selection The currently selected nodes.
	 * @return true if it is draggable.
	 */
	public static boolean isDraggable(IStructuredSelection selection) {
		if (selection.isEmpty()) {
			return false;
		}
		Object[] objects = selection.toArray();
		for (Object object : objects) {
			if (!isDraggableObject(object)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * If the specified object is a draggable element.
	 *
	 * @param object The object to be dragged.
	 * @return true if it is draggable.
	 */
	private static boolean isDraggableObject(Object object) {
		return object instanceof IPeerModel;
	}

	/**
	 * Perform the drop operation over dragged selection.
	 *
	 * @param dropAdapter The common drop adapter.
	 * @param target The target Object to be moved to.
	 * @param operations The current DnD operation.
	 * @param selection The local selection being dropped.
	 * @return true if the dropping is successful.
	 */
	public static boolean dropLocalSelection(CommonDropAdapter dropAdapter, Object target, int operations, IStructuredSelection selection) {

		boolean result = false;
		boolean refreshModel = false;

		ICategory catToSelect = null;
		Object elementToSelect = null;

		Iterator<?> iterator = selection.iterator();
		while (iterator.hasNext()) {
			Object element = iterator.next();
			if (!isDraggableObject(element)) {
				continue;
			}

			ICategorizable categorizable = getCategorizable(element);
			if (categorizable == null || !isDraggableObject(element)) {
				continue;
			}
			ICategory[] parentCategories = getParentCategories(element, selection);
			if (parentCategories.length == 0) {
				continue;
			}

			for (ICategory parentCategory : parentCategories) {
				if (target instanceof ICategory) {
					ICategory category = (ICategory) target;

					if (element instanceof IPeerModel && category.getId().equals(parentCategory.getId()) && ((IPeerModel)element).isStatic()) {
						List<String> usedNames = getUsedNames();
						Map<String,String> attrs = new HashMap<String,String>(((IPeerModel)element).getPeer().getAttributes());
						attrs.put(IPeer.ATTR_ID, UUID.randomUUID().toString());
						attrs.remove(IPersistableNodeProperties.PROPERTY_URI);
						int i = 0;
						String baseName = attrs.get(IPeer.ATTR_NAME);
						baseName = baseName.replaceAll("\\s*\\([\\d*]\\)$", ""); //$NON-NLS-1$ //$NON-NLS-2$
						String name = baseName + " (" + i + ")"; //$NON-NLS-1$ //$NON-NLS-2$
						while (usedNames.contains(name.toUpperCase())) {
							i++;
							name = baseName + " (" + i + ")"; //$NON-NLS-1$ //$NON-NLS-2$
						}
						attrs.put(IPeer.ATTR_NAME, name);
						IPeer newPeer = new Peer(attrs);
						// Save the new peer
						IURIPersistenceService persistenceService = ServiceManager.getInstance().getService(IURIPersistenceService.class);
						if (persistenceService != null) {
							try {
								persistenceService.write(newPeer, null);
								refreshModel = true;
								if (catToSelect == null || elementToSelect == null) {
									catToSelect = category;
									elementToSelect = newPeer;
								}
								result = true;
							}
							catch (Exception e) {
							}
						}
					}
					else if (!Managers.getCategoryManager().isLinked(category.getId(), categorizable.getId()) &&
								categorizable.isValid(ICategorizable.OPERATION.ADD, parentCategory, category)) {
						Managers.getCategoryManager().add(category.getId(), categorizable.getId());
						if (catToSelect == null || elementToSelect == null) {
							catToSelect = category;
							elementToSelect = element;
						}
						result = true;
					}
				}
				else if (target instanceof IRoot) {
					if (Managers.getCategoryManager().isLinked(parentCategory.getId(), categorizable.getId())) {
						Managers.getCategoryManager().remove(parentCategory.getId(), categorizable.getId());
						catToSelect = parentCategory;
						result = true;
					}
				}
			}
		}

		if (result) {
			final CommonNavigator cNav = getCommonNavigator();
			if (refreshModel) {
				final ICategory finalCat = catToSelect;
				final Object finalElement = elementToSelect;
				final IPeer finalNewPeer = (elementToSelect instanceof IPeer) ? (IPeer)elementToSelect : null;
                // Trigger a refresh of the model to read in the newly created static peer
                final ILocatorModelRefreshService service = Model.getModel().getService(ILocatorModelRefreshService.class);
                if (service != null) {
                	Runnable runnable = new Runnable() {
                		@Override
                        public void run() {
                            service.refresh(new Callback() {
                            	@Override
                                protected void internalDone(Object caller, org.eclipse.core.runtime.IStatus status) {
            						IPeerModel peerModel = null;
                    				if (finalNewPeer != null) {
                    					ILocatorModelLookupService service = Model.getModel().getService(ILocatorModelLookupService.class);
                    					if (service != null) {
                    						peerModel = service.lkupPeerModelById(finalNewPeer.getID());
                    					}
                    				}
                    				refresh(cNav, finalCat, peerModel != null ? peerModel : finalElement);
                            	}
                            });

                		}
                	};
                	if (Protocol.isDispatchThread())
                		runnable.run();
                	else
                		Protocol.invokeLater(runnable);
                }
                else {
    				refresh(cNav, catToSelect, elementToSelect);
                }
			}
			else {
				refresh(cNav, catToSelect, elementToSelect);
			}
		}

		return result;
	}

	protected static void refresh(final CommonNavigator cNav, final ICategory category, final Object element) {
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				cNav.getCommonViewer().refresh();
				if (category != null) {
					cNav.getCommonViewer().setSelection(new StructuredSelection(category), true);
					cNav.getCommonViewer().expandToLevel(category, 1);
				}
				if (element != null)
					cNav.getCommonViewer().setSelection(new TreeSelection(new TreePath(new Object[]{category, element})), true);
			}
		};

		// Execute asynchronously
		if (PlatformUI.isWorkbenchRunning()) {
			PlatformUI.getWorkbench().getDisplay().asyncExec(runnable);
		}
	}

	protected static CommonNavigator getCommonNavigator() {
		final AtomicReference<IViewPart> viewPart = new AtomicReference<IViewPart>();
		// Create the runnable
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				// Check the active workbench window and active page instances
				if (PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null && PlatformUI
				                .getWorkbench().getActiveWorkbenchWindow().getActivePage() != null) {
					// show the view
					try {
						viewPart.set(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(IUIConstants.ID_EXPLORER));
					}
					catch (Exception e) {
					}
				}
			}
		};
		// Execute asynchronously
		if (PlatformUI.isWorkbenchRunning()) {
			PlatformUI.getWorkbench().getDisplay().syncExec(runnable);
		}

		return viewPart.get() instanceof CommonNavigator ? (CommonNavigator)viewPart.get() : null;
	}

	/**
	 * Validate dropping when the elements being dragged are local selection.
	 *
	 * @param dropAdapter The common drop adapter.
	 * @param target The target object.
	 * @param operation The DnD operation.
	 * @param transferType The transfered data type.
	 *
	 * @return true if it is valid for dropping.
	 */
	public static boolean validateLocalSelectionDrop(CommonDropAdapter dropAdapter, Object target, int operation, TransferData transferType) {
		int overrideOperation = -1;
		boolean valid = false;

		LocalSelectionTransfer transfer = LocalSelectionTransfer.getTransfer();
		IStructuredSelection selection = (IStructuredSelection) transfer.getSelection();

		boolean allow = true;
		boolean link = false;
		boolean copy = false;

		Iterator<?> iterator = selection.iterator();
		while (allow && iterator.hasNext()) {
			Object element = iterator.next();
			if (!isDraggableObject(element)) {
				allow = false;
				break;
			}

			ICategorizable categorizable = getCategorizable(element);
			if (categorizable == null || !isDraggableObject(element)) {
				allow = false;
				break;
			}
			ICategory[] parentCategories = getParentCategories(element, selection);
			if (parentCategories.length == 0) {
				allow = false;
			}

			for (ICategory parentCategory : parentCategories) {
				if (target instanceof ICategory) {
					ICategory category = (ICategory) target;

					if (!link && element instanceof IPeerModel && category.getId().equals(parentCategory.getId()) && ((IPeerModel)element).isStatic()) {
						overrideOperation = DND.DROP_COPY;
						copy = true;
					}
					else if (!copy && !Managers.getCategoryManager().isLinked(category.getId(), categorizable.getId()) &&
								categorizable.isValid(ICategorizable.OPERATION.ADD, parentCategory, category)) {
						overrideOperation = DND.DROP_LINK;
						link = true;
					}
					else {
						allow = false;
						break;
					}

				}
				else if (target instanceof IRoot) {
					overrideOperation = DND.DROP_DEFAULT;
					if (!Managers.getCategoryManager().isLinked(parentCategory.getId(), categorizable.getId())) {
						allow = false;
						break;
					}
				}
			}
		}
		valid = allow;

		if (dropAdapter != null) {
			if (!valid) {
				dropAdapter.overrideOperation(DND.DROP_NONE);
			}
			else if (overrideOperation != -1) {
				dropAdapter.overrideOperation(overrideOperation);
			}
			else {
				dropAdapter.overrideOperation(operation);
			}
		}

		return valid;
	}

	protected static ICategory[] getParentCategories(Object element, ISelection selection) {
		List<ICategory> candidates = new ArrayList<ICategory>();
		// To determine the parent category, we have to look at the tree path
		TreePath[] pathes = selection instanceof TreeSelection ? ((TreeSelection)selection).getPathsFor(element) : null;
		if (pathes != null && pathes.length > 0) {
			for (TreePath path : pathes) {
				TreePath parentPath = path.getParentPath();
				while (parentPath != null) {
					if (parentPath.getLastSegment() instanceof ICategory) {
						if (!candidates.contains(parentPath.getLastSegment())) {
							candidates.add((ICategory)parentPath.getLastSegment());
							break;
						}
					}
					parentPath = parentPath.getParentPath();
				}
			}
		}
		return candidates.toArray(new ICategory[candidates.size()]);
	}

	protected static ICategorizable getCategorizable(Object element) {
	    ICategorizable categorizable = element instanceof IAdaptable ? (ICategorizable)((IAdaptable)element).getAdapter(ICategorizable.class) : null;
    	if (categorizable == null) categorizable = (ICategorizable)Platform.getAdapterManager().getAdapter(element, ICategorizable.class);
    	return categorizable;
	}

	protected static List<String> getUsedNames() {
		final List<String> usedNames = new ArrayList<String>();

		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				// Get all peer model objects
				IPeerModel[] peers = Model.getModel().getPeers();
				// Loop them and find the ones which are of our handled types
				for (IPeerModel peerModel : peers) {
					if (peerModel.isStatic()) {
						String name = peerModel.getPeer().getName();
						Assert.isNotNull(name);
						if (!"".equals(name) && !usedNames.contains(name)) { //$NON-NLS-1$
							usedNames.add(name.trim().toUpperCase());
						}
					}
				}
			}
		};

		if (Protocol.isDispatchThread())
			runnable.run();
		else
			Protocol.invokeAndWait(runnable);
		return usedNames;
	}
}

Back to the top