From: Anna Koskinen Date: Fri, 5 Mar 2021 11:05:22 +0000 (+0200) Subject: Merged column headers should work within declarative Grid. (#12206) (#12223) X-Git-Tag: 8.12.4~7 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=82ee2b665c7aef68605780391be2b03e1b79583d;p=vaadin-framework.git Merged column headers should work within declarative Grid. (#12206) (#12223) * Merged column headers should work within declarative Grid. (#12206) - null check to prevent NPE from the merged column headers - convert to internal columnIds for merge handling - use correct cell in merge handling - switch away from streams in merge handling for easier readability - regression test Fixes: #10464 --- diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index eb90328725..d4914ad175 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -4750,7 +4750,8 @@ public class Grid extends AbstractListing implements HasComponents, for (Column c : getColumns()) { HeaderCell headerCell = getDefaultHeaderRow().getCell(c); if (headerCell.getCellType() == GridStaticCellType.TEXT) { - c.setCaption(headerCell.getText()); + String text = headerCell.getText(); + c.setCaption(text == null ? "" : text); } } } diff --git a/server/src/main/java/com/vaadin/ui/components/grid/StaticSection.java b/server/src/main/java/com/vaadin/ui/components/grid/StaticSection.java index 29af8bfc6c..96e374253a 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/StaticSection.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/StaticSection.java @@ -290,18 +290,22 @@ public abstract class StaticSection> + columnIdsString + "'"); } - Stream.of(columnIds).forEach(this::addCell); - - Stream idsStream = Stream.of(columnIds); + CELL cell; if (colspan > 1) { - CELL newCell = createCell(); - addMergedCell(createCell(), - idsStream.collect(Collectors.toSet())); - newCell.readDesign(element, designContext); + Set columnGroup = new HashSet<>(); + for (String columnId : columnIds) { + addCell(columnId); + // convert the public columnIds into internal columnIds + columnGroup.add(getCell(columnId).getColumnId()); + } + cell = createCell(); + addMergedCell(cell, columnGroup); } else { - idsStream.map(this::getCell).forEach( - cell -> cell.readDesign(element, designContext)); + String columnId = columnIds[0]; + addCell(columnId); + cell = getCell(columnId); } + cell.readDesign(element, designContext); } } diff --git a/uitest/src/main/java/com/vaadin/tests/declarative/DeclarativeGrid.java b/uitest/src/main/java/com/vaadin/tests/declarative/DeclarativeGrid.java new file mode 100644 index 0000000000..082d99b309 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/declarative/DeclarativeGrid.java @@ -0,0 +1,65 @@ +package com.vaadin.tests.declarative; + +import static java.nio.charset.StandardCharsets.UTF_8; + +import java.io.ByteArrayInputStream; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.declarative.Design; +import com.vaadin.ui.declarative.DesignContext; + +public class DeclarativeGrid extends AbstractTestUI { + + private String design = "" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + " \n" + // + "
Project and StatusDate
Customer Project 1OK2020-12-31
Customer Project 2OK2020-07-02
Customer Project 3OK2019-10-01
\n" + // + "
"; + + @Override + protected void setup(VaadinRequest request) { + DesignContext dc = Design + .read(new ByteArrayInputStream(design.getBytes(UTF_8)), null); + addComponent(dc.getRootComponent()); + } + + @Override + protected Integer getTicketNumber() { + return 10464; + } + + @Override + protected String getTestDescription() { + return "Merged column header should not cause an exception."; + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/declarative/DeclarativeGridTest.java b/uitest/src/test/java/com/vaadin/tests/declarative/DeclarativeGridTest.java new file mode 100644 index 0000000000..5babbae75b --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/declarative/DeclarativeGridTest.java @@ -0,0 +1,19 @@ +package com.vaadin.tests.declarative; + +import org.junit.Test; + +import com.vaadin.testbench.By; +import com.vaadin.testbench.elements.GridElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class DeclarativeGridTest extends MultiBrowserTest { + + @Test + public void testMergedHeaderCell() { + openTestURL(); + waitForElementPresent(By.className("v-label")); + // ensure the grid gets loaded and has the merged header + GridElement grid = $(GridElement.class).first(); + grid.getHeaderCellByCaption("Project and Status"); + } +}