public class Form extends Object
This class represents a form.
Instances of this class can be used to deal with forms, e.g. initializing the
form's widgets with data obtained from a model object or validating the
user's input. A Form
object must be initialized with objects
representing the form elements or fields. These objects are of type
FieldHandler
and contain all information needed for correctly
handling GUI widgets and the data they may contain.
An important functionality of this class is to enable data transfer between
the form's fields and the properties of a model object. By properly
initializing the FieldHandler
objects with transformers and
validators it can be assured that suitable validation and data conversion
take place. Model objects are accessed through a BindingStrategy
;
therefore this class can collaborate with different types of model objects
provided that a corresponding BindingStrategy
implementation exists.
After a Form
object and its corresponding fields haven been
initialized usage of this class is quite simple. To initialize the GUI
widgets associated with this form call the initFields(Object)
method
and pass in a model object instance with the values for the fields. (Of
course, this model object must be compatible with the BindingStrategy
the form was initialized with.) To perform validation and read the user's
input back into a model object the validate(Object)
method can be
used. This method invokes all registered validators, and if validation
succeeds, the user input is converted into the correct types and transfered
into the given model object.
Implementation node: This class is not thread safe; instances should be accessed by a single thread only.
Constructor and Description |
---|
Form(TransformerContext ctx,
BindingStrategy strat)
Creates a new instance of
Form and initializes it with all
required helper objects. |
Modifier and Type | Method and Description |
---|---|
void |
addField(String name,
FieldHandler fld)
Adds the specified field to this form.
|
BindingStrategy |
getBindingStrategy()
Returns the
BindingStrategy used by this form. |
ComponentStore |
getComponentStore()
Returns the component store of this form.
|
String |
getDisplayName(String fldName)
Returns the display name for the specified field.
|
FieldHandler |
getField(String name)
Returns the
FieldHandler object for the field with the given
name. |
Set<String> |
getFieldNames()
Returns a set with the names of all defined fields.
|
FormValidator |
getFormValidator()
Returns the form validator.
|
TransformerContext |
getTransformerContext()
Returns the transformer context.
|
void |
initFields(Object bean)
Fills the form's fields with the properties of the passed in bean.
|
void |
initFields(Object bean,
Set<String> names)
Fills a sub set of the form's fields with the properties of the passed in
bean.
|
void |
readFields(Object bean)
Reads the form's fields and copies their content into the passed in form
bean.
|
void |
readFields(Object bean,
Set<String> names)
Reads a sub set of this form's fields and writes their content into the
specified bean.
|
void |
setFormValidator(FormValidator formValidator)
Sets the form validator.
|
FormValidatorResults |
validate(Object model)
Validates this form and writes its content into the specified model
object if validation is successful.
|
FormValidatorResults |
validateFields()
Validates the fields of this form.
|
FormValidatorResults |
validateFields(Set<String> names)
Validates a sub set of the fields of this form.
|
FormValidatorResults |
validateForm(Object model)
Validates the whole form using the
FormValidator . |
public Form(TransformerContext ctx, BindingStrategy strat)
Form
and initializes it with all
required helper objects.ctx
- the TransformerContext
(must not be null)strat
- the BindingStrategy
(must not be null)IllegalArgumentException
- if a required parameter is nullpublic TransformerContext getTransformerContext()
public final BindingStrategy getBindingStrategy()
BindingStrategy
used by this form.BindingStrategy
public FormValidator getFormValidator()
public void setFormValidator(FormValidator formValidator)
formValidator
- the form validatorpublic void addField(String name, FieldHandler fld)
name
- the field's (internal) namefld
- the field handler for this fieldpublic void initFields(Object bean)
bean
- the form bean; can be null, then this operation has no
effectFormRuntimeException
- if an error occurs when initializing a fieldpublic void initFields(Object bean, Set<String> names)
bean
- the form bean; can be null, then this operation has no
effectnames
- a set with the names of the fields to be initializedFormRuntimeException
- if a field cannot be initializedIllegalArgumentException
- if the set is nullpublic FormValidatorResults validate(Object model)
BindingStrategy
used by the form. It is modified only if
validation is successful; otherwise it is not changed.model
- the model object in which to write the form fields; can be
null, then no data is copiedpublic Set<String> getFieldNames()
public FieldHandler getField(String name)
FieldHandler
object for the field with the given
name. If no such field exists, null is returned.name
- the name of the desired fieldpublic String getDisplayName(String fldName)
fldName
- the name of the fieldpublic ComponentStore getComponentStore()
public FormValidatorResults validateFields()
public FormValidatorResults validateFields(Set<String> names)
names
- a set with the names of the fields to be validatedFormRuntimeException
- if an invalid field name is specifiedIllegalArgumentException
- if the set is nullvalidateFields()
public FormValidatorResults validateForm(Object model)
FormValidator
. This is an
additional validation that can be performed after it was ensured that all
fields are syntactically and semantically correct. The aim of this method
is to apply high level validation rules that are able to check
dependencies between form fields. Calling this method requires that
validation of the field level has already been performed (e.g. by
validateFields()
). If validation is successful (or if no FormValidator
is defined), the passed in model object is populated with
the content of this form. Otherwise it is not modified.model
- the model object; can be null, then no data is copiedpublic void readFields(Object bean)
validateFields()
must have been
invoked and returned a positive result. If validateFields()
has
not been called before, the passed in bean won't contain the current data
of the form's fields. The contents of the fields is converted to the
correct data types and written into the bean's properties.bean
- the bean in which to store the fields' content; can be
null, then this operation has no effectFormRuntimeException
- if a field cannot be readpublic void readFields(Object bean, Set<String> names)
bean
- the bean in which to store the fields' content; can be
null, then this operation has no effectnames
- the set with the names of the fields to readFormRuntimeException
- if a field cannot be readIllegalArgumentException
- if the set is nullCopyright © 2016 The JGUIraffe Team. All rights reserved.