Class ObjectForm<D>
- Type Parameters:
D
- Type of object to be edited
- All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier
,com.vaadin.flow.component.DetachNotifier
,com.vaadin.flow.component.HasElement
,com.vaadin.flow.component.HasStyle
,Serializable
- Direct Known Subclasses:
AbstractDataEditor.DForm
Form (see AbstractForm
) contains "fields" (HasValue
) and each field has a name. By default, "field names" are automatically determined
from the attributes of the object under editing via reflection. So, getXXX (for example, getFirstName()) methods are used to
determine the field names and its value types.
Field values can be loaded from the object instance that is being edited and edited values can be committed to the object instance.
By default, getXXX/setXXX
methods of the object class are used for this. However, a particular getXXX or setXXX method can be
overridden by defining a method with the same name in the form itself. Yet another facility is to define some of those methods in some
other class and set an instance of that class in the form using setMethodHandlerHost(Object)
. However, the signatures of the getXXX/setXXX
methods that are overridden like this take the object instance as an additional parameter as its first parameter. For example, let's assume that
our object class represents a "Person" and we want to override the "DateOfBirth" field's "set" method. In the Person class, we may be
having the getDateOfBirth() method that returns "date of birth" and setDateOfBirth(Date) to set the "date of birth". So, the overridden "set"
method in the form should have signatures as follows: setDateOfBirth(Person, Date). If we want to override the "get" method, it should look
like getDateOfBirth(Person).
The order of precedence when it has to determine get/set methods - First preference is given to the available methods in the form, then the available methods in the "method handler host" (if ones is set) and finally, in the object class itself. If it can't determine the "set" method for a field, the field will be set as "read only".
If a getXXX method of the "host" should not be treated like this, it can be annotated with NoField
.
What is the purpose of overriding get/set methods? One can implement extra logic such as value conversion, validation etc.
before getting/setting the values from/to the object instance. (It is better to add validators if that is the only purpose - see
AbstractForm.addValidator(HasValue, Function)
, AbstractForm.addValidator(HasValue, Function, String)
). When extra fields (see next paragraph)
need to be added, this mechanism is anyway required.
Any number of extra fields can be added using addField(String...)
, addField(Iterable)
,
addField(String, Method, Method)
, addField(String, Method)
, addField(String, Function, BiConsumer)
,
addField(String, Function)
, AbstractForm.addField(String, HasValue)
, AbstractForm.addField(HasValue)
. When you add additional
fields, depending on the method you use, it can determine how to get/set values. If not enough information is provided as parameters
(for example, methods addField(String...)
), reflection is used to determine it as mentioned in the previous paragraphs.
An "object form" is not used directly in most cases. Instead, a View
derived from AbstractDataEditor
or
DataEditor
is used where an "object form" is already embedded. All overridable methods in the form can be defined in these
"views" too.
- Author:
- Syam
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.storedobject.vaadin.AbstractForm
AbstractForm.ValueHandler
-
Field Summary
Fields inherited from class com.storedobject.vaadin.AbstractForm
container, data, objectClass, objectData
-
Constructor Summary
ConstructorsConstructorDescriptionObjectForm
(Class<D> objectClass) ConstructorObjectForm
(Class<D> objectClass, com.vaadin.flow.component.HasComponents container) Constructor -
Method Summary
Modifier and TypeMethodDescriptionprotected <V> String
Add an extra fieldprotected <V> String
addField
(com.vaadin.flow.component.HasValue<?, V> field, Function<D, V> valueGetter, BiConsumer<D, V> valueSetter) Add an extra field.protected void
Add extra fields.protected void
Add extra fields.protected void
Add an extra fieldprotected void
Add an extra fieldprotected void
Add an extra fieldprotected void
Add an extra fieldprotected void
This method is invoked when the "fields" are created and the form is ready to display.protected final com.vaadin.flow.component.HasValue
<?, ?> createField
(String fieldName) Create the field for the particular name.protected com.vaadin.flow.component.HasValue
<?, ?> createField
(String fieldName, Class<?> fieldType, String label) Create the field for the particular name.protected void
customizeField
(String fieldName, com.vaadin.flow.component.HasValue<?, ?> field) Customize a field.protected void
Generate field names.Get the class of the object being edited.protected Method
getFieldGetMethod
(String fieldName) Get the field's "get" method.Get the valid/allowed field names.protected int
getFieldOrder
(String fieldName) Get the order in which a field to appear in the form.protected Method
getFieldSetMethod
(String fieldName, Method getMethod) Get the field's "set" method.Get the label for the field.Get the class of the object being edited.protected boolean
handleValueSetError
(String fieldName, com.vaadin.flow.component.HasValue<?, ?> field, Object fieldValue, Object objectValue, Throwable error) Handle errors while setting (committing) values.protected boolean
includeField
(String fieldName) Check whether field should be included in the form or not.final boolean
isFieldIncluded
(String fieldName) Check whether this field was included while creating this form or not.void
setFieldCustomizer
(FieldCustomizer fieldCustomizer) Set an external field customizer.void
setMethodHandlerHost
(Object host) Set the method handler host.Methods inherited from class com.storedobject.vaadin.AbstractForm
add, addField, addField, addFields, addIncludeFieldChecker, addValidator, addValidator, attachField, clearError, clearErrors, clearFields, commit, connect, connect, connect, createContainer, createField, createObjectInstance, detachField, dumpValues, getBinder, getColumns, getColumnSpan, getComponent, getContainer, getContent, getErrorDisplay, getField, getFieldLabel, getFieldLabel, getFieldName, getObject, getObject, getView, initContent, isFieldEditable, isFieldEditable, isFieldVisible, isFieldVisible, isReadOnly, load, markError, newLine, remove, removeAll, removeField, removeField, removeField, setColumns, setColumnSpan, setErrorDisplay, setFieldLabel, setFieldLabel, setIncludeFieldChecker, setObject, setObject, setReadOnly, setRequired, setRequired, setRequired, setRequired, setRequired, setRequired, setRequired, setRequired, setView, streamFieldNamesCreated, streamFieldsCreated
Methods inherited from class com.vaadin.flow.component.Component
addListener, findAncestor, fireEvent, from, get, 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, setVisible
-
Constructor Details
-
Method Details
-
constructed
protected void constructed()Description copied from class:AbstractForm
This method is invoked when the "fields" are created and the form is ready to display.- Overrides:
constructed
in classAbstractForm<D>
-
setMethodHandlerHost
Set the method handler host. (See the documentation of this class to get an idea).- Parameters:
host
- Method handler host
-
getObjectClass
Get the class of the object being edited. (Same asgetDataClass()
).- Returns:
- Object's class.
-
getDataClass
Get the class of the object being edited. (Same asgetObjectClass()
).- Returns:
- Object's class.
-
addField
-
addField
Add extra fields.- Parameters:
fieldNames
- Names of the fields to be added
-
addField
protected <V> String addField(com.vaadin.flow.component.HasValue<?, V> field, Function<D, V> valueGetter) Add an extra field- Type Parameters:
V
- Value type of the field.- Parameters:
field
- Field.valueGetter
- Function that determines how to get the value to load the field.- Returns:
- A field name will be generated (starting with an underscore character followed by a random number) and returned.
-
addField
protected <V> String addField(com.vaadin.flow.component.HasValue<?, V> field, Function<D, V> valueGetter, BiConsumer<D, V> valueSetter) Add an extra field.- Type Parameters:
V
- Value type of the field.- Parameters:
field
- Field.valueGetter
- Function that determines how to get the value to load the field.valueSetter
- Function that determines how to commit value from the field to the object's instance.- Returns:
- A field name will be generated (starting with an underscore character followed by a random number) and returned.
-
addField
-
addField
Add an extra field- Parameters:
fieldName
- Name of the field.valueGetter
- Function that determines how to get the value to load the fieldvalueSetter
- Function that determines how to commit value from the field to the object's instance
-
addField
-
addField
Add an extra field- Parameters:
fieldName
- Name of the field.getMethod
- Method that determines how to get the value to load the fieldsetMethod
- Method that determines how to commit the value from the field to the object's instance
-
getFieldGetMethod
-
getFieldSetMethod
Get the field's "set" method. The default implementation checks the availability of setXXX method.- Parameters:
fieldName
- Name of the fieldgetMethod
- "get" method of this field (determined throughgetFieldGetMethod(String)
)- Returns:
- Field's "set" method (if method is found, it will return
null
).
-
generateFieldNames
protected void generateFieldNames()Generate field names. This method is generally not overridden.- Overrides:
generateFieldNames
in classAbstractForm<D>
-
getFieldNames
Get the valid/allowed field names. (For internal use only).- Overrides:
getFieldNames
in classAbstractForm<D>
- Returns:
- Stream of field names.
-
getFieldOrder
Get the order in which a field to appear in the form. Any integer value can be returned and the field is placed in the form in ascending order of the values returned by this method. Default implementation try to obtain the value from the "field creator" (ObjectFieldCreator.getFieldOrder(String)
).- Parameters:
fieldName
- Name of the field- Returns:
- Field order.
-
includeField
Check whether field should be included in the form or not.- Parameters:
fieldName
- Name of the field.- Returns:
- True or false. (Default return value is
true
).
-
isFieldIncluded
Check whether this field was included while creating this form or not.- Parameters:
fieldName
- Name of the field.- Returns:
- True or false.
-
createField
Create the field for the particular name.- Overrides:
createField
in classAbstractForm<D>
- Parameters:
fieldName
- Name of the field- Returns:
- Field.
-
setFieldCustomizer
Set an external field customizer. If set, the methodFieldCustomizer.customizeField(String, HasValue)
will be invoked when each field gets created. This will be invoked aftercustomizeField(String, HasValue)
andObjectFieldCreator.customizeField(String, HasValue)
are invoked.- Parameters:
fieldCustomizer
- Field customizer.
-
getLabel
Get the label for the field. Default implementation try to obtain the value from the "field creator" (ObjectFieldCreator.getLabel(String)
).- Overrides:
getLabel
in classAbstractForm<D>
- Parameters:
fieldName
- Name of the field- Returns:
- Label
-
createField
protected com.vaadin.flow.component.HasValue<?,?> createField(String fieldName, Class<?> fieldType, String label) Create the field for the particular name. Default implementation try to obtain the value from the "field creator" (ObjectFieldCreator.createField(String, Class, String)
).- Parameters:
fieldName
- Name of the fieldfieldType
- Type of the field's valuelabel
- Label- Returns:
- Field
-
customizeField
Customize a field. This method is invoked when a field is created. One may do some customization here.- Parameters:
fieldName
- Name of the fieldfield
- Field
-
handleValueSetError
protected boolean handleValueSetError(String fieldName, com.vaadin.flow.component.HasValue<?, ?> field, Object fieldValue, Object objectValue, Throwable error) Handle errors while setting (committing) values. Default implementation returnstrue
without doing anything.- Parameters:
fieldName
- Name of the fieldfield
- FieldfieldValue
- Field's value that's being committedobjectValue
- Value currently being in the objecterror
- Error occurred while setting the value- Returns:
- Whether the error situation is handled or not.
-