From 076aa0e6201db1fe9345e256266cb9227384e290 Mon Sep 17 00:00:00 2001 From: John Ahlroos Date: Mon, 25 Nov 2013 13:51:17 +0200 Subject: Ensure setting value on detached column group throws exception #3153 Change-Id: I4cef3424dfe846ab814fcf70c5cda4a305cbdf1c --- .../com/vaadin/ui/components/grid/ColumnGroup.java | 26 +++++++++++++++++++++- .../vaadin/ui/components/grid/ColumnGroupRow.java | 4 ++-- .../server/component/grid/GridColumnGroups.java | 25 +++++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) (limited to 'server') diff --git a/server/src/com/vaadin/ui/components/grid/ColumnGroup.java b/server/src/com/vaadin/ui/components/grid/ColumnGroup.java index 0ab1f61a46..6b14ef81d4 100644 --- a/server/src/com/vaadin/ui/components/grid/ColumnGroup.java +++ b/server/src/com/vaadin/ui/components/grid/ColumnGroup.java @@ -42,6 +42,11 @@ public class ColumnGroup implements Serializable { */ private final Grid grid; + /** + * The column group row the column group is attached to + */ + private final ColumnGroupRow row; + /** * The common state between the server and the client */ @@ -61,13 +66,15 @@ public class ColumnGroup implements Serializable { * the sub groups who should be included in this group * */ - ColumnGroup(Grid grid, ColumnGroupState state, List propertyIds) { + ColumnGroup(Grid grid, ColumnGroupRow row, ColumnGroupState state, + List propertyIds) { if (propertyIds == null) { throw new IllegalArgumentException( "propertyIds cannot be null. Use empty list instead."); } this.state = state; + this.row = row; columns = Collections.unmodifiableList(new ArrayList( propertyIds)); this.grid = grid; @@ -80,6 +87,7 @@ public class ColumnGroup implements Serializable { * the text displayed in the header of the column */ public void setHeaderCaption(String header) { + checkGroupIsAttached(); state.header = header; grid.markAsDirty(); } @@ -90,6 +98,7 @@ public class ColumnGroup implements Serializable { * @return the text displayed in the header of the column */ public String getHeaderCaption() { + checkGroupIsAttached(); return state.header; } @@ -100,6 +109,7 @@ public class ColumnGroup implements Serializable { * the text displayed in the footer of the column */ public void setFooterCaption(String footer) { + checkGroupIsAttached(); state.footer = footer; grid.markAsDirty(); } @@ -110,6 +120,7 @@ public class ColumnGroup implements Serializable { * @return the text displayed in the footer of the column */ public String getFooterCaption() { + checkGroupIsAttached(); return state.footer; } @@ -138,4 +149,17 @@ public class ColumnGroup implements Serializable { return columns; } + /** + * Checks if column group is attached to a row and throws an + * {@link IllegalStateException} if it is not. + * + * @throws IllegalStateException + * if the column is no longer attached to any grid + */ + protected void checkGroupIsAttached() throws IllegalStateException { + if (!row.getState().groups.contains(state)) { + throw new IllegalStateException( + "Column Group has been removed from the row."); + } + } } diff --git a/server/src/com/vaadin/ui/components/grid/ColumnGroupRow.java b/server/src/com/vaadin/ui/components/grid/ColumnGroupRow.java index 326d2826f5..e82e24abec 100644 --- a/server/src/com/vaadin/ui/components/grid/ColumnGroupRow.java +++ b/server/src/com/vaadin/ui/components/grid/ColumnGroupRow.java @@ -111,7 +111,7 @@ public class ColumnGroupRow implements Serializable { } this.state.groups.add(state); - ColumnGroup group = new ColumnGroup(grid, state, + ColumnGroup group = new ColumnGroup(grid, this, state, Arrays.asList(propertyIds)); groups.add(group); @@ -160,7 +160,7 @@ public class ColumnGroupRow implements Serializable { } ColumnGroupState state = new ColumnGroupState(); - ColumnGroup group = new ColumnGroup(grid, state, propertyIds); + ColumnGroup group = new ColumnGroup(grid, this, state, propertyIds); this.groups.add(group); // Update state diff --git a/server/tests/src/com/vaadin/tests/server/component/grid/GridColumnGroups.java b/server/tests/src/com/vaadin/tests/server/component/grid/GridColumnGroups.java index cf1ee14437..11dc48f7d5 100644 --- a/server/tests/src/com/vaadin/tests/server/component/grid/GridColumnGroups.java +++ b/server/tests/src/com/vaadin/tests/server/component/grid/GridColumnGroups.java @@ -19,6 +19,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -208,4 +209,28 @@ public class GridColumnGroups { group.setFooterCaption(null); assertNull(group.getFooterCaption()); } + + @Test + public void testColumnGroupDetachment() throws Exception { + + ColumnGroupRow row = grid.addColumnGroupRow(); + ColumnGroup group = row.addGroup("column1", "column2"); + + // Remove group + row.removeGroup(group); + + try { + group.setHeaderCaption("Header"); + fail("Should throw exception for setting header caption on detached group"); + } catch (IllegalStateException ise) { + + } + + try { + group.setFooterCaption("Footer"); + fail("Should throw exception for setting footer caption on detached group"); + } catch (IllegalStateException ise) { + + } + } } -- cgit v1.2.3