]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix Grid column reorder with partially hidden joined header cells. (#12429)
authorAnna Koskinen <Ansku@users.noreply.github.com>
Thu, 14 Oct 2021 05:20:07 +0000 (08:20 +0300)
committerGitHub <noreply@github.com>
Thu, 14 Oct 2021 05:20:07 +0000 (08:20 +0300)
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 14bf795e2e04c78fbffe163c55b45f5762230fa7..bb12a71c7c1129c344c92ca39d6b5a461c198164 100755 (executable)
@@ -175,7 +175,6 @@ import com.vaadin.client.widget.grid.sort.SortOrder;
 import com.vaadin.client.widgets.Escalator.AbstractRowContainer;
 import com.vaadin.client.widgets.Escalator.SubPartArguments;
 import com.vaadin.client.widgets.Grid.Editor.State;
-import com.vaadin.client.widgets.Grid.StaticSection.StaticCell;
 import com.vaadin.client.widgets.Grid.StaticSection.StaticRow;
 import com.vaadin.shared.Range;
 import com.vaadin.shared.Registration;
@@ -5123,8 +5122,11 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
         @Override
         public void onDrop() {
             final int draggedColumnIndex = eventCell.getColumnIndex();
-            final int colspan = header.getRow(eventCell.getRowIndex())
-                    .getCell(eventCell.getColumn()).getColspan();
+            final StaticRow<?> draggedCellRow = header
+                    .getRow(eventCell.getRowIndex());
+            int colspan = draggedCellRow
+                    .getSizeOfCellGroup(getColumn(draggedColumnIndex));
+            colspan = colspan == 0 ? 1 : colspan;
             if (latestColumnDropIndex != draggedColumnIndex
                     && latestColumnDropIndex != draggedColumnIndex + colspan) {
                 List<Column<?, T>> columns = getColumns();
@@ -5261,13 +5263,12 @@ 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();
+                    int colspan = row
+                            .getSizeOfCellGroup(getColumn(cellColumnIndex));
                     if (colspan <= 1) {
                         continue;
                     }
-                    final int cellColumnRightIndex = cellColumnIndex + row
-                            .getSizeOfCellGroup(getColumn(cellColumnIndex));
+                    final int cellColumnRightIndex = cellColumnIndex + colspan;
                     final Range cellRange = Range.between(cellColumnIndex,
                             cellColumnRightIndex);
                     final boolean intersects = draggedCellRange
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..62ddef2
--- /dev/null
@@ -0,0 +1,43 @@
+package com.vaadin.tests.components.grid;
+
+import java.util.ArrayList;
+import java.util.List;
+
+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.components.grid.HeaderRow;
+
+public class GridReorderMerged extends AbstractTestUI {
+
+    @Override
+    protected void setup(VaadinRequest request) {
+        Grid<String> grid = new Grid<>();
+        List<String> items = new ArrayList<>();
+        HeaderRow headerRow = grid.prependHeaderRow();
+        for (int i = 1; i < 10; ++i) {
+            String propertyId = "" + i;
+            Column<String, ?> column = grid
+                    .addColumn(item -> propertyId.equals(item) ? item : "")
+                    .setId(propertyId).setCaption(propertyId);
+            column.setHidable(true);
+            if (i == 5) {
+                column.setHidden(true);
+            }
+            items.add(propertyId);
+        }
+        grid.setItems(items);
+        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 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..80be635
--- /dev/null
@@ -0,0 +1,44 @@
+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).dragAndDrop(headerCell0_0, headerCell0_4).perform();
+
+        // ensure the first merged block got dragged over the entire second
+        // merged block
+        assertEquals("Unexpected column order,", "6",
+                grid.getHeaderCell(1, 1).getText());
+    }
+
+    @Test
+    public void dragMergedReverse() {
+        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_4)
+                .moveByOffset(-headerCell0_0.getSize().getWidth(),
+                        headerCell0_0.getSize().getHeight() / 2)
+                .release().perform();
+
+        // ensure the second merged block got dragged over the first merged
+        // block entirely
+        assertEquals("Unexpected column order,", "6",
+                grid.getHeaderCell(1, 1).getText());
+    }
+}