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;
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;
+ }
+ }
}
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;
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.
*
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,
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);
}
}
}
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.
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);
+ }
+ }
});
}
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);
}
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) {
--- /dev/null
+/*
+ * 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);
+ }
+}