]> source.dussan.org Git - vaadin-framework.git/commitdiff
Modified FieldGroup so that fields configured with read only properties 29/229/2
authortapio <tapio@vaadin.com>
Thu, 8 Nov 2012 11:32:06 +0000 (13:32 +0200)
committertapio <tapio@vaadin.com>
Thu, 8 Nov 2012 11:32:06 +0000 (13:32 +0200)
will also be made read only (#9076).

Change-Id: I183ae263f4952a51513623d38dc9e04ce1482f45

server/src/com/vaadin/data/fieldgroup/FieldGroup.java
server/src/com/vaadin/data/util/TransactionalPropertyWrapper.java
server/tests/src/com/vaadin/tests/data/bean/BeanWithReadOnlyField.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/server/component/fieldgroup/FieldGroupWithReadOnlyPropertiesTest.java [new file with mode: 0644]

index 6bef69fe5bf6c30f546035b9a604e17697a9e593..5a69cad62e6b5f68eb3b906ba9ce8cba15298185 100644 (file)
@@ -188,7 +188,8 @@ public class FieldGroup implements Serializable {
     }
 
     /**
-     * Returns the read only status for the fields.
+     * Returns the read only status that is used by default with all fields that
+     * have a writable data source.
      * <p>
      * Note that this will not accurately represent the read only status of all
      * fields if you change the read only status of the fields through some
@@ -201,16 +202,22 @@ public class FieldGroup implements Serializable {
     }
 
     /**
-     * Updates the read only state of all bound fields.
+     * Sets the read only state to the given value for all fields with writable
+     * data source. Fields with read only data source will always be set to read
+     * only.
      * 
      * @param fieldsReadOnly
-     *            true to set all bound fields to read only, false to set them
-     *            to read write
+     *            true to set the fields with writable data source to read only,
+     *            false to set them to read write
      */
     public void setReadOnly(boolean fieldsReadOnly) {
         readOnly = fieldsReadOnly;
         for (Field<?> field : getFields()) {
-            field.setReadOnly(fieldsReadOnly);
+            if (!field.getPropertyDataSource().isReadOnly()) {
+                field.setReadOnly(fieldsReadOnly);
+            } else {
+                field.setReadOnly(true);
+            }
         }
     }
 
@@ -325,7 +332,8 @@ public class FieldGroup implements Serializable {
      * Configures a field with the settings set for this FieldBinder.
      * <p>
      * By default this updates the buffered, read only and enabled state of the
-     * field. Also adds validators when applicable.
+     * field. Also adds validators when applicable. Fields with read only data
+     * source are always configured as read only.
      * 
      * @param field
      *            The field to update
@@ -334,7 +342,12 @@ public class FieldGroup implements Serializable {
         field.setBuffered(isBuffered());
 
         field.setEnabled(isEnabled());
-        field.setReadOnly(isReadOnly());
+
+        if (field.getPropertyDataSource().isReadOnly()) {
+            field.setReadOnly(true);
+        } else {
+            field.setReadOnly(isReadOnly());
+        }
     }
 
     /**
index d8d27ae4c80acbe1fdaf0c7f8a8957f74f391975..503bb1c7430517617950d4e8d20a9a2fc85ad531 100644 (file)
@@ -122,4 +122,18 @@ public class TransactionalPropertyWrapper<T> extends AbstractProperty<T>
         return wrappedProperty;
     }
 
+    @Override
+    public boolean isReadOnly() {
+        return wrappedProperty.isReadOnly();
+    }
+
+    @Override
+    public void setReadOnly(boolean newStatus) {
+        boolean oldStatus = isReadOnly();
+        wrappedProperty.setReadOnly(newStatus);
+        if (oldStatus != isReadOnly()) {
+            fireReadOnlyStatusChange();
+        }
+    }
+
 }
diff --git a/server/tests/src/com/vaadin/tests/data/bean/BeanWithReadOnlyField.java b/server/tests/src/com/vaadin/tests/data/bean/BeanWithReadOnlyField.java
new file mode 100644 (file)
index 0000000..77f5613
--- /dev/null
@@ -0,0 +1,18 @@
+package com.vaadin.tests.data.bean;
+
+public class BeanWithReadOnlyField {
+    private String readOnlyField;
+    private String writableField;
+
+    public String getWritableField() {
+        return writableField;
+    }
+
+    public void setWritableField(String writableField) {
+        this.writableField = writableField;
+    }
+
+    public String getReadOnlyField() {
+        return readOnlyField;
+    }
+}
diff --git a/server/tests/src/com/vaadin/tests/server/component/fieldgroup/FieldGroupWithReadOnlyPropertiesTest.java b/server/tests/src/com/vaadin/tests/server/component/fieldgroup/FieldGroupWithReadOnlyPropertiesTest.java
new file mode 100644 (file)
index 0000000..60a92d7
--- /dev/null
@@ -0,0 +1,51 @@
+package com.vaadin.tests.server.component.fieldgroup;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import com.vaadin.data.fieldgroup.FieldGroup;
+import com.vaadin.data.util.BeanItem;
+import com.vaadin.tests.data.bean.BeanWithReadOnlyField;
+import com.vaadin.ui.TextField;
+
+public class FieldGroupWithReadOnlyPropertiesTest {
+
+    private TextField readOnlyField = new TextField();
+    private TextField writableField = new TextField();
+
+    @Test
+    public void bindReadOnlyPropertyToFieldGroup() {
+        BeanWithReadOnlyField bean = new BeanWithReadOnlyField();
+        BeanItem<BeanWithReadOnlyField> beanItem = new BeanItem<BeanWithReadOnlyField>(
+                bean);
+        beanItem.getItemProperty("readOnlyField").setReadOnly(true);
+
+        FieldGroup fieldGroup = new FieldGroup(beanItem);
+        fieldGroup.bindMemberFields(this);
+
+        assertTrue(readOnlyField.isReadOnly());
+        assertFalse(writableField.isReadOnly());
+    }
+
+    @Test
+    public void fieldGroupSetReadOnlyTest() {
+        BeanWithReadOnlyField bean = new BeanWithReadOnlyField();
+        BeanItem<BeanWithReadOnlyField> beanItem = new BeanItem<BeanWithReadOnlyField>(
+                bean);
+        beanItem.getItemProperty("readOnlyField").setReadOnly(true);
+
+        FieldGroup fieldGroup = new FieldGroup(beanItem);
+        fieldGroup.bindMemberFields(this);
+
+        fieldGroup.setReadOnly(true);
+        assertTrue(readOnlyField.isReadOnly());
+        assertTrue(writableField.isReadOnly());
+
+        fieldGroup.setReadOnly(false);
+        assertTrue(readOnlyField.isReadOnly());
+        assertFalse(writableField.isReadOnly());
+    }
+
+}