C
- the source type for value change eventsT
- the value typepublic abstract class AbstractField<C extends AbstractField<C,T>,T> extends Component implements HasValueAndElement<AbstractField.ComponentValueChangeEvent<C,T>,T>
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:
HasValue
interface. 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:
setModelValue(Object, boolean)
with an updated value.
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.
Modifier and Type | Class and Description |
---|---|
static class |
AbstractField.ComponentValueChangeEvent<C extends Component,V>
Value change event fired by components.
|
HasValue.ValueChangeEvent<V>, HasValue.ValueChangeListener<E extends HasValue.ValueChangeEvent<?>>
Constructor and Description |
---|
AbstractField(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 the
Tag
annotation of the sub class. |
Modifier and Type | Method and Description |
---|---|
Registration |
addValueChangeListener(HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<C,T>> listener)
Adds a value change listener.
|
T |
getEmptyValue()
Returns the value that represents an empty value.
|
T |
getValue()
Returns the current value of this object.
|
boolean |
isEmpty()
Returns whether this
HasValue is considered to be empty. |
protected void |
setModelValue(T newModelValue,
boolean fromClient)
Updates the model value if the value has actually changed.
|
protected abstract void |
setPresentationValue(T newPresentationValue)
Updates the presentation of this field to display the provided value.
|
void |
setValue(T value)
Sets the value of this object.
|
protected boolean |
valueEquals(T value1,
T value2)
Compares to value instances to each other to determine whether they are
equal.
|
addListener, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, set, setElement, setId, setVisible
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
isReadOnly, isRequiredIndicatorVisible, setReadOnly, setRequiredIndicatorVisible
clear, getOptionalValue
isEnabled, setEnabled
getElement
addAttachListener
addDetachListener
public AbstractField(T defaultValue)
Tag
annotation of the sub class. The provided default value is used by
getEmptyValue()
and as the initial model value of this instance.defaultValue
- the default value for fields of this typepublic AbstractField(Element element, T defaultValue)
getEmptyValue()
and as the initial model value
of this instance.element
- the root element for the componentdefaultValue
- the default value for fields of this typepublic Registration addValueChangeListener(HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<C,T>> listener)
HasValue
HasValue
is changed either by the user or programmatically.addValueChangeListener
in interface HasValue<AbstractField.ComponentValueChangeEvent<C extends AbstractField<C,T>,T>,T>
listener
- the value change listener, not nullpublic void setValue(T value)
HasValue
getValue()
, fires a value change event. May throw
IllegalArgumentException
if 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 not null
.
setValue
in interface HasValue<AbstractField.ComponentValueChangeEvent<C extends AbstractField<C,T>,T>,T>
value
- the new valueprotected abstract void setPresentationValue(T newPresentationValue)
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 case
setPresentationValue(Object)
will not be called again. Changing
the provided value might be useful if the provided value is sanitized.
See AbstractField
for an overall description on the difference
between model values and presentation values.
newPresentationValue
- the new value to showprotected void setModelValue(T newModelValue, boolean fromClient)
valueEquals(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 AbstractField
for an overall description on the difference
between model values and presentation values.
newModelValue
- the new internal value to usefromClient
- true
if the new value originates from the client;
otherwise false
protected boolean valueEquals(T value1, T value2)
setValue(Object)
or
setModelValue(Object, boolean)
is called. Subclasses can
override this method to define an alternative comparison method instead
of Object.equals(Object)
.value1
- the first instancevalue2
- the second instancetrue
if the instances are equal; otherwise
false
public boolean isEmpty()
HasValue
HasValue
is considered to be empty.
By default this is an equality check between current value and empty value.
isEmpty
in interface HasValue<AbstractField.ComponentValueChangeEvent<C extends AbstractField<C,T>,T>,T>
true
if considered empty; false
if notpublic T getValue()
HasValue
Implementation note: the implementing class should document
whether null values may be returned or not, and override
HasValue.getEmptyValue()
if the empty value is not null
.
getValue
in interface HasValue<AbstractField.ComponentValueChangeEvent<C extends AbstractField<C,T>,T>,T>
public T getEmptyValue()
HasValue
By default HasValue
is expected to support null
as empty
values. Specific implementations might not support this.
getEmptyValue
in interface HasValue<AbstractField.ComponentValueChangeEvent<C extends AbstractField<C,T>,T>,T>
Copyright © 2023. All rights reserved.