Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » emf databinding with unsetable primitive
emf databinding with unsetable primitive [message #1734445] Wed, 08 June 2016 12:12 Go to next message
Martin Jacob is currently offline Martin JacobFriend
Messages: 185
Registered: July 2009
Senior Member
Hi,

Use case:
displaying an empty text field for an unset


Problem:
I am unsing EMFDatabinding.


DataBindingContext bindingContext = new EMFDataBindingContext();
//
IObservableValue observeTextMteObserveWidget =
WidgetProperties.text(SWT.Modify).observeDelayed(400, mte);
IObservableValue propulsionTypeMaxTractEffortObserveValue =
EMFEditProperties.value(editingDomain,
Literals.PROPULSION_TYPE__MAX_TRACT_EFFORT)
.observe(propulsionType);
mte_b = bindingContext.bindValue(observeTextMteObserveWidget,
propulsionTypeMaxTractEffortObserveValue, null, null);

Question:

Does any one have an hint how to display an empty string in case the
property is unset?

Thanks in advance, Martin
Re: emf databinding with unsetable primitive [message #1734469 is a reply to message #1734445] Wed, 08 June 2016 14:44 Go to previous messageGo to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 836
Registered: July 2009
Senior Member
I assume your feature has a default value set in the ecore model.

You could use a converter that holds a reference to the EObject and checks if the feature is set when it is about to convert from model to target/UI.
Re: emf databinding with unsetable primitive [message #1734483 is a reply to message #1734469] Wed, 08 June 2016 15:53 Go to previous messageGo to next message
Martin Jacob is currently offline Martin JacobFriend
Messages: 185
Registered: July 2009
Senior Member
Yes the converter might be a solution. But you are right I have to
inject the EObject reference and Structural Feature Path.

So I have to add some thing like this when binding :

EMFUpdateValueStrategy strategy = new myEMFUpdateValueStrategy(eObject,
Package.Literals.MY_BOUND_FEATURE);
bindingContext.bindValue(observeTextObserveWidget, ObserveValue,
strategy, strategy);

Right?
Re: emf databinding with unsetable primitive [message #1734487 is a reply to message #1734483] Wed, 08 June 2016 16:17 Go to previous messageGo to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 836
Registered: July 2009
Senior Member
Yes, like that Smile
You need to have two separate strategies target-to-model and model-to-target (have a look at the bindValue() parameter names when setting the strateties, else it might be a no-op (convert from unset value to empty string, then convert from any string to empty string if the feature might not have been set), or keep the target-to-model strategy null as it will be set to a default.

I would suggest to have a look at Lars' data binding tutorial:

http://www.vogella.com/tutorials/EclipseDataBinding/article.html#databinding_converter

Martin Jacob wrote on Wed, 08 June 2016 17:53
Yes the converter might be a solution. But you are right I have to
inject the EObject reference and Structural Feature Path.

So I have to add some thing like this when binding :

EMFUpdateValueStrategy strategy = new myEMFUpdateValueStrategy(eObject,
Package.Literals.MY_BOUND_FEATURE);
bindingContext.bindValue(observeTextObserveWidget, ObserveValue,
strategy, strategy);

Right?

Re: emf databinding with unsetable primitive [message #1737256 is a reply to message #1734487] Wed, 06 July 2016 16:04 Go to previous message
Martin Jacob is currently offline Martin JacobFriend
Messages: 185
Registered: July 2009
Senior Member
Solved ;).

Note: Separate instances for target to model and model to target have to
be instantiated!

package de.bahntechnik.dd.util.eclipse.emf.databinding;

import org.eclipse.core.databinding.conversion.Converter;
import org.eclipse.core.databinding.conversion.IConverter;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.databinding.EMFUpdateValueStrategy;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.command.SetCommand;

public class EMFUpdateValueStrategyUnsettable extends 
EMFUpdateValueStrategy {

	private IConverter mSuperConverter = null;
	private String mUnsetDisplay = "";
	private EObject mEObj = null;

	public EMFUpdateValueStrategyUnsettable(EObject pEObj) {
		this(pEObj, "");
	}

	public EMFUpdateValueStrategyUnsettable(EObject pEObj, String 
pUnsetDisplay) {
		super();
		mEObj = pEObj;
		mUnsetDisplay = pUnsetDisplay;
	}

	@Override
	public IStatus validateAfterConvert(Object value) {
		return SetCommand.UNSET_VALUE.equals(value) ? Status.OK_STATUS : 
super.validateAfterConvert(value);
	}

	@Override
	public IStatus validateBeforeSet(Object value) {
		return SetCommand.UNSET_VALUE.equals(value) ? Status.OK_STATUS : 
super.validateBeforeSet(value);
	}

	@Override
	protected IConverter createConverter(Object fromType, Object toType) {
		mSuperConverter = super.createConverter(fromType, toType);

		if (fromType == String.class) {
			if (toType instanceof EAttribute) {
				final EAttribute eAttribute = (EAttribute) toType;
				if (eAttribute.isUnsettable()) {
					return new Converter(fromType, toType) {
						public Object convert(Object fromObject) {
							Object lRet = null;
							String value = fromObject == null ? null : fromObject.toString();
							if (value != null && value.isEmpty()) {
								lRet = SetCommand.UNSET_VALUE;
								// System.out.println("String2Attribute Unset: " + fromObject + 
": " + eAttribute);
							} else {
								lRet = mSuperConverter.convert(fromObject);
								// System.out.println("String2Attribute Set__: " + fromObject + 
": " + eAttribute);
							}
							return lRet;
						}
					};
				}
				return mSuperConverter;
			}
		} else if (toType == String.class) {
			if (fromType instanceof EAttribute) {
				final EAttribute eAttribute = (EAttribute) fromType;
				if (eAttribute.isUnsettable()) {
					return new Converter(fromType, toType) {
						public Object convert(Object fromObject) {
							Object lRet = null;
							if (false == mEObj.eIsSet(eAttribute)) {
								lRet = mUnsetDisplay;
								// System.out.println("Attribute2String Unset: " + fromObject + 
": " + eAttribute);
							} else {
								// System.out.println("Attribute2String Set__: " + fromObject + 
": " + eAttribute);
								lRet = mSuperConverter.convert(fromObject);
							}
							return lRet;
						}
					};
				}

				return mSuperConverter;
			}
		}
		return mSuperConverter;
	}
}

Previous Topic:[EEF 1.6] Using EEF 1.6 Standalone (Without Sirius)
Next Topic:UI - Search of a model element inside ecore file
Goto Forum:
  


Current Time: Thu Sep 21 08:53:37 GMT 2017

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

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