Browse Source

Setting of tooltips for grid header/footer cells (#10489)

Fixes #7527
tags/8.4.0.alpha1
Artur 6 years ago
parent
commit
e813c97e0b

+ 29
- 1
client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java View File

@@ -59,6 +59,7 @@ import com.vaadin.client.widgets.Grid.Column;
import com.vaadin.client.widgets.Grid.FooterRow;
import com.vaadin.client.widgets.Grid.HeaderRow;
import com.vaadin.client.widgets.Grid.SelectionColumn;
import com.vaadin.client.widgets.Grid.StaticSection.StaticCell;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.shared.data.sort.SortDirection;
import com.vaadin.shared.ui.Connect;
@@ -457,6 +458,8 @@ public class GridConnector extends AbstractListingConnector
"unexpected cell type: " + cellState.type);
}
cell.setStyleName(cellState.styleName);
cell.setDescription(cellState.description);
cell.setDescriptionContentMode(cellState.descriptionContentMode);
}

/**
@@ -677,7 +680,10 @@ public class GridConnector extends AbstractListingConnector

if (cell != null) {
JsonObject row = cell.getRow();

TooltipInfo tooltip = getHeaderFooterTooltip(cell);
if (tooltip != null) {
return tooltip;
}
if (row != null && (row.hasKey(GridState.JSONKEY_ROWDESCRIPTION)
|| row.hasKey(GridState.JSONKEY_CELLDESCRIPTION))) {

@@ -708,6 +714,28 @@ public class GridConnector extends AbstractListingConnector
return null;
}

private TooltipInfo getHeaderFooterTooltip(CellReference cell) {
Section section = Section.BODY;
if (cell instanceof EventCellReference) {
// Header or footer
section = ((EventCellReference) cell).getSection();
}
StaticCell staticCell = null;
if (section == Section.HEADER) {
staticCell = getWidget().getHeaderRow(cell.getRowIndex())
.getCell(cell.getColumn());
} else if (section == Section.FOOTER) {
staticCell = getWidget().getFooterRow(cell.getRowIndex())
.getCell(cell.getColumn());
}
if (staticCell != null && staticCell.getDescription() != null) {
return new TooltipInfo(staticCell.getDescription(),
staticCell.getDescriptionContentMode());
}

return null;
}

@Override
protected void sendContextClickEvent(MouseEventDetails details,
EventTarget eventTarget) {

+ 78
- 2
client/src/main/java/com/vaadin/client/widgets/Grid.java View File

@@ -176,6 +176,7 @@ import com.vaadin.client.widgets.Grid.StaticSection.StaticRow;
import com.vaadin.shared.Range;
import com.vaadin.shared.Registration;
import com.vaadin.shared.data.sort.SortDirection;
import com.vaadin.shared.ui.ContentMode;
import com.vaadin.shared.ui.grid.ColumnResizeMode;
import com.vaadin.shared.ui.grid.GridConstants;
import com.vaadin.shared.ui.grid.GridConstants.Section;
@@ -258,6 +259,10 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,

private String styleName = null;

private String description = null;

private ContentMode descriptionContentMode = ContentMode.TEXT;

/**
* Sets the text displayed in this cell.
*
@@ -426,11 +431,82 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
* @since 7.6.3
*/
void detach() {
if (this.content instanceof Widget) {
if (content instanceof Widget) {
// Widget in the cell, detach it
section.getGrid().detachWidget((Widget) this.content);
section.getGrid().detachWidget((Widget) content);
}
}

/**
* Gets the tooltip for the cell.
* <p>
* The tooltip is shown in the mode returned by
* {@link #getDescriptionContentMode()}.
*
* @since
*/
public String getDescription() {
return description;
}

/**
* Sets the tooltip for the cell.
* <p>
* By default, tooltips are shown as plain text. For HTML tooltips,
* see {@link #setDescription(String, ContentMode)} or
* {@link #setDescriptionContentMode(ContentMode)}.
*
* @param description
* the tooltip to show when hovering the cell
* @since
*/
public void setDescription(String description) {
this.description = description;
}

/**
* Sets the tooltip for the cell to be shown with the given content
* mode.
*
* @see ContentMode
* @param description
* the tooltip to show when hovering the cell
* @param descriptionContentMode
* the content mode to use for the tooltip (HTML or plain
* text)
* @since
*/
public void setDescription(String description,
ContentMode descriptionContentMode) {
setDescription(description);
setDescriptionContentMode(descriptionContentMode);
}

/**
* Gets the content mode for the tooltip.
* <p>
* The content mode determines how the tooltip is shown.
*
* @see ContentMode
* @return the content mode for the tooltip
* @since
*/
public ContentMode getDescriptionContentMode() {
return descriptionContentMode;
}

/**
* Sets the content mode for the tooltip.
*
* @see ContentMode
* @param descriptionContentMode
* the content mode for the tooltip
* @since
*/
public void setDescriptionContentMode(
ContentMode descriptionContentMode) {
this.descriptionContentMode = descriptionContentMode;
}
}

