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
|
/*******************************************************************************
* Copyright (c) 2011 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.qompass.designer.core.handlers;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.ListSelectionDialog;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageImport;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.UMLFactory;
/**
* This class adds the FCM profile, parts of the MARTE profile and required package imports to your model.
*/
public class AddProfileAndModelLibsHandler extends CmdHandler {
public static final String FCM_PROFILE_URI = "pathmap://FCM_PROFILES/FCM.profile.uml"; //$NON-NLS-1$
public static final String MARTE_PROFILE_URI = "pathmap://Papyrus_PROFILES/MARTE.profile.uml";//$NON-NLS-1$
public static final String EC3M_BASIC_CALLS_URI = "pathmap://QML_CORE/core.uml"; //$NON-NLS-1$
public static final String EC3M_MARTE_CALLS_URI = "pathmap://QML_MARTE/marte.uml"; //$NON-NLS-1$
static final String MARTE_FOUNDATIONS = "MARTE_Foundations"; //$NON-NLS-1$
static final String MARTE_F_ALLOC = "Alloc"; //$NON-NLS-1$
static final String MARTE_DESIGN_MODEL = "MARTE_DesignModel"; //$NON-NLS-1$
static final String MARTE_DM_HLAM ="HLAM"; //$NON-NLS-1$
static final String MARTE_DM_GCM ="GCM"; //$NON-NLS-1$
public static final int APPLY_FCM = 1;
public static final int APPLY_ALLOC = 2;
public static final int APPLY_HLAM_GCM = 4;
/**
* {@inheritDoc}
*/
@Override
public boolean isEnabled() {
updateSelectedEObject();
EObject selectedObj = getSelectedEObject();
if(selectedObj instanceof Package) {
return true;
}
return false;
}
/**
* Retrieve a model library from the repository
*
* @param uri
* the URI of the repository
*
* @return
*/
public PackageImport getModelLibraryImportFromURI(URI uri, ResourceSet resourceSet) {
// Try to reach model
Element root = getContent(uri, resourceSet);
if(root instanceof Package) {
// Import model library
Package libToImport = (Package)root;
// create import package
PackageImport modelLibImport = UMLFactory.eINSTANCE.createPackageImport();
modelLibImport.setImportedPackage(libToImport);
return modelLibImport;
}
return null;
}
public static Element getContent(URI uri, ResourceSet rs) {
// Resource resource = getTransactionalEditingDomain ().getResourceSet().getResource (uri, true);
Resource resource = rs.getResource(uri, true);
return getContent(resource);
}
public static Element getContent(Resource resource) {
EList<EObject> contentObj = resource.getContents();
if((contentObj.size() > 0) && (contentObj.get(0) instanceof Element)) {
return (Element)contentObj.get(0);
}
return null;
}
/**
* Check whether a package import is already done
*
* @param selectedPkg
* @param pi
* @return
*/
boolean isAlreadyImported(Package selectedPkg, PackageImport pi) {
for(PackageImport existingPI : selectedPkg.getPackageImports()) {
if((existingPI.getImportedPackage() == null) ||
(pi.getImportedPackage() == null)) {
// import package are null (should not happen?!)
continue;
}
if((existingPI.getImportedPackage().getName() == null) ||
(pi.getImportedPackage().getName() == null)) {
// import package name not set (should not happen?!)
continue;
} else if(existingPI.getImportedPackage().getName().equals(pi.getImportedPackage().getName())) {
return true;
}
}
return false;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
*/
/**
* {@inheritDoc}
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
if(!(getSelectedEObject() instanceof Package)) {
return null;
}
final Package selectedPkg = (Package)getSelectedEObject();
final TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(selectedPkg);
final ResourceSet resourceSet = selectedPkg.eResource().getResourceSet();
CommandStack stack = domain.getCommandStack();
PackageImport pi = getModelLibraryImportFromURI(URI.createURI(EC3M_BASIC_CALLS_URI), resourceSet);
EList<Object> list = new BasicEList<Object>();
EList<Object> selection = new BasicEList<Object>();
if(pi != null) {
list.add(pi);
if(isAlreadyImported(selectedPkg, pi)) {
selection.add(pi);
}
}
pi = getModelLibraryImportFromURI(URI.createURI(EC3M_MARTE_CALLS_URI), resourceSet);
if(pi != null) {
list.add(pi);
if(isAlreadyImported(selectedPkg, pi)) {
selection.add(pi);
}
}
final ListSelectionDialog dialog = new ListSelectionDialog(new Shell(),
list.toArray(),
new ArrayContentProvider(),
new LabelProvider() {
public String getText(Object pi) {
return ((PackageImport)pi).getImportedPackage().getName();
}
},
"Select Qompass model libraries for package import.\n" + //$NON-NLS-1$
"Libraries that are already imported, are selected. Please note that additional\n" + //$NON-NLS-1$
"Qompass model library might be loaded with the standard \"import registered library\" option\n" + //$NON-NLS-1$
"\n" +//$NON-NLS-1$
"Pressing \"Ok\" will continue with the application of the FCM profile and (parts of) the MARTE profile\n");//$NON-NLS-1$
dialog.setTitle("Select import library"); //$NON-NLS-1$
dialog.setInitialElementSelections(selection);
// dialog.setElements (list.toArray ());
int result = dialog.open();
if(result == IDialogConstants.OK_ID) {
stack.execute(new RecordingCommand(domain, "Add libraries and profiles") //$NON-NLS-1$
{
public void doExecute() {
// add primitive types
// create import package to primitiveType
// PackageImport pi = getModelLibraryImportFromURI (URI.createURI(UMLResource.UML_PRIMITIVE_TYPES_LIBRARY_URI));
// selectedPkg.getPackageImports().add(pi);
for(Object obj : dialog.getResult()) {
PackageImport pi = (PackageImport)obj;
if(!isAlreadyImported(selectedPkg, pi)) {
selectedPkg.getPackageImports().add(pi);
}
}
addProfiles(selectedPkg, APPLY_FCM | APPLY_ALLOC | APPLY_HLAM_GCM);
}
});
}
return null;
}
public static void addProfiles(Package selectedPkg, int applyCode) {
final ResourceSet resourceSet = selectedPkg.eResource().getResourceSet();
try {
/*
* // Apply UML Standard profile
* // Retrieve standard profile
* Profile umlStdProfile =
* (Profile) getContent (URI.createURI(UMLResource.STANDARD_PROFILE_URI));
* // Apply to new model
* umlModel.applyProfile(umlStdProfile);
*/
if((applyCode & APPLY_FCM) != 0) {
// Retrieve FCM profile
Profile fcmProfile =
(Profile)getContent(URI.createURI(FCM_PROFILE_URI), resourceSet);
// Apply FCM profile and its nested profiles to new model
if(fcmProfile instanceof Profile) {
Profile profile = selectedPkg.getAppliedProfile(fcmProfile.getQualifiedName());
if((profile == null) && (!fcmProfile.getOwnedStereotypes().isEmpty())) {
selectedPkg.applyProfile(fcmProfile);
}
}
else {
MessageDialog.openWarning(new Shell(),
"Warning", //$NON-NLS-1$
"The FCM profile is not available."); //$NON-NLS-1$
}
}
if((applyCode & (APPLY_ALLOC | APPLY_HLAM_GCM)) != 0) {
// Retrieve MARTE profile
Profile marteProfile =
(Profile)getContent(URI.createURI(MARTE_PROFILE_URI), resourceSet);
// Apply MARTE::MARTE_DesignModel::HLAM
// & MARTE::MARTE_DesignModel::GCM
if(marteProfile != null) {
PackageableElement foundationModel = marteProfile.getPackagedElement(MARTE_FOUNDATIONS);
if((foundationModel instanceof Package) && ((applyCode & APPLY_ALLOC) != 0)) {
PackageableElement alloc = ((Package)foundationModel).getPackagedElement(MARTE_F_ALLOC);
// if(selectedPkg.getAppliedProfile(alloc.getQualifiedName()) == null) {
selectedPkg.applyProfile((Profile)alloc);
// }
}
PackageableElement designModel = marteProfile.getPackagedElement(MARTE_DESIGN_MODEL);
if((designModel instanceof Package) && ((applyCode & APPLY_HLAM_GCM) != 0)) {
PackageableElement hlam = ((Package)designModel).getPackagedElement(MARTE_DM_HLAM);
if(hlam instanceof Profile) {
// if(selectedPkg.getAppliedProfile(hlam.getQualifiedName()) == null) {
selectedPkg.applyProfile((Profile)hlam);
// }
}
PackageableElement gcm = ((Package)designModel).getPackagedElement(MARTE_DM_GCM);
if(gcm instanceof Profile) {
// if(selectedPkg.getAppliedProfile(gcm.getQualifiedName()) == null) {
selectedPkg.applyProfile((Profile)gcm);
// }
}
}
}
else {
MessageDialog.openWarning(new Shell(),
"Warning", "The MARTE profile is not available."); //$NON-NLS-1$ //$NON-NLS-2$
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
|