emf databinding with unsetable primitive [message #1734445] |
Wed, 08 June 2016 08:12  |
Eclipse User |
|
|
|
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 #1734487 is a reply to message #1734483] |
Wed, 08 June 2016 12:17   |
Eclipse User |
|
|
|
Yes, like that 
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:53Yes 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 12:04  |
Eclipse User |
|
|
|
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;
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03822 seconds