/** * Finds and removes all Bindings for the given field. Note that this method * and other overloads of removeBinding method do not reset component errors * that might have been added to the field and do not remove required * indicator of the field no matter if it was set by Binder or not. To reset * component errors, {@code field.setComponentError(null)} should be called * and to remove required indicator, * {@code field.setRequiredIndicatorVisible(false)} should be called. * * @see com.vaadin.ui.AbstractComponent#setComponentError * @see com.vaadin.ui.AbstractComponent#setRequiredIndicatorVisible * * @param field * the field to remove from bindings * * @since 8.2 */ public void removeBinding(HasValue<?> field) { Objects.requireNonNull(field, "Field can not be null"); Set<BindingImpl<BEAN, ?, ?>> toRemove = getBindings().stream() .filter(binding -> field.equals(binding.getField())) .collect(Collectors.toSet()); toRemove.forEach(Binding::unbind); }
@Override public void setReadOnly(boolean readOnly) { if (setter == null && !readOnly) { throw new IllegalStateException( "Binding with a null setter has to be read-only"); } this.readOnly = readOnly; getField().setReadOnly(readOnly); }
@Override public Binding<BEAN, TARGET> bind(ValueProvider<BEAN, TARGET> getter, Setter<BEAN, TARGET> setter) { checkUnbound(); Objects.requireNonNull(getter, "getter cannot be null"); BindingImpl<BEAN, FIELDVALUE, TARGET> binding = new BindingImpl<>( this, getter, setter); getBinder().bindings.add(binding); if (getBinder().getBean() != null) { binding.initFieldValue(getBinder().getBean()); } if (setter == null) { binding.getField().setReadOnly(true); } getBinder().fireStatusChangeEvent(false); bound = true; getBinder().incompleteBindings.remove(getField()); return binding; }
if (binding.getField() != null) binding.initFieldValue(bean); });
/** * Sets the field value by invoking the getter function on the given * bean. The default listener attached to the field will be removed for * the duration of this update. * * @param bean * the bean to fetch the property value from */ private void initFieldValue(BEAN bean) { assert bean != null; assert onValueChange != null; valueInit = true; try { getField().setValue(convertDataToFieldType(bean)); } finally { valueInit = false; } }
/** * Finds an appropriate locale to be used in conversion and validation. * * @return the found locale, not null */ protected Locale findLocale() { Locale l = null; if (getField() instanceof Component) { l = ((Component) getField()).getLocale(); } if (l == null && UI.getCurrent() != null) { l = UI.getCurrent().getLocale(); } if (l == null) { l = Locale.getDefault(); } return l; }
public BindingImpl(BindingBuilderImpl<BEAN, FIELDVALUE, TARGET> builder, ValueProvider<BEAN, TARGET> getter, Setter<BEAN, TARGET> setter) { this.binder = builder.getBinder(); this.field = builder.field; this.statusHandler = builder.statusHandler; converterValidatorChain = ((Converter<FIELDVALUE, TARGET>) builder.converterValidatorChain); onValueChange = getField() .addValueChangeListener(this::handleFieldValueChange); this.getter = getter; this.setter = setter; readOnly = setter == null; }
@Override public void read(BEAN bean) { getField().setValue(convertDataToFieldType(bean)); }