diff options
author | Leif Åstrand <leif@vaadin.com> | 2015-09-09 14:22:47 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2015-09-09 12:56:23 +0000 |
commit | 8b9cb7b88e7bb442c058aa44bad454ad45460fec (patch) | |
tree | a06bbeb60625b9569352511f192bcd10530b3ea6 | |
parent | 3356c1e1e439217909f05b1177869a051d5e3a6b (diff) | |
download | vaadin-framework-8b9cb7b88e7bb442c058aa44bad454ad45460fec.tar.gz vaadin-framework-8b9cb7b88e7bb442c058aa44bad454ad45460fec.zip |
Implement ItemStyleGenerators for ComboBox (#9276)
Change-Id: I899c21e3f71bc728cb613685134b99961b557c5b
5 files changed, 176 insertions, 0 deletions
diff --git a/WebContent/VAADIN/themes/tests-components/styles.css b/WebContent/VAADIN/themes/tests-components/styles.css index 0680e2b472..c0b545609c 100644 --- a/WebContent/VAADIN/themes/tests-components/styles.css +++ b/WebContent/VAADIN/themes/tests-components/styles.css @@ -17,6 +17,7 @@ color: blue; } +.v-filterselect-item-bold, .v-tree-node-caption-bold { font-weight: bold; } diff --git a/client/src/com/vaadin/client/ui/VFilterSelect.java b/client/src/com/vaadin/client/ui/VFilterSelect.java index cf03382333..288fdada8e 100644 --- a/client/src/com/vaadin/client/ui/VFilterSelect.java +++ b/client/src/com/vaadin/client/ui/VFilterSelect.java @@ -101,6 +101,7 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, private final String key; private final String caption; private String untranslatedIconUri; + private String style; /** * Constructor @@ -111,6 +112,8 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, public FilterSelectSuggestion(UIDL uidl) { key = uidl.getStringAttribute("key"); caption = uidl.getStringAttribute("caption"); + style = uidl.getStringAttribute("style"); + if (uidl.hasAttribute("icon")) { untranslatedIconUri = uidl.getStringAttribute("icon"); } @@ -170,6 +173,19 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, } /** + * Gets the style set for this suggestion item. Styles are typically set + * by a server-side {@link com.vaadin.ui.ComboBox.ItemStyleGenerator}. + * The returned style is prefixed by <code>v-filterselect-item-</code>. + * + * @since + * @return the style name to use, or <code>null</code> to not apply any + * custom style. + */ + public String getStyle() { + return style; + } + + /** * Executes a selection of this item. */ @@ -196,6 +212,9 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, other.untranslatedIconUri)) { return false; } + if (!SharedUtil.equals(style, other.style)) { + return false; + } return true; } } @@ -819,6 +838,10 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, while (it.hasNext()) { final FilterSelectSuggestion s = it.next(); final MenuItem mi = new MenuItem(s.getDisplayString(), true, s); + String style = s.getStyle(); + if (style != null) { + mi.addStyleName("v-filterselect-item-" + style); + } Roles.getListitemRole().set(mi.getElement()); WidgetUtil.sinkOnloadForImages(mi.getElement()); diff --git a/server/src/com/vaadin/ui/ComboBox.java b/server/src/com/vaadin/ui/ComboBox.java index 033ec3cd14..077a27006e 100644 --- a/server/src/com/vaadin/ui/ComboBox.java +++ b/server/src/com/vaadin/ui/ComboBox.java @@ -16,6 +16,7 @@ package com.vaadin.ui; +import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -49,6 +50,30 @@ public class ComboBox extends AbstractSelect implements AbstractSelect.Filtering, FieldEvents.BlurNotifier, FieldEvents.FocusNotifier { + /** + * ItemStyleGenerator can be used to add custom styles to combo box items + * shown in the popup. The CSS class name that will be added to the item + * style names is <tt>v-filterselect-item-[style name]</tt>. + * + * @since + * @see ComboBox#setItemStyleGenerator(ItemStyleGenerator) + */ + public interface ItemStyleGenerator extends Serializable { + + /** + * Called by ComboBox when an item is painted. + * + * @param source + * the source combo box + * @param itemId + * The itemId of the item to be painted. Can be + * <code>null</code> if null selection is allowed. + * @return The style name to add to this item. (the CSS class name will + * be v-filterselect-item-[style name] + */ + public String getStyle(ComboBox source, Object itemId); + } + private String inputPrompt = null; /** @@ -102,6 +127,8 @@ public class ComboBox extends AbstractSelect implements */ private boolean textInputAllowed = true; + private ItemStyleGenerator itemStyleGenerator = null; + public ComboBox() { initDefaults(); } @@ -239,6 +266,9 @@ public class ComboBox extends AbstractSelect implements target.startTag("so"); target.addAttribute("caption", ""); target.addAttribute("key", ""); + + paintItemStyle(target, null); + target.endTag("so"); } @@ -275,6 +305,9 @@ public class ComboBox extends AbstractSelect implements // at most one item can be selected at a time selectedKeys[keyIndex++] = key; } + + paintItemStyle(target, id); + target.endTag("so"); } target.endTag("options"); @@ -311,6 +344,16 @@ public class ComboBox extends AbstractSelect implements } + private void paintItemStyle(PaintTarget target, Object itemId) + throws PaintException { + if (itemStyleGenerator != null) { + String style = itemStyleGenerator.getStyle(this, itemId); + if (style != null && !style.isEmpty()) { + target.addAttribute("style", style); + } + } + } + /** * Sets whether it is possible to input text into the field or whether the * field area of the component is just used to show what is selected. By @@ -866,4 +909,30 @@ public class ComboBox extends AbstractSelect implements return scrollToSelectedItem; } + /** + * Sets the item style generator that is used to produce custom styles for + * showing items in the popup. The CSS class name that will be added to the + * item style names is <tt>v-filterselect-item-[style name]</tt>. + * + * @param itemStyleGenerator + * the item style generator to set, or <code>null</code> to not + * use any custom item styles + * @since + */ + public void setItemStyleGenerator(ItemStyleGenerator itemStyleGenerator) { + this.itemStyleGenerator = itemStyleGenerator; + markAsDirty(); + } + + /** + * Gets the currently used item style generator. + * + * @return the itemStyleGenerator the currently used item style generator, + * or <code>null</code> if no generator is used + * @since + */ + public ItemStyleGenerator getItemStyleGenerator() { + return itemStyleGenerator; + } + } diff --git a/uitest/src/com/vaadin/tests/components/combobox/ComboBoxItemStyleGeneratorTest.java b/uitest/src/com/vaadin/tests/components/combobox/ComboBoxItemStyleGeneratorTest.java new file mode 100644 index 0000000000..20c460e342 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/combobox/ComboBoxItemStyleGeneratorTest.java @@ -0,0 +1,57 @@ +/* + * 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.components.combobox; + +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import com.vaadin.testbench.elements.ComboBoxElement; +import com.vaadin.tests.tb3.SingleBrowserTest; + +public class ComboBoxItemStyleGeneratorTest extends SingleBrowserTest { + @Test + public void testItemStyleGenerator() { + openTestURL(); + + ComboBoxElement comboBox = $(ComboBoxElement.class).first(); + + selectMenuPath("Component", "Features", "Item style generator", + "Bold fives"); + + comboBox.openPopup(); + + List<WebElement> boldItems = findElements(By + .className("v-filterselect-item-bold")); + + Assert.assertEquals(1, boldItems.size()); + Assert.assertEquals("Item 5", boldItems.get(0).getText()); + + selectMenuPath("Component", "Features", "Item style generator", "-"); + + boldItems = findElements(By.className("v-filterselect-item-bold")); + Assert.assertEquals(0, boldItems.size()); + } + + @Override + protected Class<?> getUIClass() { + return ComboBoxes2.class; + } + +} diff --git a/uitest/src/com/vaadin/tests/components/combobox/ComboBoxes2.java b/uitest/src/com/vaadin/tests/components/combobox/ComboBoxes2.java index 867ef6b35c..c2ee20cf4a 100644 --- a/uitest/src/com/vaadin/tests/components/combobox/ComboBoxes2.java +++ b/uitest/src/com/vaadin/tests/components/combobox/ComboBoxes2.java @@ -6,6 +6,7 @@ import com.vaadin.server.Resource; import com.vaadin.shared.ui.combobox.FilteringMode; import com.vaadin.tests.components.select.AbstractSelectTestCase; import com.vaadin.ui.ComboBox; +import com.vaadin.ui.ComboBox.ItemStyleGenerator; public class ComboBoxes2<T extends ComboBox> extends AbstractSelectTestCase<T> { @@ -23,6 +24,13 @@ public class ComboBoxes2<T extends ComboBox> extends AbstractSelectTestCase<T> { } }; + private Command<T, ItemStyleGenerator> itemStyleGeneratorCommand = new Command<T, ItemStyleGenerator>() { + @Override + public void execute(T c, ItemStyleGenerator value, Object data) { + c.setItemStyleGenerator(value); + } + }; + @Override protected Class<T> getTestClass() { return (Class<T>) ComboBox.class; @@ -34,6 +42,7 @@ public class ComboBoxes2<T extends ComboBox> extends AbstractSelectTestCase<T> { createItemIconSelect(CATEGORY_DATA_SOURCE); createInputPromptAction(CATEGORY_FEATURES); createFilteringModeAction(CATEGORY_FEATURES); + createItemStyleGeneratorAction(CATEGORY_FEATURES); createNewItemsAllowedAction(CATEGORY_STATE); createTextInputAlowedAction(CATEGORY_STATE); } @@ -69,6 +78,23 @@ public class ComboBoxes2<T extends ComboBox> extends AbstractSelectTestCase<T> { } + private void createItemStyleGeneratorAction(String category) { + LinkedHashMap<String, ItemStyleGenerator> options = new LinkedHashMap<String, ItemStyleGenerator>(); + options.put("-", null); + options.put("Bold fives", new ItemStyleGenerator() { + @Override + public String getStyle(ComboBox source, Object itemId) { + if (String.valueOf(itemId).indexOf('5') != -1) { + return "bold"; + } else { + return null; + } + } + }); + createSelectAction("Item style generator", category, options, "-", + itemStyleGeneratorCommand); + } + private void createInputPromptAction(String category) { LinkedHashMap<String, String> options = new LinkedHashMap<String, String>(); options.put("-", null); |