]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add DescriptionGenerators for Grid and Columns
authorTeemu Suo-Anttila <teemusa@vaadin.com>
Tue, 6 Sep 2016 14:00:26 +0000 (17:00 +0300)
committerArtur Signell <artur@vaadin.com>
Thu, 8 Sep 2016 11:33:37 +0000 (11:33 +0000)
Change-Id: Ib32726ba3297a05cbc05898f37579777b9a921e0

client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java
server/src/main/java/com/vaadin/ui/Grid.java
uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java
uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridDescriptionGeneratorTest.java [new file with mode: 0644]

index 880de07d555318eecc5d3d86b2bb8798b53cf00e..006aa65318b5cf9d4c37e0275114251bef8a00bc 100644 (file)
@@ -21,15 +21,18 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import com.google.gwt.dom.client.Element;
 import com.google.gwt.event.shared.HandlerRegistration;
 import com.vaadin.client.ComponentConnector;
 import com.vaadin.client.ConnectorHierarchyChangeEvent;
 import com.vaadin.client.ConnectorHierarchyChangeEvent.ConnectorHierarchyChangeHandler;
 import com.vaadin.client.DeferredWorker;
 import com.vaadin.client.HasComponentsConnector;
+import com.vaadin.client.TooltipInfo;
 import com.vaadin.client.connectors.AbstractListingConnector;
 import com.vaadin.client.data.DataSource;
 import com.vaadin.client.ui.SimpleManagedLayout;
+import com.vaadin.client.widget.grid.CellReference;
 import com.vaadin.client.widget.grid.selection.ClickSelectHandler;
 import com.vaadin.client.widget.grid.selection.SpaceSelectHandler;
 import com.vaadin.client.widget.grid.sort.SortEvent;
@@ -235,4 +238,44 @@ public class GridConnector
             clickSelectHandler = null;
         }
     }
+
+    @Override
+    public boolean hasTooltip() {
+        // Always check for generated descriptions.
+        return true;
+    }
+
+    @Override
+    public TooltipInfo getTooltipInfo(Element element) {
+        CellReference<JsonObject> cell = getWidget().getCellReference(element);
+
+        if (cell != null) {
+            JsonObject row = cell.getRow();
+
+            if (row != null && (row.hasKey(GridState.JSONKEY_ROWDESCRIPTION)
+                    || row.hasKey(GridState.JSONKEY_CELLDESCRIPTION))) {
+
+                Column<?, JsonObject> column = cell.getColumn();
+                if (columnToIdMap.containsKey(column)) {
+                    JsonObject cellDescriptions = row
+                            .getObject(GridState.JSONKEY_CELLDESCRIPTION);
+
+                    String id = columnToIdMap.get(column);
+                    if (cellDescriptions != null
+                            && cellDescriptions.hasKey(id)) {
+                        return new TooltipInfo(cellDescriptions.getString(id));
+                    } else if (row.hasKey(GridState.JSONKEY_ROWDESCRIPTION)) {
+                        return new TooltipInfo(row
+                                .getString(GridState.JSONKEY_ROWDESCRIPTION));
+                    }
+                }
+            }
+        }
+
+        if (super.hasTooltip()) {
+            return super.getTooltipInfo(element);
+        } else {
+            return null;
+        }
+    }
 }
index 96d73768162c6036f2ed8f8d0db86895efec8c0d..4a12099977ca141cdc941ddf868d8ec06da42a33 100644 (file)
@@ -24,6 +24,7 @@ import java.util.Comparator;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -73,6 +74,17 @@ public class Grid<T> extends AbstractListing<T, SelectionModel<T>>
             extends Function<T, String>, Serializable {
     }
 
+    /**
+     * A callback interface for generating description texts for an item.
+     *
+     * @param <T>
+     *            the grid bean type
+     */
+    @FunctionalInterface
+    public interface DescriptionGenerator<T>
+            extends Function<T, String>, Serializable {
+    }
+
     /**
      * A callback interface for generating details for a particular row in Grid.
      *
@@ -320,6 +332,7 @@ public class Grid<T> extends AbstractListing<T, SelectionModel<T>>
         private Function<SortDirection, Stream<SortOrder<String>>> sortOrderProvider;
         private Comparator<T> comparator;
         private StyleGenerator<T> styleGenerator;
+        private DescriptionGenerator<T> descriptionGenerator;
 
         /**
          * Constructs a new Column configuration with given header caption,
@@ -424,7 +437,15 @@ public class Grid<T> extends AbstractListing<T, SelectionModel<T>>
                 if (style != null && !style.isEmpty()) {
                     JsonObject styleObj = getDataObject(jsonObject,
                             GridState.JSONKEY_CELLSTYLES);
-                    styleObj.put(getState(false).id, style);
+                    styleObj.put(communicationId, style);
+                }
+            }
+            if (descriptionGenerator != null) {
+                String description = descriptionGenerator.apply(data);
+                if (description != null && !description.isEmpty()) {
+                    JsonObject descriptionObj = getDataObject(jsonObject,
+                            GridState.JSONKEY_CELLDESCRIPTION);
+                    descriptionObj.put(communicationId, description);
                 }
             }
         }
@@ -627,14 +648,42 @@ public class Grid<T> extends AbstractListing<T, SelectionModel<T>>
         public StyleGenerator<T> getStyleGenerator() {
             return styleGenerator;
         }
+
+        /**
+         * Sets the description generator that is used for generating
+         * descriptions for cells in this column.
+         *
+         * @param cellDescriptionGenerator
+         *            the cell description generator to set, or
+         *            <code>null</code> to remove a previously set generator
+         * @return this column
+         */
+        public Column<T, V> setDescriptionGenerator(
+                DescriptionGenerator<T> cellDescriptionGenerator) {
+            this.descriptionGenerator = cellDescriptionGenerator;
+            getParent().getDataCommunicator().reset();
+            return this;
+        }
+
+        /**
+         * Gets the description generator that is used for generating
+         * descriptions for cells.
+         *
+         * @return the cell description generator, or <code>null</code> if no
+         *         generator is set
+         */
+        public DescriptionGenerator<T> getDescriptionGenerator() {
+            return descriptionGenerator;
+        }
     }
 
     private KeyMapper<Column<T, ?>> columnKeys = new KeyMapper<>();
