import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import java.math.BigDecimal;
+import java.text.DecimalFormat;
+import java.text.NumberFormat;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import com.vaadin.data.Binder.Binding;
import com.vaadin.data.Binder.BindingBuilder;
+import com.vaadin.data.converter.StringToBigDecimalConverter;
import com.vaadin.data.converter.StringToIntegerConverter;
import com.vaadin.data.validator.IntegerRangeValidator;
import com.vaadin.data.validator.NotEmptyValidator;
assertEquals(11, salary.intValue());
}
+ @Test
+ public void withConverter_writeBackValue() {
+ TextField rentField = new TextField();
+ rentField.setValue("");
+ binder.forField(rentField).withConverter(new EuroConverter(""))
+ .withNullRepresentation(BigDecimal.valueOf(0d))
+ .bind(Person::getRent, Person::setRent);
+ binder.setBean(item);
+ rentField.setValue("10");
+
+ assertEquals("€ 10.00", rentField.getValue());
+ }
+
@Test
public void beanBinder_nullRepresentationIsNotDisabled() {
Binder<Person> binder = new Binder<>(Person.class);
.bind(AtomicReference::get, AtomicReference::set);
return binder;
}
+
+ /**
+ * A converter that adds/removes the euro sign and formats currencies with
+ * two decimal places.
+ */
+ public class EuroConverter extends StringToBigDecimalConverter {
+
+ public EuroConverter() {
+ super("defaultErrorMessage");
+ }
+
+ public EuroConverter(String errorMessage) {
+ super(errorMessage);
+ }
+
+ @Override
+ public Result<BigDecimal> convertToModel(String value,
+ ValueContext context) {
+ if (value.isEmpty()) {
+ return Result.ok(null);
+ }
+ value = value.replaceAll("[€\\s]", "").trim();
+ if (value.isEmpty()) {
+ value = "0";
+ }
+ return super.convertToModel(value, context);
+ }
+
+ @Override
+ public String convertToPresentation(BigDecimal value,
+ ValueContext context) {
+ if (value == null) {
+ return convertToPresentation(BigDecimal.ZERO, context);
+ }
+ return "€ " + super.convertToPresentation(value, context);
+ }
+
+ @Override
+ protected NumberFormat getFormat(Locale locale) {
+ // Always display currency with two decimals
+ NumberFormat format = super.getFormat(locale);
+ if (format instanceof DecimalFormat) {
+ ((DecimalFormat) format).setMaximumFractionDigits(2);
+ ((DecimalFormat) format).setMinimumFractionDigits(2);
+ }
+ return format;
+ }
+ }
}