diff options
author | patrik <patrik@vaadin.com> | 2015-04-13 12:23:04 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2015-04-14 08:07:29 +0000 |
commit | 05ce7f4158231b9ddcea69ead3bc26681c6d77f8 (patch) | |
tree | 3e76fbc5f097b06ad79a27b9175a24c7ded259df /server | |
parent | 718eb3e49ad7fef3fca392989b246f38823a3018 (diff) | |
download | vaadin-framework-05ce7f4158231b9ddcea69ead3bc26681c6d77f8.tar.gz vaadin-framework-05ce7f4158231b9ddcea69ead3bc26681c6d77f8.zip |
Add declarative OptionGroup support (#15549)
Change-Id: I5edb32ffb8282fa76fc21cf6e134eeb48fec04d1
Diffstat (limited to 'server')
3 files changed, 206 insertions, 3 deletions
diff --git a/server/src/com/vaadin/ui/AbstractSelect.java b/server/src/com/vaadin/ui/AbstractSelect.java index 629d2fb504..6ac0dad5e4 100644 --- a/server/src/com/vaadin/ui/AbstractSelect.java +++ b/server/src/com/vaadin/ui/AbstractSelect.java @@ -2233,17 +2233,27 @@ public abstract class AbstractSelect extends AbstractField<Object> implements throw new DesignException("Unrecognized child element in " + getClass().getSimpleName() + ": " + child.tagName()); } - String itemId = child.html(); - addItem(itemId); + + String itemId; + if (child.hasAttr("item-id")) { + itemId = child.attr("item-id"); + addItem(itemId); + setItemCaption(itemId, child.html()); + } else { + addItem(itemId = child.html()); + } + if (child.hasAttr("icon")) { setItemIcon( itemId, DesignAttributeHandler.readAttribute("icon", child.attributes(), Resource.class)); } + if (child.hasAttr("selected")) { selected.add(itemId); } + return itemId; } @@ -2291,7 +2301,13 @@ public abstract class AbstractSelect extends AbstractField<Object> implements DesignContext context) { Element element = design.appendElement("option"); - element.html(itemId.toString()); + String caption = getItemCaption(itemId); + if (caption != null && !caption.equals(itemId.toString())) { + element.html(caption); + element.attr("item-id", itemId.toString()); + } else { + element.html(itemId.toString()); + } Resource icon = getItemIcon(itemId); if (icon != null) { diff --git a/server/src/com/vaadin/ui/OptionGroup.java b/server/src/com/vaadin/ui/OptionGroup.java index 393f5399f6..ff2e384285 100644 --- a/server/src/com/vaadin/ui/OptionGroup.java +++ b/server/src/com/vaadin/ui/OptionGroup.java @@ -21,6 +21,8 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import org.jsoup.nodes.Element; + import com.vaadin.data.Container; import com.vaadin.event.FieldEvents; import com.vaadin.event.FieldEvents.BlurEvent; @@ -30,6 +32,7 @@ import com.vaadin.event.FieldEvents.FocusListener; import com.vaadin.server.PaintException; import com.vaadin.server.PaintTarget; import com.vaadin.shared.ui.optiongroup.OptionGroupConstants; +import com.vaadin.ui.declarative.DesignContext; /** * Configures select to be used as an option group. @@ -252,4 +255,28 @@ public class OptionGroup extends AbstractSelect implements public boolean isHtmlContentAllowed() { return htmlContentAllowed; } + + @Override + protected String readItem(Element child, Set<String> selected, + DesignContext context) { + String itemId = super.readItem(child, selected, context); + + if (child.hasAttr("disabled")) { + setItemEnabled(itemId, false); + } + + return itemId; + } + + @Override + protected Element writeItem(Element design, Object itemId, + DesignContext context) { + Element elem = super.writeItem(design, itemId, context); + + if (!isItemEnabled(itemId)) { + elem.attr("disabled", ""); + } + + return elem; + } } diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractselect/OptionGroupDeclarativeTests.java b/server/tests/src/com/vaadin/tests/server/component/abstractselect/OptionGroupDeclarativeTests.java new file mode 100644 index 0000000000..4d75e0b59f --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstractselect/OptionGroupDeclarativeTests.java @@ -0,0 +1,160 @@ +/* + * 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.abstractselect; + +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.server.ThemeResource; +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.OptionGroup; + +public class OptionGroupDeclarativeTests extends + DeclarativeTestBase<OptionGroup> { + + private OptionGroup og; + + @Before + public void init() { + og = new OptionGroup(); + } + + @Test + public void testBasicSyntax() { + + String expected = "<v-option-group />"; + testReadWrite(expected); + + } + + @Test + public void testOptionSyntax() { + + og.addItems("foo", "bar", "baz", "bang"); + + //@formatter:off + String expected = + "<v-option-group>" + + "<option>foo</option>" + + "<option>bar</option>" + + "<option>baz</option>" + + "<option>bang</option>" + + "</v-option-group>"; + //@formatter:on + + testReadWrite(expected); + + } + + @Test + public void testDisabledOptionSyntax() { + + og.addItems("foo", "bar", "baz", "bang"); + og.setItemEnabled("baz", false); + + //@formatter:off + String expected = + "<v-option-group>" + + "<option>foo</option>" + + "<option>bar</option>" + + "<option disabled>baz</option>" + + "<option>bang</option>" + + "</v-option-group>"; + //@formatter:on + + testReadWrite(expected); + + } + + @Test + public void testIconSyntax() { + + og.addItems("foo", "bar", "baz", "bang"); + og.setItemIcon("bar", new ThemeResource("foobar.png")); + + //@formatter:off + String expected = + "<v-option-group>" + + "<option>foo</option>" + + "<option icon='theme://foobar.png'>bar</option>" + + "<option>baz</option>" + + "<option>bang</option>" + + "</v-option-group>"; + //@formatter:on + + testReadWrite(expected); + + } + + @Test + public void testHTMLCaption() { + + og.addItems("foo", "bar", "baz", "bang"); + + og.setHtmlContentAllowed(true); + + og.setItemCaption("foo", "<b>True</b>"); + og.setItemCaption("bar", "<font color='red'>False</font>"); + + //@formatter:off + String expected = + "<v-option-group html-content-allowed='true'>" + + "<option item-id=\"foo\"><b>True</b></option>" + + "<option item-id=\"bar\"><font color='red'>False</font></option>" + + "<option>baz</option>" + + "<option>bang</option>" + + "</v-option-group>"; + //@formatter:on + + testReadWrite(expected); + } + + @Test + public void testPlaintextCaption() { + + og.addItems("foo", "bar", "baz", "bang"); + + og.setItemCaption("foo", "<b>True</b>"); + og.setItemCaption("bar", "<font color='red'>False</font>"); + + //@formatter:off + String expected = + "<v-option-group>" + + "<option item-id=\"foo\"><b>True</b></option>" + + "<option item-id=\"bar\"><font color='red'>False</font></option>" + + "<option>baz</option>" + + "<option>bang</option>" + + "</v-option-group>"; + //@formatter:on + + testReadWrite(expected); + } + + private void testReadWrite(String design) { + testWrite(design, og, true); + testRead(design, og); + } + + @Override + public OptionGroup testRead(String design, OptionGroup expected) { + + OptionGroup read = super.testRead(design, expected); + testWrite(design, read, true); + + return read; + } + +} |