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;
}
}
]]>