diff options
author | John Ahlroos <john@vaadin.com> | 2013-11-25 13:51:17 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2013-12-02 13:43:50 +0000 |
commit | 076aa0e6201db1fe9345e256266cb9227384e290 (patch) | |
tree | 9681c4bc2816a6d52f1292d25f54577ff6cbb273 /server | |
parent | 42461bfebfe43d1f24948d72f29c1dd38a8d7d1a (diff) | |
download | vaadin-framework-076aa0e6201db1fe9345e256266cb9227384e290.tar.gz vaadin-framework-076aa0e6201db1fe9345e256266cb9227384e290.zip |
Ensure setting value on detached column group throws exception #3153
Change-Id: I4cef3424dfe846ab814fcf70c5cda4a305cbdf1c
Diffstat (limited to 'server')
3 files changed, 52 insertions, 3 deletions
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 @@ -43,6 +43,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 */ private final ColumnGroupState state; @@ -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<Object> propertyIds) { + ColumnGroup(Grid grid, ColumnGroupRow row, ColumnGroupState state, + List<Object> 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<Object>( 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) { + + } + } } |