blob: 18d0e4ff750f89dda4cf1a09919c582cc8614da2 [file] [log] [blame]
/*
* Copyright (c) 2007, 2010 Borland Software Corporation and others
*
* 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'»
«EXTENSION xpt::editor::Utils»
«EXTENSION xpt::GenAuditRoot»
«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) {
setTextEXPAND 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.setTraversalStrategygetValidationProviderQualifiedClassName()».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 «EXPAND CodeStyle::G('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 = «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 «EXPAND CodeStyle::G('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 = «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 «EXPAND CodeStyle::G('java.util.Set', 'org.eclipse.emf.ecore.EObject' collectTargetElements(org.eclipse.core.runtime.IStatus status,
«EXPAND CodeStyle::G('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;
}
«ENDDEFINE»
«DEFINE collectTargetElementsFromDiagnostic FOR gmfgen::GenDiagram
«EXPAND xpt::Common::generatedMemberComment»
private static «EXPAND CodeStyle::G('java.util.Set', 'org.eclipse.emf.ecore.EObject' collectTargetElements(org.eclipse.emf.common.util.Diagnostic diagnostic,
«EXPAND CodeStyle::G('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;
}
«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»