/** * Maps binding value {@code null} to given null representation and back * to {@code null} when converting back to model value. * * @param nullRepresentation * the value to use instead of {@code null} * @return a new binding with null representation handling. */ public default BindingBuilder<BEAN, TARGET> withNullRepresentation( TARGET nullRepresentation) { return withConverter( fieldValue -> Objects.equals(fieldValue, nullRepresentation) ? null : fieldValue, modelValue -> Objects.isNull(modelValue) ? nullRepresentation : modelValue); }
SerializableFunction<TARGET, NEWTARGET> toModel, SerializableFunction<NEWTARGET, TARGET> toPresentation) { return withConverter(Converter.from(toModel, toPresentation, exception -> exception.getMessage()));
public <PT> BindingBuilder<T, PT> applyConfiguration(BindingBuilder<T, PT> bindingBuilder, FieldDefinition<PT> definition, Object... parameters) { if (definition.isRequired()) { bindingBuilder.asRequired(definition.getRequiredErrorMessage()); } bindingBuilder = definition.getValidators().stream() .map(this::newValidator) .reduce(bindingBuilder, BindingBuilder::withValidator, defaultCombiner()); final Converter converter = newConverter(definition, bindingBuilder.getField(), parameters); if (converter != null) { bindingBuilder = bindingBuilder.withConverter(converter); final Class<? extends FieldDefinition> definitionType = definition.getClass(); bindingBuilder = applyTextFieldNullValueRepresentationWorkaround(bindingBuilder, definitionType); } final PT fieldValue = (PT) bindingBuilder.getField().getValue(); if (converter == null && fieldValue != null && !definition.getType().isInstance(fieldValue)) { log.warn(String.format("Type mismatch for field named [%s]. Configured type is [%s] but bound component value type is [%s] and no suitable converter has been found. Please check your configuration.", definition.getName(), definition.getType(), fieldValue.getClass())); } Optional.ofNullable(definition.getDefaultValue()).ifPresent(bindingBuilder::withNullRepresentation); return bindingBuilder; }
binder.forField(accountCode) .withNullRepresentation("") .withConverter(new AccountConverter()) .withValidator((Validator<Account>) (value, context) -> { if (value!=null && !(value instanceof FinalAccount)) .bind("account"); binder.forField(layer) .withConverter(new ShortToLayerConverter(journal)) .asRequired(app.getMessage("errorMessage.req", StringUtils.capitalize(layer.getCaption()))) .bind("layer"); .withValidator(new StringLengthValidator(app.getMessage("errorMessage.invalidField", tags.getCaption()), 0, 255)) .withValidator(new RegexpValidator(app.getMessage("errorMessage.invalidField", tags.getCaption()), TEXT_REGEX)) .withConverter(new StringToTagConverter()) .bind("tags"); binder.forField(amount) .asRequired(app.getMessage("errorMessage.req", StringUtils.capitalize(amount.getCaption()))) .withConverter(new StringToBigDecimalConverter(app.getMessage("errorMessage.invalidAmount"))) .withValidator(new BigDecimalRangeValidator(app.getMessage("errorMessage.invalidAmount"), new BigDecimal("1"), new BigDecimal("99999999999999"))) .withNullRepresentation(BigDecimal.ZERO)
TextField field = new TextField(getCaptionFromId(propertyId)); Binder.BindingBuilder<GLTransaction, String> builder = formatField(propertyId, field); builder.withNullRepresentation("").withConverter(new StringToTagConverter()).bind(propertyId); return field;
private void initialize() { optionsField = formFieldFactory.createField(definition.getField(), locale); layout.addComponent(optionsField); optionsField.addValueChangeListener(e -> { if (e.getOldValue() == null || e.getOldValue() == null) { return; } EditorView<T> oldView = subForm(e.getOldValue()); EditorView<T> newView = subForm(e.getValue()); layout.replaceComponent(oldView.asVaadinComponent(), newView.asVaadinComponent()); }); definition.getForms().forEach(formDefinition -> { ItemProviderStrategy<T> subFormProviderStrategy = create(((ComplexPropertyDefinition<T>) formDefinition).getItemProvider(), formDefinition, itemProviderStrategy); EditorView<T> subForm = getViewProvider().create(formDefinition); subForms.put(formDefinition.getName(), Pair.of(subForm, subFormProviderStrategy)); }); binder = ConfiguredBinder.withPropertySet(propertySetFactory.fromFieldDefinitions(Collections.singletonList(definition.getField()), locale)); binder.forField(optionsField) .withConverter(new StringToOptionDefinitionConverter(definition.getField())) .bind(resolvePropertyNameByLocale(definition.getField().getName(), locale, definition.getField().isI18n())); }
@Override protected Component buildAndBindCustomComponent(String propertyId) { if ("id".equals(propertyId)) { TextField id = new TextField(getCaptionFromId(propertyId)); List<Validator> validators = getFieldFactory().getValidators(propertyId); Binder<SysConfig> binder = getBinder(); Binder.BindingBuilder builder = binder.forField(id) .asRequired(getApp().getMessage("errorMessage.req", StringUtils.capitalize(getCaptionFromId(propertyId)))) .withNullRepresentation("") .withConverter(userInputValue -> userInputValue , toPresentation -> removePrefix(toPresentation)); validators.forEach(builder::withValidator); builder.bind(propertyId); return id; } return null; }
@Override protected void bind() { // DateField in Vaadin 8 uses LocalDate by default, the backend // uses plain old java.util.Date, thus we need a converter, using // built in helper here getBinder() .forMemberField(birthDay) .withConverter(new LocalDateToDateConverter()); super.bind(); }
@Override protected void bind() { getBinder().forMemberField(date).withConverter(new LocalDateToDateConverter()); super.bind(); }
SerializableFunction<NEWTARGET, TARGET> toPresentation, String errorMessage) { return withConverter(Converter.from(toModel, toPresentation, exception -> errorMessage));