Package com.storedobject.vaadin
Class AbstractForm<D>
java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.Composite<com.vaadin.flow.component.Component>
com.storedobject.vaadin.AbstractForm<D>
- Type Parameters:
D- Type of data object in the form
- 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:
Form,ObjectForm
public abstract class AbstractForm<D>
extends com.vaadin.flow.component.Composite<com.vaadin.flow.component.Component>
Class to represent a "data entry form". A "field" in the form is a
HasValue. Form internally keeps
Data that is a structure
that keeps data for all the "fields" of the form.
A form is not used directly in most cases. Instead, a View derived from AbstractDataForm
is used where a form is
already embedded. All overridable methods in the form can be defined in AbstractDataForm too.
If at all required to use it directly, one should use Form instead of AbstractForm.
- Author:
- Syam
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classBasic value-handler implementation. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected com.vaadin.flow.component.HasComponentsThe field container of the form.The "data" associated with this form.Class of the data object in the form.protected DCurrent instance of the data object in the form. -
Constructor Summary
ConstructorsModifierConstructorDescriptionAbstractForm(Class<D> objectClass) Construct a form.AbstractForm(Class<D> objectClass, com.vaadin.flow.component.HasComponents container) Construct a form with a specified field container.protectedAbstractForm(Class<D> objectClass, com.vaadin.flow.component.HasComponents container, Object dummy) For internal use only. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(com.vaadin.flow.component.Component... components) Add components to the form's field container.voidaddField(com.vaadin.flow.component.HasValue<?, ?> field) Add a field to the form.voidAdd a field to the form.voidaddFields(com.vaadin.flow.component.HasValue<?, ?>... fields) Add multiple fields to the form.com.vaadin.flow.shared.RegistrationaddIncludeFieldChecker(IncludeField includeField) Add an "include field checker" to the existing chain.<F> voidaddValidator(com.vaadin.flow.component.HasValue<?, F> field, Function<F, Boolean> validator) Add a validator for the field.<F> voidaddValidator(com.vaadin.flow.component.HasValue<?, F> field, Function<F, Boolean> validator, String errorMessage) Add a validator for the field.protected voidattachField(String fieldName, com.vaadin.flow.component.HasValue<?, ?> field) Attach a field to the form.static voidclearError(com.vaadin.flow.component.HasValue<?, ?> field) Clear the error status associated with a field.voidClear error status of all the fields.voidClear values of all the fields.booleancommit()Save data from the fields to the internal data structure.booleanconnect(com.vaadin.flow.component.HasValue<?, ?>... fields) Connect fields so that a change in any of it is propagated to others within that connected group.booleanConnect fields so that a change in any of it is propagated to others within that connected group.booleanconnect(Collection<com.vaadin.flow.component.HasValue<?, ?>> fields) Connect fields so that a change in any of it is propagated to others within that connected group.protected voidThis method is invoked when the "fields" are created and the form is ready to display.protected com.vaadin.flow.component.HasComponentsCreate the field container of this form.protected com.vaadin.flow.component.HasValue<?, ?> createField(String fieldName) Create the field for a particular "field name".protected com.vaadin.flow.component.HasValue<?, ?> createField(String fieldName, String label) Create the field for a particular "field name".protected DCreate an instance of the data object.protected voiddetachField(String fieldName, com.vaadin.flow.component.HasValue<?, ?> field) Detach a field from the form.voidPrint values from the internal data structure toSystem.err(Used for debugging purposes).protected voidFor internal use only.com.vaadin.flow.data.binder.Binder<D> Get the data binder.final intGet number of responsive columns for the form.intgetColumnSpan(com.vaadin.flow.component.Component component) Get the column span of a component.final com.vaadin.flow.component.ComponentGet the field container of the form as a component.final com.vaadin.flow.component.HasComponentsGet the field container of the form.final com.vaadin.flow.component.Componentcom.vaadin.flow.component.HasTextGet the current error display of the form.final com.vaadin.flow.component.HasValue<?, ?> Get a field associated with a field name.final StringgetFieldLabel(com.vaadin.flow.component.HasValue<?, ?> field) Get label for the given field.final StringgetFieldLabel(String fieldName) Get label for the given field.final StringgetFieldName(com.vaadin.flow.component.HasValue<?, ?> field) Get a field name associated with a field.Get the field names.This method is invoked to determine the label used for displaying the field.Get the instance of the currently editing data object.getObject(boolean create) Get the instance of the currently editing data object.getView()Get the associated view of this form.protected final com.vaadin.flow.component.ComponentbooleanisFieldEditable(com.vaadin.flow.component.HasValue<?, ?> field) This method is invoked to determine if a field needs to be made editable or not.booleanisFieldEditable(String fieldName) This method is invoked to determine if a field needs to be made editable or not.booleanisFieldVisible(com.vaadin.flow.component.HasValue<?, ?> field) This method is invoked to determine if a field needs to be made visible or not.booleanisFieldVisible(String fieldName) This method is invoked to determine if a field needs to be made visible or not.booleanCheck whether the form is "read only" or not.voidload()Load data to the fields from the internal data structure.static voidmarkError(com.vaadin.flow.component.HasValue<?, ?> field) Mark a field as not valid.voidnewLine()Add a new line to the form.voidremove(com.vaadin.flow.component.Component... components) Remove components from the form's field container.voidRemove all components from the form's field container.voidremoveField(com.vaadin.flow.component.HasValue<?, ?> field) Remove a field from the form.voidremoveField(String fieldName) Remove a field from the form.voidremoveField(String... fieldNames) Remove fields from the form.voidsetColumns(int columns) Set number of responsive columns for the form.voidsetColumnSpan(com.vaadin.flow.component.Component component, int columnSpan) Set number of columns to span for a particular component.voidsetErrorDisplay(com.vaadin.flow.component.HasText display) Error messages of the form are typically displayed usingNotification.final voidsetFieldLabel(com.vaadin.flow.component.HasValue<?, ?> field, String label) Set label for a specific field.final voidsetFieldLabel(String fieldName, String label) Set label for a specific field.voidsetIncludeFieldChecker(IncludeField includeField) Set "include field checker".voidSet the current object.voidSet the current object.voidsetReadOnly(boolean readOnly) Set the form read only.voidsetRequired(com.vaadin.flow.component.HasValue<?, ?> field) Set the "required" attribute of a field.voidsetRequired(com.vaadin.flow.component.HasValue<?, ?> field, boolean required) Set the "required" attribute of a field.voidsetRequired(com.vaadin.flow.component.HasValue<?, ?> field, boolean required, String errorMessage) Set the "required" attribute of a field.voidsetRequired(com.vaadin.flow.component.HasValue<?, ?> field, String errorMessage) Set the "required" attribute of a field.voidsetRequired(String fieldName) Set the "required" attribute of a field.voidsetRequired(String fieldName, boolean required) Set the "required" attribute of a field.voidsetRequired(String fieldName, boolean required, String errorMessage) Set the "required" attribute of a field.voidsetRequired(String fieldName, String errorMessage) Set the "required" attribute of a field.voidSet an associated view for this form.Get all fields names.final Stream<com.vaadin.flow.component.HasValue<?, ?>> Get all fields.Methods inherited from class com.vaadin.flow.component.Composite
getChildren, getElementMethods 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, 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.HasStyle
addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
-
Field Details
-
objectClass
-
objectData
Current instance of the data object in the form. -
container
protected com.vaadin.flow.component.HasComponents containerThe field container of the form. -
data
-
-
Constructor Details
-
AbstractForm
-
AbstractForm
-
AbstractForm
-
-
Method Details
-
initContent
protected final com.vaadin.flow.component.Component initContent()- Overrides:
initContentin classcom.vaadin.flow.component.Composite<com.vaadin.flow.component.Component>
-
getContent
public final com.vaadin.flow.component.Component getContent()- Overrides:
getContentin classcom.vaadin.flow.component.Composite<com.vaadin.flow.component.Component>
-
getBinder
Get the data binder. (For internal use only).- Returns:
- Data binder associated with this form.
-
setIncludeFieldChecker
Set "include field checker". It will determine if a field can be added or not.- Parameters:
includeField- The "include field checker"
-
addIncludeFieldChecker
Add an "include field checker" to the existing chain. If any one of theIncludeFieldin the chain returnsfalsefor a specific field, the field will not be included in the form.- Parameters:
includeField- The "include field checker" to add.- Returns:
- Registration.
-
createContainer
protected com.vaadin.flow.component.HasComponents createContainer()Create the field container of this form. This will be invoked only if no container was already set. Default implementation creates aFormLayout.- Returns:
- Field container created.
-
generateFieldNames
protected void generateFieldNames()For internal use only. -
getContainer
public final com.vaadin.flow.component.HasComponents getContainer()Get the field container of the form.- Returns:
- Field container.
-
constructed
protected void constructed()This method is invoked when the "fields" are created and the form is ready to display. -
getComponent
public final com.vaadin.flow.component.Component getComponent()Get the field container of the form as a component.- Returns:
- Field container as a component.
nullis returned if the field container is not a component.
-
getFieldNames
-
createField
Create the field for a particular "field name".- Parameters:
fieldName- Field name- Returns:
- Field created.
-
createField
-
addField
public void addField(com.vaadin.flow.component.HasValue<?, ?> field) Add a field to the form. The field will not be having a "field name".- Parameters:
field- Field to be added
-
addFields
public void addFields(com.vaadin.flow.component.HasValue<?, ?>... fields) Add multiple fields to the form. These fields will not be having "field names".- Parameters:
fields- Fields to be added
-
addField
Add a field to the form.- Parameters:
fieldName- Name associated with the fieldfield- Field
-
removeField
Remove a field from the form.- Parameters:
fieldName- Name of the field to be removed
-
removeField
Remove fields from the form.- Parameters:
fieldNames- Names of the field to be removed
-
removeField
public void removeField(com.vaadin.flow.component.HasValue<?, ?> field) Remove a field from the form.- Parameters:
field- Field to be removed.
-
attachField
Attach a field to the form. This is invoked whenever a field is added to the form. The default implementation adds the field component (if it is a component) to the field container.- Parameters:
fieldName- Name of the fieldfield- Field
-
detachField
Detach a field from the form. This is invoked whenever a field is removed to the form. The default implementation removes the field component (if it is a component) from the field container.- Parameters:
fieldName- Name of the fieldfield- Field
-
add
public void add(com.vaadin.flow.component.Component... components) Add components to the form's field container.- Parameters:
components- Components to add
-
remove
public void remove(com.vaadin.flow.component.Component... components) Remove components from the form's field container.- Parameters:
components- Components to remove
-
removeAll
public void removeAll()Remove all components from the form's field container. -
setColumns
public void setColumns(int columns) Set number of responsive columns for the form. (This will work only when the container is of typeFormLayout).- Parameters:
columns- Number of responsive columns required
-
getColumns
public final int getColumns()Get number of responsive columns for the form. (This will work only when the container is of typeFormLayout).- Returns:
- Number of responsive columns of the form.
-
newLine
public void newLine()Add a new line to the form. (This will work only when the container is of typeFormLayout). -
setColumnSpan
public void setColumnSpan(com.vaadin.flow.component.Component component, int columnSpan) Set number of columns to span for a particular component. (This will work only when the container is of typeFormLayout).- Parameters:
component- Component for which column span to be setcolumnSpan- Number of columns to span
-
getColumnSpan
public int getColumnSpan(com.vaadin.flow.component.Component component) Get the column span of a component. (This will work only when the container is of typeFormLayout).- Parameters:
component- Component for which column span to be retrieved- Returns:
- Column span for the component.
-
load
public void load()Load data to the fields from the internal data structure. -
commit
public boolean commit()Save data from the fields to the internal data structure.- Returns:
- True if data is saved successfully.
-
dumpValues
public void dumpValues()Print values from the internal data structure toSystem.err(Used for debugging purposes). -
streamFieldsCreated
Get all fields.- Returns:
- Stream of fields.
-
streamFieldNamesCreated
-
getField
Get a field associated with a field name.- Parameters:
fieldName- Name of the field- Returns:
- Field.
-
getFieldName
Get a field name associated with a field.- Parameters:
field- Field- Returns:
- Name of the field.
-
setRequired
public void setRequired(com.vaadin.flow.component.HasValue<?, ?> field) Set the "required" attribute of a field. If set to true, with empty data (checked by invokingHasValue.isEmpty()),commit()will fail.- Parameters:
field- Field
-
setRequired
-
setRequired
Set the "required" attribute of a field. If set to true, with empty data (checked by invokingHasValue.isEmpty()),commit()will fail.- Parameters:
field- FielderrorMessage- Error message to show when the field is empty
-
setRequired
-
setRequired
public void setRequired(com.vaadin.flow.component.HasValue<?, ?> field, boolean required) Set the "required" attribute of a field. If set to true, with empty data (checked by invokingHasValue.isEmpty()),commit()will fail.- Parameters:
field- Fieldrequired- True or false
-
setRequired
-
setRequired
public void setRequired(com.vaadin.flow.component.HasValue<?, ?> field, boolean required, String errorMessage) Set the "required" attribute of a field. If set to true, with empty data (checked by invokingHasValue.isEmpty()),commit()will fail.- Parameters:
field- Fieldrequired- True or falseerrorMessage- Error message to show when the field is empty
-
setRequired
Set the "required" attribute of a field. If set to true, with empty data (checked by invokingHasValue.isEmpty()),commit()will fail.- Parameters:
fieldName- Field namerequired- True or falseerrorMessage- Error message to show when the field is empty
-
addValidator
-
addValidator
public <F> void addValidator(com.vaadin.flow.component.HasValue<?, F> field, Function<F, Boolean> validator, String errorMessage) Add a validator for the field. Validator should returntrueif the field is valid.- Type Parameters:
F- Type of the field value.- Parameters:
field- Fieldvalidator- ValidatorerrorMessage- Error message to be displayed if the field is not valid
-
markError
public static void markError(com.vaadin.flow.component.HasValue<?, ?> field) Mark a field as not valid.- Parameters:
field- Field
-
clearError
public static void clearError(com.vaadin.flow.component.HasValue<?, ?> field) Clear the error status associated with a field.- Parameters:
field- Field
-
clearFields
public void clearFields()Clear values of all the fields. -
clearErrors
public void clearErrors()Clear error status of all the fields. -
setErrorDisplay
public void setErrorDisplay(com.vaadin.flow.component.HasText display) Error messages of the form are typically displayed usingNotification. However, one can set anyHasTextfor that.- Parameters:
display- Error messages will be displayed on this
-
getErrorDisplay
public com.vaadin.flow.component.HasText getErrorDisplay()Get the current error display of the form. (SeesetErrorDisplay(HasText)).- Returns:
- Current error display.
-
setReadOnly
public void setReadOnly(boolean readOnly) Set the form read only.- Parameters:
readOnly- True or false
-
isReadOnly
public boolean isReadOnly()Check whether the form is "read only" or not.- Returns:
- True or false.
-
isFieldVisible
This method is invoked to determine if a field needs to be made visible or not.- Parameters:
fieldName- Name of the field- Returns:
- True or false.
-
isFieldVisible
public boolean isFieldVisible(com.vaadin.flow.component.HasValue<?, ?> field) This method is invoked to determine if a field needs to be made visible or not.- Parameters:
field- Field- Returns:
- True or false.
-
isFieldEditable
This method is invoked to determine if a field needs to be made editable or not.- Parameters:
fieldName- Name of the field- Returns:
- True or false.
-
isFieldEditable
public boolean isFieldEditable(com.vaadin.flow.component.HasValue<?, ?> field) This method is invoked to determine if a field needs to be made editable or not.- Parameters:
field- Field- Returns:
- True or false.
-
getLabel
This method is invoked to determine the label used for displaying the field. The default implementation determine it by invokingApplicationEnvironment.createLabel(String).- Parameters:
fieldName- Name of the field- Returns:
- Label
-
setFieldLabel
-
setFieldLabel
Set label for a specific field. Label will be set only if the field has a setLabel(String) method.- Parameters:
field- Field for which label needs to be setlabel- Label to set
-
getFieldLabel
-
getFieldLabel
Get label for the given field.- Parameters:
field- Field- Returns:
- Label string if "getLabel" method exists in the field, otherwise null.
-
connect
Connect fields so that a change in any of it is propagated to others within that connected group.- Parameters:
fieldNames- Fields to connect.- Returns:
- True if connection is established successfully.
-
connect
public boolean connect(com.vaadin.flow.component.HasValue<?, ?>... fields) Connect fields so that a change in any of it is propagated to others within that connected group.- Parameters:
fields- Fields to connect.- Returns:
- True if connection is established successfully.
-
connect
Connect fields so that a change in any of it is propagated to others within that connected group.- Parameters:
fields- Fields to connect.- Returns:
- True if connection is established successfully.
-
setView
-
getView
-
createObjectInstance
Create an instance of the data object. Default implementation tries to invoke the default constructor to create an instance.- Returns:
- Newly created data object.
-
getObject
Get the instance of the currently editing data object. (A new data object will be created by invokingcreateObjectInstance()if there is no current object instance set).- Returns:
- Instance of the currently loaded object.
-
getObject
Get the instance of the currently editing data object. A new data object will be created by invokingcreateObjectInstance()if there is no current object instance set and the parameter passed istrue.- Parameters:
create- Whether to create a new instance or not- Returns:
- Instance of the currently loaded object.
-
setObject
Set the current object. Fields will be loaded.- Parameters:
object- Object to set
-
setObject
Set the current object. Fields will be loaded.- Parameters:
object- Object to setload- Whether to load the fields or not
-