diff options
author | Artur Signell <artur@vaadin.com> | 2013-05-21 16:51:32 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2013-05-22 13:50:40 +0000 |
commit | 6bf83a428aa747f6b46d1d4c4ad2e279971d14e7 (patch) | |
tree | 2120465ccf24b83acf01ba6c829c4fe59866b74b /server/src/com/vaadin | |
parent | 0b635061bb9b30e9f28d49fe9606eb3cab2fd3f1 (diff) | |
download | vaadin-framework-6bf83a428aa747f6b46d1d4c4ad2e279971d14e7.tar.gz vaadin-framework-6bf83a428aa747f6b46d1d4c4ad2e279971d14e7.zip |
Modified the logic in setPropertyDatasource which determines if a new converter is needed (#11863)
The previous logic had two flaws
* It allowed converter model type to be a sub type of the model type but not vice versa. Similarly for presentation type.
* If the user has set a converter it should be used and not be replaced unless it is absolutely sure that it cannot in any possible way handle conversion (e.g. converter from integer to double cannot handle string to list conversion). If there is a slight chance that it can handle conversion, let it be and let the user set another converter when needed.
Change-Id: I2e1c0b3aac90be63ddbc780195f8428398e28ada
Diffstat (limited to 'server/src/com/vaadin')
-rw-r--r-- | server/src/com/vaadin/data/util/converter/ConverterUtil.java | 50 | ||||
-rw-r--r-- | server/src/com/vaadin/ui/AbstractField.java | 15 | ||||
-rw-r--r-- | server/src/com/vaadin/ui/Label.java | 9 |
3 files changed, 58 insertions, 16 deletions
diff --git a/server/src/com/vaadin/data/util/converter/ConverterUtil.java b/server/src/com/vaadin/data/util/converter/ConverterUtil.java index 61d155bc9a..08d7363084 100644 --- a/server/src/com/vaadin/data/util/converter/ConverterUtil.java +++ b/server/src/com/vaadin/data/util/converter/ConverterUtil.java @@ -151,10 +151,14 @@ public class ConverterUtil implements Serializable { /** * Checks if the given converter can handle conversion between the given - * presentation and model type + * presentation and model type. Does strict type checking and only returns + * true if the converter claims it can handle exactly the given types. + * + * @see #canConverterPossiblyHandle(Converter, Class, Class) * * @param converter - * The converter to check + * The converter to check. If this is null the result is always + * false. * @param presentationType * The presentation type * @param modelType @@ -168,10 +172,48 @@ public class ConverterUtil implements Serializable { return false; } - if (!modelType.isAssignableFrom(converter.getModelType())) { + if (modelType != converter.getModelType()) { + return false; + } + if (presentationType != converter.getPresentationType()) { return false; } - if (!presentationType.isAssignableFrom(converter.getPresentationType())) { + + return true; + } + + /** + * Checks if it possible that the given converter can handle conversion + * between the given presentation and model type somehow. + * + * @param converter + * The converter to check. If this is null the result is always + * false. + * @param presentationType + * The presentation type + * @param modelType + * The model type + * @return true if the converter possibly support conversion between the + * given presentation and model type, false otherwise + */ + public static boolean canConverterPossiblyHandle(Converter<?, ?> converter, + Class<?> presentationType, Class<?> modelType) { + if (converter == null) { + return false; + } + Class<?> converterModelType = converter.getModelType(); + + if (!modelType.isAssignableFrom(converterModelType) + && !converterModelType.isAssignableFrom(modelType)) { + // model types are not compatible in any way + return false; + } + + Class<?> converterPresentationType = converter.getPresentationType(); + if (!presentationType.isAssignableFrom(converterPresentationType) + && !converterPresentationType + .isAssignableFrom(presentationType)) { + // presentation types are not compatible in any way return false; } diff --git a/server/src/com/vaadin/ui/AbstractField.java b/server/src/com/vaadin/ui/AbstractField.java index 3bca63a3b7..606bf5fb21 100644 --- a/server/src/com/vaadin/ui/AbstractField.java +++ b/server/src/com/vaadin/ui/AbstractField.java @@ -616,17 +616,14 @@ public abstract class AbstractField<T> extends AbstractComponent implements // Check if the current converter is compatible. if (newDataSource != null - && !ConverterUtil.canConverterHandle(getConverter(), getType(), - newDataSource.getType())) { - // Changing from e.g. Number -> Double should set a new converter, - // changing from Double -> Number can keep the old one (Property - // accepts Number) - - // Set a new converter if there is a new data source and - // there is no old converter or the old is incompatible. + && !ConverterUtil.canConverterPossiblyHandle(getConverter(), + getType(), newDataSource.getType())) { + // There is no converter set or there is no way the current + // converter can be compatible. setConverter(newDataSource.getType()); } - // Gets the value from source + // Gets the value from source. This requires that a valid converter has + // been set. try { if (dataSource != null) { T fieldValue = convertFromModel(getDataSourceValue()); diff --git a/server/src/com/vaadin/ui/Label.java b/server/src/com/vaadin/ui/Label.java index d037652a09..d7cee2a80d 100644 --- a/server/src/com/vaadin/ui/Label.java +++ b/server/src/com/vaadin/ui/Label.java @@ -242,14 +242,17 @@ public class Label extends AbstractComponent implements Property<String>, ((Property.ValueChangeNotifier) dataSource).removeListener(this); } + // Check if the current converter is compatible. if (newDataSource != null - && !ConverterUtil.canConverterHandle(getConverter(), - String.class, newDataSource.getType())) { - // Try to find a converter + && !ConverterUtil.canConverterPossiblyHandle(getConverter(), + getType(), newDataSource.getType())) { + // There is no converter set or there is no way the current + // converter can be compatible. Converter<String, ?> c = ConverterUtil.getConverter(String.class, newDataSource.getType(), getSession()); setConverter(c); } + dataSource = newDataSource; if (dataSource != null) { // Update the value from the data source. If data source was set to |