Currently there is only one parameter that can be used in conversion between model and presentation - Locale. In our project we have some additional rules determining how to convert string presentation to data model (i.e. how to interprete the string). We can save this rules in Field or Property, however both entities are not accessible from Converter. Any idea how to solve such problem (except writing own widget)?
Could you store the extra data (or a reference to the Field containing it) in the Converter instance itself? This would mean you need a dedicated Converter instance for each Field instance, though.
Could work but where is the advantage of a framework? Based on your wiki example
here
we could pass our field reference into converter constructor
Name name = new Name("Rudolph", "Reindeer");
TextField textField = new TextField("Name");
textField.setConverter(new StringToNameConverter(textField));
textField.setConvertedValue(name);
But … this is not always the solution. I never understood why, but setPropertyDataSource(…) can remove any user defined converter based on ConverterUtil.canConverterPossiblyHandle(…). Why the hell do you throw away anything set by appplication programmer? Either remove setConverter(…) method at all or accept it.
Anyway this workaround takes the Converter instantiation out of the ConverterFactory and puts it into my application code. It’s difficult to maintain, we have hundreds of specific field editors, there should be some generic way.
When Converters were introduced Vaadin introduced ConverterFactory as the way how to find proper converter. Unfortunatelly the factory lookup is based only on type comparison. This implementation does not allow or is limited in following cases:
-
Conversion based on field type. Maybe I need different presentation for a Label and for a TextField, Maybe I need different presentation in a Label placed in a Form and in Label placed in a Table.
-
Conversion based on Java reflection or interfaces. For example if we create all our simple objects using factory
modelObject = MyObject.make(String);
all our factories must have common base-interface.
In my opinion current Vaadin ConverterFactory implementation is not framework friendly, too much is hard coded. Look at usage of
ConverterUtil.canConverterHandle(...)
Vaadin, not the application programmer, decides, if a Converter can perform the right conversion. There is no chance to override it and to say ‘Yes, I know, the MyConverter does the right job’. If you would like to know, if I can swim, you have to ask me, not to check, if there is water basin near my residence. Why the factory is not based on interfaces? Let you delegate the implementation to application programmers.
I am not alone with my problem, see for example
here
.