Value Transformers
ValueTransformer<M, V> defines how to convert and validate values from view to model and viceversa (because Arena has a two-way binding).
- M viewToModel(V valueFromView): converts a value from the UI component to the model format
- V modelToView(M valueFromModel): converts a value from the model to the UI format
- Class<M> getModelType(): returns type of model
- Class<V> getViewType(): returns type of view
Date Transformer
- Implements ValueTransformer<Date, String>
- Converts a string in a dd/MM/yyy format to Date and viceversa
- Allows null values
Example
A textbox with a binding to a customer's dateOfBirth:
// Xtend
new TextBox(mainPanel) => [
width = 80
value <=> "dateOfBirth".transformer = new DateTransformer
]
// Java
new TextBox(mainPanel)
.setWidth(80)
.bindValueToProperty("dateOfBirth").setTransformer(new DateTransformer());
NotEmptyTransformer
- implements ValueTransformer<String, Boolean>
- maps a String object with a boolean value true (string is empty or null) / false (string is not empty or null)
NotNullTransformer
- implements ValueTransformer<Object, Boolean>
- maps any object with a boolean value true (object is not null) / false (object is null)
What do you need to define a new Value Transformer
- Find concrete classes for M and V
- Implement a Value Transformer for this concrete classes M and V
- Define getModelType() and getViewType() methods. They should return concrete classes.
- In viewToModel() method you define how to convert a view value to its model
- In modelToView() method you define how to convert a value model to its view
Example: BigDecimalTransformer
class BigDecimalTransformer implements ValueTransformer<BigDecimal, String> {
override getModelType() {
BigDecimal
}
override getViewType() {
String
}
override modelToView(BigDecimal valueFromModel) {
valueFromModel.toString
}
override viewToModel(String valueFromView) {
if (valueFromView == null || valueFromView.equals(""))
null
else
try
new BigDecimal(valueFromView)
catch (NumberFormatException e)
throw new UserException("Value must be a number", e)
}
}