Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 54e9d4ee56f8980309e0df30b0083815856fbaa6 (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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*******************************************************************************
 * Copyright (c) 2007, 2020 Borland Software Corporation, CEA LIST, Artal and others
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/ 
 * 
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors: 
 *    Dmitry Stadnik (Borland) - initial API and implementation
 *    Michael Golubev (Montages) - #386838 - migrate to Xtend2
 *    Aurelien Didier (ARTAL) - aurelien.didier51@gmail.com - Bug 569174
 *****************************************************************************/
package xpt.editor

import com.google.inject.Inject
import org.eclipse.papyrus.gmf.codegen.gmfgen.GenDiagram
import org.eclipse.papyrus.gmf.codegen.xtend.annotations.Localization
import org.eclipse.papyrus.gmf.codegen.xtend.annotations.MetaDef
import xpt.Common
import xpt.Externalizer
import xpt.ExternalizerUtils_qvto
import xpt.GenAuditRoot_qvto
import plugin.Activator
import xpt.providers.ValidationProvider
import xpt.providers.MarkerNavigationProvider
import xpt.providers.ValidationDecoratorProvider

@com.google.inject.Singleton class ValidateAction {
	@Inject extension Common;
	@Inject extension GenAuditRoot_qvto;
	@Inject extension ExternalizerUtils_qvto;

	@Inject Activator xptActivator;
	@Inject ValidationMarker xptValidationMarker;
	@Inject Externalizer xptExternalizer;
	@Inject DiagramEditorUtil xptDiagramEditorUtil;
	@Inject ValidationProvider xptValidationProvider;
	@Inject MarkerNavigationProvider xptMarkerNavigationProvider;
	@Inject ValidationDecoratorProvider xptValidationDecoratorProvider;

	@MetaDef def className(GenDiagram it) '''ValidateAction'''

	def packageName(GenDiagram it) '''«it.editorGen.editor.packageName»'''

	def qualifiedClassName(GenDiagram it) '''«packageName(it)».«className(it)»'''

	def fullPath(GenDiagram it) '''«qualifiedClassName(it)»'''

	def extendsList(GenDiagram it) '''extends org.eclipse.jface.action.Action'''

	def ValidateAction(GenDiagram it) '''
		«copyright(editorGen)»
		package «packageName(it)»;
		
		«generatedClassComment»
		public class «className(it)» «extendsList(it)» {
			«attributes(it)»
			«constructor(it)»
			«run(it)»
			«runValidation(it)»
			«runNonUIValidation(it)»
			«runValidationWithEP(it)»
			«runEMFValidator(it)»
			«validate(it)»
			«createMarkersForStatus(it)»
			«createMarkersForDiagnostic(it)»
			«addMarker(it)»
			«diagnosticToStatusSeverity(it)»
			«collectTargetElementsFromStatus(it)»
			«collectTargetElementsFromDiagnostic(it)»
			«additions(it)»
		}
	'''

	def attributes(GenDiagram it) '''
		«generatedMemberComment»
		private org.eclipse.ui.IWorkbenchPage page;
	'''

	def constructor(GenDiagram it) '''
		«generatedMemberComment»
		public «className(it)»(org.eclipse.ui.IWorkbenchPage page) {
			setText(«xptExternalizer.accessorCall(editorGen, messageKey(i18nKeyForValidateAction(it)))»);
			this.page = page;
		}
	'''

	def run(GenDiagram it) '''
		
		«generatedMemberComment»
		public void run() {
			org.eclipse.ui.IWorkbenchPart workbenchPart = page.getActivePart();
			if (workbenchPart instanceof org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart) {
				final org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart part =	(org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart) workbenchPart;
				try {
					«IF editorGen.application == null»
					new org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation(
					«ENDIF»
					new org.eclipse.jface.operation.IRunnableWithProgress() {
		
						public void run(org.eclipse.core.runtime.IProgressMonitor monitor)
							throws InterruptedException, java.lang.reflect.InvocationTargetException {
							runValidation(part.getDiagramEditPart(), part.getDiagram());
						}
					}
					«IF editorGen.application == null»
					)
					«ENDIF»
					.run(new org.eclipse.core.runtime.NullProgressMonitor());			
				} catch (Exception e) {
					«xptActivator.qualifiedClassName(editorGen.plugin)».getInstance().logError("Validation action failed", e); «nonNLS(1)»
				}
			}
		}
	'''

	def runValidation(GenDiagram it) '''
		
		«generatedMemberComment»
		public static void runValidation(org.eclipse.gmf.runtime.notation.View view) {
			try {
				if («xptDiagramEditorUtil.qualifiedClassName(it)».openDiagram(view.eResource())) {
					org.eclipse.ui.IEditorPart editorPart = org.eclipse.ui.PlatformUI.getWorkbench()
							.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
					if (editorPart instanceof org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart) {
						runValidation(((org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart) editorPart).
								getDiagramEditPart(), view);
					} else {
						runNonUIValidation(view);
					}
				}
			} catch (Exception e) {
				«xptActivator.qualifiedClassName(editorGen.plugin)».getInstance().logError(
						"Validation action failed", e); «nonNLS(1)»
			}
		}
	'''

	def runNonUIValidation(GenDiagram it) '''
		
		«generatedMemberComment»
		public static void runNonUIValidation(org.eclipse.gmf.runtime.notation.View view) {
			org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart diagramEditPart =
					org.eclipse.gmf.runtime.diagram.ui.OffscreenEditPartFactory.getInstance().createDiagramEditPart(
							view.getDiagram());
			runValidation(diagramEditPart, view);
		}		
	'''

	def runValidationWithEP(GenDiagram it) '''
		
		«generatedMemberComment»
		public static void runValidation(org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart diagramEditPart, org.eclipse.gmf.runtime.notation.View view) {
			final org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart fpart = diagramEditPart;
			final org.eclipse.gmf.runtime.notation.View fview = view;
			org.eclipse.emf.transaction.TransactionalEditingDomain txDomain = org.eclipse.emf.transaction.util.TransactionUtil.getEditingDomain(view);
			«xptValidationProvider.qualifiedClassName(it)».runWithConstraints(txDomain, new Runnable() {
		
			public void run() {
				validate(fpart, fview);
			}
			});
		}
	'''

	def runEMFValidator(GenDiagram it) '''
		
		«generatedMemberComment»
		private static org.eclipse.emf.common.util.Diagnostic runEMFValidator(
				org.eclipse.gmf.runtime.notation.View target) {
			if (target.isSetElement() && target.getElement() != null) {
			return new org.eclipse.emf.ecore.util.Diagnostician() {
		
			public String getObjectLabel(org.eclipse.emf.ecore.EObject eObject) {
				return org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil.getQualifiedName(eObject, true);
			}
				}.validate(target.getElement());
			}
			return org.eclipse.emf.common.util.Diagnostic.OK_INSTANCE;
		}
	'''

	def validate(GenDiagram it) '''
		
		«generatedMemberComment»
		private static void validate(org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart diagramEditPart,
				org.eclipse.gmf.runtime.notation.View view) {
			«IF editorGen.application == null»
			org.eclipse.core.resources.IFile target = view.eResource() != null ?
					org.eclipse.emf.workspace.util.WorkspaceSynchronizer.getFile(view.eResource()) : null;
			if (target != null) {
				«xptMarkerNavigationProvider.qualifiedClassName(it)».deleteMarkers(target);
			}
			«ELSE»
			org.eclipse.gmf.runtime.notation.View target = view;
			«xptValidationMarker.qualifiedClassName(it)».removeAllMarkers(diagramEditPart.getViewer());
			«ENDIF»
			org.eclipse.emf.common.util.Diagnostic diagnostic = runEMFValidator(view);
			createMarkers(target, diagnostic, diagramEditPart);
			org.eclipse.emf.validation.service.IBatchValidator validator =
			(org.eclipse.emf.validation.service.IBatchValidator)
				org.eclipse.emf.validation.service.ModelValidationService.getInstance().newValidator(
						org.eclipse.emf.validation.model.EvaluationMode.BATCH);
			validator.setIncludeLiveConstraints(true);
			if (view.isSetElement() && view.getElement() != null) {
			org.eclipse.core.runtime.IStatus status = validator.validate(view.getElement());
			createMarkers(target, status, diagramEditPart);
			}
			«IF shouldRunValidateOnDiagram(editorGen.audits)»
			«IF hasDiagramElementTargetRule(editorGen.audits)»
				validator.setTraversalStrategy(«xptValidationProvider.qualifiedClassName(it)».getNotationTraversalStrategy(validator));
			«ENDIF»
			org.eclipse.core.runtime.IStatus status = validator.validate(view);
			createMarkers(target, status, diagramEditPart);
			«ENDIF»
			«IF editorGen.application != null && validationDecorators»
			«xptValidationDecoratorProvider.qualifiedClassName(it)».refreshDecorators(view);
			for (java.util.Iterator it = view.eAllContents(); it.hasNext();) {
				org.eclipse.emf.ecore.EObject next = (org.eclipse.emf.ecore.EObject) it.next();
				if (next instanceof org.eclipse.gmf.runtime.notation.View) {
					«xptValidationDecoratorProvider.qualifiedClassName(it)».refreshDecorators(
							(org.eclipse.gmf.runtime.notation.View) next);
				}
			}
			«ENDIF»
		}
	'''

	def createMarkersForStatus(GenDiagram it) '''
		
		«generatedMemberComment»
		private static void createMarkers(
				«IF editorGen.application == null»org.eclipse.core.resources.IFile
				«ELSE»org.eclipse.gmf.runtime.notation.View«ENDIF» target,
				org.eclipse.core.runtime.IStatus validationStatus,
				org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart diagramEditPart) {
			if (validationStatus.isOK()) {
			return;
			}
			final org.eclipse.core.runtime.IStatus rootStatus = validationStatus;
			java.util.List allStatuses = new java.util.ArrayList();
			«xptDiagramEditorUtil.qualifiedClassName(it)».LazyElement2ViewMap element2ViewMap = new «xptDiagramEditorUtil.qualifiedClassName(it)».LazyElement2ViewMap(
				diagramEditPart.getDiagramView(),
				collectTargetElements(rootStatus, new java.util.HashSet<org.eclipse.emf.ecore.EObject>(), allStatuses));
			for (java.util.Iterator it = allStatuses.iterator(); it.hasNext();) {
			org.eclipse.emf.validation.model.IConstraintStatus nextStatus =
			(org.eclipse.emf.validation.model.IConstraintStatus) it.next();
			org.eclipse.gmf.runtime.notation.View view = «xptDiagramEditorUtil.qualifiedClassName(it)».findView(
			diagramEditPart, nextStatus.getTarget(), element2ViewMap);			
			addMarker(diagramEditPart.getViewer(), target, view.eResource().getURIFragment(view), 
			org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil.getQualifiedName(nextStatus.getTarget(), true), 
			nextStatus.getMessage(), nextStatus.getSeverity());
			}
		}
	'''

	def createMarkersForDiagnostic(GenDiagram it) '''
		
		«generatedMemberComment»
		private static void createMarkers(
				«IF editorGen.application == null»org.eclipse.core.resources.IFile
				«ELSE»org.eclipse.gmf.runtime.notation.View«ENDIF» target,
				org.eclipse.emf.common.util.Diagnostic emfValidationStatus,
				org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart diagramEditPart) {
			if (emfValidationStatus.getSeverity() == org.eclipse.emf.common.util.Diagnostic.OK) {
			return;
			}
			final org.eclipse.emf.common.util.Diagnostic rootStatus = emfValidationStatus;
			java.util.List allDiagnostics = new java.util.ArrayList();
			«xptDiagramEditorUtil.qualifiedClassName(it)».LazyElement2ViewMap element2ViewMap =
			new «xptDiagramEditorUtil.qualifiedClassName(it)».LazyElement2ViewMap(
				diagramEditPart.getDiagramView(),
				collectTargetElements(rootStatus, new java.util.HashSet<org.eclipse.emf.ecore.EObject>(), allDiagnostics));
			for (java.util.Iterator it = emfValidationStatus.getChildren().iterator(); it.hasNext();) {
			org.eclipse.emf.common.util.Diagnostic nextDiagnostic = (org.eclipse.emf.common.util.Diagnostic) it.next();
			java.util.List data = nextDiagnostic.getData();
			if (data != null && !data.isEmpty() && data.get(0) instanceof org.eclipse.emf.ecore.EObject) {
			org.eclipse.emf.ecore.EObject element = (org.eclipse.emf.ecore.EObject) data.get(0);
			org.eclipse.gmf.runtime.notation.View view = «xptDiagramEditorUtil.qualifiedClassName(it)».findView(
				diagramEditPart, element, element2ViewMap);
			addMarker(diagramEditPart.getViewer(), target, view.eResource().getURIFragment(view),
				org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil.getQualifiedName(element, true),
				nextDiagnostic.getMessage(), diagnosticToStatusSeverity(nextDiagnostic.getSeverity()));
			}
			}
		}
	'''

	def addMarker(GenDiagram it) '''
		
		«generatedMemberComment»
		private static void addMarker(org.eclipse.gef.EditPartViewer viewer,
				«IF editorGen.application == null»org.eclipse.core.resources.IFile
				«ELSE»org.eclipse.gmf.runtime.notation.View«ENDIF» target,
				String elementId, String location, String message, int statusSeverity) {
			if (target == null) {
			return;
			}
			«IF editorGen.application == null»
			«xptMarkerNavigationProvider.qualifiedClassName(it)».addMarker(
					target, elementId, location, message, statusSeverity);
			«ELSE»
			new «xptValidationMarker.qualifiedClassName(it)»(
					location, message, statusSeverity).add(viewer, elementId);
			«ENDIF»
		}
	'''

	def diagnosticToStatusSeverity(GenDiagram it) '''
		
		«generatedMemberComment»
		private static int diagnosticToStatusSeverity(int diagnosticSeverity) {
			if (diagnosticSeverity == org.eclipse.emf.common.util.Diagnostic.OK) {
				return org.eclipse.core.runtime.IStatus.OK;
			} else if (diagnosticSeverity == org.eclipse.emf.common.util.Diagnostic.INFO) {
				return org.eclipse.core.runtime.IStatus.INFO;
			} else if (diagnosticSeverity == org.eclipse.emf.common.util.Diagnostic.WARNING) {
				return org.eclipse.core.runtime.IStatus.WARNING;
			} else if (diagnosticSeverity == org.eclipse.emf.common.util.Diagnostic.ERROR
					|| diagnosticSeverity == org.eclipse.emf.common.util.Diagnostic.CANCEL) {
			return org.eclipse.core.runtime.IStatus.ERROR;
			}
			return org.eclipse.core.runtime.IStatus.INFO;
		}
	'''

	def collectTargetElementsFromStatus(GenDiagram it) '''
		
		«generatedMemberComment»
		private static java.util.Set<org.eclipse.emf.ecore.EObject> collectTargetElements(org.eclipse.core.runtime.IStatus status,
				java.util.Set<org.eclipse.emf.ecore.EObject> targetElementCollector, java.util.List allConstraintStatuses) {
			if (status instanceof org.eclipse.emf.validation.model.IConstraintStatus) {
			targetElementCollector.add(((org.eclipse.emf.validation.model.IConstraintStatus) status).getTarget());
			allConstraintStatuses.add(status);
			}
			if (status.isMultiStatus()) {
			org.eclipse.core.runtime.IStatus[] children = status.getChildren();
			for (int i = 0; i < children.length; i++) {
			collectTargetElements(children[i], targetElementCollector, allConstraintStatuses);				
			}
			}
			return targetElementCollector;
		}
	'''

	def collectTargetElementsFromDiagnostic(GenDiagram it) '''
		
		«generatedMemberComment»
		private static java.util.Set<org.eclipse.emf.ecore.EObject> collectTargetElements(org.eclipse.emf.common.util.Diagnostic diagnostic,
				java.util.Set<org.eclipse.emf.ecore.EObject> targetElementCollector, java.util.List allDiagnostics) {
			java.util.List data = diagnostic.getData();
			org.eclipse.emf.ecore.EObject target = null;
			if (data != null && !data.isEmpty() && data.get(0) instanceof org.eclipse.emf.ecore.EObject) {
			target = (org.eclipse.emf.ecore.EObject) data.get(0);
			targetElementCollector.add(target);	
			allDiagnostics.add(diagnostic);
			}
			if (diagnostic.getChildren() != null && !diagnostic.getChildren().isEmpty()) {
			for (java.util.Iterator it = diagnostic.getChildren().iterator(); it.hasNext();) {
			collectTargetElements((org.eclipse.emf.common.util.Diagnostic) it.next(),
				targetElementCollector, allDiagnostics);
			}
			}
			return targetElementCollector;
		}
	'''

	def additions(GenDiagram it) ''''''

	@Localization def i18nValues(GenDiagram it) '''
		«xptExternalizer.messageEntry(messageKey(i18nKeyForValidateAction(it)), 'Validate')»
	'''

	@Localization def i18nAccessors(GenDiagram it) '''
		«xptExternalizer.accessorField(messageKey(i18nKeyForValidateAction(it)))»
	'''

	@Localization def String i18nKeyForValidateAction(GenDiagram diagram) {
		return className(diagram).toString
	}

}

Back to the top