From cea1358018fc4c51cbbf1fbe3b66a013874ecb27 Mon Sep 17 00:00:00 2001 From: John Alhroos Date: Wed, 19 May 2010 10:37:24 +0000 Subject: [PATCH] Fix for #4542 + JUnit test + Test Application svn changeset:13251/svn branch:6.3 --- src/com/vaadin/ui/GridLayout.java | 19 ++++++-- .../layouts/GridLayoutRemoveFinalRow.java | 46 +++++++++++++++++++ .../TestGridLayoutLastRowRemoval.java | 32 +++++++++++++ 3 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 tests/src/com/vaadin/tests/layouts/GridLayoutRemoveFinalRow.java create mode 100644 tests/src/com/vaadin/tests/server/components/TestGridLayoutLastRowRemoval.java diff --git a/src/com/vaadin/ui/GridLayout.java b/src/com/vaadin/ui/GridLayout.java index 19abe8d142..1e31eb9b65 100644 --- a/src/com/vaadin/ui/GridLayout.java +++ b/src/com/vaadin/ui/GridLayout.java @@ -1183,6 +1183,10 @@ public class GridLayout extends AbstractLayout implements * Removes row and all components in the row. Components which span over * several rows are removed if the selected row is the component's first * row. + *

+ * If the last row is removed then all remaining components will be removed + * and the grid will be reduced to one row and one column. + *

* * @param row * The row number to remove @@ -1210,9 +1214,18 @@ public class GridLayout extends AbstractLayout implements } } - setRows(rows - 1); - if (cursorY > row) { - cursorY--; + if (rows == 1) { + /* + * Removing the last row means that the dimensions of the Grid + * layout will be truncated to 1x1 and all components removed. + */ + setColumns(1); + cursorY = 0; + } else { + setRows(rows - 1); + if (cursorY > row) { + cursorY--; + } } structuralChange = true; diff --git a/tests/src/com/vaadin/tests/layouts/GridLayoutRemoveFinalRow.java b/tests/src/com/vaadin/tests/layouts/GridLayoutRemoveFinalRow.java new file mode 100644 index 0000000000..db765e0636 --- /dev/null +++ b/tests/src/com/vaadin/tests/layouts/GridLayoutRemoveFinalRow.java @@ -0,0 +1,46 @@ +package com.vaadin.tests.layouts; + +import com.vaadin.tests.components.TestBase; +import com.vaadin.ui.Button; +import com.vaadin.ui.GridLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.Button.ClickEvent; + +/** + * Tests removing rows from a GridLayout + */ +@SuppressWarnings("serial") +public class GridLayoutRemoveFinalRow extends TestBase { + + @Override + protected void setup() { + getLayout().setSpacing(true); + + final GridLayout layout = new GridLayout(2, 2); + layout.setSpacing(true); + layout.addComponent(new Label("Label1")); + layout.addComponent(new Label("Label2")); + layout.addComponent(new Label("Label3")); + layout.addComponent(new Label("Label4")); + addComponent(layout); + + Button removeRowBtn = new Button("Remove row", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + layout.removeRow(0); + } + }); + addComponent(removeRowBtn); + } + + @Override + protected String getDescription() { + return "Removing last row of a GridLayout throws a IllegalArgumentException"; + } + + @Override + protected Integer getTicketNumber() { + return 4542; + } + +} diff --git a/tests/src/com/vaadin/tests/server/components/TestGridLayoutLastRowRemoval.java b/tests/src/com/vaadin/tests/server/components/TestGridLayoutLastRowRemoval.java new file mode 100644 index 0000000000..8881b9e273 --- /dev/null +++ b/tests/src/com/vaadin/tests/server/components/TestGridLayoutLastRowRemoval.java @@ -0,0 +1,32 @@ +package com.vaadin.tests.server.components; + +import junit.framework.TestCase; + +import com.vaadin.ui.GridLayout; +import com.vaadin.ui.Label; + +public class TestGridLayoutLastRowRemoval extends TestCase { + + public void testRemovingLastRow() { + GridLayout grid = new GridLayout(2, 1); + grid.addComponent(new Label("Col1")); + grid.addComponent(new Label("Col2")); + + try { + // Removing the last row in the grid + grid.removeRow(0); + } catch (IllegalArgumentException iae) { + // Removing the last row should not throw an + // IllegalArgumentException + fail("removeRow(0) threw an IllegalArgumentExcetion when removing the last row"); + } + + // The grid should now be empty with one row and one column + assertEquals(1, grid.getColumns()); + assertEquals(1, grid.getRows()); + + // There should be no component left in the grid layout + assertNull("A component should not be left in the layout", grid + .getComponent(0, 0)); + } +} -- 2.39.5