Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7d783690becbea591c992596eaae97ca6241a73e (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
/*****************************************************************************
 * Copyright (c) 2012 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.helper;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;

/**
 * A Helper class related to the GMF Notation metamodel.
 * 
 * @author Camille Letavernier
 */
public class NotationHelper {

	/**
	 * Retrieves the GMF View associated to the source object
	 * 
	 * @param source
	 *        the source
	 * @return the resolved view, or null if it cannot be found
	 */
	public static View findView(Object source) {
		if(source instanceof View) {
			return (View)source;
		}
		if(source instanceof IAdaptable) {
			IAdaptable adaptable = (IAdaptable)source;
			Object adapter = adaptable.getAdapter(View.class);
			if(adapter != null) {
				return (View)adapter;
			}
			adapter =  EMFHelper.getEObject(adaptable);
			if(adapter instanceof View) {
				return (View)adapter;
			}
		}
		return null;
	}

	/**
	 * Tests whether the given View is a reference to an external element.
	 * A view is an external reference if its graphical container is different from its semantic
	 * container (i.e. self.element.eContainer() != self.primaryView.eContainer().element)
	 * 
	 * @param diagramElement
	 * @return
	 */
	public static boolean isExternalRef(View diagramElement) {
		if(diagramElement == null) {
			return false;
		}

		View primaryView = SemanticElementHelper.findTopView(diagramElement);
		if(primaryView == null) {
			return false;
		}

		EObject semanticElement = primaryView.getElement();

		if(semanticElement == null) {
			return false;
		}

		EObject parentView = primaryView.eContainer();
		if(!(parentView instanceof View)) {
			return false;
		}

		EObject parentSemanticElement = ((View)parentView).getElement();
		if(parentSemanticElement == null) {
			return false;
		}

		//Relax the constraints for elements displayed on themselves (e.g. Frame in Composite Structure Diagram)
		return parentSemanticElement != semanticElement.eContainer() && parentSemanticElement != semanticElement;
	}
}

Back to the top