Generic Converter

We have lot of classes following the same pattern:

To Model:  ObjectClass.make(String) (i.e. factory)
To Presentation: object.toString() 

In Vaadin6 we implemented the Property.setValue() using reflection. Is it possible to make such a generic converter in Vaadin7? Looks like I need to write specific converter for each of my classes.

Any hint?

Well using a common base-interface for your factories and maybe some Generics in order to get it typesafe it should work.

Maybe not when using the ConverterFactory but surely whenever you set the Converter-instance directly.

I dived into the Vaadin code to found a solution and I found this part of framework poor designed.

  1. Converter algorithm is based only on class comparison between the presentation and the model and there is only one converter for each pair of presentation/model classes. Because the Converter does not take care of presentation context the developer cannot use different presentation for example for Label (viewer) and TextField (editor).

  2. Converter ist deeply integrated into the framework and Vaadin does not allow the developer to override the converter with another one based on other rules then class comparison in ConverterFactory. Examples of other conversion strategies are Java reflection or application metadata (conversion rules defined for example in the database). Let you have a look at the code snippet from Book of Vaadin (chapter 9.2.3, Basic use of converters)

final ObjectProperty<Integer> property = new ObjectProperty<Integer>(42);
final TextField tf = new TextField("Name");
tf.setConverter(new StringToIntegerConverter());
tf.setPropertyDataSource(property);

The call
setConverter(…)
in line 3 is in fact unnecessary because in the next line
tf.setPropertyDataSource(…)
still checks for both model and presentation parts if
type.isAssignableFrom(converterType)
. If the condition does not success, the existing converter, set in Line 3, is thrown away and ConverterFactory lookup is performed to find matching converter. The developer cannot override this behaviour and there is no way to set Converter-instance directly as Tobias means.

In Vaadin6 we are using Adapter pattern to adapt the setValue()/getValue() Property methods. Similar to Vaadin7 ConverterFactory we have AdapterFactory looking for best matching adapter. However there is one important difference - adapters are instance variables of Properties and if the adapter is set by developer, no AdapterFactory lookup occurs.

In Vaadin7
AbstractField.setPropertyDataSource()
should be improved. Either the existing converter should not be replaced even if Vaadin7 thinks it’s wrong (i.e. developer knows better then Vaadin team, if the defined converter works fine) or at lest the call
ConverterUtil.canConverterHandle()
should be implemented as callback, as last chance to stop the Vaadin7 automatism.

I have a converter which is absolutely working fine for textfield but not working fine for Label. Can you please let me know what is missing here:

final Label label = new Label(“someValue”);
label.setContentMode(ContentMode.TEXT);
label.setConverter(new DataConverter());

DataConverter() is having the logic for convertToPresentation(), but it’s not being called at all when label is rendered into the grid. Any ideas?