]> source.dussan.org Git - vaadin-framework.git/commitdiff
Changed logic to retrieve readonly state (#8643)
authorFelype Santiago Ferreira <felype@vaadin.com>
Fri, 29 Jul 2016 11:45:59 +0000 (14:45 +0300)
committerFelype Santiago Ferreira <felype@vaadin.com>
Fri, 29 Jul 2016 11:45:59 +0000 (14:45 +0300)
Change-Id: Ibc86d36ce4c8f68eec400ad264546651353709f1

server/src/main/java/com/vaadin/ui/AbstractField.java
server/src/test/java/com/vaadin/tests/server/component/abstractfield/AbstractFieldReadOnlyTest.java [new file with mode: 0644]

index 49eb5b050f870296debf09b612aa6368cfa70301..6d250765261136012ac7f5db19e076540496c400 100644 (file)
@@ -1087,8 +1087,8 @@ public abstract class AbstractField<T> extends AbstractComponent implements
         return new CompositeErrorMessage(
                 new ErrorMessage[] {
                         superError,
-                        AbstractErrorMessage
-                                .getErrorMessageForException(validationError),
+                AbstractErrorMessage
+                        .getErrorMessageForException(validationError),
                         AbstractErrorMessage
                                 .getErrorMessageForException(getCurrentBufferedSourceException()) });
 
@@ -1191,7 +1191,16 @@ public abstract class AbstractField<T> extends AbstractComponent implements
      */
     @Override
     public void readOnlyStatusChange(Property.ReadOnlyStatusChangeEvent event) {
-        getState().propertyReadOnly = event.getProperty().isReadOnly();
+        boolean readOnly = event.getProperty().isReadOnly();
+
+        boolean shouldFireChange = isReadOnly() != readOnly
+                || getState().propertyReadOnly != readOnly;
+
+        getState().propertyReadOnly = readOnly;
+
+        if (shouldFireChange) {
+            fireReadOnlyStatusChange();
+        }
     }
 
     /**
diff --git a/server/src/test/java/com/vaadin/tests/server/component/abstractfield/AbstractFieldReadOnlyTest.java b/server/src/test/java/com/vaadin/tests/server/component/abstractfield/AbstractFieldReadOnlyTest.java
new file mode 100644 (file)
index 0000000..756004a
--- /dev/null
@@ -0,0 +1,51 @@
+package com.vaadin.tests.server.component.abstractfield;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import com.vaadin.data.Property.ReadOnlyStatusChangeEvent;
+import com.vaadin.data.Property.ReadOnlyStatusChangeListener;
+import com.vaadin.data.util.MethodProperty;
+import com.vaadin.tests.data.bean.Address;
+import com.vaadin.tests.data.bean.Country;
+import com.vaadin.tests.data.bean.Person;
+import com.vaadin.tests.data.bean.Sex;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.TextField;
+
+public class AbstractFieldReadOnlyTest {
+
+    Person paulaBean = new Person("Paula", "Brilliant", "paula@brilliant.com",
+            34, Sex.FEMALE, new Address("Paula street 1", 12345, "P-town",
+                    Country.FINLAND));
+
+    @Test
+    public void testReadOnlyProperty() {
+        TextField tf = new TextField();
+        tf.setPropertyDataSource(new MethodProperty<String>(paulaBean,
+                "firstName"));
+        assertFalse(tf.isReadOnly());
+        tf.getPropertyDataSource().setReadOnly(true);
+        assertTrue(tf.isReadOnly());
+    }
+
+    @Test
+    public void testReadOnlyEventFromProperty() {
+        final Label valueStore = new Label("");
+        TextField tf = new TextField();
+        tf.addReadOnlyStatusChangeListener(new ReadOnlyStatusChangeListener() {
+            @Override
+            public void readOnlyStatusChange(ReadOnlyStatusChangeEvent event) {
+                valueStore.setValue("event received!");
+            }
+        });
+        tf.setPropertyDataSource(new MethodProperty<String>(paulaBean,
+                "firstName"));
+        assertTrue(valueStore.getValue().isEmpty());
+        tf.getPropertyDataSource().setReadOnly(true);
+        assertFalse(valueStore.getValue().isEmpty());
+    }
+
+}