Class DefaultDisplayFormatter
- All Implemented Interfaces:
DisplayFormatter
Default library for formatting a record to a view helper, according to converter annotations or default
converter on the view helper fields, in Struts 2. Converter annotations are found in
name.matthewgreet,strutscommons.annotation
(which allows custom converters). This must be executed in a
Struts 2 Action context, whether in Action code or in an Interceptor.
Such converter annotations are for converting a single string value to a single value form field or collection
form field, so this library only supports formatting a single record field to a single display field, formatting a
collection record field to a single display field. However, this library is designed with extensions in mind. See
getDisplayFormatMode(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>)
.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static class
static enum
Modes of how an unformatted, record field is converted to a formatted, display field, each with their own processing function. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> boolean
checkFieldClass
(Class<?> conversionClass, Class<?> fieldClass) Returns whether a converter's target data type can convert to the form field's data type.protected boolean
filterDisplayField
(Field displayField) Returns whether display field is allowed to receive a formatted value, which is not annotated withDisableFormatting
.protected <T> InterceptorCommonLibrary.AnnotationEntries
<T> getAnnotationEntries
(Field displayField) Returns all recognised annotations and their linked policies.protected <T> CollectionConverter
<?, T> getDefaultCollectionConverter
(DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Returns default collection converter for a collection form field or display field, or null if none available.protected <T> Converter
<?, T> getDefaultConverter
(DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Returns default converter for a single value form field or display field, or null if none available.getDisplayFieldSources
(Collection<Field> displayFields, Collection<Field> recordFields) Returns every display field that will receive a formatted value and the record field containing the value to be formatted.protected <T> DefaultDisplayFormatter.DisplayFieldUsage
<T> getDisplayFieldUsage
(Field recordField, Field displayField) Analyses converter annotations on display field and returns configured formatter to formatted from source field display field, or null if not applicable.getDisplayFormatMode
(DefaultDisplayFormatter.DisplayFieldUsage<?> displayFieldUsage) Returns mode for formatting a record field to the matching display field.protected Collection
<Field> getProperties
(Class<?> type) Returns all properties of a class, even private ones, whether directly declared or inherited.protected <T> Class
<T> getTypeFromCollectionField
(Field displayField) Returns element type of a collection-based form field.protected <T> Collection
<T> makeCollectionForRecipient
(Class<?> recipientClass) Returns an empty, modifiable collection from a collection type.protected <T> void
processDisplayFieldUsageArrayToArray
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Sets array display field from each member of an array record field formatted using converter.protected <T> void
processDisplayFieldUsageArrayToCollection
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Sets collection display field from each member of an array record field formatted using converter.protected <T> void
processDisplayFieldUsageArrayToSingle
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Sets single display field from first member of an array record field formatted using converter.protected <T> void
processDisplayFieldUsageCollectionToArray
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Sets array display field from each member of a collection record field formatted using converter.protected <T> void
processDisplayFieldUsageCollectionToCollection
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Sets collection display field from each member of a collection record field formatted using converter.protected <T> void
processDisplayFieldUsageCollectionToSingle
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Sets single display field from collection record field formatted using collection converter.protected <T> void
processDisplayFieldUsageOther1
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Called for OTHER1 formatting mode and does nothing.protected <T> void
processDisplayFieldUsageOther10
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Called for OTHER10 formatting mode and does nothing.protected <T> void
processDisplayFieldUsageOther2
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Called for OTHER2 formatting mode and does nothing.protected <T> void
processDisplayFieldUsageOther3
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Called for OTHER3 formatting mode and does nothing.protected <T> void
processDisplayFieldUsageOther4
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Called for OTHER4 formatting mode and does nothing.protected <T> void
processDisplayFieldUsageOther5
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Called for OTHER5 formatting mode and does nothing.protected <T> void
processDisplayFieldUsageOther6
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Called for OTHER6 formatting mode and does nothing.protected <T> void
processDisplayFieldUsageOther7
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Called for OTHER7 formatting mode and does nothing.protected <T> void
processDisplayFieldUsageOther8
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Called for OTHER8 formatting mode and does nothing.protected <T> void
processDisplayFieldUsageOther9
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Called for OTHER9 formatting mode and does nothing.protected <T> void
processDisplayFieldUsageSingleToArray
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Sets array display field from from single record field formatted using configured converter.protected <T> void
processDisplayFieldUsageSingleToCollection
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Sets collection display field from from single record field formatted using configured converter.protected <T> void
processDisplayFieldUsageSingleToSingle
(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Sets single display field from from single record field formatted using configured converter.void
updateDisplay
(Class<?> recordClass, Object record, Object display) Sets string fields ofdisplay
from fields ofrecord
with the same name but formatted according to the conversion annotations ondisplay
fields, or default converter if no annotation.
-
Constructor Details
-
DefaultDisplayFormatter
public DefaultDisplayFormatter()
-
-
Method Details
-
checkFieldClass
Returns whether a converter's target data type can convert to the form field's data type. -
filterDisplayField
Returns whether display field is allowed to receive a formatted value, which is not annotated withDisableFormatting
. -
getAnnotationEntries
protected <T> InterceptorCommonLibrary.AnnotationEntries<T> getAnnotationEntries(Field displayField) Returns all recognised annotations and their linked policies. -
getDefaultCollectionConverter
protected <T> CollectionConverter<?,T> getDefaultCollectionConverter(DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Returns default collection converter for a collection form field or display field, or null if none available. -
getDefaultConverter
protected <T> Converter<?,T> getDefaultConverter(DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) Returns default converter for a single value form field or display field, or null if none available. -
getDisplayFieldSources
protected Collection<DefaultDisplayFormatter.DisplayFieldSource> getDisplayFieldSources(Collection<Field> displayFields, Collection<Field> recordFields) Returns every display field that will receive a formatted value and the record field containing the value to be formatted. -
getDisplayFieldUsage
protected <T> DefaultDisplayFormatter.DisplayFieldUsage<T> getDisplayFieldUsage(Field recordField, Field displayField) Analyses converter annotations on display field and returns configured formatter to formatted from source field display field, or null if not applicable. -
getDisplayFormatMode
protected DefaultDisplayFormatter.DisplayFormatMode getDisplayFormatMode(DefaultDisplayFormatter.DisplayFieldUsage<?> displayFieldUsage) Returns mode for formatting a record field to the matching display field. Only SINGLE_TO_SINGLE and COLLECTION_TO_SINGLE are actually used and the rest regarded as inconsistent with converter annotation usage. Subclasses can override this support other modes. -
getProperties
Returns all properties of a class, even private ones, whether directly declared or inherited. -
getTypeFromCollectionField
Returns element type of a collection-based form field. -
makeCollectionForRecipient
Returns an empty, modifiable collection from a collection type. -
processDisplayFieldUsageArrayToArray
protected <T> void processDisplayFieldUsageArrayToArray(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Sets array display field from each member of an array record field formatted using converter. If no converter is set, uses default converter for source field. If no default converter and source is a string collection, copies source values, otherwise sets empty array.
This is not actually used and exists for future use.
- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageArrayToCollection
protected <T> void processDisplayFieldUsageArrayToCollection(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Sets collection display field from each member of an array record field formatted using converter. If no converter is set, uses default converter for source field. If no default converter and source is a string collection, copies source values, otherwise sets empty collection.
This is not actually used and exists for future use.
- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageArrayToSingle
protected <T> void processDisplayFieldUsageArrayToSingle(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Sets single display field from first member of an array record field formatted using converter. If no converter is set, uses default converter for source field. If no default converter and source is a string collection, copies source values, otherwise sets empty string.
This is not actually used, not considered useful, and exists for the sake of completeness.
- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageCollectionToArray
protected <T> void processDisplayFieldUsageCollectionToArray(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Sets array display field from each member of a collection record field formatted using converter. If no converter is set, uses default converter for source field. If no default converter and source is a string collection, copies source values, otherwise sets empty array.
This is not actually used and exists for future use.
- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageCollectionToCollection
protected <T> void processDisplayFieldUsageCollectionToCollection(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Sets collection display field from each member of a collection record field formatted using converter. If no converter is set, uses default converter for source field. If no default converter and source is a string collection, copies source values, otherwise sets empty collection.
This is not actually used and exists for future use.
- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageCollectionToSingle
protected <T> void processDisplayFieldUsageCollectionToSingle(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Sets single display field from collection record field formatted using collection converter. If no collection converter is set, uses default collection converter for source field. If no default converter, sets empty string.- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageOther1
protected <T> void processDisplayFieldUsageOther1(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Called for OTHER1 formatting mode and does nothing. This exists so subclasses can override it, as well as
getDisplayFormatMode(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>)
, to define OTHER1 formatting mode.- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageOther10
protected <T> void processDisplayFieldUsageOther10(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Called for OTHER10 formatting mode and does nothing. This exists so subclasses can override it, as well as
getDisplayFormatMode(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>)
, to define OTHER10 formatting mode.- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageOther2
protected <T> void processDisplayFieldUsageOther2(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Called for OTHER2 formatting mode and does nothing. This exists so subclasses can override it, as well as
getDisplayFormatMode(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>)
, to define OTHER2 formatting mode.- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageOther3
protected <T> void processDisplayFieldUsageOther3(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Called for OTHER3 formatting mode and does nothing. This exists so subclasses can override it, as well as
getDisplayFormatMode(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>)
, to define OTHER3 formatting mode.- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageOther4
protected <T> void processDisplayFieldUsageOther4(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Called for OTHER4 formatting mode and does nothing. This exists so subclasses can override it, as well as
getDisplayFormatMode(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>)
, to define OTHER4 formatting mode.- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageOther5
protected <T> void processDisplayFieldUsageOther5(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Called for OTHER5 formatting mode and does nothing. This exists so subclasses can override it, as well as
getDisplayFormatMode(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>)
, to define OTHER5 formatting mode.- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageOther6
protected <T> void processDisplayFieldUsageOther6(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Called for OTHER6 formatting mode and does nothing. This exists so subclasses can override it, as well as
getDisplayFormatMode(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>)
, to define OTHER6 formatting mode.- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageOther7
protected <T> void processDisplayFieldUsageOther7(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Called for OTHER7 formatting mode and does nothing. This exists so subclasses can override it, as well as
getDisplayFormatMode(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>)
, to define OTHER7 formatting mode.- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageOther8
protected <T> void processDisplayFieldUsageOther8(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Called for OTHER8 formatting mode and does nothing. This exists so subclasses can override it, as well as
getDisplayFormatMode(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>)
, to define OTHER8 formatting mode.- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageOther9
protected <T> void processDisplayFieldUsageOther9(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Called for OTHER9 formatting mode and does nothing. This exists so subclasses can override it, as well as
getDisplayFormatMode(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>)
, to define OTHER9 formatting mode.- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageSingleToArray
protected <T> void processDisplayFieldUsageSingleToArray(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Sets array display field from from single record field formatted using configured converter. If no converter is set, uses default converter for source field. If no default converter and source is a string, copies source value, otherwise uses empty string. If the formatted value is not an empty string, sets an array of just that value, otherwise sets an empty array.
This is not actually used, not considered useful and exists for the sake of completeness.
- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageSingleToCollection
protected <T> void processDisplayFieldUsageSingleToCollection(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Sets collection display field from from single record field formatted using configured converter. If no converter is set, uses default converter for source field. If no default converter and source is a string, copies source value, otherwise uses empty string. If the formatted value is not an empty string, sets a collection of just that value, otherwise sets an empty collection.
This is not actually used, not considered useful and exists for the sake of completeness.
- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
processDisplayFieldUsageSingleToSingle
protected <T> void processDisplayFieldUsageSingleToSingle(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception Sets single display field from from single record field formatted using configured converter. If no converter is set, uses default converter for source field. If no default converter and source is a string, copies source value, otherwise sets empty string.- Parameters:
record
- Record containing unformatted field values. Can be null.display
- View helper containing fields to receive formatted values.displayFieldUsage
- Source and target fields and converter configured from annotation.- Throws:
Exception
-
updateDisplay
Sets string fields of
display
from fields ofrecord
with the same name but formatted according to the conversion annotations ondisplay
fields, or default converter if no annotation. Ifrecord
is null, recipientdisplay
fields are set to empty string. This function is useful for displaying database records.Miscellaneous notes.
- Source field type must apply to converter's type.
- Recipient field must be a string, not even string array or string collection.
- Custom converters only for formatting are best derived from
AbstractCustomFormatterSupport
orAbstractCustomCollectionFormatterSupport
. - The default converters for booleans and enumerations don't format to user friendly text, so custom converters are better.
- Does not follow associations.
- Specified by:
updateDisplay
in interfaceDisplayFormatter
-