diff options
author | John Ahlroos <john@vaadin.com> | 2013-12-10 16:35:50 +0200 |
---|---|---|
committer | John Ahlroos <john@vaadin.com> | 2013-12-10 16:35:50 +0200 |
commit | eb75c09574249687d9ea3899791c8daa3fcec314 (patch) | |
tree | 867b3fb40d55533a8d7cde2effd137cbe4b8bd1b | |
parent | b7dddff607f70d6f27104d98a086e7541a32aded (diff) | |
download | vaadin-framework-eb75c09574249687d9ea3899791c8daa3fcec314.tar.gz vaadin-framework-eb75c09574249687d9ea3899791c8daa3fcec314.zip |
Validate column group boundaries #12907
Change-Id: I5078d97c5a6bc92f59d0d04eca2a2cfc27c973b4
3 files changed, 137 insertions, 7 deletions
diff --git a/client/src/com/vaadin/client/ui/grid/ColumnGroupRow.java b/client/src/com/vaadin/client/ui/grid/ColumnGroupRow.java index 113e0b34c6..0ced616f3f 100644 --- a/client/src/com/vaadin/client/ui/grid/ColumnGroupRow.java +++ b/client/src/com/vaadin/client/ui/grid/ColumnGroupRow.java @@ -18,6 +18,7 @@ package com.vaadin.client.ui.grid; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -73,7 +74,8 @@ public class ColumnGroupRow<T> { * @return a column group representing the collection of columns added to * the group. */ - public ColumnGroup<T> addGroup(GridColumn<?, T>... columns) { + public ColumnGroup<T> addGroup(GridColumn<?, T>... columns) + throws IllegalArgumentException { for (GridColumn<?, T> column : columns) { if (isColumnGrouped(column)) { @@ -83,6 +85,8 @@ public class ColumnGroupRow<T> { } } + validateNewGroupProperties(Arrays.asList(columns)); + ColumnGroup<T> group = new ColumnGroup<T>(grid, Arrays.asList(columns)); groups.add(group); grid.refreshHeader(); @@ -90,6 +94,52 @@ public class ColumnGroupRow<T> { return group; } + private void validateNewGroupProperties(Collection<GridColumn<?, T>> columns) { + + int rowIndex = grid.getColumnGroupRows().indexOf(this); + int parentRowIndex = rowIndex - 1; + + // Get the parent row of this row. + ColumnGroupRow<T> parentRow = null; + if (parentRowIndex > -1) { + parentRow = grid.getColumnGroupRows().get(parentRowIndex); + } + + if (parentRow == null) { + // A parentless row is always valid and is usually the first row + // added to the grid + return; + } + + for (GridColumn<?, T> column : columns) { + if (parentRow.hasColumnBeenGrouped(column)) { + /* + * If a property has been grouped in the parent row then all of + * the properties in the parent group also needs to be included + * in the child group for the groups to be valid + */ + ColumnGroup parentGroup = parentRow.getGroupForColumn(column); + if (!columns.containsAll(parentGroup.getColumns())) { + throw new IllegalArgumentException( + "Grouped properties overlaps previous grouping bounderies"); + } + } + } + } + + private boolean hasColumnBeenGrouped(GridColumn<?, T> column) { + return getGroupForColumn(column) != null; + } + + private ColumnGroup<T> getGroupForColumn(GridColumn<?, T> column) { + for (ColumnGroup<T> group : groups) { + if (group.getColumns().contains(column)) { + return group; + } + } + return null; + } + /** * Add a new group to the row by using other already greated groups * @@ -99,7 +149,8 @@ public class ColumnGroupRow<T> { * the group. * */ - public ColumnGroup<T> addGroup(ColumnGroup<T>... groups) { + public ColumnGroup<T> addGroup(ColumnGroup<T>... groups) + throws IllegalArgumentException { assert groups != null : "groups cannot be null"; Set<GridColumn<?, T>> columns = new HashSet<GridColumn<?, T>>(); @@ -107,6 +158,8 @@ public class ColumnGroupRow<T> { columns.addAll(group.getColumns()); } + validateNewGroupProperties(columns); + ColumnGroup<T> group = new ColumnGroup<T>(grid, columns); this.groups.add(group); grid.refreshHeader(); diff --git a/server/src/com/vaadin/ui/components/grid/ColumnGroupRow.java b/server/src/com/vaadin/ui/components/grid/ColumnGroupRow.java index e82e24abec..b90b4df2c5 100644 --- a/server/src/com/vaadin/ui/components/grid/ColumnGroupRow.java +++ b/server/src/com/vaadin/ui/components/grid/ColumnGroupRow.java @@ -93,7 +93,8 @@ public class ColumnGroupRow implements Serializable { * @return a column group representing the collection of columns added to * the group */ - public ColumnGroup addGroup(Object... propertyIds) { + public ColumnGroup addGroup(Object... propertyIds) + throws IllegalArgumentException { assert propertyIds != null : "propertyIds cannot be null."; for (Object propertyId : propertyIds) { @@ -104,6 +105,8 @@ public class ColumnGroupRow implements Serializable { } } + validateNewGroupProperties(Arrays.asList(propertyIds)); + ColumnGroupState state = new ColumnGroupState(); for (Object propertyId : propertyIds) { assert propertyId != null : "null items in columns array not supported."; @@ -119,6 +122,43 @@ public class ColumnGroupRow implements Serializable { return group; } + private void validateNewGroupProperties(List<Object> propertyIds) + throws IllegalArgumentException { + + /* + * Validate parent grouping + */ + int rowIndex = grid.getColumnGroupRows().indexOf(this); + int parentRowIndex = rowIndex - 1; + + // Get the parent row of this row. + ColumnGroupRow parentRow = null; + if (parentRowIndex > -1) { + parentRow = grid.getColumnGroupRows().get(parentRowIndex); + } + + if (parentRow == null) { + // A parentless row is always valid and is usually the first row + // added to the grid + return; + } + + for (Object id : propertyIds) { + if (parentRow.hasColumnBeenGrouped(id)) { + /* + * If a property has been grouped in the parent row then all of + * the properties in the parent group also needs to be included + * in the child group for the groups to be valid + */ + ColumnGroup parentGroup = parentRow.getGroupForProperty(id); + if (!propertyIds.containsAll(parentGroup.getColumns())) { + throw new IllegalArgumentException( + "Grouped properties overlaps previous grouping bounderies"); + } + } + } + } + /** * Add a new group to the row by using column instances. * @@ -127,7 +167,8 @@ public class ColumnGroupRow implements Serializable { * @return a column group representing the collection of columns added to * the group */ - public ColumnGroup addGroup(GridColumn... columns) { + public ColumnGroup addGroup(GridColumn... columns) + throws IllegalArgumentException { assert columns != null : "columns cannot be null"; List<Object> propertyIds = new ArrayList<Object>(); @@ -150,7 +191,8 @@ public class ColumnGroupRow implements Serializable { * the group * */ - public ColumnGroup addGroup(ColumnGroup... groups) { + public ColumnGroup addGroup(ColumnGroup... groups) + throws IllegalArgumentException { assert groups != null : "groups cannot be null"; // Gather all groups columns into one list @@ -159,6 +201,8 @@ public class ColumnGroupRow implements Serializable { propertyIds.addAll(group.getColumns()); } + validateNewGroupProperties(propertyIds); + ColumnGroupState state = new ColumnGroupState(); ColumnGroup group = new ColumnGroup(grid, this, state, propertyIds); this.groups.add(group); @@ -204,12 +248,16 @@ public class ColumnGroupRow implements Serializable { * @return <code>true</code> if the column is included in a group */ private boolean hasColumnBeenGrouped(Object propertyId) { + return getGroupForProperty(propertyId) != null; + } + + private ColumnGroup getGroupForProperty(Object propertyId) { for (ColumnGroup group : groups) { if (group.isColumnInGroup(propertyId)) { - return true; + return group; } } - return false; + return null; } /** 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 11dc48f7d5..4350bf1a7b 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 @@ -233,4 +233,33 @@ public class GridColumnGroups { } } + + @Test + public void testColumnGroupLimits() throws Exception { + + ColumnGroupRow row = grid.addColumnGroupRow(); + row.addGroup("column1", "column2"); + row.addGroup("column3", "column4"); + + try { + row.addGroup("column2", "column3"); + fail("Adding a group with already grouped properties should throw exception"); + } catch (IllegalArgumentException iae) { + + } + + ColumnGroupRow row2 = grid.addColumnGroupRow(); + + try { + row2.addGroup("column2", "column3"); + fail("Adding a group that breaks previous grouping boundaries should throw exception"); + } catch (IllegalArgumentException iae) { + + } + + // This however should not throw an exception as it spans completely + // over the parent rows groups + row2.addGroup("column1", "column2", "column3", "column4"); + + } } |