initializeField(objectWithMemberFields, memberField, field); forField(field).bind(property); return true; } else {
SerializableFunction<TARGET, NEWTARGET> toModel, SerializableFunction<NEWTARGET, TARGET> toPresentation) { return withConverter(Converter.from(toModel, toPresentation, exception -> exception.getMessage()));
.withValidator(field.getDefaultValidator());
RadioButtonGroup credit = createCreditOrDebit(); binder.forField(accountCode) .withNullRepresentation("") .withConverter(new AccountConverter()) .withValidator((Validator<Account>) (value, context) -> { if (value!=null && !(value instanceof FinalAccount)) return ValidationResult.error(app.getMessage("errorMessage.accountNotFinal")); return ValidationResult.ok(); }) .asRequired(app.getMessage("errorMessage.req", StringUtils.capitalize(layer.getCaption()))) .bind("account"); binder.forField(layer) .withConverter(new ShortToLayerConverter(journal)) .asRequired(app.getMessage("errorMessage.req", StringUtils.capitalize(layer.getCaption()))) .bind("layer"); binder.forField(detail) .withValidator(new StringLengthValidator(app.getMessage("errorMessage.invalidField", detail.getCaption()), 0, 255)) .withValidator(new RegexpValidator(app.getMessage("errorMessage.invalidField", detail.getCaption()), TEXT_REGEX)) .bind("detail"); binder.forField(tags) .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")))
.asRequired(getApp().getMessage("errorMessage.req", currentPasswordField.getCaption())) .withValidator(((UsersHelper)getHelper()).getCurrentPasswordMatchValidator()) .bind(string->string,null); form.addComponent(currentPasswordField); newPasswordField.setWidth("80%"); passwordBinder.forField(newPasswordField) .asRequired(getApp().getMessage("errorMessage.req",newPasswordField.getCaption())) .withValidator(((UsersHelper)getHelper()).getNewPasswordNotUsedValidator()) .bind(string->string,null); form.addComponent(newPasswordField); repeatPasswordField.setWidth("80%"); passwordBinder.forField(repeatPasswordField) .asRequired(getApp().getMessage("errorMessage.req", repeatPasswordField.getCaption())) .withValidator(((UsersHelper)getHelper()).getPasswordsMatchValidator(newPasswordField)) .bind(string->string,null); form.addComponent(repeatPasswordField); passwordPanel.setVisible(forcePasswordChange);
TextField field = new TextField(getCaptionFromId(propertyId)); Binder.BindingBuilder<GLTransaction, String> builder = formatField(propertyId, field); builder.withNullRepresentation("").withConverter(new StringToTagConverter()).bind(propertyId); return field; ComboBox<Journal> field = new JournalsCombo(true); field.setCaption(getCaptionFromId(propertyId)); formatField(propertyId, field).bind(propertyId); return field; entryGrid = new EntryGrid(null, canEdit()); getBinder().forField(entryGrid) .bind(glTransaction -> this.getInstance(), null); if (getInstance() != null && getInstance().getJournal() != null) { newEntryForm = new NewEntryForm(getInstance(), (TransactionsHelper) getHelper(), entryGrid);
column.setEditorBinding(bindingBuilder.bind(columnDefinition.getName())); });
@Override protected Component buildAndBindCustomComponent(String propertyId) { if ("created".equalsIgnoreCase(propertyId)){ return getFieldFactory().buildAndBindDateField(propertyId); } if ("expiration".equalsIgnoreCase(propertyId)){ return getFieldFactory().buildAndBindDateField(propertyId); } if ("parent".equalsIgnoreCase(propertyId)) { ComboBox<Account> parentCombo = new ComboBox<>(getCaptionFromId(propertyId)); parentCombo.setDataProvider(((AccountsHelper)getHelper()).getParentDataProvider()); parentCombo.setItemCaptionGenerator(parent -> parent.getCode() + " - " + parent.getDescription()); formatField(propertyId,parentCombo).bind(propertyId); return parentCombo; } if ("type".equalsIgnoreCase(propertyId)) { ComboBox<Integer> typeCombo = new ComboBox<>(getCaptionFromId(propertyId)); typeCombo.setItems(Account.CHART,Account.DEBIT,Account.CREDIT); typeCombo.setItemCaptionGenerator(type -> getApp().getMessage("account." + type).toUpperCase()); formatField(propertyId,typeCombo).bind(propertyId); return typeCombo; } if ("entries".equalsIgnoreCase(propertyId)) { //todo: should bind? return createEntriesPanel(); } return null; }
/** * onstructs the entity edit form for the data entity * @param beanType The class of the data entity * @param idIsEditable Whether the id is editable */ public AbstractEntityEditForm( final Class<T> beanType, final boolean idIsEditable) { this.binder = new Binder<>(beanType); if (idIsEditable) { this.binder.forField(idField) //.withConverter(new StringToLongConverter("Unable to convert id")) .asRequired("The id field is required.") .bind("id"); } else { this.binder.forField(idField) //.withConverter(new StringToLongConverter("Unable to convert id")) .bind("id"); } this.binder.addStatusChangeListener(this::onBinderStatusChange); setSpacing(true); setMargin(false); setSizeFull(); Responsive.makeResponsive(this); idField.setReadOnly(!idIsEditable); idField.setWidth(100.0f, Unit.PERCENTAGE); }
@Override protected Component buildAndBindCustomComponent(String propertyId) { List<Validator> validators = getFieldFactory().getValidators(propertyId); if ("permissions".equals(propertyId)) { CheckBoxGroup<Permission> f = new CheckBoxGroup("Permissions"); List<SysConfig> sysconfigs = ((RolesHelper)getHelper()).getPermissions(); List<Permission> allPermissions = new ArrayList<>(); //convert SysConfigs to Permissions for (SysConfig sys : sysconfigs) { Permission p = Permission.valueOf(sys.getId().substring(5)); allPermissions.add(p); } f.setItems(allPermissions); f.setItemCaptionGenerator((ItemCaptionGenerator<Permission>) item -> sysconfigs.get(allPermissions.indexOf(item)).getId().substring(5) + " : " + //remove "perm." prefix sysconfigs.get(allPermissions.indexOf(item)).getValue()); Binder.BindingBuilder builder = getBinder().forField(f); validators.forEach(builder::withValidator); builder.bind(propertyId); return f; } return null; }
protected Component buildAndBindCustomComponent(String propertyId) { if ("roles".equalsIgnoreCase(propertyId)) { CheckBoxGroup<Role> checkBoxGroup = new CheckBoxGroup<>(QIUtils.getCaptionFromId(propertyId)); checkBoxGroup.setItems(((ConsumersHelper)getHelper()).getRoles()); checkBoxGroup.setItemCaptionGenerator(role -> StringUtils.capitalize(role.getName())); formatField(propertyId,checkBoxGroup).bind(propertyId); return checkBoxGroup; } if ("user".equalsIgnoreCase(propertyId)) { ComboBox<User> box = createUserBox(); formatField(propertyId,box).bind(propertyId); box.setEnabled(false); box.setValue(this.selectedUser); return box; } if ("startdate".equalsIgnoreCase(propertyId) || "endDate".equalsIgnoreCase(propertyId)) { return getFieldFactory().buildAndBindDateField(propertyId); } return null; }
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())); }
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; }
@SuppressWarnings({ "unchecked", "rawtypes" }) private void setEditorBinding(P property, Column column, HasValue editor, boolean readOnly, PropertyColumn<?, ?> pc) { BindingBuilder builder = getGrid().getEditor().getBinder().forField(editor); if (pc.isRequired()) { final Localizable requiredMessage = (pc.getRequiredMessage() != null) ? pc.getRequiredMessage() : RequiredInputValidator.DEFAULT_REQUIRED_ERROR; builder.asRequired(context -> { return LocalizationContext.translate(requiredMessage, true); }); } // default validators getDefaultPropertyValidators(property).forEach(v -> builder.withValidator(new ValidatorWrapper<>(v))); // validators pc.getValidators().forEach(v -> builder.withValidator(v)); // bind column.setEditorBinding(builder.bind(getColumnId(property))); // set editable if not read-only column.setEditable(!readOnly); }
@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; }
protected Component buildAndBindCustomComponent(String propertyId) { if ("roles".equals(propertyId)) { CheckBoxGroup g = new CheckBoxGroup(StringUtils.capitalize(getCaptionFromId(propertyId))); g.setItems(((UsersHelper)getHelper()).getRoles()); g.setItemCaptionGenerator((ItemCaptionGenerator<Role>) item -> StringUtils.capitalize(item.getName())); List<Validator> v = getFieldFactory().getValidators(propertyId); Binder.BindingBuilder builder = getBinder().forField(g); for (Validator val : v) { builder.withValidator(val); } builder.bind(propertyId); return g; } return null; }
@Override protected void bind() { getBinder().forMemberField(date).withConverter(new LocalDateToDateConverter()); super.bind(); }
@Override public <FIELDVALUE> Binding<T, FIELDVALUE> bind(HasValue<FIELDVALUE> field, String propertyName) { final BindingBuilder<T, FIELDVALUE> binding = forField(field); final BindingBuilder<T, FIELDVALUE> bindingBuilder = Optional.ofNullable(fieldDefinitions.get(propertyName)) .map(fieldDefinition -> this.applyConfiguration(binding, (FieldDefinition<FIELDVALUE>) fieldDefinition)) .orElse(binding); return bindingBuilder.bind(propertyName); }
private <PT> BindingBuilder<T, PT> applyTextFieldNullValueRepresentationWorkaround(BindingBuilder<T, PT> bindingBuilder, Class<? extends FieldDefinition> definitionType) { //see https://github.com/vaadin/framework/issues/9619 if (TextFieldDefinition.class.isAssignableFrom(definitionType)) { bindingBuilder = (BindingBuilder<T, PT>) ((BindingBuilder<T, String>) bindingBuilder).withNullRepresentation(""); } return bindingBuilder; } }
@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(); }