small test with 2 contraints
diff --git a/org.eclipse.bpmn2.modeler.core/plugin.xml b/org.eclipse.bpmn2.modeler.core/plugin.xml index 321019d..e87d281 100644 --- a/org.eclipse.bpmn2.modeler.core/plugin.xml +++ b/org.eclipse.bpmn2.modeler.core/plugin.xml
@@ -44,6 +44,36 @@ </event> </target> </constraint> + <constraint + lang="OCL" + severity="ERROR" + mode="Live" + name="StartEventIncoming" + id="org.eclipse.bpmn2.modeler.core.validation.StartEvent.incoming" + statusCode="2" isEnabledByDefault="true"> + <description>Start Event must not have incoming flows</description> + <message>The {0} must not have incoming sequence flows.</message> + <target class="StartEvent" /> + + <![CDATA[ + self.incoming->isEmpty() + ]]> + </constraint> + <constraint + lang="OCL" + severity="ERROR" + mode="Live" + name="EndEventOutgoing" + id="org.eclipse.bpmn2.modeler.core.validation.EndEvent.outgoing" + statusCode="3" isEnabledByDefault="true"> + <description>End Event must not have outgoing flows</description> + <message>The {0} must not have outgoing sequence flows.</message> + <target class="EndEvent" /> + + <![CDATA[ + self.outgoing->isEmpty() + ]]> + </constraint> </constraints> </constraintProvider> </extension>
diff --git a/org.eclipse.bpmn2.modeler.core/src/org/eclipse/bpmn2/modeler/core/validation/LiveValidationContentAdapter.java b/org.eclipse.bpmn2.modeler.core/src/org/eclipse/bpmn2/modeler/core/validation/LiveValidationContentAdapter.java index 5d1f90f..2ce7acb 100644 --- a/org.eclipse.bpmn2.modeler.core/src/org/eclipse/bpmn2/modeler/core/validation/LiveValidationContentAdapter.java +++ b/org.eclipse.bpmn2.modeler.core/src/org/eclipse/bpmn2/modeler/core/validation/LiveValidationContentAdapter.java
@@ -12,12 +12,23 @@ ******************************************************************************/ package org.eclipse.bpmn2.modeler.core.validation; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Path; import org.eclipse.emf.common.notify.Notification; +import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.util.EContentAdapter; +import org.eclipse.emf.validation.model.ConstraintStatus; import org.eclipse.emf.validation.model.EvaluationMode; import org.eclipse.emf.validation.service.ILiveValidator; import org.eclipse.emf.validation.service.ModelValidationService; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; public class LiveValidationContentAdapter extends EContentAdapter { private ILiveValidator validator = null; @@ -32,13 +43,40 @@ validator = (ILiveValidator)ModelValidationService.getInstance().newValidator(EvaluationMode.LIVE); } - IStatus status = validator.validate(notification); + IStatus validationStatus = validator.validate(notification); - if (!status.isOK()) { - if (status.isMultiStatus()) { - status = status.getChildren()[0]; + if (validationStatus instanceof ConstraintStatus) { + ConstraintStatus status = (ConstraintStatus) validationStatus; + Resource resource = status.getTarget().eResource(); + IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new + Path(resource.getURI().toPlatformString(true))); + if (!status.isOK()) { + switch (status.getSeverity()) { + case IStatus.ERROR: + reportError(status); + break; + case IStatus.WARNING: + createMarker(file, IMarker.SEVERITY_WARNING, status.getMessage()); + break; + } } } } - + + + void createMarker(IResource resource, int severity, String msg) { + try { + IMarker m = resource.createMarker(IMarker.PROBLEM); + m.setAttribute(IMarker.MESSAGE, msg); + m.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH); + m.setAttribute(IMarker.SEVERITY, severity); + } catch (CoreException e) { + throw new RuntimeException(e); + } + } + + public void reportError(IStatus status) { + MessageDialog.openError(new Shell(Display.getCurrent()),"Error", status.getMessage()); + } + }