Klasse Form
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.
- Version:
- $Id: Form.java 205 2012-01-29 18:29:57Z oheger $
- Autor:
- Oliver Heger
-
Konstruktorübersicht
KonstruktorenKonstruktorBeschreibungForm
(TransformerContext ctx, BindingStrategy strat) Creates a new instance ofForm
and initializes it with all required helper objects. -
Methodenübersicht
Modifizierer und TypMethodeBeschreibungvoid
addField
(String name, FieldHandler fld) Adds the specified field to this form.final BindingStrategy
Returns theBindingStrategy
used by this form.Returns the component store of this form.getDisplayName
(String fldName) Returns the display name for the specified field.Returns theFieldHandler
object for the field with the given name.Returns a set with the names of all defined fields.Returns the form validator.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.Validates this form and writes its content into the specified model object if validation is successful.Validates the fields of this form.validateFields
(Set<String> names) Validates a sub set of the fields of this form.validateForm
(Object model) Validates the whole form using theFormValidator
.
-
Konstruktordetails
-
Form
Creates a new instance ofForm
and initializes it with all required helper objects.- Parameter:
ctx
- theTransformerContext
(must not be null)strat
- theBindingStrategy
(must not be null)- Löst aus:
IllegalArgumentException
- if a required parameter is null
-
-
Methodendetails
-
getTransformerContext
Returns the transformer context.- Gibt zurück:
- the transformer context
-
getBindingStrategy
Returns theBindingStrategy
used by this form.- Gibt zurück:
- the
BindingStrategy
-
getFormValidator
Returns the form validator. This can be null if no specific form validator has been set.- Gibt zurück:
- the object used for validating the form
-
setFormValidator
Sets the form validator.- Parameter:
formValidator
- the form validator
-
addField
Adds the specified field to this form. This method must be called for each field that should be managed by this form object.- Parameter:
name
- the field's (internal) namefld
- the field handler for this field
-
initFields
Fills the form's fields with the properties of the passed in bean. This method can be used to initialize the form.- Parameter:
bean
- the form bean; can be null, then this operation has no effect- Löst aus:
FormRuntimeException
- if an error occurs when initializing a field
-
initFields
Fills a sub set of the form's fields with the properties of the passed in bean. This method will iterate over all fields specified in the given set and initialize them from the corresponding properties of the specified bean. The set must contain only valid names of fields that belong to this form; otherwise an exception will be thrown.- Parameter:
bean
- the form bean; can be null, then this operation has no effectnames
- a set with the names of the fields to be initialized- Löst aus:
FormRuntimeException
- if a field cannot be initializedIllegalArgumentException
- if the set is null
-
validate
Validates this form and writes its content into the specified model object if validation is successful. This method performs validation on both the field and the form level. The former validation ensures that all fields contain syntactically correct data, i.e. the data they contain can be converted to their expected data type (e.g. the string entered by the user is indeed a valid date). The latter validation takes the form as the whole into account. Here for instance relations between fields can be checked (e.g. the date of delivery is greater than the shipment date). The passed in model object is populated with the form's data when all validation steps succeed. It must be compatible with theBindingStrategy
used by the form. It is modified only if validation is successful; otherwise it is not changed.- Parameter:
model
- the model object in which to write the form fields; can be null, then no data is copied- Gibt zurück:
- an object with validation results
-
getFieldNames
Returns a set with the names of all defined fields.- Gibt zurück:
- a set with the field names
-
getField
Returns theFieldHandler
object for the field with the given name. If no such field exists, null is returned.- Parameter:
name
- the name of the desired field- Gibt zurück:
- the field handler for this field
-
getDisplayName
Returns the display name for the specified field. This implementation checks whether a display name is explicitly defined for the field handler with the given name. If this is the case, it is returned. Otherwise the field's name is returned. If the field is unknown, null is returned.- Parameter:
fldName
- the name of the field- Gibt zurück:
- the display name for this field
-
getComponentStore
Returns the component store of this form. In this object all components that belong to this form are stored.- Gibt zurück:
- the component store of this form
-
validateFields
Validates the fields of this form. This method ensures that all form fields are syntactically and semantically correct, i.e. it performs validation on both the fields and form level. After this method has been called and returned a positive result, the form bean is available and contains the current data.- Gibt zurück:
- an object with results of the validation
-
validateFields
Validates a sub set of the fields of this form. This method works like the overloaded version, but only fields whose name is contained in the passed in set are taken into account. This is useful if a partly validation is to be performed. If the set contains an invalid field name, a runtime exception will be thrown.- Parameter:
names
- a set with the names of the fields to be validated- Gibt zurück:
- an object with results of the validation
- Löst aus:
FormRuntimeException
- if an invalid field name is specifiedIllegalArgumentException
- if the set is null- Siehe auch:
-
validateForm
Validates the whole form using theFormValidator
. 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. byvalidateFields()
). If validation is successful (or if noFormValidator
is defined), the passed in model object is populated with the content of this form. Otherwise it is not modified.- Parameter:
model
- the model object; can be null, then no data is copied- Gibt zurück:
- an object with the results of the validation
-
readFields
Reads the form's fields and copies their content into the passed in form bean. Before this method can be called validation of the form's fields must have been successful, i.e.validateFields()
must have been invoked and returned a positive result. IfvalidateFields()
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.- Parameter:
bean
- the bean in which to store the fields' content; can be null, then this operation has no effect- Löst aus:
FormRuntimeException
- if a field cannot be read
-
readFields
Reads a sub set of this form's fields and writes their content into the specified bean. This method works like the overloaded variant, but operates on a sub set of the fields only. If the passed in set contains an invalid name, a runtime exception is thrown.- Parameter:
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 read- Löst aus:
FormRuntimeException
- if a field cannot be readIllegalArgumentException
- if the set is null
-