]> source.dussan.org Git - vaadin-framework.git/commitdiff
Correctly detach header/footer components when column is removed (#8645)
authorArtur <artur@vaadin.com>
Thu, 9 Mar 2017 06:37:54 +0000 (08:37 +0200)
committerPekka Hyvönen <pekka@vaadin.com>
Thu, 9 Mar 2017 06:37:54 +0000 (08:37 +0200)
* Correctly detach header/footer components when column is removed

Fixes #8638

* Merge branch '7.7' into _detach-header-components-on-remove-column

* Merge branch '7.7' into _detach-header-components-on-remove-column

* Merge branch '7.7' into _detach-header-components-on-remove-column

server/src/main/java/com/vaadin/ui/Grid.java
server/src/test/java/com/vaadin/tests/server/component/grid/GridChildrenTest.java

index 17ffab017f96ec201487455431689e4943f1cb4f..5bdb76938f70931c8c218b40b047d2c0949e19cf 100644 (file)
@@ -2370,6 +2370,8 @@ public class Grid extends AbstractFocusable implements SelectionNotifier,
                     Set<CELLTYPE> cellGroupForCell = getCellGroupForCell(cell);
                     if (cellGroupForCell != null) {
                         removeCellFromGroup(cell, cellGroupForCell);
+                    } else {
+                        cell.detach();
                     }
                     rowState.cells.remove(cell.getCellState());
                 }
@@ -2385,11 +2387,13 @@ public class Grid extends AbstractFocusable implements SelectionNotifier,
                             CELLTYPE mergedCell = cellGroups.remove(cellGroup);
                             cellGroup.remove(cell);
                             cellGroups.put(cellGroup, mergedCell);
-
                             group.remove(columnId);
                         } else {
+                            // Only one cell remaining in the group, disband it
+                            // The contents of the group if removed
                             rowState.cellGroups.remove(group);
-                            cellGroups.remove(cellGroup);
+                            CELLTYPE mergedCell = cellGroups.remove(cellGroup);
+                            mergedCell.detach();
                         }
                         return;
                     }
index 374a93a98c707265bc5acf265dc351e0915b1072..91da107b3bbb9e99936af62084b5117bd46f43ea 100644 (file)
@@ -21,10 +21,15 @@ import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.vaadin.data.Container.Indexed;
+import com.vaadin.data.util.IndexedContainer;
+import com.vaadin.ui.Button;
 import com.vaadin.ui.Component;
 import com.vaadin.ui.Grid;
 import com.vaadin.ui.Grid.FooterCell;
+import com.vaadin.ui.Grid.FooterRow;
 import com.vaadin.ui.Grid.HeaderCell;
+import com.vaadin.ui.Grid.HeaderRow;
 import com.vaadin.ui.Label;
 
 public class GridChildrenTest {
@@ -73,6 +78,93 @@ public class GridChildrenTest {
         Assert.assertNull(label.getParent());
     }
 
+    @Test
+    public void removeHeaderFooterComponentWhenColumnIsRemoved() {
+        HeaderRow h1 = grid.prependHeaderRow();
+        FooterRow f1 = grid.addFooterRowAt(0);
+        Button headerButton = new Button("This is a button, mkay?");
+        Button footerButton = new Button("This is a button, mkay?");
+        h1.getCell("foo").setComponent(headerButton);
+        f1.getCell("foo").setComponent(footerButton);
+
+        Assert.assertEquals(grid, headerButton.getParent());
+        Assert.assertEquals(grid, footerButton.getParent());
+        grid.removeColumn("foo");
+        Assert.assertNull(headerButton.getParent());
+        Assert.assertNull(footerButton.getParent());
+    }
+
+    @Test
+    public void joinedHeaderComponentDetachedWhenLastColumnIsRemoved() {
+        HeaderRow h1 = grid.prependHeaderRow();
+        FooterRow f1 = grid.addFooterRowAt(0);
+        Button headerButton = new Button("This is a button, mkay?");
+        Button footerButton = new Button("This is a button, mkay?");
+
+        HeaderCell mergedHeader = h1.join("foo", "bar", "baz");
+        FooterCell mergedFooter = f1.join("foo", "bar", "baz");
+
+        mergedHeader.setComponent(headerButton);
+        mergedFooter.setComponent(footerButton);
+
+        Assert.assertEquals(grid, headerButton.getParent());
+        Assert.assertEquals(grid, footerButton.getParent());
+        grid.removeColumn("foo");
+        Assert.assertEquals(grid, headerButton.getParent());
+        Assert.assertEquals(grid, footerButton.getParent());
+        Assert.assertEquals(headerButton, mergedHeader.getComponent());
+        Assert.assertEquals(footerButton, mergedFooter.getComponent());
+
+        grid.removeColumn("bar");
+        // Component is not moved from merged cell to the last remaining cell
+        Assert.assertNull(headerButton.getParent());
+        Assert.assertNull(footerButton.getParent());
+    }
+
+    @Test
+    public void joinedHeaderComponentDetachedWhenLastColumnIsRemovedReverseOrder() {
+        HeaderRow h1 = grid.prependHeaderRow();
+        FooterRow f1 = grid.addFooterRowAt(0);
+        Button headerButton = new Button("This is a button, mkay?");
+        Button footerButton = new Button("This is a button, mkay?");
+
+        HeaderCell mergedHeader = h1.join("foo", "bar", "baz");
+        FooterCell mergedFooter = f1.join("foo", "bar", "baz");
+
+        mergedHeader.setComponent(headerButton);
+        mergedFooter.setComponent(footerButton);
+
+        Assert.assertEquals(grid, headerButton.getParent());
+        Assert.assertEquals(grid, footerButton.getParent());
+        grid.removeColumn("baz");
+        Assert.assertEquals(grid, headerButton.getParent());
+        Assert.assertEquals(grid, footerButton.getParent());
+        Assert.assertEquals(headerButton, mergedHeader.getComponent());
+        Assert.assertEquals(footerButton, mergedFooter.getComponent());
+
+        grid.removeColumn("bar");
+        // Component is not moved from merged cell to the last remaining cell
+        Assert.assertNull(headerButton.getParent());
+        Assert.assertNull(footerButton.getParent());
+    }
+
+    @Test
+    public void removeHeaderComponentWhenColumnIsRemovedFromDataSource() {
+        Indexed i = new IndexedContainer();
+        i.addContainerProperty("c1", String.class, "does not matter 1");
+        i.addContainerProperty("c2", String.class, "does not matter 2");
+        Grid grid = new Grid();
+        grid.setContainerDataSource(i);
+
+        HeaderRow h1 = grid.prependHeaderRow();
+        Button button = new Button("This is a button, mkay?");
+        h1.getCell("c1").setComponent(button);
+
+        Assert.assertEquals(grid, button.getParent());
+        i.removeContainerProperty("c1");
+        Assert.assertNull(button.getParent());
+    }
+
     @Test
     public void removeComponentInMergedFooterCell() {
         FooterCell merged = grid.addFooterRowAt(0).join("foo", "bar", "baz");