-    private Set<Column<T, ?>> columnSet = new HashSet<>();
+    private Set<Column<T, ?>> columnSet = new LinkedHashSet<>();
     private List<SortOrder<Column<T, ?>>> sortOrder = new ArrayList<>();
     private DetailsManager<T> detailsManager;
     private Set<Component> extensionComponents = new HashSet<>();
     private StyleGenerator<T> styleGenerator;
+    private DescriptionGenerator<T> descriptionGenerator;
 
     /**
      * Constructor for the {@link Grid} component.
@@ -652,6 +701,12 @@ public class Grid<T> extends AbstractListing<T, SelectionModel<T>>
                     json.put(GridState.JSONKEY_ROWSTYLE, styleName);
                 }
             }
+            if (descriptionGenerator != null) {
+                String description = descriptionGenerator.apply(item);
+                if (description != null && !description.isEmpty()) {
+                    json.put(GridState.JSONKEY_ROWDESCRIPTION, description);
+                }
+            }
         });
     }
 
@@ -922,6 +977,31 @@ public class Grid<T> extends AbstractListing<T, SelectionModel<T>>
         return styleGenerator;
     }
 
+    /**
+     * Sets the description generator that is used for generating descriptions
+     * for rows.
+     *
+     * @param descriptionGenerator
+     *            the row description generator to set, or <code>null</code> to
+     *            remove a previously set generator
+     */
+    public void setDescriptionGenerator(
+            DescriptionGenerator<T> descriptionGenerator) {
+        this.descriptionGenerator = descriptionGenerator;
+        getDataCommunicator().reset();
+    }
+
+    /**
+     * Gets the description generator that is used for generating descriptions
+     * for rows.
+     *
+     * @return the row description generator, or <code>null</code> if no
+     *         generator is set
+     */
+    public DescriptionGenerator<T> getDescriptionGenerator() {
+        return descriptionGenerator;
+    }
+
     @Override
     protected GridState getState() {
         return getState(true);
index 5c9f03336e812e64797d3c1ead229c86f1f07e68..8c4aa4eec05200dea0de335cfbc3db3e64941161 100644 (file)
@@ -188,6 +188,19 @@ public class GridBasics extends AbstractTestUIWithLog {
         }
         createRowStyleMenu(stateMenu.addItem("Row style generator", null));
         createCellStyleMenu(stateMenu.addItem("Cell style generator", null));
+        stateMenu.addItem("Row description generator",
+                item -> grid.setDescriptionGenerator(item.isChecked()
+                        ? t -> "Row tooltip for row " + t.getRowNumber()
+                        : 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)))
+                .setCheckable(true);
     }
 
     private void createRowStyleMenu(MenuItem rowStyleMenu) {
diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridDescriptionGeneratorTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridDescriptionGeneratorTest.java
new file mode 100644 (file)
index 0000000..58ded39
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * 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.tests.components.grid.basics;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+import org.openqa.selenium.By;
+
+public class GridDescriptionGeneratorTest extends GridBasicsTest {
+
+    @Test
+    public void testCellDescription() {
+        openTestURL();
+        selectMenuPath("Component", "State", "Cell description generator");
+
+        getGridElement().getCell(1, 0).showTooltip();
+        String tooltipText = findElement(By.className("v-tooltip-text"))
+                .getText();
+        assertEquals("Tooltip text", "Cell tooltip for row 1, Column 0",
+                tooltipText);
+
+        getGridElement().getCell(1, 1).showTooltip();
+        assertTrue("Tooltip should not be present in cell (1, 1) ",
+                findElement(By.className("v-tooltip-text")).getText()
+                        .isEmpty());
+    }
+
+    @Test
+    public void testRowDescription() {
+        openTestURL();
+        selectMenuPath("Component", "State", "Row description generator");
+
+        getGridElement().getCell(5, 3).showTooltip();
+        String tooltipText = findElement(By.className("v-tooltip-text"))
+                .getText();
+        assertEquals("Tooltip text", "Row tooltip for row 5", tooltipText);
+
+        getGridElement().getCell(15, 3).showTooltip();
+        tooltipText = findElement(By.className("v-tooltip-text")).getText();
+        assertEquals("Tooltip text", "Row tooltip for row 15", tooltipText);
+    }
+
+    @Test
+    public void testRowAndCellDescription() {
+        openTestURL();
+        selectMenuPath("Component", "State", "Row description generator");
+        selectMenuPath("Component", "State", "Cell description generator");
+
+        getGridElement().getCell(5, 0).showTooltip();
+        String tooltipText = findElement(By.className("v-tooltip-text"))
+                .getText();
+        assertEquals("Tooltip text", "Cell tooltip for row 5, Column 0",
+                tooltipText);
+
+        getGridElement().getCell(5, 3).showTooltip();
+        tooltipText = findElement(By.className("v-tooltip-text")).getText();
+        assertEquals("Tooltip text", "Row tooltip for row 5", tooltipText);
+    }
+}