diff options
author | Aleksi Hietanen <aleksi@vaadin.com> | 2016-09-15 09:51:22 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-09-20 06:03:52 +0000 |
commit | 7476bdb00fb112eecaf503a9d4f6a33bce5618bf (patch) | |
tree | d693d1e16c9af90d478efd4488a811c95c54c701 | |
parent | edd8b9bd16a33fe6be3e5082c953aaf374884070 (diff) | |
download | vaadin-framework-7476bdb00fb112eecaf503a9d4f6a33bce5618bf.tar.gz vaadin-framework-7476bdb00fb112eecaf503a9d4f6a33bce5618bf.zip |
Move ItemStyleProvider out of ComboBox and unify with Grid
Change-Id: Ic49fdbf651e9e3ef4ffd6944de597c2fd2f185da
4 files changed, 115 insertions, 79 deletions
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 @@ -96,20 +96,6 @@ public class ComboBox<T> extends AbstractSingleSelect<T> implements HasValue<T>, } /** - * 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 <tt>v-filterselect-item-[style name]</tt>. - * - * @see ComboBox#setItemStyleProvider(ItemStyleProvider) - * @param <T> - * item type in the combo box - */ - @FunctionalInterface - public interface ItemStyleProvider<T> - extends Function<T, String>, Serializable { - } - - /** * ItemIconProvider can be used to add custom icons to combo box items shown * in the popup. * @@ -176,7 +162,7 @@ public class ComboBox<T> extends AbstractSingleSelect<T> implements HasValue<T>, private ItemCaptionProvider<T> itemCaptionProvider = String::valueOf; - private ItemStyleProvider<T> itemStyleProvider = item -> null; + private StyleGenerator<T> itemStyleGenerator = item -> null; private ItemIconProvider<T> itemIconProvider = item -> null; private ItemFilter<T> filter = (filterText, item) -> { @@ -263,7 +249,7 @@ public class ComboBox<T> extends AbstractSingleSelect<T> implements HasValue<T>, 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<T> extends AbstractSingleSelect<T> implements HasValue<T>, } /** - * 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 <tt>v-filterselect-item-[style name]</tt>. 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 <tt>v-filterselect-item-[style name]</tt>. 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<T> itemStyleProvider) { - Objects.requireNonNull(itemStyleProvider, - "Item style providers must not be null"); - this.itemStyleProvider = itemStyleProvider; + public void setStyleGenerator(StyleGenerator<T> 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<T> getItemStyleProvider() { - return itemStyleProvider; + public StyleGenerator<T> 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 @@ -243,17 +243,6 @@ public class Grid<T> extends AbstractSingleSelect<T> implements HasComponents { } /** - * A callback interface for generating style names for an item. - * - * @param <T> - * the grid bean type - */ - @FunctionalInterface - public interface StyleGenerator<T> - extends Function<T, String>, Serializable { - } - - /** * A callback interface for generating description texts for an item. * * @param <T> @@ -518,7 +507,7 @@ public class Grid<T> extends AbstractSingleSelect<T> implements HasComponents { private Function<SortDirection, Stream<SortOrder<String>>> sortOrderProvider; private Comparator<T> comparator; - private StyleGenerator<T> styleGenerator; + private StyleGenerator<T> styleGenerator = item -> null; private DescriptionGenerator<T> descriptionGenerator; /** @@ -619,13 +608,11 @@ public class Grid<T> extends AbstractSingleSelect<T> 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<T> extends AbstractSingleSelect<T> 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 <code>null</code> 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<T, V> setStyleGenerator( StyleGenerator<T> 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<T> extends AbstractSingleSelect<T> implements HasComponents { * Gets the style generator that is used for generating styles for * cells. * - * @return the cell style generator, or <code>null</code> if no - * generator is set + * @return the cell style generator */ public StyleGenerator<T> getStyleGenerator() { return styleGenerator; @@ -907,7 +897,7 @@ public class Grid<T> extends AbstractSingleSelect<T> implements HasComponents { private List<SortOrder<Column<T, ?>>> sortOrder = new ArrayList<>(); private DetailsManager<T> detailsManager; private Set<Component> extensionComponents = new HashSet<>(); - private StyleGenerator<T> styleGenerator; + private StyleGenerator<T> styleGenerator = item -> null; private DescriptionGenerator<T> descriptionGenerator; /** @@ -920,11 +910,9 @@ public class Grid<T> extends AbstractSingleSelect<T> 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<T> extends AbstractSingleSelect<T> 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 <code>null</code> 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<T> 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 <code>null</code> if no generator is - * set + * @return the row style generator */ public StyleGenerator<T> 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 <T> + * the item type + * + * @since 8.0 + * @author Vaadin Ltd + */ +@FunctionalInterface +public interface StyleGenerator<T> extends Function<T, String>, 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<DataObject>) 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<DataObject>) null, + (StyleGenerator<DataObject>) t -> null, sg -> grid.getColumns().forEach(c -> c.setStyleGenerator(sg))); addGridMethodMenu(cellStyleMenu, CELL_STYLE_GENERATOR_EMPTY, (StyleGenerator<DataObject>) 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<DataObject>) 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<DataObject>) t -> null, sg -> grid.getColumns().forEach(c -> c.setStyleGenerator(t -> { if (t.getRowNumber() % 4 == 1) { return null; |