]> source.dussan.org Git - vaadin-framework.git/commitdiff
Declarative support for AbstractTextField (#7749)
authorMatti Hosio <mhosio@vaadin.com>
Wed, 10 Dec 2014 15:01:33 +0000 (17:01 +0200)
committerVaadin Code Review <review@vaadin.com>
Wed, 10 Dec 2014 16:06:34 +0000 (16:06 +0000)
Change-Id: Ice244cd73825bde44fe02cf3d4b53d4eff5a5c35

server/src/com/vaadin/ui/AbstractTextField.java
server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java
server/tests/src/com/vaadin/tests/layoutparser/all-components.html
server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeFromDesign.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeToDesign.java [new file with mode: 0644]

index 9293d38119b97fb51ae5432858e70540ded5368a..31672a1f392bb08b8cea963835493f71aaa6ab85 100644 (file)
 
 package com.vaadin.ui;
 
+import java.util.Collection;
 import java.util.Map;
 
+import org.jsoup.nodes.Attributes;
+import org.jsoup.nodes.Element;
+
 import com.vaadin.event.FieldEvents.BlurEvent;
 import com.vaadin.event.FieldEvents.BlurListener;
 import com.vaadin.event.FieldEvents.BlurNotifier;
@@ -31,6 +35,8 @@ import com.vaadin.server.PaintException;
 import com.vaadin.server.PaintTarget;
 import com.vaadin.shared.ui.textfield.AbstractTextFieldState;
 import com.vaadin.shared.ui.textfield.TextFieldConstants;
+import com.vaadin.ui.declarative.DesignAttributeHandler;
+import com.vaadin.ui.declarative.DesignContext;
 
 public abstract class AbstractTextField extends AbstractField<String> implements
         BlurNotifier, FocusNotifier, TextChangeNotifier, LegacyComponent {
@@ -757,4 +763,54 @@ public abstract class AbstractTextField extends AbstractField<String> implements
         removeBlurListener(listener);
     }
 
+    /*
+     * (non-Javadoc)
+     * 
+     * @see
+     * com.vaadin.ui.AbstractField#synchronizeFromDesign(org.jsoup.nodes.Element
+     * , com.vaadin.ui.declarative.DesignContext)
+     */
+    @Override
+    public void synchronizeFromDesign(Element design,
+            DesignContext designContext) {
+        super.synchronizeFromDesign(design, designContext);
+        AbstractTextField def = designContext.getDefaultInstance(this
+                .getClass());
+        Attributes attr = design.attributes();
+        int maxLength = DesignAttributeHandler.readAttribute("maxlength", attr,
+                def.getMaxLength(), Integer.class);
+        setMaxLength(maxLength);
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see com.vaadin.ui.AbstractField#getCustomAttributes()
+     */
+    @Override
+    protected Collection<String> getCustomAttributes() {
+        Collection<String> customAttributes = super.getCustomAttributes();
+        customAttributes.add("maxlength");
+        customAttributes.add("max-length"); // to prevent this appearing in
+                                            // output
+        return customAttributes;
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see
+     * com.vaadin.ui.AbstractField#synchronizeToDesign(org.jsoup.nodes.Element,
+     * com.vaadin.ui.declarative.DesignContext)
+     */
+    @Override
+    public void synchronizeToDesign(Element design, DesignContext designContext) {
+        super.synchronizeToDesign(design, designContext);
+        AbstractTextField def = designContext.getDefaultInstance(this
+                .getClass());
+        Attributes attr = design.attributes();
+        DesignAttributeHandler.writeAttribute("maxlength", attr,
+                getMaxLength(), def.getMaxLength(), Integer.class);
+    }
+
 }
index 1beddf57defe3c189ca70667446f8bdf3ac20e01..2992771521cdbe51251e57ff31475f76bc94d1d3 100644 (file)
@@ -403,6 +403,10 @@ public class DesignAttributeHandler implements Serializable {
         if (targetType == Resource.class) {
             return parseResource(value);
         }
+        if (Enum.class.isAssignableFrom(targetType)) {
+            return Enum.valueOf((Class<? extends Enum>) targetType,
+                    value.toUpperCase());
+        }
         return null;
     }
 
@@ -519,8 +523,9 @@ public class DesignAttributeHandler implements Serializable {
      */
     private static boolean isSupported(Class<?> valueType) {
         return valueType != null
-                && (valueType.isPrimitive() || supportedClasses
-                        .contains(valueType));
+                && (valueType.isPrimitive()
+                        || supportedClasses.contains(valueType) || Enum.class
+                            .isAssignableFrom(valueType));
     }
 
     /**
index 99e516ff557f5a72bc19cdf9fff5e564b464711d..90d483ac8bb8891be55aa2c8c861fb74e4dd0782 100644 (file)
@@ -39,7 +39,9 @@
       </v-css-layout>
       
       <!--  abstract field -->
-         <v-text-field buffered validation-visible=false invalid-committed invalid-allowed=false required required-error="This is a required field" conversion-error="Input {0} cannot be parsed" tabindex=3 readonly/>
+         <v-text-field buffered validation-visible=false invalid-committed invalid-allowed=false required required-error="This is a required field" conversion-error="Input {0} cannot be parsed" tabindex=3 readonly />
+      <!--  abstract text field -->
+      <v-text-field null-representation="" null-setting-allowed maxlength=10 columns=5 input-prompt="Please enter a value" text-change-event-mode="eager" text-change-timeout=2 />
       
   </v-vertical-layout>
  </body>
diff --git a/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeFromDesign.java
new file mode 100644 (file)
index 0000000..f7ba67f
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.server.component.abstracttextfield;
+
+import junit.framework.TestCase;
+
+import org.jsoup.nodes.Attributes;
+import org.jsoup.nodes.Element;
+import org.jsoup.parser.Tag;
+
+import com.vaadin.ui.AbstractTextField;
+import com.vaadin.ui.AbstractTextField.TextChangeEventMode;
+import com.vaadin.ui.TextField;
+import com.vaadin.ui.declarative.DesignContext;
+
+/**
+ * Test case for reading the attributes of the AbstractTextField from design
+ */
+public class TestSynchronizeFromDesign extends TestCase {
+
+    private DesignContext ctx;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        ctx = new DesignContext();
+    }
+
+    public void testAttributes() {
+        Element design = createDesign();
+        AbstractTextField component = getComponent();
+        component.synchronizeFromDesign(design, ctx);
+        assertEquals("this-is-null", component.getNullRepresentation());
+        assertEquals(true, component.isNullSettingAllowed());
+        assertEquals(5, component.getMaxLength());
+        assertEquals(3, component.getColumns());
+        assertEquals("input", component.getInputPrompt());
+        assertEquals(TextChangeEventMode.EAGER,
+                component.getTextChangeEventMode());
+        assertEquals(100, component.getTextChangeTimeout());
+    }
+
+    private AbstractTextField getComponent() {
+        return new TextField();
+    }
+
+    private Element createDesign() {
+        Attributes attributes = new Attributes();
+        attributes.put("null-representation", "this-is-null");
+        attributes.put("null-setting-allowed", "true");
+        attributes.put("maxlength", "5");
+        attributes.put("columns", "3");
+        attributes.put("input-prompt", "input");
+        attributes.put("text-change-event-mode", "eager");
+        attributes.put("text-change-timeout", "100");
+        Element node = new Element(Tag.valueOf("v-text-field"), "", attributes);
+        return node;
+    }
+
+}
diff --git a/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeToDesign.java
new file mode 100644 (file)
index 0000000..b8f29b0
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.server.component.abstracttextfield;
+
+import junit.framework.TestCase;
+
+import org.jsoup.nodes.Attributes;
+import org.jsoup.nodes.Element;
+import org.jsoup.parser.Tag;
+
+import com.vaadin.ui.AbstractTextField;
+import com.vaadin.ui.AbstractTextField.TextChangeEventMode;
+import com.vaadin.ui.TextField;
+import com.vaadin.ui.declarative.DesignContext;
+
+/**
+ * Test case for writing the attributes of the AbstractTextField to design
+ * 
+ * @author Vaadin Ltd
+ */
+public class TestSynchronizeToDesign extends TestCase {
+
+    private DesignContext ctx;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        ctx = new DesignContext();
+    }
+
+    public void testSynchronizetestAttributes() {
+        Element design = createDesign();
+        AbstractTextField component = getComponent();
+        component.setNullRepresentation("this-is-null");
+        component.setNullSettingAllowed(true);
+        component.setMaxLength(5);
+        component.setColumns(3);
+        component.setInputPrompt("input");
+        component.setTextChangeEventMode(TextChangeEventMode.EAGER);
+        component.setTextChangeTimeout(100);
+        component.synchronizeToDesign(design, ctx);
+        assertEquals("this-is-null", design.attr("null-representation"));
+        assertEquals("true", design.attr("null-setting-allowed"));
+        assertEquals("5", design.attr("maxlength"));
+        assertEquals("3", design.attr("columns"));
+        assertEquals("input", design.attr("input-prompt"));
+        assertEquals("EAGER", design.attr("text-change-event-mode"));
+        assertEquals("100", design.attr("text-change-timeout"));
+    }
+
+    private AbstractTextField getComponent() {
+        return new TextField();
+    }
+
+    private Element createDesign() {
+        Attributes attr = new Attributes();
+        return new Element(Tag.valueOf("v-text-field"), "", attr);
+    }
+
+}