Application Framework

Linkki Converter Registry

Typically, each UI component is designed to handle a specific data type. However, with the LinkkiConverterRegistry in linkki, developers can customize conversions between model data types and presentation types. This feature expands the capabilities of UI components to support a broader range of value types beyond their defaults.

Default Converters in LinkkiConverterRegistry

Following converters are registered by default in the LinkkiConverterRegistry:

Table 1. Default Converters
Converter Description

DateToLongConverter

Calculates the number of milliseconds since January 1, 1970, 00:00:00 GMT. 1000L would be Thu Jan 01 01:00:01 CET 1970 and vice versa.

DateToSqlDateConverter

Calculates the number on milliseconds since January 1, 1970, 00:00:00 GMT but in sql form. 1000L would be 1970-01-01 and vice versa.

LocalDateTimeToDateConverter

Converts LocalDateTime to Date. Makes it possible to use Date for UI components that uses LocalDateTime, such as @UIDateTimeField.

LocalDateToDateConverter

Converts LocalDate to Date. Makes it possible to use Date for UI components that uses LocalDate, such as @UIDateField.

LocalDateToStringConverter

Converts LocalDate to String using the locale specific format. Makes ot possible to use LocalDate in String-valued components such as @UILabel.

LocalDateTimeToStringConverter

Converts LocalDateTime to String using the locale specific format. Makes it possible to use LocalDateTime in String-valued components such as @UILabel.

StringToBigDecimalConverter

Converts a String to a number of type BigDecimal. Makes it possible to use BigDecimal in String-valued components such as @UILabel.

StringToBigIntegerConverter

Converts a String to a number of type BigInteger. Makes it possible to use BigInteger in String-valued components such as @UILabel.

StringToBooleanConverter

Convert a String ("true" or "false") to Boolean. Makes it possible to use Boolean in String-valued components such as @UILabel.

StringToDateConverter

Converts a Date to a String using the locale specific format similar to the one used by a Vaadin DatePicker. Makes it possible to use Date in String-valued components such as @UILabel.

StringToDoubleConverter

Converts a String to a number of type Double. Makes it possible to use Double in String-valued components such as @UILabel.

StringToFloatConverter

Converts a String to a number of type Float. Makes it possible to use Float in String-valued components such as @UILabel.

StringToIntegerConverter

Converts a String to a number of type Integer. Makes it possible to use Integer in String-valued components such as @UILabel.

StringToLongConverter

Converts a String to a number of type Long. Makes it possible to use Long in String-valued components such as @UILabel.

StringToGregorianCalendarConverter

Converts GregorianCalendar to String and vice versa, taking locale-specific date formats into account. Makes it possible to use GregorianCalendar in String-valued components such as @UILabel.

Custom date formats

Custom date formats for a specific Locale or language code can be registered with DateFormats#register(…​). All converters that come with linkki respect these formats. It is recommended to also respect these date formats in custom converters.

Custom converters

It possible to use custom converters in linkki, e.g. for data types that are not handled by default, or to change the conversion of a certain data type.

Implementing custom converters

All converters must implement the interface Converter<Presentation, Model>. PRESENTATION is the type needed by the UI component and MODEL is the type in the PMO. This interface defines two methods to converter between the two types.

When converting to model, it is possible to check the validity of the input before the value is pushed to the PMO. This can be achieved by returning an error in the result of convertToModel. In this case, an error message is shown in the UI indicating that the input is not valid. The value of the UI component is restored to its previous value.

Registering custom converters

The converter registry is defined in the Application Configuration.

By implementing ApplicationConfig#getConverterRegistry() additional custom converters can be registered. If the registry is initiated with a custom converter which has the same types as an existing one, the custom converter is used with higher priority.