Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » How to use validation for EMFForms-generated controls embedded in a wizard?
How to use validation for EMFForms-generated controls embedded in a wizard? [message #1463185] Thu, 06 November 2014 09:48 Go to next message
Dmitri Pisarenko is currently offline Dmitri PisarenkoFriend
Messages: 24
Registered: September 2014
Junior Member
Hello!

Imagine I have an instance of an EMF model with validation methods. I've created a UI based on that model using EMFForms, and then embedded it into a wizard.

The model has several validation methods, e. g.

public class MyEntityImpl extends MinimalEObjectImpl.Container implements MyEntity {
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public boolean fieldXCorrect(DiagnosticChain chain, Map<?, ?> context) {
		// TODO: implement this method
		// -> specify the condition that violates the invariant
		// -> verify the details of the diagnostic, including severity and message
		// Ensure that you remove @generated or mark it @generated NOT
		final int fieldX = getFieldX();
		if ((fieldX < 1) || (fieldX > 10)) {
			if (chain != null) {
				chain.add
					(new BasicDiagnostic
						(Diagnostic.ERROR,
						 MyEntity.DIAGNOSTIC_SOURCE,
						 MyEntity.MY_ENTITY__FIELD_X_CORRECT,
						 "Field X is incorrect",
						 new Object [] { this }));
			}
			return false;
		}
		return true;
	}

}

In the wizard, I'm trying to

a) find out, if the currently entered data are correct and
b) if not, the error message.

Here's the code:

public class MyEntityWizard extends Wizard {
	private final MyEntity settings;
	
	public MyEntityWizard(final MyEntity settings) {
		super();
		this.settings = settings;
	}

	@Override
	public boolean performFinish() {
		final BasicDiagnostic chain = new BasicDiagnostic();
		final Map<Object, Object> context = new HashMap<Object, Object>();
				
		boolan fieldXCorrect = settings.fieldXCorrect(chain, context);
		
		System.out.println("chain.getMessage(): " + chain.getMessage()); // chain.getMessage() is null
		
		
		return false;
	}
}

How can I get the text of the error message (which is generated in MyEntityImpl.fieldXCorrect) in MyEntityWizard.performFinish?

Currently chain.getMessage() returns null.

Thanks in advance

