Class AbstractField<C extends AbstractField<C,T>,T>
- Type Parameters:
C- the source type for value change eventsT- the value type
- All Implemented Interfaces:
AttachNotifier,DetachNotifier,HasElement,HasEnabled,HasStyle,HasValue<AbstractField.ComponentValueChangeEvent<C,,T>, T> HasValueAndElement<AbstractField.ComponentValueChangeEvent<C,,T>, T> Serializable
- Direct Known Subclasses:
AbstractSinglePropertyField,CustomField
Component allowing user
input. Implements HasValue to represent the input value. Examples of
typical field components include text fields, date pickers, and check boxes.
The field value is represented in two separate ways:
- A presentation value that is shown to the user. This is typically represented in the component's server-side DOM element or through child components.
- A model value that is available for programmatic use through the
HasValueinterface. This representation is handled by this class and should not be directly accessed by subclasses.
In order to keep the two value representations in sync with each other, subclasses must take care of the two following things:
- Listen to changes from the user, and call
setModelValue(Object, boolean)with an updated value. - Implement
setPresentationValue(Object)to update the presentation value of the component so that the new value is shown to the user.
This class extends Component, which means that it cannot be used for
adding field functionality to an existing component without changing the
superclass of that component. As an alternative, you can use
AbstractCompositeField to instead wrap an instance of the existing
component.
- Since:
- 1.0
- Author:
- Vaadin Ltd
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classValue change event fired by components.Nested classes/interfaces inherited from interface com.vaadin.flow.component.HasValue
HasValue.ValueChangeEvent<V>, HasValue.ValueChangeListener<E extends HasValue.ValueChangeEvent<?>> -
Constructor Summary
ConstructorsConstructorDescriptionAbstractField(Element element, T defaultValue) Creates a new field with the given element instance.AbstractField(T defaultValue) Creates a new field with an element created based on theTagannotation of the sub class. -
Method Summary
Modifier and TypeMethodDescriptionaddValueChangeListener(HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<C, T>> listener) Adds a value change listener.voidbindValue(Signal<T> valueSignal, SerializableConsumer<T> writeCallback) Binds aSignal's value to the value state of this component and keeps the state synchronized with the signal value while the element is in attached state.Returns the value that represents an empty value.getValue()Returns the current value of this object.booleanisEmpty()Returns whether thisHasValueis considered to be empty.protected voidsetModelValue(T newModelValue, boolean fromClient) Updates the model value if the value has actually changed.protected abstract voidsetPresentationValue(T newPresentationValue) Updates the presentation of this field to display the provided value.voidSets the value of this object.protected booleanvalueEquals(T value1, T value2) Compares to value instances to each other to determine whether they are equal.Methods inherited from class com.vaadin.flow.component.Component
addListener, bindVisible, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisibleMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods inherited from interface com.vaadin.flow.component.HasEnabled
bindEnabled, isEnabled, setEnabledMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, bindClassName, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassNameMethods inherited from interface com.vaadin.flow.component.HasValue
clear, getOptionalValueMethods inherited from interface com.vaadin.flow.component.HasValueAndElement
bindReadOnly, bindRequiredIndicatorVisible, isReadOnly, isRequiredIndicatorVisible, setReadOnly, setRequiredIndicatorVisible
-
Constructor Details
-
AbstractField
Creates a new field with an element created based on theTagannotation of the sub class. The provided default value is used bygetEmptyValue()and as the initial model value of this instance.- Parameters:
defaultValue- the default value for fields of this type
-
AbstractField
Creates a new field with the given element instance. he provided default value is used bygetEmptyValue()and as the initial model value of this instance.- Parameters:
element- the root element for the componentdefaultValue- the default value for fields of this type
-
-
Method Details
-
addValueChangeListener
public Registration addValueChangeListener(HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<C, T>> listener) Description copied from interface:HasValueAdds a value change listener. The listener is called when the value of thisHasValueis changed either by the user or programmatically.- Specified by:
addValueChangeListenerin interfaceHasValue<C extends AbstractField<C,T>, T> - Parameters:
listener- the value change listener, not null- Returns:
- a registration for the listener
-
setValue
Description copied from interface:HasValueSets the value of this object. If the new value is not equal togetValue(), fires a value change event. May throwIllegalArgumentExceptionif the value is not acceptable.Implementation note: the implementing class should document whether null values are accepted or not, and override
HasValue.getEmptyValue()if the empty value is notnull. -
setPresentationValue
Updates the presentation of this field to display the provided value. Subclasses should override this method to show the value to the user. This is typically done by setting an element property or by applying changes to child components.If
setModelValue(Object, boolean)is called from within this method, the value of the last invocation will be used as the model value instead of the value passed to this method. In this casesetPresentationValue(Object)will not be called again. Changing the provided value might be useful if the provided value is sanitized.See
AbstractFieldfor an overall description on the difference between model values and presentation values.- Parameters:
newPresentationValue- the new value to show
-
setModelValue
Updates the model value if the value has actually changed. Subclasses should call this method whenever the user has changed the value. A value change event is fired if the new value is different from the previous value according tovalueEquals(Object, Object).If the value is from the client-side and this field is in readonly mode, then the new model value will be ignored.
setPresentationValue(Object)will be called with the previous model value so that the representation shown to the user can be reverted.See
AbstractFieldfor an overall description on the difference between model values and presentation values.- Parameters:
newModelValue- the new internal value to usefromClient-trueif the new value originates from the client; otherwisefalse
-
valueEquals
Compares to value instances to each other to determine whether they are equal. Equality is used to determine whether to update internal state and fire an event whensetValue(Object)orsetModelValue(Object, boolean)is called. Subclasses can override this method to define an alternative comparison method instead ofObject.equals(Object).- Parameters:
value1- the first instancevalue2- the second instance- Returns:
trueif the instances are equal; otherwisefalse
-
isEmpty
public boolean isEmpty()Description copied from interface:HasValueReturns whether thisHasValueis considered to be empty.By default this is an equality check between current value and empty value.
-
getValue
Description copied from interface:HasValueReturns the current value of this object.Implementation note: the implementing class should document whether null values may be returned or not, and override
HasValue.getEmptyValue()if the empty value is notnull. -
getEmptyValue
Description copied from interface:HasValueReturns the value that represents an empty value.By default
HasValueis expected to supportnullas empty values. Specific implementations might not support this.- Specified by:
getEmptyValuein interfaceHasValue<C extends AbstractField<C,T>, T> - Returns:
- empty value
-
bindValue
Description copied from interface:HasValueBinds aSignal's value to the value state of this component and keeps the state synchronized with the signal value while the element is in attached state. When the element is in detached state, signal value changes have no effect.While a Signal is bound to a value state, any attempt to bind a new Signal while one is already bound throws
BindingActiveException.While a Signal is bound to a value state and the element is in attached state, setting the value with
HasValue.setValue(Object)or when a change originates from the client will invoke the write callback to propagate the value back. After the callback, the signal is re-consulted viaSignal.peek()and if its value differs from what was being set, the new value is ignored and the signal's updated value is used instead, i.e. in cases where write callback has `signal.set("different")`, whereas a value being set is "a new value", the "different" value wins.If the write callback is
null, the binding is read-only and any attempt to set the value while the element is attached will throw anIllegalStateException.Examples of usage:
// Simple binding to a signal holding the component value ValueSignal<String> signal = new ValueSignal<>(""); Input component = new Input(); add(component); component.bindValue(signal, signal::set); signal.set("Hello"); // The input's value changes // Binding to a property of an immutable record using updater helper record Person(String name, int age) { Person withName(String name) { return new Person(name, this.age); } } ValueSignal<Person> personSignal = new ValueSignal<>( new Person("Alice", 30)); component.bindValue(personSignal.map(Person::name), personSignal.updater(Person::withName)); // Binding to a property of a mutable bean using modifier helper class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } ValueSignal<Person> personSignal = new ValueSignal<>(new Person()); component.bindValue(personSignal.map(Person::getName), personSignal.modifier(Person::setName));
-