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 |
Dmitri Pisarenko 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 |
Jonas Helming 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 #1463514 is a reply to message #1463479] |
Thu, 06 November 2014 16:44 |
Dmitri Pisarenko 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 |
Jonas Helming 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 |
Dmitri Pisarenko 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
|
|
|
Goto Forum:
Current Time: Thu Apr 18 21:45:17 GMT 2024
Powered by FUDForum. Page generated in 0.02217 seconds
|