]> source.dussan.org Git - vaadin-framework.git/commitdiff
Get converter from the application of the component if available (#8992)
authorArtur Signell <artur@vaadin.com>
Tue, 19 Jun 2012 13:22:13 +0000 (16:22 +0300)
committerArtur Signell <artur@vaadin.com>
Thu, 21 Jun 2012 14:11:27 +0000 (17:11 +0300)
src/com/vaadin/data/util/converter/ConverterUtil.java
src/com/vaadin/ui/AbstractField.java
src/com/vaadin/ui/Label.java
src/com/vaadin/ui/Table.java

index 5fe3101db235a28c7ba854e2c205226aa97f6e3b..239baf6b6d12b56aae18d6b9b7b0070c5f310358 100644 (file)
@@ -10,14 +10,37 @@ import com.vaadin.Application;
 
 public class ConverterUtil implements Serializable {
 
-    public static <UITYPE, MODELTYPE> Converter<UITYPE, MODELTYPE> getConverter(
-            Class<UITYPE> uiType, Class<MODELTYPE> modelType) {
-        Converter<UITYPE, MODELTYPE> converter = null;
+    /**
+     * Finds a converter that can convert from the given presentation type to
+     * the given model type and back. Uses the given application to find a
+     * {@link ConverterFactory} or, if application is null, uses the
+     * {@link Application#getCurrentApplication()}.
+     * 
+     * @param <PRESENTATIONTYPE>
+     *            The presentation type
+     * @param <MODELTYPE>
+     *            The model type
+     * @param presentationType
+     *            The presentation type
+     * @param modelType
+     *            The model type
+     * @param application
+     *            The application to use to find a ConverterFactory or null to
+     *            use the current application
+     * @return A Converter capable of converting between the given types or null
+     *         if no converter was found
+     */
+    public static <PRESENTATIONTYPE, MODELTYPE> Converter<PRESENTATIONTYPE, MODELTYPE> getConverter(
+            Class<PRESENTATIONTYPE> presentationType,
+            Class<MODELTYPE> modelType, Application application) {
+        Converter<PRESENTATIONTYPE, MODELTYPE> converter = null;
+        if (application == null) {
+            application = Application.getCurrentApplication();
+        }
 
-        Application app = Application.getCurrentApplication();
-        if (app != null) {
-            ConverterFactory factory = app.getConverterFactory();
-            converter = factory.createConverter(uiType, modelType);
+        if (application != null) {
+            ConverterFactory factory = application.getConverterFactory();
+            converter = factory.createConverter(presentationType, modelType);
         }
         return converter;
 
index b74dba6d65233a1cf292cc9f5ada2df918feef41..21ca01f5929f21db488a9bfc49286c6ee960afd8 100644 (file)
@@ -761,7 +761,7 @@ public abstract class AbstractField<T> extends AbstractComponent implements
      */
     public void setConverter(Class<?> datamodelType) {
         Converter<T, ?> c = (Converter<T, ?>) ConverterUtil.getConverter(
-                getType(), datamodelType);
+                getType(), datamodelType, getApplication());
         setConverter(c);
     }
 
index 12e1f9590ee48e1897ac27cd7224e6edafd45c96..e1c64605d7484dc2d9cfb3b0d35e8ead0eb1a8f9 100644 (file)
@@ -228,7 +228,7 @@ public class Label extends AbstractComponent implements Property<String>,
                 newDataSource.getType())) {
             // Try to find a converter
             Converter<String, ?> c = ConverterUtil.getConverter(String.class,
-                    newDataSource.getType());
+                    newDataSource.getType(), getApplication());
             setConverter(c);
         }
         dataSource = newDataSource;
index 5d4f91970483dbe28802a943ee2f88fa3559568f..b74de7e180020c613c09a6cabf349ee6af3d5807 100644 (file)
@@ -21,13 +21,13 @@ import java.util.StringTokenizer;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import com.vaadin.Application;
 import com.vaadin.data.Container;
 import com.vaadin.data.Item;
 import com.vaadin.data.Property;
 import com.vaadin.data.util.ContainerOrderedWrapper;
 import com.vaadin.data.util.IndexedContainer;
 import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ConverterUtil;
 import com.vaadin.event.Action;
 import com.vaadin.event.Action.Handler;
 import com.vaadin.event.DataBoundTransferable;
@@ -3664,12 +3664,8 @@ public class Table extends AbstractSelect implements Action.Container,
         if (hasConverter(colId)) {
             converter = getConverter(colId);
         } else {
-            Application app = Application.getCurrentApplication();
-            if (app != null) {
-                converter = (Converter<String, Object>) app
-                        .getConverterFactory().createConverter(String.class,
-                                property.getType());
-            }
+            ConverterUtil.getConverter(String.class, property.getType(),
+                    getApplication());
         }
         Object value = property.getValue();
         if (converter != null) {