]> source.dussan.org Git - vaadin-framework.git/commitdiff
Allow replacing all columns at once when some of them are frozen. (#11978)
authorAnna Koskinen <Ansku@users.noreply.github.com>
Thu, 30 Apr 2020 11:31:09 +0000 (14:31 +0300)
committerGitHub <noreply@github.com>
Thu, 30 Apr 2020 11:31:09 +0000 (14:31 +0300)
Fixes #11824

client/src/main/java/com/vaadin/client/widgets/Grid.java
uitest/src/main/java/com/vaadin/tests/components/grid/GridFrozenColumnReplace.java [new file with mode: 0644]
uitest/src/test/java/com/vaadin/tests/components/grid/GridFrozenColumnReplaceTest.java [new file with mode: 0644]

index 6d22dfc5cac747ef524852fd90cbc1dc962cd585..2faddaf68d9a1daaf77c8a2ad0291075ea202f68 100755 (executable)
@@ -7398,8 +7398,17 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
     }
 
     private void updateFrozenColumns() {
-        escalator.getColumnConfiguration()
-                .setFrozenColumnCount(getVisibleFrozenColumnCount());
+        int visibleFrozenColumnCount = getVisibleFrozenColumnCount();
+        ColumnConfiguration columnConfiguration = escalator
+                .getColumnConfiguration();
+        if (columnConfiguration.getColumnCount() < visibleFrozenColumnCount) {
+            // new columns may not have got added yet, delay and check the
+            // correct count again
+            Scheduler.get().scheduleFinally(() -> columnConfiguration
+                    .setFrozenColumnCount(getVisibleFrozenColumnCount()));
+        } else {
+            columnConfiguration.setFrozenColumnCount(visibleFrozenColumnCount);
+        }
     }
 
     private int getVisibleFrozenColumnCount() {
diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridFrozenColumnReplace.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridFrozenColumnReplace.java
new file mode 100644 (file)
index 0000000..7ddb357
--- /dev/null
@@ -0,0 +1,44 @@
+package com.vaadin.tests.components.grid;
+
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Grid;
+import com.vaadin.ui.Grid.SelectionMode;
+
+public class GridFrozenColumnReplace extends SimpleGridUI {
+
+    @Override
+    protected void setup(VaadinRequest request) {
+        Grid<String> grid = new Grid<>();
+        grid.addColumn(String::toString).setId("id1").setCaption("Frozen 1");
+        grid.addColumn(String::toString).setId("id2").setCaption("Col 2");
+        grid.setSelectionMode(SelectionMode.MULTI);
+        grid.setFrozenColumnCount(1);
+
+        Button button = new Button("Replace columns");
+        button.addClickListener(e -> {
+            grid.removeAllColumns(); // will change frozenColumnCount to 0
+
+            grid.addColumn(String::toString).setId("id1")
+                    .setCaption("New Frozen 1");
+            grid.addColumn(String::toString).setId("id2")
+                    .setCaption("New Frozen 2");
+            grid.addColumn(String::toString).setId("id3")
+                    .setCaption("New Col 3");
+            grid.setFrozenColumnCount(2);
+        });
+
+        addComponents(grid, button);
+    }
+
+    @Override
+    protected String getTestDescription() {
+        return "It should be possible to remove and replace columns on same round trip "
+                + "even if some of them are frozen.";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 11824;
+    }
+}
diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridFrozenColumnReplaceTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridFrozenColumnReplaceTest.java
new file mode 100644 (file)
index 0000000..4ad8a48
--- /dev/null
@@ -0,0 +1,34 @@
+package com.vaadin.tests.components.grid;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import com.vaadin.testbench.By;
+import com.vaadin.testbench.elements.ButtonElement;
+import com.vaadin.testbench.elements.GridElement;
+import com.vaadin.tests.tb3.SingleBrowserTest;
+
+public class GridFrozenColumnReplaceTest extends SingleBrowserTest {
+
+    @Test
+    public void testChangingColumns() {
+        openTestURL("debug");
+
+        GridElement grid = $(GridElement.class).first();
+        String caption = grid.getHeaderCell(0, 1).getText();
+        assertFalse("Unexpected column caption: " + caption,
+                caption != null && caption.startsWith("New "));
+
+        $(ButtonElement.class).first().click();
+
+        assertEquals("Unexpected error notifications,", 0,
+                findElements(By.className("v-Notification-error")).size());
+
+        caption = grid.getHeaderCell(0, 1).getText();
+        assertTrue("Unexpected column caption: " + caption,
+                caption != null && caption.startsWith("New "));
+    }
+}