com.vaadin.ui.
Class AbstractField<T>
- java.lang.Object
-
- com.vaadin.server.AbstractClientConnector
-
- com.vaadin.ui.AbstractComponent
-
- com.vaadin.ui.AbstractField<T>
-
Type Parameters:
T
- the input value typeAll Implemented Interfaces:
HasValue<T>
,ContextClickEvent.ContextClickNotifier
,MethodEventSource
,ClientConnector
,Sizeable
,Connector
,Component
,Component.Focusable
,Serializable
Direct Known Subclasses:
AbstractColorPicker
,AbstractDateField
,AbstractTextField
,CheckBox
,ColorPickerGradient
,ColorPickerGrid
,CustomField
,RichTextArea
,Slider
public abstract class AbstractField<T> extends AbstractComponent implements HasValue<T>, Component.Focusable
An abstract implementation of a field, or a
Component
allowing user input. ImplementsHasValue
to represent the input value. Examples of typical field components include text fields, date pickers, and check boxes.This class replaces the Vaadin 7
com.vaadin.ui.AbstractField
class. The oldAbstractField
is retained, under the new namecom.vaadin.v7.ui.AbstractField
, for compatibility and migration purposes.Since:
8.0
Author:
Vaadin Ltd.
See Also:
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.vaadin.server.ClientConnector
ClientConnector.AttachEvent, ClientConnector.AttachListener, ClientConnector.ConnectorErrorEvent, ClientConnector.DetachEvent, ClientConnector.DetachListener
-
Nested classes/interfaces inherited from interface com.vaadin.ui.Component
Component.ErrorEvent, Component.Event, Component.Focusable, Component.Listener
-
Nested classes/interfaces inherited from interface com.vaadin.data.HasValue
HasValue.ValueChangeEvent<V>, HasValue.ValueChangeListener<V>
-
Nested classes/interfaces inherited from interface com.vaadin.server.Sizeable
Sizeable.Unit
-
-
Field Summary
-
Fields inherited from class com.vaadin.ui.AbstractComponent
DESIGN_ATTR_PLAIN_TEXT
-
Fields inherited from interface com.vaadin.server.Sizeable
SIZE_UNDEFINED, UNITS_CM, UNITS_EM, UNITS_EX, UNITS_INCH, UNITS_MM, UNITS_PERCENTAGE, UNITS_PICAS, UNITS_PIXELS, UNITS_POINTS
-
-
Constructor Summary
Constructors Constructor Description AbstractField()
-
Method Summary
All Methods Modifier and Type Method Description Registration
addValueChangeListener(HasValue.ValueChangeListener<T> listener)
Adds a value change listener.
protected HasValue.ValueChangeEvent<T>
createValueChange(T oldValue, boolean userOriginated)
Returns a new value change event instance.
protected abstract void
doSetValue(T value)
Sets the value of this field.
void
focus()
Sets the focus for this component if the component is
Component.Focusable
.protected Collection<String>
getCustomAttributes()
Returns a collection of attributes that should not be handled by the basic implementation of the
AbstractComponent.readDesign(Element, DesignContext)
andAbstractComponent.writeDesign(Element, DesignContext)
methods.protected AbstractFieldState
getState()
Returns the shared state bean with information to be sent from the server to the client.
protected AbstractFieldState
getState(boolean markAsDirty)
Returns the shared state for this connector.
int
getTabIndex()
Gets the tabulator index of the
Focusable
component.protected boolean
isDifferentValue(T newValue)
Called when a new value is set to determine whether the provided new value is considered to be a change compared to the current value.
boolean
isReadOnly()
Returns the read-only status from the state of this
AbstractComponent
.boolean
isRequiredIndicatorVisible()
Checks whether the required indicator is visible or not.
void
readDesign(org.jsoup.nodes.Element design, DesignContext designContext)
Reads the component state from the given design.
void
setReadOnly(boolean readOnly)
Sets the read-only status in the state of this
AbstractComponent
.void
setRequiredIndicatorVisible(boolean visible)
Sets the visibility of the required indicator.
void
setTabIndex(int tabIndex)
Sets the tabulator index of the
Focusable
component.void
setValue(T value)
Sets the value of this object.
protected boolean
setValue(T value, boolean userOriginated)
Sets the value of this field if it has changed and fires a value change event.
void
writeDesign(org.jsoup.nodes.Element design, DesignContext designContext)
Writes the component state to the given design.
-
Methods inherited from class com.vaadin.ui.AbstractComponent
addContextClickListener, addListener, addShortcutListener, addStyleName, attach, beforeClientResponse, detach, findAncestor, fireComponentErrorEvent, fireComponentEvent, getActionManager, getCaption, getComponentError, getData, getDebugId, getDescription, getErrorMessage, getHeight, getHeightUnits, getIcon, getId, getLocale, getParent, getPrimaryStyleName, getStyleName, getWidth, getWidthUnits, isCaptionAsHtml, isConnectorEnabled, isEnabled, isOrHasAncestor, isResponsive, isVisible, removeContextClickListener, removeListener, removeShortcutListener, removeStyleName, setCaption, setCaptionAsHtml, setComponentError, setData, setDebugId, setDescription, setDescription, setEnabled, setHeight, setHeight, setHeightFull, setHeightUndefined, setIcon, setId, setLocale, setParent, setPrimaryStyleName, setResponsive, setSizeFull, setSizeUndefined, setStyleName, setVisible, setWidth, setWidth, setWidthFull, setWidthUndefined
-
Methods inherited from class com.vaadin.server.AbstractClientConnector
addAttachListener, addDetachListener, addExtension, addListener, addListener, addListener, addListener, addListener, addListener, addMethodInvocationToQueue, createState, encodeState, equals, fireEvent, getAllChildrenIterable, getConnectorId, getErrorHandler, getExtensions, getListeners, getResource, getRpcManager, getRpcProxy, getSession, getStateType, getUI, handleConnectorRequest, hashCode, hasListeners, isAttached, isThis, markAsDirty, markAsDirtyRecursive, registerRpc, registerRpc, removeAttachListener, removeDetachListener, removeExtension, removeListener, removeListener, removeListener, removeListener, removeListener, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler, setResource, updateDiffstate
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.vaadin.server.ClientConnector
addAttachListener, addDetachListener, beforeClientResponse, detach, encodeState, getErrorHandler, getExtensions, getRpcManager, getStateType, handleConnectorRequest, isAttached, isConnectorEnabled, markAsDirty, markAsDirtyRecursive, removeAttachListener, removeDetachListener, removeExtension, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler
-
Methods inherited from interface com.vaadin.ui.Component
addListener, addStyleName, addStyleNames, attach, getCaption, getDescription, getIcon, getId, getLocale, getParent, getPrimaryStyleName, getStyleName, getUI, isEnabled, isVisible, removeListener, removeStyleName, removeStyleNames, setCaption, setEnabled, setIcon, setId, setParent, setPrimaryStyleName, setStyleName, setStyleName, setVisible
-
Methods inherited from interface com.vaadin.shared.Connector
getConnectorId
-
Methods inherited from interface com.vaadin.data.HasValue
clear, getDefaultValidator, getEmptyValue, getOptionalValue, getValue, isEmpty
-
Methods inherited from interface com.vaadin.server.Sizeable
getHeight, getHeightUnits, getWidth, getWidthUnits, setHeight, setHeight, setHeightFull, setHeightUndefined, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFull, setWidthUndefined
-
-
-
-
Method Detail
-
setValue
public void setValue(T value)
Description copied from interface:
HasValue
Sets the value of this object. If the new value is not equal to
getValue()
, fires a value change event. May throwIllegalArgumentException
if the value is not acceptable.Implementation note: the implementing class should document whether null values are accepted or not.
-
isReadOnly
public boolean isReadOnly()
Description copied from class:
AbstractComponent
Returns the read-only status from the state of this
AbstractComponent
. This method should be made public inComponents
that implementHasValue
.Specified by:
isReadOnly
in interfaceHasValue<T>
Overrides:
isReadOnly
in classAbstractComponent
Returns:
true
if state has read-only on;false
if notSee Also:
-
setReadOnly
public void setReadOnly(boolean readOnly)
Sets the read-only status in the state of this
AbstractComponent
. This method should be made public inComponents
that implementHasValue
.The server ignores (potentially forged) value change requests from the client to fields that are read-only. Programmatically changing the field value via
setValue(Object)
is still possible.The read-only mode is distinct from the disabled state. When disabled, a component cannot be interacted with at all, and its content should be considered irrelevant or not applicable. In contrast, the user should still be able to read the content and otherwise interact with a read-only field even though changing the value is disallowed.
Specified by:
setReadOnly
in interfaceHasValue<T>
Overrides:
setReadOnly
in classAbstractComponent
Parameters:
readOnly
-true
to set read-only mode,false
otherwise.
-
addValueChangeListener
public Registration addValueChangeListener(HasValue.ValueChangeListener<T> listener)
Description copied from interface:
HasValue
Adds a value change listener. The listener is called when the value of this
HasValue
is changed either by the user or programmatically.Specified by:
addValueChangeListener
in interfaceHasValue<T>
Parameters:
listener
- the value change listener, not nullReturns:
a registration for the listener
-
readDesign
public void readDesign(org.jsoup.nodes.Element design, DesignContext designContext)
Description copied from interface:
Component
Reads the component state from the given design.
The component is responsible not only for updating its own state but also for ensuring that its children update their state based on the design.
It is assumed that the component is in its default state when this method is called. Reading should only take into consideration attributes specified in the design and not reset any unspecified attributes to their defaults.
This method must not modify the design.
Specified by:
readDesign
in interfaceComponent
Overrides:
readDesign
in classAbstractComponent
Parameters:
design
- The element to obtain the state fromdesignContext
- The DesignContext instance used for parsing the design
-
writeDesign
public void writeDesign(org.jsoup.nodes.Element design, DesignContext designContext)
Description copied from interface:
Component
Writes the component state to the given design.
The component is responsible not only for writing its own state but also for ensuring that its children write their state to the design.
This method must not modify the component state.
Specified by:
writeDesign
in interfaceComponent
Overrides:
writeDesign
in classAbstractComponent
Parameters:
design
- The element to write the component state to. Any previous attributes or child nodes are not cleared.designContext
- The DesignContext instance used for writing the design
-
getCustomAttributes
protected Collection<String> getCustomAttributes()
Description copied from class:
AbstractComponent
Returns a collection of attributes that should not be handled by the basic implementation of the
AbstractComponent.readDesign(Element, DesignContext)
andAbstractComponent.writeDesign(Element, DesignContext)
methods. Typically these are handled in a custom way in the overridden versions of the above methodsOverrides:
getCustomAttributes
in classAbstractComponent
Returns:
the collection of attributes that are not handled by the basic implementation
-
setValue
protected boolean setValue(T value, boolean userOriginated)
Sets the value of this field if it has changed and fires a value change event. If the value originates from the client and this field is read-only, does nothing. Invokes
doSetValue
to actually store the value.Parameters:
value
- the new value to setuserOriginated
-true
if this event originates from the client,false
otherwise.Returns:
true
if the value was updated,false
otherwise
-
isDifferentValue
protected boolean isDifferentValue(T newValue)
Called when a new value is set to determine whether the provided new value is considered to be a change compared to the current value. This is used to determine whether
doSetValue(Object)
should be called and a value change event fired.Parameters:
newValue
- the new value candidate to check, may benull
Returns:
true
if the provided value is considered to be different and a value change event should be fired;false
if the values are considered to be the same and no value change should be fired
-
doSetValue
protected abstract void doSetValue(T value)
Sets the value of this field. May do sanitization or throw
IllegalArgumentException
if the value is invalid. Typically saves the value to shared state.Parameters:
value
- the new value of the fieldThrows:
IllegalArgumentException
- if the value is invalid
-
createValueChange
protected HasValue.ValueChangeEvent<T> createValueChange(T oldValue, boolean userOriginated)
Returns a new value change event instance.
Parameters:
oldValue
- the value of this field before this value change eventuserOriginated
-true
if this event originates from the client,false
otherwise.Returns:
the new event
-
getState
protected AbstractFieldState getState()
Description copied from class:
AbstractComponent
Returns the shared state bean with information to be sent from the server to the client. Subclasses should override this method and set any relevant fields of the state returned by super.getState().
Overrides:
getState
in classAbstractComponent
Returns:
updated component shared state
-
getState
protected AbstractFieldState getState(boolean markAsDirty)
Description copied from class:
AbstractClientConnector
Returns the shared state for this connector.
Overrides:
getState
in classAbstractComponent
Parameters:
markAsDirty
- true if the connector should automatically be marked dirty, false otherwiseReturns:
The shared state for this connector. Never null.
See Also:
-
focus
public void focus()
Description copied from class:
AbstractComponent
Sets the focus for this component if the component is
Component.Focusable
.Specified by:
focus
in interfaceComponent.Focusable
Overrides:
focus
in classAbstractComponent
See Also:
FieldEvents
,FieldEvents.FocusEvent
,FieldEvents.FocusListener
,FieldEvents.BlurEvent
,FieldEvents.BlurListener
-
getTabIndex
public int getTabIndex()
Description copied from interface:
Component.Focusable
Gets the tabulator index of the
Focusable
component.Specified by:
getTabIndex
in interfaceComponent.Focusable
Returns:
tab index set for the
Focusable
componentSee Also:
-
setTabIndex
public void setTabIndex(int tabIndex)
Description copied from interface:
Component.Focusable
Sets the tabulator index of the
Focusable
component. The tab index property is used to specify the order in which the fields are focused when the user presses the Tab key. Components with a defined tab index are focused sequentially first, and then the components with no tab index.Form loginBox = new Form(); loginBox.setCaption("Login"); layout.addComponent(loginBox); // Create the first field which will be focused TextField username = new TextField("User name"); loginBox.addField("username", username); // Set focus to the user name username.focus(); TextField password = new TextField("Password"); loginBox.addField("password", password); Button login = new Button("Login"); loginBox.getFooter().addComponent(login); // An additional component which natural focus order would // be after the button. CheckBox remember = new CheckBox("Remember me"); loginBox.getFooter().addComponent(remember); username.setTabIndex(1); password.setTabIndex(2); remember.setTabIndex(3); // Different than natural place login.setTabIndex(4);
After all focusable user interface components are done, the browser can begin again from the component with the smallest tab index, or it can take the focus out of the page, for example, to the location bar.
If the tab index is not set (is set to zero), the default tab order is used. The order is somewhat browser-dependent, but generally follows the HTML structure of the page.
A negative value means that the component is completely removed from the tabulation order and can not be reached by pressing the Tab key at all.
Specified by:
setTabIndex
in interfaceComponent.Focusable
Parameters:
tabIndex
- the tab order of this component. Indexes usually start from 1. Zero means that default tab order should be used. A negative value means that the field should not be included in the tabbing sequence.See Also:
-
setRequiredIndicatorVisible
public void setRequiredIndicatorVisible(boolean visible)
Description copied from class:
AbstractComponent
Sets the visibility of the required indicator. NOTE: Does not apply for all components!.
If the component supports the required indicator (state extends
AbstractFieldState
), then expose this method andAbstractComponent.isRequiredIndicatorVisible()
aspublic
in the component and call this method.This method will throw a
IllegalStateException
if the component state (returned byAbstractComponent.getState()
) does not inheritAbstractFieldState
.Specified by:
setRequiredIndicatorVisible
in interfaceHasValue<T>
Overrides:
setRequiredIndicatorVisible
in classAbstractComponent
Parameters:
visible
-true
to make the required indicator visible,false
if not
-
isRequiredIndicatorVisible
public boolean isRequiredIndicatorVisible()
Description copied from class:
AbstractComponent
Checks whether the required indicator is visible or not. NOTE: Does not apply for all components!.
This method will throw a
IllegalStateException
if the component state (returned byAbstractComponent.getState()
) does not inheritAbstractFieldState
.Specified by:
isRequiredIndicatorVisible
in interfaceHasValue<T>
Overrides:
isRequiredIndicatorVisible
in classAbstractComponent
Returns:
true
if visible,false
if notSee Also:
-
-