diff options
author | Artur Signell <artur@vaadin.com> | 2011-12-12 14:51:22 +0200 |
---|---|---|
committer | Artur Signell <artur@vaadin.com> | 2011-12-12 15:00:50 +0200 |
commit | 582e3e697aedceacba7f8d83e8c7b0c4e812c8ef (patch) | |
tree | 19a3d706c65e8e3572f6cbb96354fc116f25f03e | |
parent | 3da1452bf2e27f7305db05c10b183882e009a733 (diff) | |
download | vaadin-framework-582e3e697aedceacba7f8d83e8c7b0c4e812c8ef.tar.gz vaadin-framework-582e3e697aedceacba7f8d83e8c7b0c4e812c8ef.zip |
#8103 ConverterFactory is no longer static + javadoc updates
-rw-r--r-- | src/com/vaadin/Application.java | 26 | ||||
-rw-r--r-- | src/com/vaadin/ui/AbstractField.java | 68 | ||||
-rw-r--r-- | src/com/vaadin/ui/Table.java | 10 |
3 files changed, 67 insertions, 37 deletions
diff --git a/src/com/vaadin/Application.java b/src/com/vaadin/Application.java index 7bbb12032d..cd41a1165a 100644 --- a/src/com/vaadin/Application.java +++ b/src/com/vaadin/Application.java @@ -517,11 +517,10 @@ public class Application implements Terminal.ErrorListener, Serializable { private Terminal.ErrorListener errorHandler = this; /** - * The converter factory that is used for all fields in the application. + * The converter factory that is used to provide default converters for the + * application. */ - // private ConverterFactory converterFactory = new - // DefaultConverterFactory(); - private static ConverterFactory converterFactory = new DefaultConverterFactory(); + private ConverterFactory converterFactory = new DefaultConverterFactory(); private LinkedList<RequestHandler> requestHandlers = new LinkedList<RequestHandler>(); @@ -1217,32 +1216,31 @@ public class Application implements Terminal.ErrorListener, Serializable { * * @return The converter factory used in the application */ - // public ConverterFactory getConverterFactory() { - // return converterFactory; - // } - // FIXME: Should not be static - public static ConverterFactory getConverterFactory() { + public ConverterFactory getConverterFactory() { return converterFactory; } /** * Sets the {@link ConverterFactory} used to locate a suitable * {@link Converter} for fields in the application. - * + * <p> * The {@link ConverterFactory} is used to find a suitable converter when * binding data to a UI component and the data type does not match the UI * component type, e.g. binding a Double to a TextField (which is based on a * String). - * + * </p> + * <p> * The {@link Converter} for an individual field can be overridden using * {@link AbstractField#setValueConverter(Converter)}. + * </p> + * <p> + * The converter factory must never be set to null. * * @param converterFactory * The converter factory used in the application */ - // FIXME: Should not be static - public static void setConverterFactory(ConverterFactory converterFactory) { - Application.converterFactory = converterFactory; + public void setConverterFactory(ConverterFactory converterFactory) { + this.converterFactory = converterFactory; } /** diff --git a/src/com/vaadin/ui/AbstractField.java b/src/com/vaadin/ui/AbstractField.java index 3e35b4936e..2f41929a8e 100644 --- a/src/com/vaadin/ui/AbstractField.java +++ b/src/com/vaadin/ui/AbstractField.java @@ -665,11 +665,14 @@ public abstract class AbstractField<T> extends AbstractComponent implements // Sets the new data source dataSource = newDataSource; - // Check if the current converter is compatible. If not, get a new one - if (newDataSource == null) { - setValueConverter(null); - } else if (!isValueConverterType(newDataSource.getType())) { - setValueConverterFromFactory(newDataSource.getType()); + // Check if the current converter is compatible. + if (newDataSource != null + && (getValueConverter() == null || !getValueConverter() + .getSourceType().isAssignableFrom( + newDataSource.getType()))) { + // Set a new value converter if there is a new data source and the + // there is no old converter or the old is incompatible. + updateValueConverterFromFactory(newDataSource.getType()); } // Gets the value from source try { @@ -713,23 +716,39 @@ public abstract class AbstractField<T> extends AbstractComponent implements } } - private void setValueConverterFromFactory(Class<?> datamodelType) { - // FIXME Use thread local to get application - ConverterFactory factory = Application.getConverterFactory(); - - Converter<?, T> converter = (Converter<?, T>) factory.createConverter( - datamodelType, getType()); - - setValueConverter(converter); - } - - private boolean isValueConverterType(Class<?> type) { - if (getValueConverter() == null) { - return false; + /** + * Sets the value converter for the field from the converter factory defined + * for the application. Clears the value converter if no application + * reference is available or if the factory returns null. + * + * @param datamodelType + * The type of the data model that we want to be able to convert + * from + */ + private void updateValueConverterFromFactory(Class<?> datamodelType) { + Converter<?, T> converter = null; + + Application app = Application.getCurrentApplication(); + if (app != null) { + ConverterFactory factory = app.getConverterFactory(); + converter = (Converter<?, T>) factory.createConverter( + datamodelType, getType()); } - return getValueConverter().getSourceType().isAssignableFrom(type); + setValueConverter(converter); } + /** + * Convert the given value from the data source type to the UI type. + * + * @param newValue + * The data source value to convert. + * @return The converted value that is compatible with the UI type or the + * original value if its type is compatible and no value converter + * is set. + * @throws Converter.ConversionException + * if there is no converter and the type is not compatible with + * the data source type. + */ @SuppressWarnings("unchecked") private T convertFromDataSource(Object newValue) throws Converter.ConversionException { @@ -753,6 +772,17 @@ public abstract class AbstractField<T> extends AbstractComponent implements } } + /** + * Convert the given value from the UI type to the data source type. + * + * @param fieldValue + * The value to convert. Typically returned by + * {@link #getFieldValue()} + * @return The converted value that is compatible with the data source type. + * @throws Converter.ConversionException + * if there is no converter and the type is not compatible with + * the data source type. + */ private Object convertToDataSource(T fieldValue) throws Converter.ConversionException { if (valueConverter != null) { diff --git a/src/com/vaadin/ui/Table.java b/src/com/vaadin/ui/Table.java index 574704d3bf..8bd40f944d 100644 --- a/src/com/vaadin/ui/Table.java +++ b/src/com/vaadin/ui/Table.java @@ -3467,10 +3467,12 @@ public class Table extends AbstractSelect implements Action.Container, if (hasConverter(colId)) { converter = getConverter(colId); } else { - // FIXME: Use thread local - converter = (Converter<Object, String>) Application - .getConverterFactory().createConverter(property.getType(), - String.class); + Application app = Application.getCurrentApplication(); + if (app != null) { + converter = (Converter<Object, String>) app + .getConverterFactory().createConverter( + property.getType(), String.class); + } } Object value = property.getValue(); if (converter != null) { |