From: Matti Hosio Date: Wed, 10 Dec 2014 15:01:33 +0000 (+0200) Subject: Declarative support for AbstractTextField (#7749) X-Git-Tag: 7.4.0.beta1~12^2^2~38 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=775276d4a987844fbbb88730f0eb11e285ae6e09;p=vaadin-framework.git Declarative support for AbstractTextField (#7749) Change-Id: Ice244cd73825bde44fe02cf3d4b53d4eff5a5c35 --- diff --git a/server/src/com/vaadin/ui/AbstractTextField.java b/server/src/com/vaadin/ui/AbstractTextField.java index 9293d38119..31672a1f39 100644 --- a/server/src/com/vaadin/ui/AbstractTextField.java +++ b/server/src/com/vaadin/ui/AbstractTextField.java @@ -16,8 +16,12 @@ 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 implements BlurNotifier, FocusNotifier, TextChangeNotifier, LegacyComponent { @@ -757,4 +763,54 @@ public abstract class AbstractTextField extends AbstractField 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 getCustomAttributes() { + Collection 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); + } + } diff --git a/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java b/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java index 1beddf57de..2992771521 100644 --- a/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java +++ b/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java @@ -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) 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)); } /** diff --git a/server/tests/src/com/vaadin/tests/layoutparser/all-components.html b/server/tests/src/com/vaadin/tests/layoutparser/all-components.html index 99e516ff55..90d483ac8b 100644 --- a/server/tests/src/com/vaadin/tests/layoutparser/all-components.html +++ b/server/tests/src/com/vaadin/tests/layoutparser/all-components.html @@ -39,7 +39,9 @@ - + + + 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 index 0000000000..f7ba67f506 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeFromDesign.java @@ -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 index 0000000000..b8f29b0ac9 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeToDesign.java @@ -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); + } + +}