Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » [EMF Forms] Editing optional domain elements(How to edit optional elements and have them created only if some inner fields are set?)
[EMF Forms] Editing optional domain elements [message #1772811] Fri, 15 September 2017 14:51 Go to next message
Heiko Schaefer is currently offline Heiko SchaeferFriend
Messages: 7
Registered: April 2016
Junior Member
Greetings Community,

I need to modify a domain model with optional elements. It looks like this:
<root>
  <object>
    <optionalAttribute>
      <value>foo</value>
    </optionalAttribute>
    <mandatoryAttribute>
      <value>bar</value>
    </mandatoryAttribute>
  </object>
</root>

I use EMF Forms (1.10.0) to edit the model and Resource.save with a default XMI factory to serialize the model.
The processing shall follow the following idea:

  • The UI should only provide input fields for the "value" elements. There should be no controls to directly manipulate the parent elements.
  • If the input field for optionalAttribute.value is not empty, I want the element optionalAttribute to be created within the model and later serialized with the corresponding value as seen above.
  • If the input field is empty, I do not want the element to be created at all. At least it should not be serialized when the model is saved.

This means the result for an empty optionalAttribute.value should look like this:
<root>
  <object>
    <mandatoryAttribute>
      <value>bar</value>
    </mandatoryAttribute>
  </object>
</root>

Unfortunately, I am observing the following serialization:
<root>
  <object>
    <optionalAttribute/>
    <mandatoryAttribute>
      <value>bar</value>
    </mandatoryAttribute>
  </object>
</root>

This is a problem, because the serialization result also needs to withstand an XML Schema-based validation to be processed in subsequent steps.
For a valid XML model each optionalAttribute needs a value. This means if there is no optionalAttribute.value, then there should be no optionalAttribute element at all.
I tried to explicitly remove such empty elements from the domain model, but EMF Forms listens to such changes and instantly creates a new element when resolving the feature path domain model reference.

Callstack when EMF Forms recreates the optional element:
VFeaturePathDomainModelReferenceImpl.resolve(EObject, boolean) line: 381	
VFeaturePathDomainModelReferenceImpl.init(EObject) line: 475	
SettingToControlExpandHelper.expandAndInitDMR(EObject, EMFFormsDomainExpander, EObject, EMFFormsViewContext) line: 65	
SettingToControlExpandHelper.resolveDomainReferences(EObject, EObject, EMFFormsViewContext) line: 54	
ViewModelListener.notifyChange(ModelChangeNotification) line: 105	
ViewModelContextImpl$DomainModelContentAdapter.notifyChanged(Notification) line: 660	

Possibly the model element is needed to bind it to the UI element.

I would be pleased if you have any hint how to solve the problem. Is there a way to edit optional elements with EMF Forms and have them created only if some inner fields are set?

With kind regards
Heiko
Re: [EMF Forms] Editing optional domain elements [message #1772942 is a reply to message #1772811] Tue, 19 September 2017 08:40 Go to previous messageGo to next message
Eugen Neufeld is currently offline Eugen NeufeldFriend
Messages: 174
Registered: May 2015
Senior Member
Hi Heiko,

this is a tricky problem you have. EMFForms does not support this case out of the box. The reason for this is, that emfforms creates all intermediate objects for the controls as it doesn't know otherwise where to save the data.
I can think of two possible solutions:
1. You clean up your model during the save. So you create a copy of your data model (EcoreUtil.copy) and then manually remove all optional elements which are empty.
2. You create a custom renderer. In the view model you would bound the control to the optionalAttribute. This way EMFForms will not create the optionalAttribute object. In your custom renderer you would now need to handle the creation of this object whenever you enter a value into the control and delete the object whenever you clear the input.

I think that solution 1 would be easier to implement and be more stable.

Cheers,
Eugen


--
Eugen Neufeld

Get professional Eclipse developer support:
http://eclipsesource.com/en/services/developer-support/
Re: [EMF Forms] Editing optional domain elements [message #1772948 is a reply to message #1772942] Tue, 19 September 2017 09:55 Go to previous messageGo to next message
Heiko Schaefer is currently offline Heiko SchaeferFriend
Messages: 7
Registered: April 2016
Junior Member
Thank you for the advice, Eugen. Indeed, I already discussed possible implementations for your first approach with my colleagues. But eventually, we want to investigate the second approach in more detail, because in the long term we also expect other sub-attributes, which possibly require a special processing which can be dealt with a custom renderer.
Re: [EMF Forms] Editing optional domain elements [message #1773137 is a reply to message #1772948] Fri, 22 September 2017 07:10 Go to previous messageGo to next message
Heiko Schaefer is currently offline Heiko SchaeferFriend
Messages: 7
Registered: April 2016
Junior Member
I have implemented a custom renderer for the superordinate attribute as described in your second solution now. The renderer can create and delete the attribute on demand, depending on whether the subordinate value is set or not. But my problem is the validation. The model has constraints for both, the superordinate attribute and the subordinate value element. The validation result for the superordinate attribute is displayed correctly, but the validation result for the subordinate value element is missing. I suppose that the validation works, but EMF Forms has no information to which control the validation result should be linked. Is there a way to tell EMF Forms that both validation result should be linked to the same control?
Re: [EMF Forms] Editing optional domain elements [message #1773199 is a reply to message #1773137] Sat, 23 September 2017 14:01 Go to previous messageGo to next message
Eugen Neufeld is currently offline Eugen NeufeldFriend
Messages: 174
Registered: May 2015
Senior Member
Hi Heiko,

you will need to provide a Component based on the org.eclipse.emfforms.spi.core.services.mappingprovider.EMFFormsMappingProvider interface.
By default the org.eclipse.emfforms.internal.core.services.mappingprovider.defaultheuristic.EMFFormsMappingProviderDefaultHeuristic is used.
You would need to provide your own implementation. Relevant is the isApplicable method. If your implementation returns a higher value then the EMFFormsMappingProviderDefaultHeuristic, it will be used.
After the addition of the object in your control you also will need to call org.eclipse.emfforms.spi.core.services.controlmapper.EMFFormsSettingToControlMapper.checkAndUpdateSettingToControlMapping(EObject) with the VControl used to render your renderer as the passed EObejct.

I hope this helps.
Eugen


--
Eugen Neufeld

Get professional Eclipse developer support:
http://eclipsesource.com/en/services/developer-support/
Re: [EMF Forms] Editing optional domain elements [message #1773378 is a reply to message #1773199] Wed, 27 September 2017 05:54 Go to previous messageGo to next message
Heiko Schaefer is currently offline Heiko SchaeferFriend
Messages: 7
Registered: April 2016
Junior Member
Hello Eugen,

your hints were very helpful, thank you. My implementation of the EMFFormsMappingProvider uses the mapping of the best of the other implementations as a basis (typically the DefaultHeuristic Provider) and then adds the setting for the subordinate value element. However, I didn't fully understand the hint with the checkAndUpdateSettingToControlMapping. My perception is, that the application works without this update.

Kind regards,
Heiko
Re: [EMF Forms] Editing optional domain elements [message #1773408 is a reply to message #1773378] Wed, 27 September 2017 14:27 Go to previous message
Eugen Neufeld is currently offline Eugen NeufeldFriend
Messages: 174
Registered: May 2015
Senior Member
Hi Heiko,

I just added the hint about the ControlMapping as I was not sure whether it would be enough to add a custom EMFFormsMappingProvider.
If it is working for you then just ignore my comment.
If you encounter problems don't hesitate to ask.

Cheers,
Eugen


--
Eugen Neufeld

Get professional Eclipse developer support:
http://eclipsesource.com/en/services/developer-support/
Previous Topic:Dynamic creation of EObject
Next Topic:Instantiating and EMF object
Goto Forum:
  


Current Time: Tue Apr 23 07:39:19 GMT 2024

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

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

Back to the top