From e8f7de722a7561e25c6bfd1fb007f498db22b1c7 Mon Sep 17 00:00:00 2001 From: Teemu Suo-Anttila Date: Wed, 19 Jul 2017 09:34:05 +0300 Subject: Allow null Components in ComponentRenderer (#9692) --- .../connectors/grid/ComponentRendererConnector.java | 10 +++++++--- .../java/com/vaadin/ui/renderers/ComponentRenderer.java | 7 ++++--- .../com/vaadin/tests/components/grid/GridComponents.java | 3 +++ .../vaadin/tests/components/grid/GridComponentsTest.java | 16 ++++++++++++++++ 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/ComponentRendererConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/ComponentRendererConnector.java index c094437538..6315f119cc 100644 --- a/client/src/main/java/com/vaadin/client/connectors/grid/ComponentRendererConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/grid/ComponentRendererConnector.java @@ -51,9 +51,13 @@ public class ComponentRendererConnector @Override public void render(RendererCellReference cell, String connectorId, SimplePanel widget) { - ComponentConnector connector = (ComponentConnector) ConnectorMap - .get(getConnection()).getConnector(connectorId); - widget.setWidget(connector.getWidget()); + if (connectorId != null) { + ComponentConnector connector = (ComponentConnector) ConnectorMap + .get(getConnection()).getConnector(connectorId); + widget.setWidget(connector.getWidget()); + } else if (widget.getWidget() != null) { + widget.remove(widget.getWidget()); + } } }; } diff --git a/server/src/main/java/com/vaadin/ui/renderers/ComponentRenderer.java b/server/src/main/java/com/vaadin/ui/renderers/ComponentRenderer.java index f5dd507cff..0f8ca4401d 100644 --- a/server/src/main/java/com/vaadin/ui/renderers/ComponentRenderer.java +++ b/server/src/main/java/com/vaadin/ui/renderers/ComponentRenderer.java @@ -38,10 +38,11 @@ import elemental.json.JsonValue; * instances. *

* Example of how to add a {@link Label} component to {@link Grid}: + * *

  * Grid grid;
- * grid.addColumn(person -> new Label(person.getFullName()), 
- *     new ComponentRenderer()).setCaption("Full Name");
+ * grid.addColumn(person -> new Label(person.getFullName()),
+ *         new ComponentRenderer()).setCaption("Full Name");
  * 
* * @author Vaadin Ltd @@ -59,7 +60,7 @@ public class ComponentRenderer extends AbstractRenderer { @Override public JsonValue encode(Component value) { - return Json.create(value.getConnectorId()); + return value != null ? Json.create(value.getConnectorId()) : null; } @Override 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 6398ef1055..e1bf0f354a 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 @@ -45,6 +45,9 @@ public class GridComponents extends AbstractTestUIWithLog { return textField; }).setId("textField").setCaption("TextField"); grid.addColumn(string -> { + if (string.contains("30")) { + return null; + } Button button = new Button("Click Me!", e -> Notification.show( "Clicked button on row for: " + string, 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 295f06e4ce..70966b373e 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 @@ -1,5 +1,8 @@ package com.vaadin.tests.components.grid; +import java.util.stream.IntStream; +import java.util.stream.Stream; + import org.junit.Assert; import org.junit.Test; import org.openqa.selenium.WebElement; @@ -98,6 +101,13 @@ public class GridComponentsTest extends MultiBrowserTest { assertRowExists(999, "Row 999"); } + @Test + public void testRow30() { + openTestURL(); + Stream.of(30, 130, 230, 330).forEach(this::assertNoButton); + IntStream.range(300, 310).forEach(this::assertNoButton); + } + @Test public void testHeaders() { openTestURL(); @@ -118,4 +128,10 @@ public class GridComponentsTest extends MultiBrowserTest { $(NotificationElement.class).first().getText() .contains(string)); } + + private void assertNoButton(int i) { + GridRowElement row = $(GridElement.class).first().getRow(i); + Assert.assertFalse("Row " + i + " should not have a button", + row.getCell(2).isElementPresent(ButtonElement.class)); + } } -- cgit v1.2.3