]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix Grid column reorder with partially hidden joined cells (#12386)
authorAnna Koskinen <Ansku@users.noreply.github.com>
Thu, 26 Aug 2021 09:49:13 +0000 (12:49 +0300)
committerGitHub <noreply@github.com>
Thu, 26 Aug 2021 09:49:13 +0000 (12:49 +0300)
Modified cherry-pick of #12385

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

index ebeeab67bc462fda83fc22ee05315420501df795..f1f6be9cf7a741469d02a25e7b061f2700e69307 100755 (executable)
@@ -4621,11 +4621,14 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
                 }
                 final boolean isDraggedCellRow = row.equals(draggedCellRow);
                 for (int cellColumnIndex = frozenColumns; cellColumnIndex < getColumnCount(); cellColumnIndex++) {
-                    StaticCell cell = row.getCell(getColumn(cellColumnIndex));
-                    int colspan = cell.getColspan();
-                    if (colspan <= 1) {
+                    // some of the columns might be hidden, use cell groups
+                    // rather than cell spans to determine actual span
+                    Set<Column<?, ?>> cellGroup = row
+                            .getCellGroupForColumn(getColumn(cellColumnIndex));
+                    if (cellGroup == null) {
                         continue;
                     }
+                    int colspan = cellGroup.size();
                     final int cellColumnRightIndex = cellColumnIndex + colspan;
                     final Range cellRange = Range.between(cellColumnIndex,
                             cellColumnRightIndex);
diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridReorderMerged.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridReorderMerged.java
new file mode 100644 (file)
index 0000000..8362da0
--- /dev/null
@@ -0,0 +1,44 @@
+package com.vaadin.tests.components.grid;
+
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Grid;
+import com.vaadin.ui.Grid.Column;
+import com.vaadin.ui.Grid.HeaderRow;
+
+public class GridReorderMerged extends AbstractTestUI {
+
+    @SuppressWarnings("unchecked")
+    @Override
+    protected void setup(VaadinRequest request) {
+        Grid grid = new Grid();
+        HeaderRow headerRow = grid.prependHeaderRow();
+        for (int i = 1; i < 10; ++i) {
+            String propertyId = "" + i;
+            Column column = grid.addColumn(propertyId);
+            column.setHidable(true);
+            if (i == 5) {
+                column.setHidden(true);
+            }
+            // add one value per row for easier visualization
+            grid.getContainerDataSource().addItem(i).getItemProperty(propertyId)
+                    .setValue(propertyId);
+        }
+        headerRow.join("1", "2", "3").setText("1");
+        headerRow.join("4", "5", "6").setText("2"); // middle column hidden
+        headerRow.join("7", "8", "9").setText("3");
+        grid.setColumnReorderingAllowed(true);
+        addComponent(grid);
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 12377;
+    }
+
+    @Override
+    protected String getTestDescription() {
+        return "Reordering columns should respect joined cells "
+                + "even when some columns are hidden.";
+    }
+}
diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridReorderMergedTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridReorderMergedTest.java
new file mode 100644 (file)
index 0000000..24ed6fc
--- /dev/null
@@ -0,0 +1,30 @@
+package com.vaadin.tests.components.grid;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.openqa.selenium.interactions.Actions;
+
+import com.vaadin.testbench.elements.GridElement;
+import com.vaadin.testbench.elements.GridElement.GridCellElement;
+import com.vaadin.tests.tb3.MultiBrowserTest;
+
+public class GridReorderMergedTest extends MultiBrowserTest {
+
+    @Test
+    public void dragMerged() {
+        openTestURL();
+        GridElement grid = $(GridElement.class).first();
+        GridCellElement headerCell0_0 = grid.getHeaderCell(0, 0);
+        GridCellElement headerCell0_4 = grid.getHeaderCell(0, 4);
+        new Actions(driver).clickAndHold(headerCell0_0)
+                .moveToElement(headerCell0_4,
+                        headerCell0_4.getSize().getWidth() / 3, 0)
+                .release().perform();
+
+        // ensure the first merged block got dragged over the entire second
+        // merged block
+        assertEquals("Unexpected column order,", "6",
+                grid.getHeaderCell(1, 1).getText());
+    }
+}