verifyClassAnnotatedWith(formClazz, Form.class);
Field formField;
ParameterizedType modelFieldType;
Class<?> modelFieldInternalType;
Object formElement;
DataBindingContext context = new DataBindingContext();
for (Field modelField : modelClazz.getFields()) {
// we care for IObservableValues only
if (!(modelField.getGenericType() instanceof ParameterizedType)) continue;
modelFieldType = (ParameterizedType) modelField.getGenericType();
if (!IObservableValue.class.isAssignableFrom((Class<?>) modelFieldType.getRawType())) continue;
modelFieldInternalType = (Class<?>) modelFieldType.getActualTypeArguments()[0];
// determine the form representative
formField = formClazz.getField(modelField.getName());
boolean accessible = formField.isAccessible();
try {
setAccessible(formField, true);
formElement = formField.get(formObject);
if (formElement instanceof Container) {
try {
Object modelValue = modelField.get(modelObject);
if (String.class == modelFieldInternalType) {
IObservableValue<String> value = uncheckedCast(modelValue);
context.bind(value, observe((Container) formElement).text());
} else if (Boolean.class == modelFieldInternalType) {
IObservableValue<Boolean> value = uncheckedCast(modelValue);
context.bind(value, observe((Container) formElement).selected());
} else {
IObservableValue<?> value = (IObservableValue<?>) modelValue;
context.bind(value, observe((Container) formElement).value());
}
} catch(IllegalAccessException e) {
// can't access model-fields.. no binding.. all good..
}
}