]> source.dussan.org Git - vaadin-framework.git/commitdiff
Use BeanItem.addNestedProperty
authorArtur Signell <artur@vaadin.com>
Mon, 19 Dec 2011 13:40:12 +0000 (15:40 +0200)
committerArtur Signell <artur@vaadin.com>
Mon, 19 Dec 2011 14:40:58 +0000 (16:40 +0200)
src/com/vaadin/data/fieldbinder/BeanFieldBinder.java

index 8ec78bb7e85661d96f95d86649b9624e5265f82e..6777f1b9af55a10285cdd19f24cb134febc796df 100644 (file)
@@ -4,9 +4,7 @@
 package com.vaadin.data.fieldbinder;\r
 \r
 import com.vaadin.data.Item;\r
-import com.vaadin.data.Property;\r
 import com.vaadin.data.util.BeanItem;\r
-import com.vaadin.data.util.NestedMethodProperty;\r
 import com.vaadin.data.validator.BeanValidationValidator;\r
 import com.vaadin.ui.Field;\r
 \r
@@ -70,6 +68,18 @@ public class BeanFieldBinder<T> extends FieldBinder {
         }\r
     }\r
 \r
+    /**\r
+     * Helper method for setting the data source directly using a bean. This\r
+     * method wraps the bean in a {@link BeanItem} and calls\r
+     * {@link #setItemDataSource(Item)}.\r
+     * \r
+     * @param bean\r
+     *            The bean to use as data source.\r
+     */\r
+    public void setItemDataSource(T bean) {\r
+        setItemDataSource(new BeanItem(bean));\r
+    }\r
+\r
     @Override\r
     public void setItemDataSource(Item item) {\r
         if (!(item instanceof BeanItem)) {\r
@@ -86,24 +96,19 @@ public class BeanFieldBinder<T> extends FieldBinder {
 \r
     @Override\r
     public void bind(Field field, Object propertyId) {\r
-        try {\r
-            super.bind(field, propertyId);\r
-        } catch (BindException e) {\r
-            // System.out.println("Trying to add nested property " +\r
-            // propertyId);\r
-            if (getItemDataSource() != null\r
-                    && field.getPropertyDataSource() == null) {\r
-                // Workaround for nested properties in BeanItem.\r
-\r
-                // FIXME: BeanItem.setAddNestedPropertiesAutomatically would be\r
-                // a nice, real fix..\r
-                Property p = new NestedMethodProperty(getItemDataSource()\r
-                        .getBean(), (String) propertyId);\r
-                getItemDataSource().addItemProperty(propertyId, p);\r
-                super.bind(field, propertyId);\r
+        if (getItemDataSource() != null) {\r
+            // The data source is set so the property must be found in the item.\r
+            // If it is not we try to add it.\r
+            try {\r
+                getItemProperty(propertyId);\r
+            } catch (BindException e) {\r
+                // Not found, try to add a nested property;\r
+                // BeanItem property ids are always strings so this is safe\r
+                getItemDataSource().addNestedProperty((String) propertyId);\r
             }\r
         }\r
 \r
+        super.bind(field, propertyId);\r
     }\r
 \r
     @Override\r