]> source.dussan.org Git - vaadin-framework.git/commitdiff
Move ItemStyleProvider out of ComboBox and unify with Grid
authorAleksi Hietanen <aleksi@vaadin.com>
Thu, 15 Sep 2016 06:51:22 +0000 (09:51 +0300)
committerVaadin Code Review <review@vaadin.com>
Tue, 20 Sep 2016 06:03:52 +0000 (06:03 +0000)
Change-Id: Ic49fdbf651e9e3ef4ffd6944de597c2fd2f185da

server/src/main/java/com/vaadin/ui/ComboBox.java
server/src/main/java/com/vaadin/ui/Grid.java
server/src/main/java/com/vaadin/ui/StyleGenerator.java [new file with mode: 0644]
uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java

index 8fdc8452ed7b81b539004b191dc09454e87d8ac3..0b1b4923f85a6725ce20b031c9769183a17e5ecc 100644 (file)
@@ -95,20 +95,6 @@ public class ComboBox<T> extends AbstractSingleSelect<T> implements HasValue<T>,
             extends Function<T, String>, 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 <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;
     }
 
     /**
index d776906afdb91dea37a6116f0e82832e5d9d9de4..806f0d270fb3b410e1e90b859d38bd9d6fbc9302 100644 (file)
@@ -242,17 +242,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.
      *
@@ -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 (file)
index 0000000..ddec2a2
--- /dev/null
@@ -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);
+}
index 0278149be939e9d612b59616ab0396fbb2e0e56c..3a0646ed273edf5ccded594d7ba53378966a57e5 100644 (file)
@@ -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;