diff options
8 files changed, 118 insertions, 2 deletions
diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java index 1c457ec4db..c7b759c7fb 100644 --- a/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java @@ -328,6 +328,20 @@ public class GridConnector extends AbstractListingConnector }); } + @OnStateChange("rowHeight") + void updateRowHeight() { + double rowHeight = getState().rowHeight; + if (rowHeight >= 0) { + getWidget().getEscalator().getHeader() + .setDefaultRowHeight(rowHeight); + getWidget().getEscalator().getBody().setDefaultRowHeight(rowHeight); + getWidget().getEscalator().getFooter() + .setDefaultRowHeight(rowHeight); + } else if (getWidget().isAttached()) { + getWidget().resetSizesFromDom(); + } + } + private void updateStaticRow(RowState rowState, Grid.StaticSection.StaticRow row) { rowState.cells.forEach((columnId, cellState) -> { diff --git a/documentation/components/components-grid.asciidoc b/documentation/components/components-grid.asciidoc index 223be80e6f..f903a962bd 100644 --- a/documentation/components/components-grid.asciidoc +++ b/documentation/components/components-grid.asciidoc @@ -518,7 +518,9 @@ must be between 0.0 and 1.0. [classname]#ComponentRenderer#:: Renders a Vaadin [classname]#Component# in a column. Since components are quite complex, the [classname]#ComponentRenderer# comes with possible performance issues. -To use it efficiently you should use as few nested components as possible. +To use it efficiently you should use as few nested components as possible. If the components used are +of a different size than the default row height, [methodname]#Grid.setRowHeight()# can be used to adjust +the height of all rows in the Grid. + Use [classname]#Button# in [classname]#Grid#: @@ -530,6 +532,8 @@ grid.addColumn(person -> { Notification.show("Clicked: " + person.toString())); return button; }, new ComponentRenderer()); +// make sure the buttons fit in the cells of the Grid +grid.setRowHeight(40); ---- Components will occasionally be generated again during runtime. If you have a state in your diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index 6b1d5bcb79..872e14a44f 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -2738,6 +2738,30 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, } /** + * Sets the height of a row. If -1 (default), the row height is calculated + * based on the theme for an empty row before the Grid is displayed. + * <p> + * Note that all header, body and footer rows get the same height if + * explicitly set. In automatic mode, each section is calculated separately + * based on an empty row of that type. + * + * @param rowHeight + * The height of a row in pixels or -1 for automatic calculation + */ + public void setRowHeight(double rowHeight) { + getState().rowHeight = rowHeight; + } + + /** + * Returns the currently explicitly set row height or -1 if automatic. + * + * @return explicitly set row height in pixels or -1 if in automatic mode + */ + public double getRowHeight() { + return getState(false).rowHeight; + } + + /** * 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. diff --git a/shared/src/main/java/com/vaadin/shared/ui/grid/GridState.java b/shared/src/main/java/com/vaadin/shared/ui/grid/GridState.java index 6725d01137..77744a9bb0 100644 --- a/shared/src/main/java/com/vaadin/shared/ui/grid/GridState.java +++ b/shared/src/main/java/com/vaadin/shared/ui/grid/GridState.java @@ -22,6 +22,7 @@ import java.util.List; import com.vaadin.shared.annotations.DelegateToWidget; import com.vaadin.shared.data.sort.SortDirection; import com.vaadin.shared.ui.AbstractSingleSelectState; + import elemental.json.JsonArray; /** @@ -102,7 +103,7 @@ public class GridState extends AbstractSingleSelectState { /** * Column resize mode in grid. - * + * * @since 7.7.5 */ public ColumnResizeMode columnResizeMode = ColumnResizeMode.ANIMATED; @@ -145,4 +146,12 @@ public class GridState extends AbstractSingleSelectState { @DelegateToWidget public boolean columnReorderingAllowed; + /** + * Explicit row height in pixels for grid rows, or -1 to calculate + * automatically based on the theme. + * + * @since 8.1 + */ + public double rowHeight = -1; + } diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridComponents.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridComponents.java index 2eb48f7697..1a546a767d 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/grid/GridComponents.java +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridComponents.java @@ -50,6 +50,8 @@ public class GridComponents extends AbstractTestUIWithLog { button.setId(string.replace(' ', '_').toLowerCase()); return button; }, new ComponentRenderer()); + // make sure the buttons and focus outlines fit completely in a row + grid.setRowHeight(40); addComponent(grid); grid.setSizeFull(); 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 4c7283dfcc..944f729efe 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 @@ -382,6 +382,10 @@ public class GridBasics extends AbstractTestUIWithLog { MenuItem heightMenu = sizeMenu.addItem("Height", null); Stream.of(50, 100, 200, 400).map(i -> i + "px").forEach( i -> addGridMethodMenu(heightMenu, i, i, grid::setHeight)); + + MenuItem rowHeightMenu = sizeMenu.addItem("Row Height", null); + Stream.of(-1, 20, 40, 100).forEach(i -> addGridMethodMenu(rowHeightMenu, + String.valueOf(i), (double) i, grid::setRowHeight)); } private void createStateMenu(MenuItem stateMenu) { diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridComponentsTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridComponentsTest.java index 89226c147e..b74386e2d9 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/grid/GridComponentsTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/GridComponentsTest.java @@ -72,6 +72,8 @@ public class GridComponentsTest extends MultiBrowserTest { public void testRow0() { openTestURL(); assertRowExists(0, "Row 0"); + Assert.assertEquals("Grid row height is not what it should be", 40, + $(GridElement.class).first().getRow(0).getSize().getHeight()); } @Test diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridRowHeightTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridRowHeightTest.java new file mode 100644 index 0000000000..73811bddc7 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridRowHeightTest.java @@ -0,0 +1,57 @@ +package com.vaadin.tests.components.grid.basics; + +import org.junit.Assert; +import org.junit.Test; + +public class GridRowHeightTest extends GridBasicsTest { + + @Test + public void testRowHeights() { + selectMenuPath("Component", "Footer", "Add default footer row"); + + int initialHeaderHeight = getHeaderHeight(); + int initialBodyRowHeight = getBodyRowHeight(); + int initialFooterHeight = getFooterHeight(); + + // set automatic size and check that no change + setRowHeight(-1); + checkRowHeights(initialHeaderHeight, initialBodyRowHeight, + initialFooterHeight); + + // set explicit size and check height + setRowHeight(20); + checkRowHeights(20, 20, 20); + + // set automatic size and check that initial size + setRowHeight(-1); + checkRowHeights(initialHeaderHeight, initialBodyRowHeight, + initialFooterHeight); + } + + private void checkRowHeights(int expectedHeaderHeight, + int expectedBodyRowHeight, int expectedFooterHeight) { + Assert.assertEquals("Header height does not match expected value", + expectedHeaderHeight, getHeaderHeight()); + Assert.assertEquals("Body row height does not match expected value", + expectedBodyRowHeight, getBodyRowHeight()); + Assert.assertEquals("Footer height does not match expected value", + expectedFooterHeight, getFooterHeight()); + } + + private void setRowHeight(int height) { + selectMenuPath("Component", "Size", "Row Height", "" + height); + } + + private int getHeaderHeight() { + return getGridElement().getHeader().getSize().getHeight(); + } + + private int getBodyRowHeight() { + return getGridElement().getRow(0).getSize().getHeight(); + } + + private int getFooterHeight() { + return getGridElement().getFooter().getSize().getHeight(); + } + +} |