Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9f1257e2ed05b810ef2c84810b428d6cb74adc42 (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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 *
 * 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:
 *  Jerome Benois (OBEO) - Initial API and implementation
 *  Francisco Javier Cano (PRODEVELOP)
 *  Thomas Szadel (ATOS) - Remove Backbone dependency
 *
 *****************************************************************************/
package org.eclipse.papyrus.views.modelexplorer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.ECrossReferenceAdapter;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.transaction.RunnableWithResult;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramGraphicalViewer;
import org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.papyrus.infra.core.resource.sasheditor.DiModel;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationModel;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

/**
 * Utility method for Model Navigator.
 *
 * @author Jerome Benois
 */
public class NavigatorUtils {

	/**
	 * Gets the roots of the notations resources related to the given object
	 *
	 * @param element
	 *            The object from which to retrieve the notation resources
	 * @return An iterator of notation resources' roots, or <code>null</code> if none cannot be resolved
	 */
	public static Iterator<EObject> getNotationRoots(EObject element) {
		Iterator<Resource> notations = getResources(element, NotationModel.NOTATION_FILE_EXTENSION);
		if (notations == null) {
			return null;
		}
		return new RootsIterator(notations);
	}

	public static Iterator<EObject> getDiRoots(EObject element) {
		Iterator<Resource> diResources = getResources(element, DiModel.DI_FILE_EXTENSION);
		if (diResources == null) {
			return null;
		}
		return new RootsIterator(diResources);
	}

	/**
	 * Represents an iterator on all the roots of a set of resources of a ResourceSet
	 *
	 * @author Laurent Wouters
	 */
	private static class RootsIterator implements Iterator<EObject> {

		private Iterator<Resource> resources;

		private Iterator<EObject> inner;

		public RootsIterator(Iterator<Resource> resources) {
			this.resources = resources;
			if (resources.hasNext()) {
				inner = resources.next().getContents().iterator();
			}
		}

		public boolean hasNext() {
			if (inner == null) {
				return false;
			}

			if (inner.hasNext()) {
				return true;
			}

			while (resources.hasNext()) {
				inner = resources.next().getContents().iterator();
				if (inner.hasNext()) {
					return true;
				}
			}

			inner = null;
			return false;
		}

		public EObject next() {
			if (inner == null) {
				return null;
			}

			if (inner.hasNext()) {
				return inner.next();
			}

			while (resources.hasNext()) {
				inner = resources.next().getContents().iterator();
				if (inner.hasNext()) {
					return inner.next();
				}
			}

			inner = null;
			return null;
		}

		public void remove() {
			throw new UnsupportedOperationException();
		}
	}


	/**
	 * Gets the notation resources related to the given object
	 *
	 * @param element
	 *            The object from which to retrieve the notation resources
	 * @return An iterator of notation resources, or <code>null</code> if none cannot be resolved
	 */
	public static Iterator<Resource> getResources(EObject element, String fileExtension) {
		Iterator<Resource> result = tryGetResources(element, fileExtension);
		if (result != null) {
			return result;
		}

		IAdaptable input = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getInput();
		if (input != null) {
			EObject obj = EMFHelper.getEObject(input);
			return tryGetResources(obj, fileExtension);
		}
		return null;
	}



	/**
	 * Tries to get the notation resources related to the given object
	 *
	 * @param element
	 *            The object from which to retrieve the notation resources
	 * @return An iterator of notation resources, or <code>null</code> if none cannot be resolved
	 */
	private static Iterator<Resource> tryGetResources(EObject element, String fileExtension) {
		if (element == null) {
			return null;
		}
		if (element.eResource() == null) {
			return null;
		}
		if (element.eResource().getResourceSet() == null) {
			return null;
		}
		return new ResourcesIterator(element.eResource().getResourceSet(), fileExtension);
	}

	/**
	 * Represents an iterator over the notation resources of a ResourceSet
	 *
	 * @author Laurent Wouters
	 */
	private static class ResourcesIterator implements Iterator<Resource> {

		private Iterator<Resource> inner;

		private Resource next;

		private String fileExtension;

		public ResourcesIterator(ResourceSet set, String fileExtension) {
			inner = set.getResources().iterator();
			this.fileExtension = fileExtension;
			next = getNextResource();
		}

		private Resource getNextResource() {
			while (inner.hasNext()) {
				Resource resource = inner.next();

				if (fileExtension.equalsIgnoreCase(resource.getURI().fileExtension())) {
					return resource;
				}

			}
			return null;
		}

		public boolean hasNext() {
			return (next != null);
		}

		public Resource next() {
			Resource result = next;
			next = getNextResource();
			return result;
		}

		public void remove() {
			throw new UnsupportedOperationException();
		}
	}


	/**
	 * Find a <IViewPart> by it's id string.
	 *
	 * @param viewID
	 *            the view id
	 *
	 * @return the i view part
	 */
	public static IViewPart findViewPart(final String viewID) {
		RunnableWithResult<IViewPart> runnable;
		Display.getDefault().syncExec(runnable = new RunnableWithResult.Impl<IViewPart>() {

			public void run() {
				IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				if (page == null) {
					setResult(null);
					return;
				}
				IViewReference reference = page.findViewReference(viewID);
				if (reference == null) {
					setResult(null);
					return;
				}
				IWorkbenchPart part = reference.getPart(false);
				if (part instanceof IViewPart) {
					setResult((IViewPart) part);
					return;
				} else {
					setResult(null);
					return;
				}
			}
		});

		return runnable.getResult();
	}

	/**
	 * Unwraps selection. Gets <EObject>s from <EditPart>s, from <View>s or from
	 * <EObject>s
	 *
	 * @param selection
	 *            the selection
	 *
	 * @return the i selection
	 */
	public static ISelection unwrapSelection(ISelection selection) {
		if (selection instanceof StructuredSelection && !selection.isEmpty()) {
			List<EObject> selectionList = new ArrayList<EObject>();
			StructuredSelection structuredSelection = (StructuredSelection) selection;
			for (Iterator<?> iterator = structuredSelection.iterator(); iterator.hasNext();) {
				Object next = iterator.next();
				if (next instanceof EditPart) {
					Object model = ((EditPart) next).getModel();
					EObject element = null;
					if (model instanceof View) {
						element = ((View) model).getElement();
					} else if (model instanceof EObject) {
						element = (EObject) model;
					}
					if (element != null) {
						selectionList.add(element);
					}
				} else if (next instanceof View) {
					EObject element = ((View) next).getElement();
					if (element != null) {
						selectionList.add(element);
					}
				}
				EObject eObject = EMFHelper.getEObject(next);
				if (eObject != null) {
					selectionList.add(eObject);
				}
			}
			return new StructuredSelection(selectionList);
		} else {
			return selection;
		}
	}

	/**
	 * Finds the <EditPart>s for the <EObject>s in the selection.
	 *
	 * @param selection
	 *            the selection
	 * @param viewer
	 *            the viewer
	 *
	 * @return the edits the parts from selection
	 */
	public static List<EditPart> getEditPartsFromSelection(ISelection selection, IDiagramGraphicalViewer viewer) {
		if (selection instanceof StructuredSelection && !selection.isEmpty()) {
			StructuredSelection structuredSelection = (StructuredSelection) selection;
			// look for Views of the EObjects in the selection
			List<View> views = new ArrayList<View>();
			for (Object o : structuredSelection.toList()) {
				if (o instanceof EObject) {
					List<Object> referencerViews = getEObjectViews((EObject) o);
					for (Object ro : referencerViews) {
						if (ro instanceof View) {
							views.add((View) ro);
						}
					}
				}
			}
			if (!views.isEmpty()) {
				List<EditPart> editParts = new ArrayList<EditPart>();
				for (View view : views) {
					Object ep = viewer.getEditPartRegistry().get(view);
					if (ep instanceof EditPart) {
						editParts.add((EditPart) ep);
					}
				}
				if (!editParts.isEmpty()) {
					return editParts;
				}
			}
		}
		return Collections.emptyList();
	}

	/**
	 * Gets the given <EObject> views.
	 *
	 * @param element
	 *            the element
	 *
	 * @return the e object views
	 */
	// @unused
	public static List<Object> getEObjectViews(EObject element) {
		List<Object> views = new ArrayList<Object>();
		if (element != null) {
			EReference[] features = { NotationPackage.eINSTANCE.getView_Element() };
			Collection<?> referencers = EMFCoreUtil.getReferencers(element, features);
			views.addAll(referencers);
		}
		return views;
	}

	// // //
	// // get an object name
	// // //
	//
	// /**
	// * Gets the object name or empty string.
	// *
	// * @param object
	// * the object
	// *
	// * @return the object name or empty string
	// */
	// // @unused
	// public static String getObjectNameOrEmptyString(Object object) {
	// String name = getObjectName(object);
	// return name == null ? "" : name;
	// }
	//
	// /** The Constant getNameNames. */
	// private static final String[] getNameNames = { "getName", "getname" };
	//
	// /**
	// * Gets the object name.
	// *
	// * @param object
	// * the object
	// *
	// * @return the object name
	// */
	// // @unused
	// public static String getObjectName(Object object) {
	// if (object == null) {
	// return null;
	// }
	// Method method = null;
	// Object o = null;
	// for (String methodName : getNameNames) {
	// try {
	// method = object.getClass()
	// .getMethod(methodName, (Class[]) null);
	// } catch (NoSuchMethodException e) {
	// method = null;
	// }
	// if (method != null) {
	// break;
	// }
	// }
	// if (method != null) {
	// try {
	// o = method.invoke(object, (Object[]) null);
	// } catch (IllegalAccessException ex) {
	// return null;
	// } catch (InvocationTargetException ex) {
	// return null;
	// }
	// if (o instanceof String) {
	// return (String) o;
	// }
	// }
	// return null;
	// }


	/**
	 * Opens a view part in the workbench with the specified ID.
	 *
	 * @param viewPartID
	 */
	// @unused
	public static void openViewPart(String viewPartID) {
		if (viewPartID == null) {
			return;
		}
		try {
			PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(viewPartID);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Opens the property sheets view.
	 */
	public static void openPropertySheetsView() {
		openViewPart("org.eclipse.ui.views.PropertySheet");
	}

	/**
	 * Use the IAdaptable mechanism
	 *
	 * @param o
	 * @param theClass
	 * @return
	 *
	 * @deprecated This method doesn't work with EMF Facet >= 0.2, for retrieving EObjects.
	 *             Use {@link EMFHelper#getEObject(Object)} instead
	 *             Note: If used for anything else than EObject.class, the method is still OK.
	 *             But it seems that it was its only usage
	 */
	@Deprecated
	@SuppressWarnings("unchecked")
	public static <T> T getElement(Object o, Class<T> theClass) {
		T result = null;
		if (o instanceof IAdaptable) {
			IAdaptable adaptable = (IAdaptable) o;
			result = adaptable.getAdapter(theClass);
		}
		if (result == null) {
			result = Platform.getAdapterManager().getAdapter(o, theClass);
		}
		if (result == null && theClass.isInstance(o)) {
			result = (T) o;
		}
		return result;
	}

	/**
	 * Tests whether at least one elements matches the given Predicate in the resource set
	 * This doesn't rely on the CrossReferencer. Instead, it will browse all the objects of the given Type
	 * in the ResourceSet.
	 *
	 * If "search all contents" is false, it will restrict the search to root elements
	 * of the same EPackage as the researched Type.
	 *
	 * For example, if we're looking for GMF Diagrams (type == Diagram), we will only look for root elements
	 * from the Notation metamodel (Excluding the underlying semantic model.
	 *
	 * @param referencedElement
	 *            The referenced element
	 * @param type
	 *            The type of the element which may contain references to the "referencedElement"
	 * @param searchAllContents
	 *            If false, the research will be restricted to the root elements of the same EPackage as "type"
	 * @param predicate
	 *            The predicate used to determine whether an object of type "type" has a reference to the "referencedElement"
	 *
	 *
	 * @return
	 *         True if at least one object matches the predicate and targets the referencedElement
	 */
	public static boolean any(EObject referencedElement, final EClass type, final boolean searchAllContents, Predicate<EObject> predicate) {
		if (referencedElement == null || referencedElement.eResource() == null || referencedElement.eResource().getResourceSet() == null) {
			return false;
		}

		ResourceSet resourceSet = referencedElement.eResource().getResourceSet();

		Predicate<EObject> composedPredicate = Predicates.and(new Predicate<EObject>() {

			public boolean apply(EObject arg0) {
				return type.isSuperTypeOf(arg0.eClass());
			}
		}, predicate);

		for (final Resource resource : resourceSet.getResources()) {

			Iterable<EObject> iterable = new Iterable<EObject>() {

				public Iterator<EObject> iterator() {
					Iterator<EObject> allContentsIterator;

					if (searchAllContents) {
						allContentsIterator = resource.getAllContents();
					} else {
						Collection<EObject> contentsToBrowse = new LinkedList<EObject>();

						for (EObject rootElement : resource.getContents()) {
							if (rootElement.eClass().getEPackage() == type.getEPackage()) {
								contentsToBrowse.add(rootElement);
							}
						}

						allContentsIterator = EcoreUtil.getAllProperContents(contentsToBrowse, false);
					}

					return allContentsIterator;
				}
			};

			if (Iterables.any(iterable, composedPredicate)) {
				return true;
			}
		}

		return false;
	}

	/**
	 * Search all the elements referencing the context,
	 * filter the results by the predicate
	 *
	 * @return
	 */
	// @unused for efficiency issues
	public static boolean find(EObject toFind, Predicate<Setting> predicate) {
		if (toFind == null || toFind.eResource() == null || toFind.eResource().getResourceSet() == null) {
			return false;
		}
		ResourceSet resourceSet = toFind.eResource().getResourceSet();
		ECrossReferenceAdapter adapter = ECrossReferenceAdapter.getCrossReferenceAdapter(resourceSet);
		if (adapter == null) {
			adapter = new ECrossReferenceAdapter();
			resourceSet.eAdapters().add(adapter);
		}
		Collection<Setting> settings = adapter.getInverseReferences(toFind, false);
		return Iterables.any(settings, predicate);
	}

	/**
	 * Search all the elements referencing the context,
	 * filter the results by the predicate and apply the function to return the desired types
	 *
	 * @return
	 */
	public static <T> Collection<T> findFilterAndApply(EObject toFind, Predicate<Setting> predicate, Function<Setting, T> function) {
		if (toFind == null || toFind.eResource() == null || toFind.eResource().getResourceSet() == null) {
			return Collections.emptyList();
		}
		ResourceSet resourceSet = toFind.eResource().getResourceSet();
		ECrossReferenceAdapter adapter = ECrossReferenceAdapter.getCrossReferenceAdapter(resourceSet);
		if (adapter == null) {
			adapter = new ECrossReferenceAdapter();
			resourceSet.eAdapters().add(adapter);
		}
		Collection<Setting> settings = adapter.getInverseReferences(toFind, false);
		return Lists.newLinkedList(Iterables.transform(Iterables.filter(settings, predicate), function));
	}
}

Back to the top