/**

+ 57
- 0
server/src/main/java/com/vaadin/ui/components/grid/FooterCell.java View File

@@ -17,6 +17,7 @@ package com.vaadin.ui.components.grid;

import java.io.Serializable;

import com.vaadin.shared.ui.ContentMode;
import com.vaadin.shared.ui.grid.GridStaticCellType;
import com.vaadin.ui.Component;

@@ -102,4 +103,60 @@ public interface FooterCell extends Serializable {
* the style name to set or null to not use any style name
*/
public void setStyleName(String styleName);

/**
* Gets the tooltip for the cell.
* <p>
* The tooltip is shown in the mode returned by
* {@link #getDescriptionContentMode()}.
*
* @since
*/
public String getDescription();

/**
* Sets the tooltip for the cell.
* <p>
* By default, tooltips are shown as plain text. For HTML tooltips, see
* {@link #setDescription(String, ContentMode)} or
* {@link #setDescriptionContentMode(ContentMode)}.
*
* @param description
* the tooltip to show when hovering the cell
* @since
*/
public void setDescription(String description);

/**
* Sets the tooltip for the cell to be shown with the given content mode.
*
* @see ContentMode
* @param description
* the tooltip to show when hovering the cell
* @param descriptionContentMode
* the content mode to use for the tooltip (HTML or plain text)
* @since
*/
public void setDescription(String description,
ContentMode descriptionContentMode);

/**
* Gets the content mode for the tooltip.

* @see ContentMode
* @return the content mode for the tooltip
* @since
*/
public ContentMode getDescriptionContentMode();

/**
* Sets the content mode for the tooltip.
*
* @see ContentMode
* @param descriptionContentMode
* the content mode for the tooltip
* @since
*/
public void setDescriptionContentMode(ContentMode descriptionContentMode);

}

+ 57
- 0
server/src/main/java/com/vaadin/ui/components/grid/HeaderCell.java View File

@@ -17,6 +17,7 @@ package com.vaadin.ui.components.grid;

import java.io.Serializable;

import com.vaadin.shared.ui.ContentMode;
import com.vaadin.shared.ui.grid.GridStaticCellType;
import com.vaadin.ui.Component;

@@ -102,4 +103,60 @@ public interface HeaderCell extends Serializable {
* the style name to set or null to not use any style name
*/
public void setStyleName(String styleName);

/**
* Gets the tooltip for the cell.
* <p>
* The tooltip is shown in the mode returned by
* {@link #getDescriptionContentMode()}.
*
* @since
*/
public String getDescription();

/**
* Sets the tooltip for the cell.
* <p>
* By default, tooltips are shown as plain text. For HTML tooltips, see
* {@link #setDescription(String, ContentMode)} or
* {@link #setDescriptionContentMode(ContentMode)}.
*
* @param description
* the tooltip to show when hovering the cell
* @since
*/
public void setDescription(String description);

/**
* Sets the tooltip for the cell to be shown with the given content mode.
*
* @see ContentMode
* @param description
* the tooltip to show when hovering the cell
* @param descriptionContentMode
* the content mode to use for the tooltip (HTML or plain text)
* @since
*/
public void setDescription(String description,
ContentMode descriptionContentMode);

/**
* Gets the content mode for the tooltip.
* <p>
*
* @see ContentMode
* @return the content mode for the tooltip
* @since
*/
public ContentMode getDescriptionContentMode();

/**
* Sets the content mode for the tooltip.
*
* @see ContentMode
* @param descriptionContentMode
* the content mode for the tooltip
* @since
*/
public void setDescriptionContentMode(ContentMode descriptionContentMode);
}

