@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 void read(BEAN bean) { getField().setValue(convertDataToFieldType(bean)); }
private void updateColorComponents(Component component) { if (component instanceof HasValue<?>) { ((HasValue<?>) component).setReadOnly(isReadOnly()); ((HasValue<?>) component) .setRequiredIndicatorVisible(isRequiredIndicatorVisible()); } if (component instanceof HasComponents) { for (Component c : (HasComponents) component) { updateColorComponents(c); } } }
/** * Resets the value to the empty one. * <p> * This is just a shorthand for resetting the value, see the methods * {@link #setValue(Object)} and {@link #getEmptyValue()}. * * @see #setValue(Object) * @see #getEmptyValue() */ public default void clear() { setValue(getEmptyValue()); }
/** * Returns whether this {@code HasValue} is considered to be empty. * <p> * By default this is an equality check between current value and empty * value. * * @return {@code true} if considered empty; {@code false} if not */ public default boolean isEmpty() { return Objects.equals(getValue(), getEmptyValue()); }
/** * Returns the current value of this object, wrapped in an {@code Optional}. * <p> * The {@code Optional} will be empty if the value is {@code null} or * {@code isEmpty()} returns {@code true}. * * @return the current value, wrapped in an {@code Optional} */ public default Optional<V> getOptionalValue() { return isEmpty() ? Optional.empty() : Optional.ofNullable(getValue()); }
@Override public BindingBuilder<BEAN, TARGET> asRequired( Validator<TARGET> customRequiredValidator) { checkUnbound(); field.setRequiredIndicatorVisible(true); return withValidator(customRequiredValidator); }
private void colorChanged(ValueChangeEvent<Color> event) { event.getSource().setValue(event.getValue()); setValue(event.getValue()); updatingColors = true; setRgbSliderValues(selectedColor); float[] hsv = selectedColor.getHSV(); setHsvSliderValues(hsv); updatingColors = false; for (HasValue<Color> s : selectors) { if (event.getSource() != s && s != this && s.getValue() != selectedColor) { s.setValue(selectedColor); } } }
/** * Binds a HasValue<String> to a cookie that lives for a month. The cookies value is updated via a * ValueChangeListener. Its crrent value is copied into the HasValue<String>. * * @param field The HasValue<String> that gets bound. * @param name The name of the cookie * * @throws IllegalArgumentException if field or name are null or if name is empty. */ public static void bindValueToCookie(HasValue<String> field, String name) { if (Objects.isNull(name) || name.isEmpty()) { throw new IllegalArgumentException("Name must not be null or empty"); } if (Objects.isNull(field)) { throw new IllegalArgumentException("Field must not be null"); } detectCookieValue(name, (v) -> { if (v != null) { field.setValue(v); } }); field.addValueChangeListener((event) -> { setCookie(name, event.getValue(), LocalDateTime.now().plusMonths(1l)); }); } }
/** * Creates a new {@code ValueChange} event containing the given value, * originating from the given source component. * * @param component * the component, not null * @param hasValue * the HasValue instance bearing the value, not null * @param oldValue * the previous value held by the source of this event * @param userOriginated * {@code true} if this event originates from the client, * {@code false} otherwise. */ public ValueChangeEvent(Component component, HasValue<V> hasValue, V oldValue, boolean userOriginated) { super(hasValue); this.userOriginated = userOriginated; this.component = component; this.oldValue = oldValue; value = hasValue.getValue(); }
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; }
/** * Clear all the bound fields for this binder. */ private void clearFields() { bindings.forEach(binding -> { binding.getField().clear(); clearError(binding.getField()); }); if (hasChanges()) { fireStatusChangeEvent(false); } changedBindings.clear(); }
@Override public boolean isReadOnly() { return field.isReadOnly(); }
@Override public boolean isEmpty() { return field.isEmpty(); }
private void configureRequired(BindingBuilder<BEAN, ?> binding, PropertyDefinition<BEAN, ?> definition, BeanValidator validator) { assert requiredConfigurator != null; Class<?> propertyHolderType = definition.getPropertyHolderType(); BeanDescriptor descriptor = validator.getJavaxBeanValidator() .getConstraintsForClass(propertyHolderType); PropertyDescriptor propertyDescriptor = descriptor .getConstraintsForProperty(definition.getTopLevelName()); if (propertyDescriptor == null) { return; } if (propertyDescriptor.getConstraintDescriptors().stream() .map(ConstraintDescriptor::getAnnotation) .anyMatch(requiredConfigurator)) { binding.getField().setRequiredIndicatorVisible(true); } }
/** * Returns the field value run through all converters and validators, * but doesn't pass the {@link BindingValidationStatus} to any status * handler. * * @return the result of the conversion */ private Result<TARGET> doConversion() { FIELDVALUE fieldValue = field.getValue(); return converterValidatorChain.convertToModel(fieldValue, createValueContext()); }
/** * Adds a value change listener. The listener is called when the value of * this {@code HasValue} is changed either by the user or programmatically. * * @see HasValue#addValueChangeListener(com.vaadin.data.HasValue.ValueChangeListener) * @param listener * the value change listener, not null * @return this (for method chaining) <b>NOT</b> a registration */ public default S withValueChangeListener(ValueChangeListener<V> listener) { ((HasValue<V>) this).addValueChangeListener(listener); return (S) this; } }
@Override public void clear() { field.clear(); }
@Override public boolean isEnabled() throws MException { if (!(getComponent() instanceof HasValue)) return true; return !((HasValue<?>)getComponent()).isReadOnly(); } @Override
private void updateColorComponents(Component component) { if (component instanceof HasValue<?>) { ((HasValue<?>) component).setReadOnly(isReadOnly()); } if (component instanceof HasComponents) { for (Component c : (HasComponents) component) { updateColorComponents(c); } } }