]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add Grid.refreshRows to allow refreshing individual rows (#16765)
authorArtur Signell <artur@vaadin.com>
Mon, 10 Oct 2016 19:15:16 +0000 (22:15 +0300)
committerArtur Signell <artur@vaadin.com>
Fri, 14 Oct 2016 11:58:43 +0000 (14:58 +0300)
Change-Id: I554d7b6ca60840bab3a032daa4847e7271086e27

server/src/main/java/com/vaadin/ui/Grid.java
uitest/src/main/java/com/vaadin/tests/components/grid/GridRefreshRow.java [new file with mode: 0644]
uitest/src/test/java/com/vaadin/tests/components/grid/GridRefreshRowTest.java [new file with mode: 0644]

index e7eed7725f627be0c4612ad799a78de7fdee190d..34826bf6eec836e54c9ffdc9e882666c3ee27ccf 100644 (file)
@@ -6670,6 +6670,22 @@ public class Grid extends AbstractFocusable implements SelectionNotifier,
         return itemId;
     }
 
+    /**
+     * Refreshes, i.e. causes the client side to re-render the rows with the
+     * given item ids.
+     * <p>
+     * Calling this for a row which is not currently rendered on the client side
+     * has no effect.
+     * 
+     * @param itemIds
+     *            the item id(s) of the row to refresh.
+     */
+    public void refreshRows(Object... itemIds) {
+        for (Object itemId : itemIds) {
+            datasourceExtension.updateRowData(itemId);
+        }
+    }
+
     private static Logger getLogger() {
         return Logger.getLogger(Grid.class.getName());
     }
diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridRefreshRow.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridRefreshRow.java
new file mode 100644 (file)
index 0000000..0084e33
--- /dev/null
@@ -0,0 +1,118 @@
+package com.vaadin.tests.components.grid;
+
+import com.vaadin.annotations.Theme;
+import com.vaadin.data.Property.ValueChangeEvent;
+import com.vaadin.data.Property.ValueChangeListener;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUIWithLog;
+import com.vaadin.tests.util.Person;
+import com.vaadin.tests.util.PersonContainer;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.Button.ClickListener;
+import com.vaadin.ui.CheckBox;
+import com.vaadin.ui.Component;
+import com.vaadin.ui.Grid;
+import com.vaadin.ui.Grid.CellReference;
+import com.vaadin.ui.Grid.CellStyleGenerator;
+import com.vaadin.ui.Grid.RowReference;
+import com.vaadin.ui.Grid.RowStyleGenerator;
+import com.vaadin.ui.HorizontalLayout;
+
+@Theme("valo")
+public class GridRefreshRow extends AbstractTestUIWithLog {
+
+    private PersonContainer container;
+    private Grid grid;
+
+    private boolean styles[] = new boolean[] { false, false, false };
+
+    @Override
+    protected void setup(VaadinRequest request) {
+        getPage().getStyles()
+                .add(".rowstyle td {background-color: lightgreen !important;}");
+        getPage().getStyles()
+                .add("td.cellstyle {background-color: lightblue !important;}");
+        container = PersonContainer.createWithTestData(100);
+        container.addNestedContainerBean("address");
+        grid = new Grid(container);
+        grid.setWidth("800px");
+        grid.setRowStyleGenerator(new RowStyleGenerator() {
+            @Override
+            public String getStyle(RowReference row) {
+                int index = container.indexOfId(row.getItemId());
+                if (index < 3 && styles[index]) {
+                    return "rowstyle";
+                }
+
+                return null;
+            }
+        });
+        grid.setCellStyleGenerator(new CellStyleGenerator() {
+            @Override
+            public String getStyle(CellReference cell) {
+                int index = container.indexOfId(cell.getItemId());
+                if (index < 3 && styles[index]
+                        && "email".equals(cell.getPropertyId())) {
+                    return "cellstyle";
+                }
+
+                return null;
+            }
+        });
+        addComponent(grid);
+
+        addComponents(new HorizontalLayout(update(0), update(1), update(2)));
+        Button refresh10 = new Button("Refresh 0-9", new ClickListener() {
+            @Override
+            public void buttonClick(ClickEvent event) {
+                grid.refreshRows(container.getItemIds(0, 9).toArray());
+            }
+        });
+        refresh10.setId("refresh10");
+        addComponents(new HorizontalLayout(refresh(0), refresh(1), refresh(2),
+                new Button("Refresh non-existant", new ClickListener() {
+                    @Override
+                    public void buttonClick(ClickEvent event) {
+                        grid.refreshRows("foobar");
+                    }
+                })), refresh10);
+        addComponents(new HorizontalLayout(style(0), style(1), style(2)));
+    }
+
+    private Component style(final int i) {
+        final CheckBox checkBox = new CheckBox("Style for " + i);
+        checkBox.addValueChangeListener(new ValueChangeListener() {
+            @Override
+            public void valueChange(ValueChangeEvent event) {
+                styles[i] = checkBox.getValue();
+            }
+        });
+        checkBox.setId("style" + i);
+        return checkBox;
+    }
+
+    private Component update(final int i) {
+        Button button = new Button("Update " + i, new ClickListener() {
+            @Override
+            public void buttonClick(ClickEvent event) {
+                Person p = container.getIdByIndex(i);
+                p.setFirstName("!" + p.getFirstName());
+            }
+        });
+        button.setId("update" + i);
+        return button;
+    }
+
+    protected Component refresh(final int i) {
+        Button button = new Button("Refresh row " + i, new ClickListener() {
+            @Override
+            public void buttonClick(ClickEvent event) {
+                grid.refreshRows(container.getIdByIndex(i));
+            }
+        });
+        button.setId("refresh" + i);
+        return button;
+    }
+
+}
diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridRefreshRowTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridRefreshRowTest.java
new file mode 100644 (file)
index 0000000..3b1e2b9
--- /dev/null
@@ -0,0 +1,73 @@
+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.CheckBoxElement;
+import com.vaadin.testbench.elements.GridElement;
+import com.vaadin.tests.tb3.SingleBrowserTest;
+
+public class GridRefreshRowTest extends SingleBrowserTest {
+
+    private GridElement grid;
+
+    @Test
+    public void refreshRow() {
+        openTestURL();
+        grid = $(GridElement.class).first();
+        update(0);
+        update(1);
+        update(2);
+        style(1);
+        style(2);
+
+        Assert.assertEquals("Lisa", grid.getCell(0, 1).getText());
+        Assert.assertEquals("Joshua", grid.getCell(1, 1).getText());
+        Assert.assertEquals("Marge", grid.getCell(2, 1).getText());
+
+        Assert.assertFalse(hasCssClass(grid.getRow(0), "rowstyle"));
+        Assert.assertFalse(hasCssClass(grid.getRow(1), "rowstyle"));
+        Assert.assertFalse(hasCssClass(grid.getRow(2), "rowstyle"));
+        Assert.assertFalse(hasCssClass(grid.getCell(0, 0), "cellstyle"));
+        Assert.assertFalse(hasCssClass(grid.getCell(1, 0), "cellstyle"));
+        Assert.assertFalse(hasCssClass(grid.getCell(2, 0), "cellstyle"));
+
+        refresh(1);
+        Assert.assertEquals("Lisa", grid.getCell(0, 1).getText());
+        Assert.assertEquals("!Joshua", grid.getCell(1, 1).getText());
+        Assert.assertEquals("Marge", grid.getCell(2, 1).getText());
+
+        Assert.assertFalse(hasCssClass(grid.getRow(0), "rowstyle"));
+        Assert.assertTrue(hasCssClass(grid.getRow(1), "rowstyle"));
+        Assert.assertFalse(hasCssClass(grid.getRow(2), "rowstyle"));
+        Assert.assertFalse(hasCssClass(grid.getCell(0, 0), "cellstyle"));
+        Assert.assertTrue(hasCssClass(grid.getCell(1, 0), "cellstyle"));
+        Assert.assertFalse(hasCssClass(grid.getCell(2, 0), "cellstyle"));
+
+        // Assert refreshing works many times and for many rows at the same time
+        update(0);
+        update(1);
+        update(2);
+        refresh10First();
+        Assert.assertEquals("!!Lisa", grid.getCell(0, 1).getText());
+        Assert.assertEquals("!!Joshua", grid.getCell(1, 1).getText());
+        Assert.assertEquals("!!Marge", grid.getCell(2, 1).getText());
+    }
+
+    private void refresh10First() {
+        $(ButtonElement.class).id("refresh10").click();
+    }
+
+    private void update(int i) {
+        $(ButtonElement.class).id("update" + i).click();
+    }
+
+    private void style(int i) {
+        $(CheckBoxElement.class).id("style" + i).click();
+    }
+
+    private void refresh(int i) {
+        $(ButtonElement.class).id("refresh" + i).click();
+    }
+}