From 7476bdb00fb112eecaf503a9d4f6a33bce5618bf Mon Sep 17 00:00:00 2001 From: Aleksi Hietanen Date: Thu, 15 Sep 2016 09:51:22 +0300 Subject: Move ItemStyleProvider out of ComboBox and unify with Grid Change-Id: Ic49fdbf651e9e3ef4ffd6944de597c2fd2f185da --- server/src/main/java/com/vaadin/ui/ComboBox.java | 53 +++++++--------- server/src/main/java/com/vaadin/ui/Grid.java | 71 +++++++++++----------- .../main/java/com/vaadin/ui/StyleGenerator.java | 42 +++++++++++++ .../tests/components/grid/basics/GridBasics.java | 28 +++++---- 4 files changed, 115 insertions(+), 79 deletions(-) create mode 100644 server/src/main/java/com/vaadin/ui/StyleGenerator.java diff --git a/server/src/main/java/com/vaadin/ui/ComboBox.java b/server/src/main/java/com/vaadin/ui/ComboBox.java index 8fdc8452ed..0b1b4923f8 100644 --- a/server/src/main/java/com/vaadin/ui/ComboBox.java +++ b/server/src/main/java/com/vaadin/ui/ComboBox.java @@ -95,20 +95,6 @@ public class ComboBox extends AbstractSingleSelect implements HasValue, extends Function, Serializable { } - /** - * ItemStyleProvider 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 v-filterselect-item-[style name]. - * - * @see ComboBox#setItemStyleProvider(ItemStyleProvider) - * @param - * item type in the combo box - */ - @FunctionalInterface - public interface ItemStyleProvider - extends Function, Serializable { - } - /** * ItemIconProvider can be used to add custom icons to combo box items shown * in the popup. @@ -176,7 +162,7 @@ public class ComboBox extends AbstractSingleSelect implements HasValue, private ItemCaptionProvider itemCaptionProvider = String::valueOf; - private ItemStyleProvider itemStyleProvider = item -> null; + private StyleGenerator itemStyleGenerator = item -> null; private ItemIconProvider itemIconProvider = item -> null; private ItemFilter filter = (filterText, item) -> { @@ -263,7 +249,7 @@ public class ComboBox extends AbstractSingleSelect implements HasValue, addDataGenerator((T data, JsonObject jsonObject) -> { jsonObject.put(DataCommunicatorConstants.NAME, getItemCaptionProvider().apply(data)); - String style = itemStyleProvider.apply(data); + String style = itemStyleGenerator.apply(data); if (style != null) { jsonObject.put(ComboBoxConstants.STYLE, style); } @@ -469,32 +455,37 @@ public class ComboBox extends AbstractSingleSelect implements HasValue, } /** - * Sets the item style provider 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 v-filterselect-item-[style name]. Returning - * null from the provider results in no custom style name being set. + * Sets the style generator that is used to produce custom class names for + * items visible in the popup. The CSS class name that will be added to the + * item is v-filterselect-item-[style name]. Returning null from + * the generator results in no custom style name being set. + * + * @see StyleGenerator * - * @param itemStyleProvider - * the item style provider to set, not null + * @param itemStyleGenerator + * the item style generator to set, not null + * @throws NullPointerException + * if {@code itemStyleGenerator} is {@code null} */ - public void setItemStyleProvider(ItemStyleProvider itemStyleProvider) { - Objects.requireNonNull(itemStyleProvider, - "Item style providers must not be null"); - this.itemStyleProvider = itemStyleProvider; + public void setStyleGenerator(StyleGenerator itemStyleGenerator) { + Objects.requireNonNull(itemStyleGenerator, + "Item style generator must not be null"); + this.itemStyleGenerator = itemStyleGenerator; getDataCommunicator().reset(); } /** - * Gets the currently used item style provider that is used to generate CSS + * Gets the currently used style generator that is used to generate CSS * class names for items. The default item style provider returns null for * all items, resulting in no custom item class names being set. * - * @see #setItemStyleProvider(ItemStyleProvider) + * @see StyleGenerator + * @see #setStyleGenerator(StyleGenerator) * - * @return the currently used item style provider, not null + * @return the currently used item style generator, not null */ - public ItemStyleProvider getItemStyleProvider() { - return itemStyleProvider; + public StyleGenerator getStyleGenerator() { + return itemStyleGenerator; } /** diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index d776906afd..806f0d270f 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -242,17 +242,6 @@ public class Grid extends AbstractSingleSelect implements HasComponents { } } - /** - * A callback interface for generating style names for an item. - * - * @param - * the grid bean type - */ - @FunctionalInterface - public interface StyleGenerator - extends Function, Serializable { - } - /** * A callback interface for generating description texts for an item. * @@ -518,7 +507,7 @@ public class Grid extends AbstractSingleSelect implements HasComponents { private Function>> sortOrderProvider; private Comparator comparator; - private StyleGenerator styleGenerator; + private StyleGenerator styleGenerator = item -> null; private DescriptionGenerator descriptionGenerator; /** @@ -619,13 +608,11 @@ public class Grid extends AbstractSingleSelect implements HasComponents { obj.put(communicationId, rendererValue); - if (styleGenerator != null) { - String style = styleGenerator.apply(data); - if (style != null && !style.isEmpty()) { - JsonObject styleObj = getDataObject(jsonObject, - GridState.JSONKEY_CELLSTYLES); - styleObj.put(communicationId, style); - } + String style = styleGenerator.apply(data); + if (style != null && !style.isEmpty()) { + JsonObject styleObj = getDataObject(jsonObject, + GridState.JSONKEY_CELLSTYLES); + styleObj.put(communicationId, style); } if (descriptionGenerator != null) { String description = descriptionGenerator.apply(data); @@ -810,16 +797,20 @@ public class Grid extends AbstractSingleSelect implements HasComponents { } /** - * Sets the style generator that is used for generating styles for cells - * in this column. + * Sets the style generator that is used for generating class names for + * cells in this column. Returning null from the generator results in no + * custom style name being set. * * @param cellStyleGenerator - * the cell style generator to set, or null to - * remove a previously set generator + * the cell style generator to set, not null * @return this column + * @throws NullPointerException + * if {@code cellStyleGenerator} is {@code null} */ public Column setStyleGenerator( StyleGenerator cellStyleGenerator) { + Objects.requireNonNull(cellStyleGenerator, + "Cell style generator must not be null"); this.styleGenerator = cellStyleGenerator; getParent().getDataCommunicator().reset(); return this; @@ -829,8 +820,7 @@ public class Grid extends AbstractSingleSelect implements HasComponents { * Gets the style generator that is used for generating styles for * cells. * - * @return the cell style generator, or null if no - * generator is set + * @return the cell style generator */ public StyleGenerator getStyleGenerator() { return styleGenerator; @@ -907,7 +897,7 @@ public class Grid extends AbstractSingleSelect implements HasComponents { private List>> sortOrder = new ArrayList<>(); private DetailsManager detailsManager; private Set extensionComponents = new HashSet<>(); - private StyleGenerator styleGenerator; + private StyleGenerator styleGenerator = item -> null; private DescriptionGenerator descriptionGenerator; /** @@ -920,11 +910,9 @@ public class Grid extends AbstractSingleSelect implements HasComponents { addExtension(detailsManager); addDataGenerator(detailsManager); addDataGenerator((item, json) -> { - if (styleGenerator != null) { - String styleName = styleGenerator.apply(item); - if (styleName != null && !styleName.isEmpty()) { - json.put(GridState.JSONKEY_ROWSTYLE, styleName); - } + String styleName = styleGenerator.apply(item); + if (styleName != null && !styleName.isEmpty()) { + json.put(GridState.JSONKEY_ROWSTYLE, styleName); } if (descriptionGenerator != null) { String description = descriptionGenerator.apply(item); @@ -1197,22 +1185,31 @@ public class Grid extends AbstractSingleSelect implements HasComponents { } /** - * Sets the style generator that is used for generating styles for rows. + * Sets the style generator that is used for generating class names for rows + * in this grid. Returning null from the generator results in no custom + * style name being set. + * + * @see StyleGenerator * * @param styleGenerator - * the row style generator to set, or null to remove - * a previously set generator + * the row style generator to set, not null + * @throws NullPointerException + * if {@code styleGenerator} is {@code null} */ public void setStyleGenerator(StyleGenerator styleGenerator) { + Objects.requireNonNull(styleGenerator, + "Style generator must not be null"); this.styleGenerator = styleGenerator; getDataCommunicator().reset(); } /** - * Gets the style generator that is used for generating styles for rows. + * Gets the style generator that is used for generating class names for + * rows. + * + * @see StyleGenerator * - * @return the row style generator, or null if no generator is - * set + * @return the row style generator */ public StyleGenerator getStyleGenerator() { return styleGenerator; diff --git a/server/src/main/java/com/vaadin/ui/StyleGenerator.java b/server/src/main/java/com/vaadin/ui/StyleGenerator.java new file mode 100644 index 0000000000..ddec2a2071 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/StyleGenerator.java @@ -0,0 +1,42 @@ +/* + * Copyright 2000-2016 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.ui; + +import java.io.Serializable; +import java.util.function.Function; + +/** + * A callback interface for generating custom CSS class names for items. + * + * @param + * the item type + * + * @since 8.0 + * @author Vaadin Ltd + */ +@FunctionalInterface +public interface StyleGenerator extends Function, Serializable { + + /** + * Gets a class name for the {@code item}. + * + * @param item + * the item to get the class name for + * @return the generated class name + */ + @Override + String apply(T item); +} diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java b/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java index 0278149be9..3a0646ed27 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java @@ -17,13 +17,13 @@ import com.vaadin.ui.Button; import com.vaadin.ui.Component; import com.vaadin.ui.Grid; import com.vaadin.ui.Grid.DetailsGenerator; -import com.vaadin.ui.Grid.StyleGenerator; import com.vaadin.ui.Label; import com.vaadin.ui.MenuBar; import com.vaadin.ui.MenuBar.Command; import com.vaadin.ui.MenuBar.MenuItem; import com.vaadin.ui.Notification; import com.vaadin.ui.Panel; +import com.vaadin.ui.StyleGenerator; import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.renderers.DateRenderer; import com.vaadin.ui.renderers.HtmlRenderer; @@ -193,12 +193,15 @@ public class GridBasics extends AbstractTestUIWithLog { : null)) .setCheckable(true); stateMenu - .addItem("Cell description generator", item -> grid.getColumns() - .stream().findFirst() - .ifPresent(c -> c.setDescriptionGenerator( - item.isChecked() ? t -> "Cell tooltip for row " - + t.getRowNumber() + ", Column 0" - : null))) + .addItem("Cell description generator", + item -> grid.getColumns().stream().findFirst() + .ifPresent( + c -> c.setDescriptionGenerator( + item.isChecked() + ? t -> "Cell tooltip for row " + + t.getRowNumber() + + ", Column 0" + : null))) .setCheckable(true); stateMenu.addItem("Item click listener", new Command() { @@ -230,7 +233,8 @@ public class GridBasics extends AbstractTestUIWithLog { } private void createRowStyleMenu(MenuItem rowStyleMenu) { - addGridMethodMenu(rowStyleMenu, ROW_STYLE_GENERATOR_NONE, null, + addGridMethodMenu(rowStyleMenu, ROW_STYLE_GENERATOR_NONE, + (StyleGenerator) t -> null, grid::setStyleGenerator); addGridMethodMenu(rowStyleMenu, ROW_STYLE_GENERATOR_ROW_NUMBERS, t -> "row" + t.getRowNumber(), grid::setStyleGenerator); @@ -247,16 +251,18 @@ public class GridBasics extends AbstractTestUIWithLog { private void createCellStyleMenu(MenuItem cellStyleMenu) { addGridMethodMenu(cellStyleMenu, CELL_STYLE_GENERATOR_NONE, - (StyleGenerator) null, + (StyleGenerator) t -> null, sg -> grid.getColumns().forEach(c -> c.setStyleGenerator(sg))); addGridMethodMenu(cellStyleMenu, CELL_STYLE_GENERATOR_EMPTY, (StyleGenerator) t -> "", sg -> grid.getColumns().forEach(c -> c.setStyleGenerator(sg))); addGridMethodMenu(cellStyleMenu, - CELL_STYLE_GENERATOR_PROPERTY_TO_STRING, null, + CELL_STYLE_GENERATOR_PROPERTY_TO_STRING, + (StyleGenerator) t -> null, sg -> grid.getColumns().forEach(c -> c.setStyleGenerator( t -> c.getCaption().replaceAll(" ", "-")))); - addGridMethodMenu(cellStyleMenu, CELL_STYLE_GENERATOR_SPECIAL, null, + addGridMethodMenu(cellStyleMenu, CELL_STYLE_GENERATOR_SPECIAL, + (StyleGenerator) t -> null, sg -> grid.getColumns().forEach(c -> c.setStyleGenerator(t -> { if (t.getRowNumber() % 4 == 1) { return null; -- cgit v1.2.3