io.mateu.mdd.vaadin.components.fieldBuilders.JPAMoneyFieldBuilder Maven / Gradle / Ivy
package io.mateu.mdd.vaadin.components.fieldBuilders;
import com.google.common.base.Strings;
import com.vaadin.data.Binder;
import com.vaadin.data.Converter;
import com.vaadin.data.Result;
import com.vaadin.data.ValueContext;
import com.vaadin.data.validator.BeanValidator;
import com.vaadin.ui.TextField;
import io.mateu.mdd.shared.reflection.FieldInterfaced;
import io.mateu.mdd.vaadin.data.MDDBinder;
import org.javamoney.moneta.Money;
public class JPAMoneyFieldBuilder extends JPAStringFieldBuilder {
public boolean isSupported(FieldInterfaced field) {
return Money.class.equals(field.getType());
}
@Override
protected void bind(MDDBinder binder, TextField tf, FieldInterfaced field, boolean forSearchFilter) {
Binder.BindingBuilder aux = binder.forField(tf).withConverter(new Converter() {
@Override
public Result convertToModel(Object o, ValueContext valueContext) {
String s = (String) o;
if (Strings.isNullOrEmpty(s)) return Result.ok(Money.of(0, "EUR"));
else return Result.ok(Money.parse(s));
}
@Override
public Object convertToPresentation(Object o, ValueContext valueContext) {
return (o != null)?o.toString():"";
}
});
if (!forSearchFilter && field.getDeclaringClass() != null) aux.withValidator(new BeanValidator(field.getDeclaringClass(), field.getName()));
completeBinding(aux, binder, field);
}
}