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

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PrecisionRectangle;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.Bounds;
import org.eclipse.gmf.runtime.notation.Node;
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 instanceof View) {
				return (View) adapter;
			}
		}

		if (source != null) {
			return Platform.getAdapterManager().getAdapter(source, View.class);
		}

		EObject obj = EMFHelper.getEObject(source);
		return (obj instanceof View) ? (View) obj : 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;
	}

	/**
	 * get the absolute position form the notation
	 * 
	 * @param node
	 *            the current node
	 * @return
	 * @since 3.0
	 */
	public static PrecisionRectangle getAbsoluteBounds(Node node) {
		if (node.getLayoutConstraint() instanceof Bounds) {
			PrecisionRectangle bounds = new PrecisionRectangle(((Bounds) node.getLayoutConstraint()).getX(), ((Bounds) node.getLayoutConstraint()).getY(), ((Bounds) node.getLayoutConstraint()).getWidth(), ((Bounds) node.getLayoutConstraint()).getHeight());
			EObject currentView = (EObject) node.eContainer();
			while (currentView != null) {

				if (currentView instanceof Node) {
					Point ptCurrenview = new Point(((Bounds) ((Node) currentView).getLayoutConstraint()).getX(), ((Bounds) ((Node) currentView).getLayoutConstraint()).getY());
					bounds.translate(ptCurrenview);
				}
				currentView = currentView.eContainer();
			}
			return bounds;
		} else
			return null;
	}
}

Back to the top