Class DefaultFieldHandler

  • All Implemented Interfaces:
    FieldHandler

    public class DefaultFieldHandler
    extends Object
    implements FieldHandler

    A default implementation of the FieldHandler interface.

    This class implements all necessary functionality to deal with form fields of different types. It supports both validation and conversion of data. Conversion works in two directions: from a bean property to a representation that can be processed by a GUI widget and vice versa. Validation (on the field and form level) ensures that data entered by the user is syntactically and semantically valid.

    To perform its tasks this class makes use of some Validator and Transformer objects that can be associated with an instance. It ensures that the services provided by these objects are accessed when necessary. When creating an instance of this class it must be ensured that the correct validators and transformers are set; especially the data types they operate on must be compatible.

    Version:
    $Id: DefaultFieldHandler.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Constructor Detail

      • DefaultFieldHandler

        public DefaultFieldHandler()
        Creates a new instance of DefaultFieldHandler.
    • Method Detail

      • getSyntaxValidator

        public ValidatorWrapper getSyntaxValidator()
        Returns the validator used for validation on the syntax level. If no specific validator is set, a dummy validator is used.
        Returns:
        the validator on the field level
      • setSyntaxValidator

        public void setSyntaxValidator​(ValidatorWrapper fieldValidator)
        Sets the validator used for validation on the syntax level.
        Parameters:
        fieldValidator - the field level validator
      • getLogicValidator

        public ValidatorWrapper getLogicValidator()
        Returns the validator used for validation on the logic level. If no specific validator has been set, a dummy validator is used.
        Returns:
        the validator on the form level
      • setLogicValidator

        public void setLogicValidator​(ValidatorWrapper formValidator)
        Sets the validator used for validation on the logic level.
        Parameters:
        formValidator - the form level validator
      • getReadTransformer

        public TransformerWrapper getReadTransformer()
        Returns the read transformer. If no specific transformer is set, a dummy transformer is used.
        Returns:
        the read transformer
      • setReadTransformer

        public void setReadTransformer​(TransformerWrapper readTransformer)
        Sets the read transformer. This transformer is used when reading data from the associated GUI widget. It transforms from the widget's data type to the target data type (provided that field level validation has been successful).
        Parameters:
        readTransformer - the read transformer
      • getWriteTransformer

        public TransformerWrapper getWriteTransformer()
        Returns the write transformer. If no specific write transformer has been set, a dummy transformer is used.
        Returns:
        the write transformer
      • setWriteTransformer

        public void setWriteTransformer​(TransformerWrapper writeTransformer)
        Sets the write transformer. This transformer is used when initializing the associated widget's data with form data. It transforms from the field's target data type to the widget's data type.
        Parameters:
        writeTransformer - the write transformer
      • setComponentHandler

        public void setComponentHandler​(ComponentHandler<?> componentHandler)
        Sets the component handler.
        Parameters:
        componentHandler - the component handler
      • getType

        public Class<?> getType()
        Returns the data type of this field. If a type was explicitely set, this type is returned. Otherwise the ComponentHandler's type is returned.
        Specified by:
        getType in interface FieldHandler
        Returns:
        the type of this field
      • setType

        public void setType​(Class<?> type)
        Sets the data type for this form field.
        Parameters:
        type - the data type
      • getPropertyName

        public String getPropertyName()
        Returns the property name of this field if one is set.
        Specified by:
        getPropertyName in interface FieldHandler
        Returns:
        the property name
      • setPropertyName

        public void setPropertyName​(String n)
        Sets the property name of this field.
        Parameters:
        n - the property name
      • getDisplayName

        public String getDisplayName()
        Returns the display name.
        Specified by:
        getDisplayName in interface FieldHandler
        Returns:
        the display name (can be null)
      • setDisplayName

        public void setDisplayName​(String displayName)
        Sets the display name.
        Parameters:
        displayName - the new display name
      • validate

        public ValidationResult validate​(ValidationPhase phase)
        Performs validation for the specified phase and returns the results.
        Specified by:
        validate in interface FieldHandler
        Parameters:
        phase - the validation phase
        Returns:
        the validation result
      • getData

        public Object getData()
        Fetches the data of this field. This method requires that validation of both the field and form level succeeded (it is guaranteed that the validate(ValidationPhase) method was called for these phases before the form framework invokes this method).
        Specified by:
        getData in interface FieldHandler
        Returns:
        the data of this field (converted to the resulting type)
      • setData

        public void setData​(Object data)
        Sets the data for this field. Converts the passed in object to the component handler's data type using the write transformer.
        Specified by:
        setData in interface FieldHandler
        Parameters:
        data - the data object for this field
      • validateFieldLevel

        protected ValidationResult validateFieldLevel()
        Performs validation on the field level.
        Returns:
        validation results
      • validateFormLevel

        protected ValidationResult validateFormLevel()
        Performs validation on the form level. This implementation transforms the GUI component's data into its target data type (assuming that field level validation was successful). Then the correctness is checked. If this validation is successful, too, the data object is stored internally so it can be accessed by the getData() method.
        Returns:
        validation results
      • fetchFieldData

        protected Object fetchFieldData()
        Fetches the form field's data directly from the component handler. This method is called whenever access to the field data is needed, especially by the validate() method. It assumes that validation on the field level was successful.
        Returns:
        the field's data
      • validateFieldData

        protected ValidationResult validateFieldData​(Object data)
        Performs a form level validation of this field. Checks if the passed in data object is semantically correct.
        Parameters:
        data - the data to check
        Returns:
        a validation result object with the results of the form level validation