Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eee9e4b07a23f9e73079322c82c1d499c4140b3d (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
/*****************************************************************************
 * 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:
 *  Saadia Dhouib (CEA LIST) saadia.dhouib@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.robotml.diagram.common.utils;

import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.ModelsReader;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForSelection;
import org.eclipse.papyrus.infra.ui.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.uml.tools.model.UmlModel;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Package;

/**
 * This class is a Property tester used to check if current model (meaning the model currently opened in Papyrus) is a Proteus Model.
 * This class is used in order to create test for deciding whether a diagram creation command should be visible or not.
 * This property tester assumes that currently active editor is Papyrus, it should be used with care (simultaneously with a test to ensure Papyrus is
 * currently opened and active).
 *
 */
public class RobotmlSelectionTester extends PropertyTester {

	/** Tester ID for UML Model nature */
	public final static String IS_ROBOTML_MODEL = "isRobotmlModel";


	// public static String ROBOTML_ID = "RobotML";

	/** Default constructor */
	public RobotmlSelectionTester() {
	}

	/** Test the receiver against the selected property */
	public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {

		// Ensure Papyrus is the active editor
		IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		if ((editor == null) || (!(editor instanceof IMultiDiagramEditor))) {
			return false;
		}

		Object currentValue = null;
		if (IS_ROBOTML_MODEL.equals(property)) {
			currentValue = testRobotmlModelNature(receiver);
			return (currentValue == expectedValue);
		}


		return false;
	}

	/** True is root object is a UML Model with RobotML Profile (and sub profiles) applied */
	protected boolean testRobotmlModelNature(Object receiver) {
		boolean isRobotmlModel = false;



		EObject root = getRoot(receiver);
		if (root instanceof Package) {
			return (((Package) root).getAppliedProfile("RobotML") != null);

			// FIX: UMLUtil.getProfile() loads the profile into the resource set. This is not desired.
			//
			// Profile robotml = UMLUtil.getProfile(RobotMLPackage.eINSTANCE, root);
			//
			// if(((Package)root).isProfileApplied(robotml)) {
			// isRobotmlModel = true;
			// }
		}


		return isRobotmlModel;
	}


	/** Returns the root EObject of currently opened model */
	private EObject getRoot(Object receiver) {
		EObject root = null;

		if (receiver instanceof ISelection) {
			ISelection selection = (ISelection) receiver;
			if (selection.isEmpty()) {
				return null;
			}

			try {
				// this is the case where the selection is on the Project Explorer
				IStructuredSelection selectionstructured = (IStructuredSelection) selection;

				Object selectedElement = selectionstructured.getFirstElement();

				Object selectedAdapter = Platform.getAdapterManager().getAdapter(selectedElement, IFile.class);


				if (selectedAdapter instanceof IFile) {
					final IFile selectedFile = (IFile) selectedAdapter;
					ModelSet modelSet = new ModelSet();
					ModelsReader reader = new ModelsReader();
					reader.readModel(modelSet);


					IPath workspacePath = selectedFile.getFullPath();

					URI workspaceURI = URI.createPlatformResourceURI(workspacePath.toString(), true);
					modelSet.loadModels(workspaceURI);

					UmlModel openedModel = (UmlModel) modelSet.getModel(UmlModel.MODEL_ID);
					if (openedModel != null) {
						root = openedModel.lookupRoot();
					}
				} else {
					// this is the case where the selection is on the Model Explorer
					ServiceUtilsForSelection serviceUtils = ServiceUtilsForSelection.getInstance();
					UmlModel openedModel = (UmlModel) serviceUtils.getModelSet(selection).getModel(UmlModel.MODEL_ID);
					if (openedModel != null) {
						root = openedModel.lookupRoot();
					}
				}

			} catch (Exception e) {
				// Ignored: The selection cannot be used to retrieve the ServicesRegistry.
				// Do not log exceptions: this is just not a Papyrus/RobotML model
			}
		}

		return root;
	}


}

Back to the top