[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.tools.emf] Re: Attribute dependency validation

José,

I'm fond of the expression "Where there's a will there's a way". So few things are truly impossible given a genuine desire to achieve the goal.

Thanks for sharing the result of your effort.


José Parera wrote:
OK. It's not an easy trick but it works.

In my editor I use the EDataTypeValueHandler to validate inputs. So I've created a MyEDataTypeValueHandler class.

In this class I override the isValid(Object) method copying the overrided code and merging it with some code from the validate(EDataType, Object) method of the Diagnostician class. With some tweaking I could finally put the EObject in the context map and get it in the validateA method.

This is the code. Maybe it could be useful for somebody.

public class MyEDataTypeValueHandler extends EDataTypeValueHandler {

private EObject eObject;
public MyEDataTypeValueHandler(EDataType eDataType, EObject eObject) {
super(eDataType);
this.eObject = eObject;
}
public String isValid(Object valueObject) {
Object value;
if (eDataType.isInstance(valueObject)
&& !(eDataType.getInstanceClass() == Object.class && valueObject instanceof String)) {
value = valueObject;
} else {
String string = (String) valueObject;
try {
value = eDataType.getEPackage().getEFactoryInstance()
.createFromString(eDataType, string);
} catch (Exception exception) {
String message = exception.getClass().getName();
int index = message.lastIndexOf('.');
if (index >= 0) {
message = message.substring(index + 1);
}
if (exception.getLocalizedMessage() != null) {
message = message + ": " + exception.getLocalizedMessage();
}
return message;
}
}
Map context = new HashMap();
context.put(EValidator.SubstitutionLabelProvider.class,
Diagnostician.INSTANCE);
context.put(EValidator.class, Diagnostician.INSTANCE);
context.put(EObject.class, eObject);
BasicDiagnostic diagnostics = new BasicDiagnostic(
EObjectValidator.DIAGNOSTIC_SOURCE, 0,
EcorePlugin.INSTANCE.getString("_UI_DiagnosticRoot_diagnostic",
new Object[] { Diagnostician.INSTANCE.
getValueLabel(eDataType, value) }),
new Object[] { value, eDataType });
Diagnostician.INSTANCE.validate(eDataType, value, diagnostics, context);
if (diagnostics.getSeverity() == Diagnostic.OK) {
return null;
} else {
return ((Diagnostic) diagnostics.getChildren().get(0))
.getMessage().replaceAll("'", "''").replaceAll("\\{","'{'"); // }}
}
}


Thanks