+ 70
- 0
server/src/main/java/com/vaadin/ui/components/grid/StaticSection.java View File

@@ -35,6 +35,7 @@ import java.util.stream.Stream;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.vaadin.shared.ui.ContentMode;
import com.vaadin.shared.ui.grid.GridStaticCellType;
import com.vaadin.shared.ui.grid.SectionState;
import com.vaadin.shared.ui.grid.SectionState.CellState;
@@ -615,6 +616,75 @@ public abstract class StaticSection<ROW extends StaticSection.StaticRow<?>>
void detach() {
removeComponentIfPresent();
}

/**
* Gets the tooltip for the cell.
* <p>
* The tooltip is shown in the mode returned by
* {@link #getDescriptionContentMode()}.
*
* @since
*/
public String getDescription() {
return cellState.description;
}

/**
* Sets the tooltip for the cell.
* <p>
* By default, tooltips are shown as plain text. For HTML tooltips, see
* {@link #setDescription(String, ContentMode)} or
* {@link #setDescriptionContentMode(ContentMode)}.
*
* @param description
* the tooltip to show when hovering the cell
* @since
*/
public void setDescription(String description) {
cellState.description = description;
}

/**
* Sets the tooltip for the cell to be shown with the given content
* mode.
*
* @see ContentMode
* @param description
* the tooltip to show when hovering the cell
* @param descriptionContentMode
* the content mode to use for the tooltip (HTML or plain
* text)
* @since
*/
public void setDescription(String description,
ContentMode descriptionContentMode) {
setDescription(description);
setDescriptionContentMode(descriptionContentMode);
}

/**
* Gets the content mode for the tooltip.
*
* @see ContentMode
* @return the content mode for the tooltip
* @since
*/
public ContentMode getDescriptionContentMode() {
return cellState.descriptionContentMode;
}

/**
* Sets the content mode for the tooltip.
*
* @see ContentMode
* @param descriptionContentMode
* the content mode for the tooltip
* @since
*/
public void setDescriptionContentMode(
ContentMode descriptionContentMode) {
cellState.descriptionContentMode = descriptionContentMode;
}
}

private final List<ROW> rows = new ArrayList<>();

+ 7
- 0
shared/src/main/java/com/vaadin/shared/ui/grid/SectionState.java View File

@@ -23,6 +23,7 @@ import java.util.Map;
import java.util.Set;

import com.vaadin.shared.Connector;
import com.vaadin.shared.ui.ContentMode;

/**
* Shared state for Grid headers and footers.
@@ -75,6 +76,12 @@ public class SectionState implements Serializable {

/** The id of the column that this cell belongs to. */
public String columnId;

/** The tooltip for the cell */
public String description;

/** The content mode for the tooltip for the cell */
public ContentMode descriptionContentMode = ContentMode.TEXT;
}

/** The rows in this section. */

+ 113
- 0
uitest/src/main/java/com/vaadin/tests/components/grid/GridHeaderFooterTooltip.java View File

