Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c50389d043bb51819aea2e235fd114bbb84e152a (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
/*****************************************************************************
 * Copyright (c) 2014 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:
 *  CEA LIST - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.junit.utils;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.Shape;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.resource.AbstractBaseModel;
import org.eclipse.papyrus.infra.core.resource.IModel;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationModel;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.uml2.uml.NamedElement;
import org.junit.Assert;

/**
 * Utility class for diagrams
 */
public class DiagramUtils {

	public static IGraphicalEditPart findEditPartforView(IMultiDiagramEditor papyrusEditor, View view, Class<? extends EditPart> editPartClass) {
		DiagramEditPart diagramEditPart = findDiagramEditPart(papyrusEditor);
		Object part = diagramEditPart.getViewer().getEditPartRegistry().get(view);
		Assert.assertNotNull("Cannot find the edit part", part);
		Assert.assertNotNull("part has not the right class", editPartClass.isAssignableFrom(part.getClass()));
		return (IGraphicalEditPart) part;
	}

	public static DiagramEditPart findDiagramEditPart(IMultiDiagramEditor papyrusEditor) {
		DiagramEditPart diagramEditPart = (DiagramEditPart) papyrusEditor.getAdapter(DiagramEditPart.class);
		Assert.assertNotNull("Cannot find the Diagram edit part", diagramEditPart);
		return diagramEditPart;
	}

	/**
	 * Search and returns the first view in the specified container view with the given name
	 * 
	 * @param container
	 *            the view container of the searched view
	 * @param elementName
	 *            the name of the element represented by the search view
	 * @return the found view or <code>null</code> if none was found
	 */
	public static Shape findShape(View container, String elementName) {
		for (Object viewObject : container.getChildren()) {
			View view = (View) viewObject;
			if (view instanceof Shape && view.getElement() instanceof NamedElement) {
				NamedElement element = (NamedElement) view.getElement();
				if (elementName.equals(element.getName())) {
					return (Shape) view;
				}
			}
		}


		// Assert.fail("Cannot find the view associated to " + elementName);
		return null;
	}



	/**
	 * Search and returns the first view in the specified container view with the given name
	 *
	 * @param container
	 *            the container
	 * @param elementName
	 *            the element name
	 * @return the edge
	 */
	public static Edge findEdge(View container, String elementName) {
		for (Object viewObject : container.eContents()) {
			if (viewObject instanceof View) {
				View view = (View) viewObject;
				if (view instanceof Edge && view.getElement() instanceof NamedElement) {
					NamedElement element = (NamedElement) view.getElement();
					if (elementName.equals(element.getName())) {
						return (Edge) view;
					}
				}
			}
		}


		// Assert.fail("Cannot find the view associated to " + elementName);
		return null;
	}


	/**
	 * Returns the int corresponding to the given tuple
	 * 
	 * @param red
	 * @param green
	 * @param blue
	 * @return
	 */
	public static int rgb(int red, int green, int blue) {
		return red | green << 8 | blue << 16;
	}

	public static RGB integerToRGB(int value) {
		int blue = value & 255;
		int green = (value >> 8) & 255;
		int red = (value >> 16) & 255;
		return new RGB(red, green, blue);
	}

	public static String integerToRGBString(int value) {
		RGB rgb = integerToRGB(value);
		return rgb.toString();

	}

	public static Diagram getNotationDiagram(ModelSet modelSet, String string) {
		IModel notationModel = modelSet.getModel(NotationModel.MODEL_ID);

		AbstractBaseModel notationBaseModel = null;
		if (notationModel instanceof AbstractBaseModel) {
			notationBaseModel = (AbstractBaseModel) notationModel;
		} else {
			Assert.fail("notation model is not an abstract base model"); //$NON-NLS-1$
			return null;
		}
		Assert.assertTrue("notation resource contains nothing", notationBaseModel.getResource().getContents().size() >= 1); //$NON-NLS-1$
		for (EObject object : notationBaseModel.getResource().getContents()) {
			if (object instanceof Diagram && string.equals(((Diagram) object).getName())) {
				return (Diagram) object;
			}
		}
		return null;
	}

	public static Collection<Diagram> getAllNotationDiagram(ModelSet modelSet, String string) {
		IModel notationModel = modelSet.getModel(NotationModel.MODEL_ID);
		Collection<Diagram> arrayList = new ArrayList<Diagram>();
		AbstractBaseModel notationBaseModel = null;
		if (notationModel instanceof AbstractBaseModel) {
			notationBaseModel = (AbstractBaseModel) notationModel;
		} else {
			Assert.fail("notation model is not an abstract base model");
			return null;
		}
		Assert.assertTrue("notation resource contains nothing", notationBaseModel.getResource().getContents().size() >= 1);
		for (EObject object : notationBaseModel.getResource().getContents()) {
			if (object instanceof Diagram && string.equals(((Diagram) object).getName())) {
				arrayList.add((Diagram) object);
			}
		}
		return arrayList;
	}


}

Back to the top