diff options
author | Mika Murtojarvi <mika@vaadin.com> | 2014-12-04 17:25:05 +0200 |
---|---|---|
committer | Mika Murtojarvi <mika@vaadin.com> | 2014-12-05 16:22:10 +0200 |
commit | 117bf2b791b70d4e85916acf5e43a5c3b2ba66ca (patch) | |
tree | d9883cc7697906b81fc88d612383461836294dd0 | |
parent | be35a9b3f1250d9139baf5eeb3f48cb4a20c2822 (diff) | |
download | vaadin-framework-117bf2b791b70d4e85916acf5e43a5c3b2ba66ca.tar.gz vaadin-framework-117bf2b791b70d4e85916acf5e43a5c3b2ba66ca.zip |
Declarative: handle inner html for Button and Label (#7749).
Change-Id: Ie98d633e8583efed142a6a3d2cc980070cbc73f7
10 files changed, 463 insertions, 9 deletions
diff --git a/server/src/com/vaadin/ui/Button.java b/server/src/com/vaadin/ui/Button.java index e58ad7bee5..809fbd5e55 100644 --- a/server/src/com/vaadin/ui/Button.java +++ b/server/src/com/vaadin/ui/Button.java @@ -18,6 +18,9 @@ package com.vaadin.ui; import java.io.Serializable; import java.lang.reflect.Method; +import java.util.Collection; + +import org.jsoup.nodes.Element; import com.vaadin.event.Action; import com.vaadin.event.FieldEvents; @@ -35,6 +38,7 @@ import com.vaadin.shared.MouseEventDetails; import com.vaadin.shared.ui.button.ButtonServerRpc; import com.vaadin.shared.ui.button.ButtonState; import com.vaadin.ui.Component.Focusable; +import com.vaadin.ui.declarative.DesignContext; import com.vaadin.util.ReflectTools; /** @@ -660,4 +664,46 @@ public class Button extends AbstractComponent implements return getState(false).htmlContentAllowed; } + /* + * (non-Javadoc) + * + * @see + * com.vaadin.ui.DesignSynchronizable#synchronizeFromDesign(org.jsoup.nodes + * .Element, com.vaadin.ui.declarative.DesignContext) + */ + @Override + public void synchronizeFromDesign(Element design, + DesignContext designContext) { + super.synchronizeFromDesign(design, designContext); + String content = design.html(); + setCaption(content); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.ui.AbstractComponent#getCustomAttributes() + */ + @Override + protected Collection<String> getCustomAttributes() { + Collection<String> result = super.getCustomAttributes(); + result.add("caption"); + return result; + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.ui.DesignSynchronizable#synchronizeToDesign(org.jsoup.nodes + * .Element, com.vaadin.ui.declarative.DesignContext) + */ + @Override + public void synchronizeToDesign(Element design, DesignContext designContext) { + super.synchronizeToDesign(design, designContext); + String content = getCaption(); + if (content != null) { + design.html(content); + } + } } diff --git a/server/src/com/vaadin/ui/Label.java b/server/src/com/vaadin/ui/Label.java index c73840e6e9..6001e30446 100644 --- a/server/src/com/vaadin/ui/Label.java +++ b/server/src/com/vaadin/ui/Label.java @@ -17,8 +17,11 @@ package com.vaadin.ui; import java.lang.reflect.Method; +import java.util.Collection; import java.util.Locale; +import org.jsoup.nodes.Element; + import com.vaadin.data.Property; import com.vaadin.data.util.AbstractProperty; import com.vaadin.data.util.LegacyPropertyHelper; @@ -27,6 +30,7 @@ import com.vaadin.data.util.converter.ConverterUtil; import com.vaadin.shared.ui.label.ContentMode; import com.vaadin.shared.ui.label.LabelState; import com.vaadin.shared.util.SharedUtil; +import com.vaadin.ui.declarative.DesignContext; /** * Label component for showing non-editable short texts. @@ -570,4 +574,49 @@ public class Label extends AbstractComponent implements Property<String>, return LegacyPropertyHelper.legacyPropertyToString(this); } } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.ui.DesignSynchronizable#synchronizeFromDesign(org.jsoup.nodes + * .Element, com.vaadin.ui.declarative.DesignContext) + */ + @Override + public void synchronizeFromDesign(Element design, + DesignContext designContext) { + super.synchronizeFromDesign(design, designContext); + String innerHtml = design.html(); + if (innerHtml != null && !"".equals(innerHtml)) { + setValue(innerHtml); + } + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.ui.AbstractComponent#getCustomAttributes() + */ + @Override + protected Collection<String> getCustomAttributes() { + Collection<String> result = super.getCustomAttributes(); + result.add("value"); + return result; + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.ui.DesignSynchronizable#synchronizeToDesign(org.jsoup.nodes + * .Element, com.vaadin.ui.declarative.DesignContext) + */ + @Override + public void synchronizeToDesign(Element design, DesignContext designContext) { + super.synchronizeToDesign(design, designContext); + String content = getValue(); + if (content != null) { + design.html(getValue()); + } + } } diff --git a/server/tests/src/com/vaadin/tests/layoutparser/testFile.html b/server/tests/src/com/vaadin/tests/layoutparser/testFile.html index 3e4fdc4cd3..79ae1e9eaf 100644 --- a/server/tests/src/com/vaadin/tests/layoutparser/testFile.html +++ b/server/tests/src/com/vaadin/tests/layoutparser/testFile.html @@ -7,10 +7,10 @@ <v-vertical-layout width="500px"> <v-horizontal-layout> <v-label plain-text caption="FooBar"></v-label> - <v-native-button caption="Native click me" _id=firstButton></v-native-button> - <v-native-button caption="Another button" id = secondButton _id="localID"></v-native-button> - <v-native-button caption="Yet another button"></v-native-button> - <v-button plain-text caption="Click me" width = "150px"></v-button> + <v-native-button _id=firstButton>Native click me</v-native-button> + <v-native-button id = secondButton _id="localID">Another button</v-native-button> + <v-native-button>Yet another button</v-native-button> + <v-button plain-text width = "150px">Click me</v-button> </v-horizontal-layout> <v-text-field caption = "Text input"/> <v-text-area caption = "Text area" height="200px" width="300px"/> diff --git a/server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestSynchronizeFromDesign.java index 95dc0ad996..12c3a60451 100644 --- a/server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestSynchronizeFromDesign.java +++ b/server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestSynchronizeFromDesign.java @@ -100,11 +100,11 @@ public class TestSynchronizeFromDesign extends TestCase { node.appendChild(firstChild); Attributes secondChildAttributes = new Attributes(); - secondChildAttributes.put("caption", "test-button"); secondChildAttributes.put(":bottom", "100px"); secondChildAttributes.put(":right", "50%"); Element secondChild = new Element(Tag.valueOf("v-button"), "", secondChildAttributes); + secondChild.html("test-button"); node.appendChild(secondChild); return node; } diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeToDesign.java index 00505e1e19..408176f4b3 100644 --- a/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeToDesign.java +++ b/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeToDesign.java @@ -75,9 +75,10 @@ public class TestSynchronizeToDesign extends TestCase { AbstractComponent component = getComponent(); component.setCaption("test-caption"); component.synchronizeToDesign(design, ctx); - // we only changed one of the attributes, others are at default values - assertEquals(1, design.attributes().size()); - assertEquals("test-caption", design.attr("caption")); + // We only changed the caption, which is not + // an attribute. + assertEquals(0, design.attributes().size()); + assertEquals("test-caption", design.html()); } public void testSynchronizeLocale() { diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestSynchronizeFromDesign.java index 038801844d..028578e8ea 100644 --- a/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestSynchronizeFromDesign.java +++ b/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestSynchronizeFromDesign.java @@ -94,13 +94,13 @@ public class TestSynchronizeFromDesign extends TestCase { node.appendChild(firstChild); Attributes secondChildAttributes = new Attributes(); - secondChildAttributes.put("caption", "test-button"); secondChildAttributes.put(":expand", String.valueOf(expandRatio)); for (String alignment : alignments) { secondChildAttributes.put(alignment, ""); } Element secondChild = new Element(Tag.valueOf("v-button"), "", secondChildAttributes); + secondChild.html("test-button"); node.appendChild(secondChild); return node; } diff --git a/server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeFromDesign.java new file mode 100644 index 0000000000..7c2c3f8330 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeFromDesign.java @@ -0,0 +1,99 @@ +/* + * 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.button; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; +import org.junit.Test; + +import com.vaadin.ui.Button; +import com.vaadin.ui.NativeButton; +import com.vaadin.ui.declarative.DesignContext; + +/** + * + * Test cases for reading the contents of a Button and a NativeButton from a + * design. + * + */ +public class TestSynchronizeFromDesign extends TestCase { + + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + @Test + public void testWithContent() { + createAndTestButtons("Click", null); + } + + @Test + public void testWithHtmlCaption() { + createAndTestButtons("<b>Click me</b>", null); + } + + @Test + public void testWithContentAndCaption() { + createAndTestButtons("Click me", "caption"); + } + + @Test + public void testWithCaption() { + createAndTestButtons(null, "Click me"); + } + + /* + * Test both Button and NativeButton. Caption should always be ignored. If + * content is null, the created button should have empty content. + */ + private void createAndTestButtons(String content, String caption) { + Element e1 = createElement("v-button", content, caption); + Button b1 = (Button) ctx.createChild(e1); + Element e2 = createElement("v-native-button", content, caption); + NativeButton b2 = (NativeButton) ctx.createChild(e2); + if (content != null) { + assertEquals("The button has the wrong text content.", content, + b1.getCaption()); + assertEquals("The button has the wrong text content.", content, + b2.getCaption()); + } else { + assertTrue("The button has the wrong content.", + b1.getCaption() == null || "".equals(b1.getCaption())); + assertTrue("The button has the wrong content.", + b2.getCaption() == null || "".equals(b2.getCaption())); + } + } + + private Element createElement(String elementName, String content, + String caption) { + Attributes attributes = new Attributes(); + if (caption != null) { + attributes.put("caption", caption); + } + Element node = new Element(Tag.valueOf(elementName), "", attributes); + if (content != null) { + node.html(content); + } + return node; + } +}
\ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeToDesign.java new file mode 100644 index 0000000000..1ee4c11e78 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeToDesign.java @@ -0,0 +1,68 @@ +/* + * 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.button; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Element; +import org.junit.Test; + +import com.vaadin.ui.Button; +import com.vaadin.ui.NativeButton; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Tests generating html tree nodes corresponding to the contents of a Button + * and a NativeButton. + */ +public class TestSynchronizeToDesign extends TestCase { + + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + @Test + public void testWithTextContent() { + createAndTestButtons("Click me"); + } + + @Test + public void testWithHtmlContent() { + createAndTestButtons("<b>Click</b>"); + } + + private void createAndTestButtons(String content) { + Button b1 = new Button(content); + Element e1 = ctx.createNode(b1); + assertEquals("Wrong tag name for button.", "v-button", e1.tagName()); + assertEquals("Unexpected content in the v-button element.", content, + e1.html()); + assertTrue("The v-button element should not have attributes.", e1 + .attributes().size() == 0); + NativeButton b2 = new NativeButton(content); + Element e2 = ctx.createNode(b2); + assertEquals("Wrong tag name for button.", "v-native-button", + e2.tagName()); + assertEquals("Unexpected content in the v-button element.", content, + e2.html()); + assertTrue("The v-button element should not have attributes.", e2 + .attributes().size() == 0); + } +}
\ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeFromDesign.java new file mode 100644 index 0000000000..168c44cb90 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeFromDesign.java @@ -0,0 +1,102 @@ +/* + * 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.label; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; +import org.junit.Test; + +import com.vaadin.ui.Label; +import com.vaadin.ui.declarative.DesignContext; + +/** + * + * Test case for reading the contents of a Label from a design. + * + */ +public class TestSynchronizeFromDesign extends TestCase { + + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + @Test + public void testWithContent() { + createAndTestLabel("A label", null); + } + + @Test + public void testWithHtmlContent() { + createAndTestLabel("<b>A label</b>", null); + } + + @Test + public void testWithContentAndCaption() { + createAndTestLabel("A label", "This is a label"); + } + + @Test + public void testWithCaption() { + createAndTestLabel(null, "This is a label"); + } + + @Test + public void testWithoutContentAndCaption() { + createAndTestLabel(null, null); + } + + /* + * Test creating a Label. A Label can have both caption and content. + */ + private void createAndTestLabel(String content, String caption) { + Element e = createElement("v-label", content, caption); + Label l = (Label) ctx.createChild(e); + if (content != null) { + assertEquals("The label has wrong text content.", content, + l.getValue()); + } else { + assertTrue("The label has wrong text content.", + l.getValue() == null || "".equals(l.getValue())); + } + if (caption != null) { + assertEquals("The label has wrong caption.", caption, + l.getCaption()); + } else { + assertTrue("The label has wrong caption.", l.getCaption() == null + || "".equals(l.getCaption())); + } + } + + private Element createElement(String elementName, String content, + String caption) { + Attributes attributes = new Attributes(); + if (caption != null) { + attributes.put("caption", caption); + } + Element node = new Element(Tag.valueOf(elementName), "", attributes); + if (content != null) { + node.html(content); + } + return node; + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeToDesign.java new file mode 100644 index 0000000000..462e6a087b --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeToDesign.java @@ -0,0 +1,89 @@ +/* + * 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.label; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Element; +import org.junit.Test; + +import com.vaadin.ui.Label; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Tests generating an html tree node corresponding to a Label. + */ +public class TestSynchronizeToDesign extends TestCase { + + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + @Test + public void testWithContent() { + createAndTestLabel("A label", null); + } + + @Test + public void testWithHtmlContent() { + createAndTestLabel("<b>A label</b>", null); + } + + @Test + public void testWithCaption() { + createAndTestLabel(null, "Label caption"); + } + + @Test + public void testWithContentAndCaption() { + createAndTestLabel("A label", "Label caption"); + } + + @Test + public void testWithoutContentAndCaption() { + createAndTestLabel(null, null); + } + + private void createAndTestLabel(String content, String caption) { + Label l = new Label(content); + if (caption != null) { + l.setCaption(caption); + } + Element e = ctx.createNode(l); + assertEquals("Wrong tag name for label.", "v-label", e.tagName()); + if (content != null) { + assertEquals("Unexpected content in the v-label element.", content, + e.html()); + } else { + assertTrue("Unexpected content in the v-label element.", + e.html() == null || "".equals(e.html())); + } + int numAttributes = (caption == null ? 0 : 1); + assertEquals("Wrong number of attributes in v-label.", numAttributes, e + .attributes().size()); + if (caption != null) { + assertEquals("Wrong caption in the v-label element.", caption, + e.attr("caption")); + } else { + assertTrue("Unexpected caption in the v-label element.", + e.attr("caption") == null || "".equals(e.attr("caption"))); + } + } +}
\ No newline at end of file |