From 64a029571955ee52b0c416b597fb718bc0a66c46 Mon Sep 17 00:00:00 2001 From: Matti Hosio Date: Thu, 11 Dec 2014 09:05:19 +0200 Subject: Declarative support for TextField, PasswordField and TextArea (#7749) Change-Id: I41d04c55c65820f0270742468e94f47099783950 --- server/src/com/vaadin/ui/AbstractField.java | 2 + server/src/com/vaadin/ui/PasswordField.java | 41 +++++++++++++++ server/src/com/vaadin/ui/TextArea.java | 29 +++++++++++ server/src/com/vaadin/ui/TextField.java | 40 +++++++++++++++ .../vaadin/tests/layoutparser/all-components.html | 8 ++- .../textarea/TestSynchronizeFromDesign.java | 59 +++++++++++++++++++++ .../textarea/TestSynchronizeToDesign.java | 60 ++++++++++++++++++++++ .../textfield/TestSynchronizeFromDesign.java | 59 +++++++++++++++++++++ .../textfield/TestSynchronizeToDesign.java | 59 +++++++++++++++++++++ 9 files changed, 355 insertions(+), 2 deletions(-) create mode 100644 server/tests/src/com/vaadin/tests/server/component/textarea/TestSynchronizeFromDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/textarea/TestSynchronizeToDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/textfield/TestSynchronizeFromDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/textfield/TestSynchronizeToDesign.java (limited to 'server') diff --git a/server/src/com/vaadin/ui/AbstractField.java b/server/src/com/vaadin/ui/AbstractField.java index 3728696233..fc3129d1ba 100644 --- a/server/src/com/vaadin/ui/AbstractField.java +++ b/server/src/com/vaadin/ui/AbstractField.java @@ -1790,6 +1790,8 @@ public abstract class AbstractField extends AbstractComponent implements Collection attributes = super.getCustomAttributes(); attributes.add("readonly"); attributes.add("tabindex"); + // must be handled by subclasses + attributes.add("value"); return attributes; } diff --git a/server/src/com/vaadin/ui/PasswordField.java b/server/src/com/vaadin/ui/PasswordField.java index 107e40c149..b842fc5569 100644 --- a/server/src/com/vaadin/ui/PasswordField.java +++ b/server/src/com/vaadin/ui/PasswordField.java @@ -15,7 +15,12 @@ */ package com.vaadin.ui; +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; + import com.vaadin.data.Property; +import com.vaadin.ui.declarative.DesignAttributeHandler; +import com.vaadin.ui.declarative.DesignContext; /** * A field that is used to enter secret text information like passwords. The @@ -76,4 +81,40 @@ public class PasswordField extends AbstractTextField { this(); setCaption(caption); } + + /* + * (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(); + String value = DesignAttributeHandler.readAttribute("value", attr, + def.getValue(), String.class); + setValue(value); + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.ui.AbstractTextField#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("value", attr, getValue(), + def.getValue(), String.class); + } } diff --git a/server/src/com/vaadin/ui/TextArea.java b/server/src/com/vaadin/ui/TextArea.java index e38be8ad3c..70b9268228 100644 --- a/server/src/com/vaadin/ui/TextArea.java +++ b/server/src/com/vaadin/ui/TextArea.java @@ -16,8 +16,11 @@ package com.vaadin.ui; +import org.jsoup.nodes.Element; + import com.vaadin.data.Property; import com.vaadin.shared.ui.textarea.TextAreaState; +import com.vaadin.ui.declarative.DesignContext; /** * A text field that supports multi line editing. @@ -133,4 +136,30 @@ public class TextArea extends AbstractTextField { return getState(false).wordwrap; } + /* + * (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); + setValue(design.html()); + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.ui.AbstractTextField#synchronizeToDesign(org.jsoup.nodes.Element + * , com.vaadin.ui.declarative.DesignContext) + */ + @Override + public void synchronizeToDesign(Element design, DesignContext designContext) { + super.synchronizeToDesign(design, designContext); + design.html(getValue()); + } } diff --git a/server/src/com/vaadin/ui/TextField.java b/server/src/com/vaadin/ui/TextField.java index fb1e4284a2..bb855c7b90 100644 --- a/server/src/com/vaadin/ui/TextField.java +++ b/server/src/com/vaadin/ui/TextField.java @@ -16,7 +16,12 @@ package com.vaadin.ui; +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; + import com.vaadin.data.Property; +import com.vaadin.ui.declarative.DesignAttributeHandler; +import com.vaadin.ui.declarative.DesignContext; /** *

@@ -99,4 +104,39 @@ public class TextField extends AbstractTextField { setCaption(caption); } + /* + * (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(); + String value = DesignAttributeHandler.readAttribute("value", attr, + def.getValue(), String.class); + setValue(value); + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.ui.AbstractTextField#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("value", attr, getValue(), + def.getValue(), String.class); + } } 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 90d483ac8b..f633b06cb3 100644 --- a/server/tests/src/com/vaadin/tests/layoutparser/all-components.html +++ b/server/tests/src/com/vaadin/tests/layoutparser/all-components.html @@ -40,8 +40,12 @@ - - + + + + + + test value diff --git a/server/tests/src/com/vaadin/tests/server/component/textarea/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/textarea/TestSynchronizeFromDesign.java new file mode 100644 index 0000000000..787eac0a03 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/textarea/TestSynchronizeFromDesign.java @@ -0,0 +1,59 @@ +/* + * 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.textarea; + +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.TextArea; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for reading the value of the TextField from design + * + * @author Vaadin Ltd + */ +public class TestSynchronizeFromDesign extends TestCase { + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + public void testValue() { + Element design = createDesign(); + AbstractTextField component = getComponent(); + component.synchronizeFromDesign(design, ctx); + assertEquals("test value", component.getValue()); + } + + private AbstractTextField getComponent() { + return new TextArea(); + } + + private Element createDesign() { + Attributes attributes = new Attributes(); + Element node = new Element(Tag.valueOf("v-text-area"), "", attributes); + node.html("test value"); + return node; + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/textarea/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/textarea/TestSynchronizeToDesign.java new file mode 100644 index 0000000000..a316b02ddc --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/textarea/TestSynchronizeToDesign.java @@ -0,0 +1,60 @@ +/* + * 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.textarea; + +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.TextArea; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for writing the value of the TextField 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 testSynchronizeValue() { + Element design = createDesign(); + AbstractTextField component = getComponent(); + component.setValue("test value"); + component.synchronizeToDesign(design, ctx); + assertEquals("test value", design.html()); + assertFalse(design.hasAttr("value")); + } + + private AbstractTextField getComponent() { + return new TextArea(); + } + + private Element createDesign() { + Attributes attr = new Attributes(); + return new Element(Tag.valueOf("v-text-area"), "", attr); + } + +} diff --git a/server/tests/src/com/vaadin/tests/server/component/textfield/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/textfield/TestSynchronizeFromDesign.java new file mode 100644 index 0000000000..2df0ab4855 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/textfield/TestSynchronizeFromDesign.java @@ -0,0 +1,59 @@ +/* + * 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.textfield; + +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.TextField; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for reading the value of the TextField from design + * + * @author Vaadin Ltd + */ +public class TestSynchronizeFromDesign extends TestCase { + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + public void testValue() { + Element design = createDesign(); + AbstractTextField component = getComponent(); + component.synchronizeFromDesign(design, ctx); + assertEquals("test value", component.getValue()); + } + + private AbstractTextField getComponent() { + return new TextField(); + } + + private Element createDesign() { + Attributes attributes = new Attributes(); + attributes.put("value", "test value"); + Element node = new Element(Tag.valueOf("v-text-field"), "", attributes); + return node; + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/textfield/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/textfield/TestSynchronizeToDesign.java new file mode 100644 index 0000000000..58d361e683 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/textfield/TestSynchronizeToDesign.java @@ -0,0 +1,59 @@ +/* + * 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.textfield; + +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.TextField; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for writing the value of the TextField 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 testSynchronizeValue() { + Element design = createDesign(); + AbstractTextField component = getComponent(); + component.setValue("test value"); + component.synchronizeToDesign(design, ctx); + assertEquals("test value", design.attr("value")); + } + + private AbstractTextField getComponent() { + return new TextField(); + } + + private Element createDesign() { + Attributes attr = new Attributes(); + return new Element(Tag.valueOf("v-text-field"), "", attr); + } + +} -- cgit v1.2.3