Dmitri
Re: How to use validation for EMFForms-generated controls embedded in a wizard? [message #1463309 is a reply to message #1463185] Thu, 06 November 2014 12:34 Go to previous messageGo to next message
Jonas Helming is currently offline Jonas HelmingFriend
Messages: 699
Registered: July 2009
Senior Member
To validate objects, you can use the following call:
Diagnostic validate = Diagnostician.INSTANCE.validate(eObject)

Best regards

Jonas

Am 06.11.2014 10:48, schrieb Dmitri Pisarenko:
> Hello!
>
> Imagine I have an instance of an EMF model with validation methods. I've
> created a UI based on that model using EMFForms, and then embedded it
> into a wizard.
>
> The model has several validation methods, e. g.
> public class MyEntityImpl extends MinimalEObjectImpl.Container
> implements MyEntity {
> /**
> * <!-- begin-user-doc -->
> * <!-- end-user-doc -->
> * @generated NOT
> */
> public boolean fieldXCorrect(DiagnosticChain chain, Map<?, ?>
> context) {
> // TODO: implement this method
> // -> specify the condition that violates the invariant
> // -> verify the details of the diagnostic, including severity
> and message
> // Ensure that you remove @generated or mark it @generated NOT
> final int fieldX = getFieldX();
> if ((fieldX < 1) || (fieldX > 10)) {
> if (chain != null) {
> chain.add
> (new BasicDiagnostic
> (Diagnostic.ERROR,
> MyEntity.DIAGNOSTIC_SOURCE,
> MyEntity.MY_ENTITY__FIELD_X_CORRECT,
> "Field X is incorrect",
> new Object [] { this }));
> }
> return false;
> }
> return true;
> }
>
> }
>
> In the wizard, I'm trying to
>
> a) find out, if the currently entered data are correct and
> b) if not, the error message.
>
> Here's the code:
>
> public class MyEntityWizard extends Wizard {
> private final MyEntity settings;
>
> public MyEntityWizard(final MyEntity settings) {
> super();
> this.settings = settings;
> }
>
> @Override
> public boolean performFinish() {
> final BasicDiagnostic chain = new BasicDiagnostic();
> final Map<Object, Object> context = new HashMap<Object, Object>();
>
> boolan fieldXCorrect = settings.fieldXCorrect(chain, context);
>
> System.out.println("chain.getMessage(): " + chain.getMessage());
> // chain.getMessage() is null
>
>
> return false;
> }
> }
>
> How can I get the text of the error message (which is generated in
> MyEntityImpl.fieldXCorrect) in MyEntityWizard.performFinish?
>
> Currently chain.getMessage() returns null.
>
> Thanks in advance
>
> Dmitri
Re: How to use validation for EMFForms-generated controls embedded in a wizard? [message #1463354 is a reply to message #1463309] Thu, 06 November 2014 13:35 Go to previous messageGo to next message
Dmitri Pisarenko is currently offline Dmitri PisarenkoFriend
Messages: 24
Registered: September 2014
Junior Member
Hello Jonas!

Thanks for your answer.

Now I can get the severity of the error, but when I call validationResult.getMessage(), it returns a text like "Diagnosis of MyEntityImpl@717ee050{VIRTAUAL_URI#/}".

How can I make validationResult.getMessage() return some human-readable error?

Thanks

Dmitri
Re: How to use validation for EMFForms-generated controls embedded in a wizard? [message #1463479 is a reply to message #1463354] Thu, 06 November 2014 15:56 Go to previous messageGo to next message
Dmitri Pisarenko is currently offline Dmitri PisarenkoFriend
Messages: 24
Registered: September 2014
Junior Member
Hello again!

I also noticed that whenever I change the value of a particular validated field, I get the exception

Caused by: org.eclipse.emf.common.util.BasicEList$BasicIndexOutOfBoundsException: index=1, size=1
	at org.eclipse.emf.common.util.BasicEList.get(BasicEList.java:346)
	at org.eclipse.emf.ecp.view.internal.validation.ValidationServiceImpl.getDiagnosticForEObject(ValidationServiceImpl.java:529)
	at org.eclipse.emf.ecp.view.internal.validation.ValidationServiceImpl.validateAndCollectSettings(ValidationServiceImpl.java:468)
	at org.eclipse.emf.ecp.view.internal.validation.ValidationServiceImpl.validate(ValidationServiceImpl.java:359)
	at org.eclipse.emf.ecp.view.internal.validation.ValidationServiceImpl$ValidationDomainModelChangeListener.notifyChange(ValidationServiceImpl.java:185)
	at org.eclipse.emf.ecp.view.internal.context.ViewModelContextImpl$DomainModelContentAdapter.notifyChanged(ViewModelContextImpl.java:602)
	at org.eclipse.emf.common.notify.impl.BasicNotifierImpl.eNotify(BasicNotifierImpl.java:374)
	at MyEntityImpl.setGeneralPurposeRegisterCount(MyEntityImpl.java:201)
	at MyEntityImpl.eSet(MyEntityImpl.java:315)
	at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eSet(BasicEObjectImpl.java:1071)
	at org.eclipse.emf.edit.command.SetCommand.doExecute(SetCommand.java:722)
	at org.eclipse.emf.edit.command.AbstractOverrideableCommand.execute(AbstractOverrideableCommand.java:125)
	at org.eclipse.emf.common.command.BasicCommandStack.execute(BasicCommandStack.java:78)
	... 108 more


How can I fix it?

Thanks

Dmitri
Re: How to use validation for EMFForms-generated controls embedded in a wizard? [message #1463514 is a reply to message #1463479] Thu, 06 November 2014 16:44 Go to previous messageGo to next message
Dmitri Pisarenko is currently offline Dmitri PisarenkoFriend
Messages: 24
Registered: September 2014
Junior Member
Hello again!

That error seems to occur in method

org.eclipse.emf.ecp.view.internal.validation.ValidationServiceImpl.getDiagnosticForEObject

	public Diagnostic getDiagnosticForEObject(EObject object) {
		EValidator validator = EValidator.Registry.INSTANCE.getEValidator(object.eClass().getEPackage());
		final BasicDiagnostic diagnostics = Diagnostician.INSTANCE.createDefaultDiagnostic(object);

		if (validator == null) {
			validator = new EObjectValidator();
		}
		final ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(new AdapterFactory[] {
			new ReflectiveItemProviderAdapterFactory(),
			new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE) });
		final Map<Object, Object> context = new LinkedHashMap<Object, Object>();
		context.put(EValidator.SubstitutionLabelProvider.class, new ECPSubstitutionLabelProvider(adapterFactory));
		context.put(EValidator.class, validator);

		validator.validate(object, diagnostics, context);

		adapterFactory.dispose();

		final Map<EStructuralFeature, DiagnosticChain> diagnosticMap = new LinkedHashMap<EStructuralFeature, DiagnosticChain>();
		for (final Diagnostic child : diagnostics.getChildren()) {
			if (DiagnosticChain.class.isInstance(child)) {
				diagnosticMap.put((EStructuralFeature) child.getData().get(1), (DiagnosticChain) child); // Error occurs here
			}
		}
		[...]
	}

child is an instance of BasicDiagnostic ("Diagnostic ERROR source=MyEntity code=2 Field X is incorrect data=[MyEntityImpl@4f13c045 (...)]").

child.getData().size() is 1, therefore a call to child.getData().get(1) leads to an exception.

If there are any ideas on how to fix, please tell me.

Thanks

Dmitri
Re: How to use validation for EMFForms-generated controls embedded in a wizard? [message #1464147 is a reply to message #1463514] Fri, 07 November 2014 08:30 Go to previous messageGo to next message
Jonas Helming is currently offline Jonas HelmingFriend
Messages: 699
Registered: July 2009
Senior Member
Hi,

you need to adapt the result you produce in the method #fieldXCorrect
The Object array should contain the affected EFeature as a second
parameter. Also you can add a message here. As it seems you already
added a message. Of what type is your validationResult?

Best regards

Jonas

Am 06.11.2014 17:44, schrieb Dmitri Pisarenko:
> Hello again!
>
> That error seems to occur in method
>
> org.eclipse.emf.ecp.view.internal.validation.ValidationServiceImpl.getDiagnosticForEObject
>
>
> public Diagnostic getDiagnosticForEObject(EObject object) {
> EValidator validator =
> EValidator.Registry.INSTANCE.getEValidator(object.eClass().getEPackage());
> final BasicDiagnostic diagnostics =
> Diagnostician.INSTANCE.createDefaultDiagnostic(object);
>
> if (validator == null) {
> validator = new EObjectValidator();
> }
> final ComposedAdapterFactory adapterFactory = new
> ComposedAdapterFactory(new AdapterFactory[] {
> new ReflectiveItemProviderAdapterFactory(),
> new
> ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE)
> });
> final Map<Object, Object> context = new LinkedHashMap<Object,
> Object>();
> context.put(EValidator.SubstitutionLabelProvider.class, new
> ECPSubstitutionLabelProvider(adapterFactory));
> context.put(EValidator.class, validator);
>
> validator.validate(object, diagnostics, context);
>
> adapterFactory.dispose();
>
> final Map<EStructuralFeature, DiagnosticChain> diagnosticMap =
> new LinkedHashMap<EStructuralFeature, DiagnosticChain>();
> for (final Diagnostic child : diagnostics.getChildren()) {
> if (DiagnosticChain.class.isInstance(child)) {
> diagnosticMap.put((EStructuralFeature)
> child.getData().get(1), (DiagnosticChain) child); // Error occurs here
> }
> }
> [...]
> }
>
> child is an instance of BasicDiagnostic ("Diagnostic ERROR
> source=MyEntity code=2 Field X is incorrect data=[MyEntityImpl@4f13c045
> (...)]").
>
> child.getData().size() is 1, therefore a call to child.getData().get(1)
> leads to an exception.
>
> If there are any ideas on how to fix, please tell me.
>
> Thanks
>
> Dmitri
Re: How to use validation for EMFForms-generated controls embedded in a wizard? [message #1469222 is a reply to message #1464147] Tue, 11 November 2014 12:07 Go to previous message
Dmitri Pisarenko is currently offline Dmitri PisarenkoFriend
Messages: 24
Registered: September 2014
Junior Member
Hello!

Thanks.

I fixed the problem by replacing

(new BasicDiagnostic
						(Diagnostic.ERROR,
						 MyEntity.DIAGNOSTIC_SOURCE,
						 MyEntity.MY_ENTITY__FIELD_X_CORRECT,
						 "Field X is incorrect",
						 new Object [] { this }));


by

(new BasicDiagnostic
						(Diagnostic.ERROR,
						 MyEntity.DIAGNOSTIC_SOURCE,
						 MyEntity.MY_ENTITY__FIELD_X_CORRECT,
						 "Field X is incorrect",
						 new Object [] { this, eClass().getEStructuralFeature("fieldX") }));


Best regards

Dmitri
Previous Topic:How to increase margins in EMFForms-generated forms?
Next Topic:[EMFClient] Running e3 demo application
Goto Forum:
  


Current Time: Thu Apr 18 21:45:17 GMT 2024

Powered by FUDForum. Page generated in 0.02217 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top