Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d8123bf84be5a957efa1f745b35735e34ab2baae (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
/*
 * Copyright (c) 2007, 2009 Borland Software Corporation
 * 
 * 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:
 *    Dmitry Stadnik (Borland) - initial API and implementation
 */

«IMPORT 'http://www.eclipse.org/gmf/2009/GenModel'»
«IMPORT 'http://www.eclipse.org/emf/2002/GenModel'»
«IMPORT 'http://www.eclipse.org/emf/2002/Ecore'»

«EXTENSION xpt::editor::Utils»
«EXTENSION xpt::GenAuditRoot»
«EXTENSION xpt::GenEditorGenerator»

«DEFINE className FOR gmfgen::GenDiagram»«defaultValidateActionClassName(self)»«ENDDEFINE»

«DEFINE qualifiedClassName FOR gmfgen::GenDiagram»«editorGen.editor.packageName».«EXPAND className»«ENDDEFINE»

«DEFINE ValidateAction FOR gmfgen::GenDiagram-»
«EXPAND xpt::Common::copyright FOR editorGen-»
package «editorGen.editor.packageName»;

«EXPAND xpt::Common::generatedClassComment»
public class «EXPAND className» extends org.eclipse.jface.action.Action {
	«EXPAND attributes-»
	«EXPAND _constructor-»
	«EXPAND run-»
	«EXPAND runValidation-»
	«EXPAND runNonUIValidation-»
	«EXPAND runValidationWithEP-»
	«EXPAND runEMFValidator-»
	«EXPAND validate-»
	«EXPAND createMarkersForStatus-»
	«EXPAND createMarkersForDiagnostic-»
	«EXPAND addMarker-»
	«EXPAND diagnosticToStatusSeverity-»
	«EXPAND collectTargetElementsFromStatus-»
	«EXPAND collectTargetElementsFromDiagnostic-»
	«EXPAND additions-»
}
«ENDDEFINE»

«DEFINE attributes FOR gmfgen::GenDiagram»
	«EXPAND xpt::Common::generatedMemberComment»
	private org.eclipse.ui.IWorkbenchPage page;
«ENDDEFINE»

«DEFINE _constructor FOR gmfgen::GenDiagram»
	«EXPAND xpt::Common::generatedMemberComment»
	public «EXPAND className»(org.eclipse.ui.IWorkbenchPage page) {
		setText(«EXPAND xpt::Externalizer::accessorCall(messageKey(i18nKeyForValidateAction(self))) FOR editorGen»);
		this.page = page;
	}
«ENDDEFINE»

«DEFINE run FOR gmfgen::GenDiagram-»

	«EXPAND xpt::Common::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) {
				«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError("Validation action failed", e);«EXPAND xpt::Common::nonNLS»
			}
		}
	}
«ENDDEFINE»

«DEFINE runValidation FOR gmfgen::GenDiagram-»

	«EXPAND xpt::Common::generatedMemberComment»
	public static void runValidation(org.eclipse.gmf.runtime.notation.View view) {
		try {
			if («getDiagramEditorUtilQualifiedClassName()».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) {
			«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError(
					"Validation action failed", e); «EXPAND xpt::Common::nonNLS»
		}
	}
«ENDDEFINE»

«DEFINE runNonUIValidation FOR gmfgen::GenDiagram-»

	«EXPAND xpt::Common::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);
	}		
«ENDDEFINE»

«DEFINE runValidationWithEP FOR gmfgen::GenDiagram-»

	«EXPAND xpt::Common::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);
		«getValidationProviderQualifiedClassName()».runWithConstraints(txDomain, new Runnable() {

			public void run() {
				validate(fpart, fview);
			}
		});
	}
«ENDDEFINE»

«DEFINE runEMFValidator FOR gmfgen::GenDiagram-»

	«EXPAND xpt::Common::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;
	}
«ENDDEFINE»

«DEFINE validate FOR gmfgen::GenDiagram-»

	«EXPAND xpt::Common::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) {
			«getMarkerNavigationProviderQualifiedClassName()».deleteMarkers(target);
		}
		«ELSE-»
		org.eclipse.gmf.runtime.notation.View target = view;
		«EXPAND xpt::editor::ValidationMarker::qualifiedClassName».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(«getValidationProviderQualifiedClassName()».getNotationTraversalStrategy(validator));
«ENDIF-»
		org.eclipse.core.runtime.IStatus status = validator.validate(view);
		createMarkers(target, status, diagramEditPart);
«ENDIF-»
		«IF editorGen.application <> null and validationDecorators-»
		«getValidationDecoratorProviderQualifiedClassName()».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) {
				«getValidationDecoratorProviderQualifiedClassName()».refreshDecorators(
						(org.eclipse.gmf.runtime.notation.View) next);
			}
		}
		«ENDIF-»
	}
«ENDDEFINE»

«DEFINE createMarkersForStatus FOR gmfgen::GenDiagram-»

	«EXPAND xpt::Common::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();
		«getDiagramEditorUtilQualifiedClassName()».LazyElement2ViewMap element2ViewMap =
				new «getDiagramEditorUtilQualifiedClassName()».LazyElement2ViewMap(
						diagramEditPart.getDiagramView(),
						collectTargetElements(rootStatus, new java.util.HashSet(), 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 = «getDiagramEditorUtilQualifiedClassName()».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());
		}
	}
«ENDDEFINE»

«DEFINE createMarkersForDiagnostic FOR gmfgen::GenDiagram-»

	«EXPAND xpt::Common::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();
		«getDiagramEditorUtilQualifiedClassName()».LazyElement2ViewMap element2ViewMap =
				new «getDiagramEditorUtilQualifiedClassName()».LazyElement2ViewMap(
						diagramEditPart.getDiagramView(),
						collectTargetElements(rootStatus, new java.util.HashSet(), 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 = «getDiagramEditorUtilQualifiedClassName()».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()));
			}
		}
	}
«ENDDEFINE»

«DEFINE addMarker FOR gmfgen::GenDiagram-»

	«EXPAND xpt::Common::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-»
		«getMarkerNavigationProviderQualifiedClassName()».addMarker(
				target, elementId, location, message, statusSeverity);
		«ELSE-»
		new «EXPAND xpt::editor::ValidationMarker::qualifiedClassName»(
				location, message, statusSeverity).add(viewer, elementId);
		«ENDIF-»
	}
«ENDDEFINE»

«DEFINE diagnosticToStatusSeverity FOR gmfgen::GenDiagram-»

	«EXPAND xpt::Common::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;
	}
«ENDDEFINE»

«DEFINE collectTargetElementsFromStatus FOR gmfgen::GenDiagram-»

	«EXPAND xpt::Common::generatedMemberComment»
	private static java.util.Set collectTargetElements(org.eclipse.core.runtime.IStatus status,
			java.util.Set 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;
	}
«ENDDEFINE»

«DEFINE collectTargetElementsFromDiagnostic FOR gmfgen::GenDiagram-»

	«EXPAND xpt::Common::generatedMemberComment»
	private static java.util.Set collectTargetElements(org.eclipse.emf.common.util.Diagnostic diagnostic,
			java.util.Set 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;
	}
«ENDDEFINE»

«DEFINE additions FOR gmfgen::GenDiagram»«ENDDEFINE»

«DEFINE i18nValues FOR gmfgen::GenDiagram-»
«EXPAND xpt::Externalizer::messageEntry(messageKey(i18nKeyForValidateAction(self)), 'Validate')-»
«ENDDEFINE»

«DEFINE i18nAccessors FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Externalizer::accessorField(messageKey(i18nKeyForValidateAction(self)))-»
«ENDDEFINE»

Back to the top