|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.vaadin.terminal.AbstractClientConnector com.vaadin.ui.AbstractComponent com.vaadin.ui.AbstractField<T>
public abstract class AbstractField<T>
Abstract field component for implementing buffered property editors. The
field may hold an internal value, or it may be connected to any data source
that implements the Property
interface.
AbstractField
implements that interface itself, too, so
accessing the Property value represented by it is straightforward.
AbstractField also provides the Buffered
interface
for buffering the data source value. By default the Field is in write
through-mode and setWriteThrough(boolean)
should be called to enable
buffering.
The class also supports validators
to make
sure the value contained in the field is valid.
Nested Class Summary | |
---|---|
static class |
AbstractField.FocusShortcut
A ready-made ShortcutListener that focuses the given
Focusable (usually a Field ) when the keyboard shortcut is
invoked. |
static class |
AbstractField.ReadOnlyStatusChangeEvent
An Event object specifying the Property whose read-only
status has changed. |
Nested classes/interfaces inherited from class com.vaadin.ui.AbstractComponent |
---|
AbstractComponent.ComponentErrorEvent, AbstractComponent.ComponentErrorHandler |
Nested classes/interfaces inherited from interface com.vaadin.ui.Field |
---|
Field.ValueChangeEvent |
Nested classes/interfaces inherited from interface com.vaadin.data.Buffered |
---|
Buffered.SourceException |
Nested classes/interfaces inherited from interface com.vaadin.data.Property |
---|
Property.Editor, Property.ReadOnlyException, Property.ReadOnlyStatusChangeListener, Property.ReadOnlyStatusChangeNotifier, Property.Transactional<T>, Property.ValueChangeListener, Property.ValueChangeNotifier, Property.Viewer |
Nested classes/interfaces inherited from interface com.vaadin.ui.Component |
---|
Component.ErrorEvent, Component.ErrorListener, Component.Event, Component.Focusable, Component.Listener |
Nested classes/interfaces inherited from interface com.vaadin.terminal.Sizeable |
---|
Sizeable.Unit |
Field Summary |
---|
Fields inherited from interface com.vaadin.terminal.Sizeable |
---|
SIZE_UNDEFINED, UNITS_CM, UNITS_EM, UNITS_EX, UNITS_INCH, UNITS_MM, UNITS_PERCENTAGE, UNITS_PICAS, UNITS_PIXELS, UNITS_POINTS |
Constructor Summary | |
---|---|
AbstractField()
|
Method Summary | |
---|---|
void |
addListener(Property.ReadOnlyStatusChangeListener listener)
Registers a new read-only status change listener for this Property. |
void |
addListener(Property.ValueChangeListener listener)
Registers a new value change listener for this Property. |
void |
addValidator(Validator validator)
Adds a new validator for the field's value. |
void |
attach()
Notifies the component that it is connected to an application. |
void |
commit()
Updates all changes since the previous commit to the data source. |
void |
detach()
Notifies the component that it is detached from the application. |
void |
discard()
Discards all changes since last commit. |
protected void |
fireReadOnlyStatusChange()
Emits the read-only status change event. |
protected void |
fireValueChange(boolean repaintIsNotNeeded)
Emits the value change event. |
void |
focus()
Sets the focus for this component if the component is Focusable . |
String |
getConversionError()
Gets the error that is shown if the field value cannot be converted to the data source type. |
protected String |
getConversionError(Class<?> dataSourceType)
Returns the conversion error with {0} replaced by the data source type. |
Object |
getConvertedValue()
Returns the current value (as returned by getValue() ) converted
to the data source type. |
Converter<T,Object> |
getConverter()
Gets the converter used to convert the property data source value to the field value. |
protected Buffered.SourceException |
getCurrentBufferedSourceException()
Gets the current buffered source exception. |
ErrorMessage |
getErrorMessage()
Error messages shown by the fields are composites of the error message thrown by the superclasses (that is the component error message), validation errors and buffered source errors. |
protected T |
getInternalValue()
Returns the internal field value, which might not match the data source value e.g. |
Property |
getPropertyDataSource()
Gets the current data source of the field, if any. |
String |
getRequiredError()
Gets the error message that is to be displayed if a required field is empty. |
AbstractFieldState |
getState()
Returns the shared state bean with information to be sent from the server to the client. |
int |
getTabIndex()
Gets the tabulator index of the Focusable component. |
abstract Class<? extends T> |
getType()
Returns the type of the Field. |
Collection<Validator> |
getValidators()
Gets the validators of the field. |
T |
getValue()
Gets the current value of the field. |
boolean |
isBuffered()
Checks the buffered mode of this Field. |
protected boolean |
isEmpty()
Is the field empty? In general, "empty" state is same as null. |
boolean |
isInvalidAllowed()
Fields allow invalid values by default. |
boolean |
isInvalidCommitted()
Tests if the invalid data is committed to datasource. |
boolean |
isModified()
Tests if the value stored in the object has been modified since it was last updated from the data source. |
boolean |
isReadOnly()
The abstract field is read only also if the data source is in read only mode. |
boolean |
isReadThrough()
Tests if the object is in read-through mode. |
boolean |
isRequired()
Is this field required. |
boolean |
isValid()
Tests the current value against registered validators if the field is not empty. |
boolean |
isValidationVisible()
Is automatic, visible validation enabled? If automatic validation is enabled, any validators connected to this component are evaluated while painting the component and potential error messages are sent to client. |
boolean |
isWriteThrough()
Tests if the object is in write-through mode. |
void |
readOnlyStatusChange(Property.ReadOnlyStatusChangeEvent event)
React to read only status changes of the property by requesting a repaint. |
void |
removeAllValidators()
Removes all validators from the field. |
void |
removeListener(Property.ReadOnlyStatusChangeListener listener)
Removes a previously registered read-only status change listener. |
void |
removeListener(Property.ValueChangeListener listener)
Removes a previously registered value change listener. |
void |
removeValidator(Validator validator)
Removes the validator from the field. |
void |
setBuffered(boolean buffered)
Sets the buffered mode of this Field. |
void |
setConversionError(String valueConversionError)
Sets the error that is shown if the field value cannot be converted to the data source type. |
void |
setConvertedValue(Object value)
Sets the value of the field using a value of the data source type. |
void |
setConverter(Class<?> datamodelType)
Retrieves a converter for the field from the converter factory defined for the application. |
void |
setConverter(Converter<T,?> converter)
Sets the converter used to convert the field value to property data source type. |
void |
setCurrentBufferedSourceException(Buffered.SourceException currentBufferedSourceException)
Sets the current buffered source exception. |
protected void |
setInternalValue(T newValue)
Sets the internal field value. |
void |
setInvalidAllowed(boolean invalidAllowed)
Fields allow invalid values by default. |
void |
setInvalidCommitted(boolean isCommitted)
Sets if the invalid data should be committed to datasource. |
void |
setPropertyDataSource(Property newDataSource)
Sets the specified Property as the data source for the field. |
void |
setReadOnly(boolean readOnly)
Changes the readonly state and throw read-only status change events. |
void |
setReadThrough(boolean readThrough)
Deprecated. Use setBuffered(boolean) instead. Note that
setReadThrough(true), setWriteThrough(true) equals
setBuffered(false) |
void |
setRequired(boolean required)
Sets the field required. |
void |
setRequiredError(String requiredMessage)
Set the error that is show if this field is required, but empty. |
void |
setTabIndex(int tabIndex)
Sets the tabulator index of the Focusable component. |
void |
setValidationVisible(boolean validateAutomatically)
Enable or disable automatic, visible validation. |
void |
setValue(Object newFieldValue)
Sets the value of the field. |
protected void |
setValue(T newFieldValue,
boolean repaintIsNotNeeded)
Sets the value of the field. |
void |
setWriteThrough(boolean writeThrough)
Deprecated. Use setBuffered(boolean) instead. Note that
setReadThrough(true), setWriteThrough(true) equals
setBuffered(false) |
protected boolean |
shouldHideErrors()
Returns true if the error indicator be hidden when painting the component even when there are errors. |
String |
toString()
Deprecated. Instead use getValue() to get the value of the
field, getConvertedValue() to get the field value
converted to the data model type or
getPropertyDataSource() .getValue() to get the value
of the data source. |
void |
updateState()
Called before the shared state is sent to the client. |
void |
validate()
Checks the validity of the Field. |
protected void |
validate(T fieldValue)
Validates that the given value pass the validators for the field. |
void |
valueChange(Property.ValueChangeEvent event)
This method listens to data source value changes and passes the changes forwards. |
Methods inherited from class com.vaadin.terminal.AbstractClientConnector |
---|
addExtension, addMethodInvocationToQueue, createState, getAllChildrenIterable, getConnectorId, getExtensions, getRpcManager, getRpcProxy, getStateType, registerRpc, registerRpc, removeExtension, requestRepaintAll, retrievePendingRpcCalls |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface com.vaadin.ui.Component |
---|
addListener, addStyleName, getApplication, getCaption, getDebugId, getIcon, getLocale, getParent, getRoot, getStyleName, isEnabled, isVisible, removeListener, removeStyleName, setCaption, setDebugId, setEnabled, setIcon, setStyleName, setVisible |
Methods inherited from interface com.vaadin.terminal.gwt.server.ClientConnector |
---|
getExtensions, getStateType, isConnectorEnabled, removeExtension, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setParent |
Methods inherited from interface com.vaadin.terminal.gwt.client.Connector |
---|
getConnectorId |
Methods inherited from interface com.vaadin.terminal.gwt.server.RpcTarget |
---|
getRpcManager |
Methods inherited from interface com.vaadin.terminal.Sizeable |
---|
getHeight, getHeightUnits, getWidth, getWidthUnits, setHeight, setHeight, setSizeFull, setSizeUndefined, setWidth, setWidth |
Methods inherited from interface com.vaadin.event.Action.ShortcutNotifier |
---|
addShortcutListener, removeShortcutListener |
Constructor Detail |
---|
public AbstractField()
Method Detail |
---|
protected boolean shouldHideErrors()
public abstract Class<? extends T> getType()
getValue
and
setValue
must be compatible with this type: one must be able
to safely cast the value returned from getValue
to the given
type and pass any variable assignable to this type as an argument to
setValue
.
getType
in interface Property<T>
public boolean isReadOnly()
isReadOnly
in interface Property<T>
isReadOnly
in interface Component
isReadOnly
in class AbstractComponent
true
if the component or any of its parents is in
read-only mode, false
if not.Component.setReadOnly(boolean)
public void setReadOnly(boolean readOnly)
setReadOnly
in interface Property<T>
setReadOnly
in interface Component
setReadOnly
in class AbstractComponent
readOnly
- a boolean value specifying whether the component is put
read-only mode or notComponent.setReadOnly(boolean)
public boolean isInvalidCommitted()
isInvalidCommitted
in interface BufferedValidatable
BufferedValidatable.isInvalidCommitted()
public void setInvalidCommitted(boolean isCommitted)
setInvalidCommitted
in interface BufferedValidatable
BufferedValidatable.setInvalidCommitted(boolean)
public void commit() throws Buffered.SourceException, Validator.InvalidValueException
Buffered
commit
is called.
commit
in interface Buffered
Buffered.SourceException
- if the operation fails because of an exception is thrown by
the data source. The cause is included in the exception.
Validator.InvalidValueException
- if the operation fails because validation is enabled and the
values do not validatepublic void discard() throws Buffered.SourceException
Buffered
discard
in interface Buffered
Buffered.SourceException
- if the operation fails because of an exception is thrown by
the data source. The cause is included in the exception.public boolean isModified()
Buffered
isModified
in interface Buffered
true
if the value in the object has been modified
since the last data source update, false
if not.public boolean isWriteThrough()
Buffered
commit
being called after the modification.
isWriteThrough
in interface Buffered
true
if the object is in write-through mode,
false
if it's not.@Deprecated public void setWriteThrough(boolean writeThrough) throws Buffered.SourceException, Validator.InvalidValueException
setBuffered(boolean)
instead. Note that
setReadThrough(true), setWriteThrough(true) equals
setBuffered(false)
commit()
will be
performed.
setWriteThrough
in interface Buffered
writeThrough
- Boolean value to indicate if the object should be in
write-through mode after the call.
SourceException
- If the operation fails because of an exception is thrown by
the data source.
Validator.InvalidValueException
- If the implicit commit operation fails because of a
validation error.
Buffered.SourceException
for an easier way to control read through and
write through modes
public boolean isReadThrough()
Buffered
The only exception to this rule is that when the object is not in write-through mode and it's buffer contains a modified value, the value retrieved from the object will be the locally modified value in the buffer which may differ from the value in the data source.
isReadThrough
in interface Buffered
true
if the object is in read-through mode,
false
if it's not.@Deprecated public void setReadThrough(boolean readThrough) throws Buffered.SourceException
setBuffered(boolean)
instead. Note that
setReadThrough(true), setWriteThrough(true) equals
setBuffered(false)
setReadThrough
in interface Buffered
readThrough
- Boolean value to indicate if the object should be in
read-through mode after the call.
SourceException
- If the operation fails because of an exception is thrown by
the data source. The cause is included in the exception.
Buffered.SourceException
for an easier way to control read through and
write through modes
public void setBuffered(boolean buffered)
When the field is in buffered mode, changes will not be committed to the
property data source until commit()
is called.
Changing buffered mode will change the read through and write through state for the field.
Mixing calls to setBuffered(boolean)
and
setReadThrough(boolean)
or setWriteThrough(boolean)
is
generally a bad idea.
setBuffered
in interface Buffered
buffered
- true if buffered mode should be turned on, false otherwisepublic boolean isBuffered()
This method only returns true if both read and write buffering is used.
isBuffered
in interface Buffered
@Deprecated public String toString()
getValue()
to get the value of the
field, getConvertedValue()
to get the field value
converted to the data model type or
getPropertyDataSource()
.getValue() to get the value
of the data source.
toString
in class Object
Object.toString()
public T getValue()
This is the visible, modified and possible invalid value the user have entered to the field.
Note that the object returned is compatible with getType(). For example,
if the type is String, this returns Strings even when the underlying
datasource is of some other type. In order to access the converted value,
use getConvertedValue()
and to access the value of the property
data source, use Property.getValue()
for the property data
source.
Since Vaadin 7.0, no implicit conversions between other data types and String are performed, but a converter is used if set.
getValue
in interface Property<T>
public void setValue(Object newFieldValue) throws Property.ReadOnlyException, Converter.ConversionException
setValue
in interface Property<T>
newFieldValue
- the New value of the field.
Property.ReadOnlyException
Converter.ConversionException
protected void setValue(T newFieldValue, boolean repaintIsNotNeeded) throws Property.ReadOnlyException, Converter.ConversionException, Validator.InvalidValueException
newFieldValue
- the New value of the field.repaintIsNotNeeded
- True iff caller is sure that repaint is not needed.
Property.ReadOnlyException
Converter.ConversionException
Validator.InvalidValueException
public Property getPropertyDataSource()
getPropertyDataSource
in interface Property.Viewer
null
if
none defined.public void setPropertyDataSource(Property newDataSource)
Sets the specified Property as the data source for the field. All uncommitted changes are replaced with a value from the new data source.
If the datasource has any validators, the same validators are added to the field. Because the default behavior of the field is to allow invalid values, but not to allow committing them, this only adds visual error messages to fields and do not allow committing them as long as the value is invalid. After the value is valid, the error message is not shown and the commit can be done normally.
If the data source implements
Property.ValueChangeNotifier
and/or
Property.ReadOnlyStatusChangeNotifier
, the field
registers itself as a listener and updates itself according to the events
it receives. To avoid memory leaks caused by references to a field no
longer in use, the listener registrations are removed on
detach
and re-added on
attach
.
Note: before 6.5 we actually called discard() method in the beginning of the method. This was removed to simplify implementation, avoid excess calls to backing property and to avoid odd value change events that were previously fired (developer expects 0-1 value change events if this method is called). Some complex field implementations might now need to override this method to do housekeeping similar to discard().
setPropertyDataSource
in interface Property.Viewer
newDataSource
- the new data source Property.public void setConverter(Class<?> datamodelType)
datamodelType
- The type of the data model that we want to be able to convert
fromprotected String getConversionError(Class<?> dataSourceType)
dataSourceType
- The type of the data source
public Object getConvertedValue()
getValue()
) converted
to the data source type.
This returns the same as getValue()
if no converter
has been set. The value is not necessarily the same as the data source
value e.g. if the field is in buffered mode and has been modified.
public void setConvertedValue(Object value)
setValue(Object)
is called.
value
- The value to set. Must be the same type as the data source.public void addValidator(Validator validator)
addValidator
in interface Validatable
validator
- the new validator to be added.public Collection<Validator> getValidators()
getValidators
in interface Validatable
public void removeValidator(Validator validator)
removeValidator
in interface Validatable
validator
- the validator to remove.public void removeAllValidators()
public boolean isValid()
validate()
should be used instead of
isValid()
to also get the error message.
isValid
in interface Validatable
true
if all registered validators claim that the
current value is valid or if the field is empty and not required,
false
otherwise.public void validate() throws Validator.InvalidValueException
setRequired(boolean)
and is empty, if one or several of the
validators added to the field indicate it is invalid or if the value
cannot be converted provided a converter has been set.
The "required" validation is a built-in validation feature. If the field
is required and empty this method throws an EmptyValueException with the
error message set using setRequiredError(String)
.
validate
in interface Validatable
Validator.InvalidValueException
- if the value is not validValidatable.validate()
protected void validate(T fieldValue) throws Validator.InvalidValueException
This method does not check the requiredness of the field.
fieldValue
- The value to check
Validator.InvalidValueException
- if one or several validators failpublic boolean isInvalidAllowed()
isInvalidAllowed
in interface Validatable
Validatable.isInvalidAllowed()
public void setInvalidAllowed(boolean invalidAllowed) throws UnsupportedOperationException
In common setting where the user wants to assure the correctness of the datasource, but allow temporarily invalid contents in the field, the user should add the validators to datasource, that should not allow invalid values. The validators are automatically copied to the field when the datasource is set.
setInvalidAllowed
in interface Validatable
UnsupportedOperationException
- if the setInvalidAllowed is not supported.Validatable.setInvalidAllowed(boolean)
public ErrorMessage getErrorMessage()
getErrorMessage
in class AbstractComponent
AbstractComponent.getErrorMessage()
public void addListener(Property.ValueChangeListener listener)
Property.ValueChangeNotifier
addListener
in interface Property.ValueChangeNotifier
listener
- the new Listener to be registeredpublic void removeListener(Property.ValueChangeListener listener)
Property.ValueChangeNotifier
removeListener
in interface Property.ValueChangeNotifier
listener
- listener to be removedprotected void fireValueChange(boolean repaintIsNotNeeded)
public void readOnlyStatusChange(Property.ReadOnlyStatusChangeEvent event)
readOnlyStatusChange
in interface Property.ReadOnlyStatusChangeListener
event
- Read-only status change event objectProperty.ReadOnlyStatusChangeListener
public void addListener(Property.ReadOnlyStatusChangeListener listener)
Property.ReadOnlyStatusChangeNotifier
addListener
in interface Property.ReadOnlyStatusChangeNotifier
listener
- the new Listener to be registeredpublic void removeListener(Property.ReadOnlyStatusChangeListener listener)
Property.ReadOnlyStatusChangeNotifier
removeListener
in interface Property.ReadOnlyStatusChangeNotifier
listener
- listener to be removedprotected void fireReadOnlyStatusChange()
public void valueChange(Property.ValueChangeEvent event)
valueChange
in interface Property.ValueChangeListener
event
- the value change event telling the data source contents have
changed.public void focus()
Focusable
.
focus
in interface Component.Focusable
focus
in class AbstractComponent
FieldEvents
,
FieldEvents.FocusEvent
,
FieldEvents.FocusListener
,
FieldEvents.BlurEvent
,
FieldEvents.BlurListener
public int getTabIndex()
Component.Focusable
Focusable
component.
getTabIndex
in interface Component.Focusable
Focusable
componentComponent.Focusable.setTabIndex(int)
public void setTabIndex(int tabIndex)
Component.Focusable
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.
setTabIndex
in interface Component.Focusable
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.Component.Focusable.getTabIndex()
protected T getInternalValue()
setInternalValue(Object)
to compute internal field value at
runtime. When doing so, typically also isModified()
needs to be
overridden and care should be taken in the management of the empty state
and buffering support.
protected void setInternalValue(T newValue)
getInternalValue()
if necessary.
newValue
- the new value to be set.public void attach()
attach
in interface ClientConnector
attach
in interface Component
attach
in class AbstractComponent
Component.attach()
public void detach()
AbstractClientConnector
The caller of this method is ClientConnector.setParent(ClientConnector)
if the
parent is in the application. When the parent is detached from the
application it is its response to call ClientConnector.detach()
for all the
children and to detach itself from the terminal.
The AbstractClientConnector.getApplication()
and AbstractClientConnector.getRoot()
methods might return
null
after this method is called.
detach
in interface ClientConnector
detach
in class AbstractComponent
public boolean isRequired()
isRequired
in interface Field<T>
true
if the field is required, otherwise
false
.public void setRequired(boolean required)
setRequired
in interface Field<T>
required
- Is the field required.public void setRequiredError(String requiredMessage)
setRequiredError
in interface Field<T>
requiredMessage
- Message to be shown when this field is required, but empty.public String getRequiredError()
Field
getRequiredError
in interface Field<T>
public String getConversionError()
public void setConversionError(String valueConversionError)
valueConversionError
- Message to be shown when conversion of the value failsprotected boolean isEmpty()
public boolean isValidationVisible()
public void setValidationVisible(boolean validateAutomatically)
validateAutomatically
- True, if automatic validation is enabled.public void setCurrentBufferedSourceException(Buffered.SourceException currentBufferedSourceException)
currentBufferedSourceException
- protected Buffered.SourceException getCurrentBufferedSourceException()
public Converter<T,Object> getConverter()
public void setConverter(Converter<T,?> converter)
converter
- The new converter to use.public AbstractFieldState getState()
AbstractComponent
getState
in interface Connector
getState
in interface Component
getState
in class AbstractComponent
public void updateState()
Component
This method must not alter the component hierarchy in any way.
updateState
in interface Component
updateState
in class AbstractComponent
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |