]> source.dussan.org Git - vaadin-framework.git/commitdiff
Added convenience methods for bean binding (#13068)
authorMatti Tahvonen <matti@vaadin.com>
Sat, 7 Dec 2013 17:38:43 +0000 (19:38 +0200)
committerVaadin Code Review <review@vaadin.com>
Thu, 27 Mar 2014 13:21:06 +0000 (13:21 +0000)
Reduces common bean binding code from 4 lines to 1

Change-Id: Ie5f20ec2791284b850a9f3e11e21face3b5e4276

server/src/com/vaadin/data/fieldgroup/BeanFieldGroup.java
server/tests/src/com/vaadin/tests/server/component/fieldgroup/BeanFieldGroupTest.java

index cb8a044f00615875a28f5c17d4eac9bf99b4c1c4..6616f9f24e5acb2713e951ff4a054b51d1797da2 100644 (file)
@@ -225,4 +225,58 @@ public class BeanFieldGroup<T> extends FieldGroup {
         }
         return beanValidationImplementationAvailable;
     }
+
+    /**
+     * Convenience method to bind Fields from a given "field container" to a
+     * given bean with buffering disabled.
+     * <p>
+     * The returned {@link BeanFieldGroup} can be used for further
+     * configuration.
+     * 
+     * @see #bindFieldsBuffered(Object, Object)
+     * @see #bindMemberFields(Object)
+     * @since 7.2
+     * @param bean
+     *            the bean to be bound
+     * @param objectWithMemberFields
+     *            the class that contains {@link Field}s for bean properties
+     * @return the bean field group used to make binding
+     */
+    public static <T> BeanFieldGroup<T> bindFieldsUnbuffered(T bean,
+            Object objectWithMemberFields) {
+        return createAndBindFields(bean, objectWithMemberFields, false);
+    }
+
+    /**
+     * Convenience method to bind Fields from a given "field container" to a
+     * given bean with buffering enabled.
+     * <p>
+     * The returned {@link BeanFieldGroup} can be used for further
+     * configuration.
+     * 
+     * @see #bindFieldsUnbuffered(Object, Object)
+     * @see #bindMemberFields(Object)
+     * @since 7.2
+     * @param bean
+     *            the bean to be bound
+     * @param objectWithMemberFields
+     *            the class that contains {@link Field}s for bean properties
+     * @return the bean field group used to make binding
+     */
+    public static <T> BeanFieldGroup<T> bindFieldsBuffered(T bean,
+            Object objectWithMemberFields) {
+        return createAndBindFields(bean, objectWithMemberFields, true);
+    }
+
+    private static <T> BeanFieldGroup<T> createAndBindFields(T bean,
+            Object objectWithMemberFields, boolean buffered) {
+        @SuppressWarnings("unchecked")
+        BeanFieldGroup<T> beanFieldGroup = new BeanFieldGroup<T>(
+                (Class<T>) bean.getClass());
+        beanFieldGroup.setItemDataSource(bean);
+        beanFieldGroup.setBuffered(buffered);
+        beanFieldGroup.bindMemberFields(objectWithMemberFields);
+        return beanFieldGroup;
+    }
+
 }
index 44b77e88e20c76b142f041328b0c8e5dcf2eefc3..112d36d8843cd6fabce58d6ec0ab4c56f1babbba 100644 (file)
@@ -2,19 +2,60 @@ package com.vaadin.tests.server.component.fieldgroup;
 
 import static org.junit.Assert.assertEquals;
 
+import org.junit.Assert;
 import org.junit.Test;
 
 import com.vaadin.data.fieldgroup.BeanFieldGroup;
+import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
+import com.vaadin.data.fieldgroup.PropertyId;
+import com.vaadin.ui.Field;
+import com.vaadin.ui.TextField;
 
 public class BeanFieldGroupTest {
 
+    private static final String DEFAULT_FOR_BASIC_FIELD = "default";
+
     public static class MyBean {
 
+        private String basicField = DEFAULT_FOR_BASIC_FIELD;
+
+        private String anotherField;
+
         private MyNestedBean nestedBean = new MyNestedBean();
 
         public MyNestedBean getNestedBean() {
             return nestedBean;
         }
+
+        /**
+         * @return the basicField
+         */
+        public String getBasicField() {
+            return basicField;
+        }
+
+        /**
+         * @param basicField
+         *            the basicField to set
+         */
+        public void setBasicField(String basicField) {
+            this.basicField = basicField;
+        }
+
+        /**
+         * @return the anotherField
+         */
+        public String getAnotherField() {
+            return anotherField;
+        }
+
+        /**
+         * @param anotherField
+         *            the anotherField to set
+         */
+        public void setAnotherField(String anotherField) {
+            this.anotherField = anotherField;
+        }
     }
 
     public static class MyNestedBean {
@@ -26,6 +67,59 @@ public class BeanFieldGroupTest {
         }
     }
 
+    public static class ViewStub {
+
+        TextField basicField = new TextField();
+
+        @PropertyId("anotherField")
+        TextField boundWithAnnotation = new TextField();
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testStaticBindingHelper() {
+        MyBean myBean = new MyBean();
+
+        ViewStub viewStub = new ViewStub();
+        BeanFieldGroup<MyBean> bindFields = BeanFieldGroup
+                .bindFieldsUnbuffered(myBean, viewStub);
+
+        Field<String> field = (Field<String>) bindFields.getField("basicField");
+        Assert.assertEquals(DEFAULT_FOR_BASIC_FIELD, myBean.basicField);
+        field.setValue("Foo");
+        Assert.assertEquals("Foo", myBean.basicField);
+
+        field = (Field<String>) bindFields.getField("anotherField");
+        field.setValue("Foo");
+        Assert.assertEquals("Foo", myBean.anotherField);
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testStaticBufferedBindingHelper() throws CommitException {
+        MyBean myBean = new MyBean();
+
+        ViewStub viewStub = new ViewStub();
+        BeanFieldGroup<MyBean> bindFields = BeanFieldGroup.bindFieldsBuffered(
+                myBean, viewStub);
+
+        Field<String> basicField = (Field<String>) bindFields
+                .getField("basicField");
+        basicField.setValue("Foo");
+        Assert.assertEquals(DEFAULT_FOR_BASIC_FIELD, myBean.basicField);
+
+        Field<String> anotherField = (Field<String>) bindFields
+                .getField("anotherField");
+        anotherField.setValue("Foo");
+        Assert.assertNull(myBean.anotherField);
+
+        bindFields.commit();
+
+        Assert.assertEquals("Foo", myBean.basicField);
+        Assert.assertEquals("Foo", myBean.anotherField);
+
+    }
+
     @Test
     public void buildAndBindNestedProperty() {