@@ -0,0 +1,113 @@
/*
* 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;

import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.shared.ui.ContentMode;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.tests.data.bean.Person;
import com.vaadin.ui.Button;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.Column;
import com.vaadin.ui.Grid.SelectionMode;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.components.grid.FooterRow;
import com.vaadin.ui.components.grid.HeaderCell;
import com.vaadin.ui.components.grid.HeaderRow;

@Widgetset("com.vaadin.DefaultWidgetSet")
public class GridHeaderFooterTooltip extends AbstractTestUI {

private static final long serialVersionUID = -2787771187365766027L;

private HeaderRow row;

@Override
protected void setup(VaadinRequest request) {
Grid<Person> grid = new Grid<>(Person.class);
grid.setWidth("600px");

grid.setSelectionMode(SelectionMode.SINGLE);
addComponent(grid);

grid.getDefaultHeaderRow().getCell("firstName").setDescription(
"Text: Header tooltip for <b>first</b> name", ContentMode.TEXT);
grid.getDefaultHeaderRow().getCell("lastName").setDescription(
"HTML: Header tooltip for <b>last</b> name", ContentMode.HTML);
grid.getDefaultHeaderRow().getCell("deceased").setDescription(
"PRE\nHeader tooltip for\n<b>deceased</b>",
ContentMode.PREFORMATTED);
grid.setDescription("Tooltip for the whole grid");

FooterRow footer = grid.addFooterRowAt(0);
footer.getCell("firstName").setDescription(
"Text: Footer tooltip for <b>first</b> name", ContentMode.TEXT);
footer.getCell("lastName").setDescription(
"HTML: Footer tooltip for <b>last</b> name", ContentMode.HTML);
footer.getCell("deceased").setDescription(
"PRE\nFooter tooltip for\n<b>deceased</b>",
ContentMode.PREFORMATTED);

grid.setItems(Person.createTestPerson1(), Person.createTestPerson2());

Button showHide = new Button("Hide firstName", event -> {
Column<Person, ?> column = grid.getColumn("firstName");
if (grid.getColumn("firstName") != null) {
grid.removeColumn(column);
event.getButton().setCaption("Show firstName");
} else {
grid.addColumn(Person::getFirstName).setId("firstName");
grid.setColumnOrder(grid.getColumn("firstName"),
grid.getColumn("lastName"),
grid.getColumn("streetAddress"), grid.getColumn("zip"),
grid.getColumn("city"));

event.getButton().setCaption("Hide firstName");
}
});
showHide.setId("show_hide");

Button join = new Button("Add Join header column", event -> {
if (row == null) {
row = grid.prependHeaderRow();
HeaderCell joinedCell = row.join(
grid.getDefaultHeaderRow()
.getCell(grid.getColumn("firstName")),
grid.getDefaultHeaderRow()
.getCell(grid.getColumn("lastName")));
joinedCell.setText("Full Name");
joinedCell.setDescription("Full name tooltip");
} else {
grid.removeHeaderRow(row);
row = null;
}
});
join.setId("join");
addComponent(new HorizontalLayout(showHide, join));
}

@Override
protected String getTestDescription() {
return "Grid for testing header re-rendering.";
}

@Override
protected Integer getTicketNumber() {
return 17131;
}

}

+ 93
- 0
uitest/src/test/java/com/vaadin/tests/components/grid/GridHeaderFooterTooltipTest.java View File

@@ -0,0 +1,93 @@
/*
* 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;

import org.junit.Assert;
import org.junit.Test;

import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.GridElement;
import com.vaadin.testbench.elements.GridElement.GridCellElement;
import com.vaadin.tests.tb3.SingleBrowserTest;

public class GridHeaderFooterTooltipTest extends SingleBrowserTest {

private GridElement grid;

@Override
public void setup() throws Exception {
super.setup();
openTestURL();
grid = $(GridElement.class).first();
}

@Test
public void headerTooltipShown() {
GridCellElement lastName = grid.getHeaderCell(0, 0);
GridCellElement firstName = grid.getHeaderCell(0, 1);
GridCellElement deceased = grid.getHeaderCell(0, 2);

lastName.showTooltip();
Assert.assertEquals("HTML: Header tooltip for last name",
getTooltipElement().getText());

firstName.showTooltip();
Assert.assertEquals("Text: Header tooltip for <b>first</b> name",
getTooltipElement().getText());

deceased.showTooltip();
Assert.assertEquals("PRE\nHeader tooltip for\n<b>deceased</b>",
getTooltipElement().getText());
}

@Test
public void headerWithoutTooltipShowsGridTooltip() {
GridCellElement otherHeader = grid.getHeaderCell(0, 3);

otherHeader.showTooltip();
Assert.assertEquals("Tooltip for the whole grid",
getTooltipElement().getText());

}

@Test
public void joinedHeaderTooltipShown() {
$(ButtonElement.class).id("join").click();
GridCellElement fullName = grid.getHeaderCell(0, 0);
fullName.showTooltip();
Assert.assertEquals("Full name tooltip", getTooltipElement().getText());
}

@Test
public void footerTooltipShown() {
GridCellElement lastName = grid.getFooterCell(0, 0);
GridCellElement firstName = grid.getFooterCell(0, 1);
GridCellElement deceased = grid.getFooterCell(0, 2);

lastName.showTooltip();
Assert.assertEquals("HTML: Footer tooltip for last name",
getTooltipElement().getText());

firstName.showTooltip();
Assert.assertEquals("Text: Footer tooltip for <b>first</b> name",
getTooltipElement().getText());

deceased.showTooltip();
Assert.assertEquals("PRE\nFooter tooltip for\n<b>deceased</b>",
getTooltipElement().getText());

}
}

Loading…
Cancel
Save