From eb75c09574249687d9ea3899791c8daa3fcec314 Mon Sep 17 00:00:00 2001 From: John Ahlroos Date: Tue, 10 Dec 2013 16:35:50 +0200 Subject: Validate column group boundaries #12907 Change-Id: I5078d97c5a6bc92f59d0d04eca2a2cfc27c973b4 --- .../com/vaadin/client/ui/grid/ColumnGroupRow.java | 57 ++++++++++++++++++++- .../vaadin/ui/components/grid/ColumnGroupRow.java | 58 ++++++++++++++++++++-- .../server/component/grid/GridColumnGroups.java | 29 +++++++++++ 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 { * @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 { for (GridColumn column : columns) { if (isColumnGrouped(column)) { @@ -83,6 +85,8 @@ public class ColumnGroupRow { } } + validateNewGroupProperties(Arrays.asList(columns)); + ColumnGroup group = new ColumnGroup(grid, Arrays.asList(columns)); groups.add(group); grid.refreshHeader(); @@ -90,6 +94,52 @@ public class ColumnGroupRow { return group; } + private void validateNewGroupProperties(Collection> columns) { + + 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 (GridColumn 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 column) { + return getGroupForColumn(column) != null; + } + + private ColumnGroup getGroupForColumn(GridColumn column) { + for (ColumnGroup 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 { * the group. * */ - public ColumnGroup addGroup(ColumnGroup... groups) { + public ColumnGroup addGroup(ColumnGroup... groups) + throws IllegalArgumentException { assert groups != null : "groups cannot be null"; Set> columns = new HashSet>(); @@ -107,6 +158,8 @@ public class ColumnGroupRow { columns.addAll(group.getColumns()); } + validateNewGroupProperties(columns); + ColumnGroup group = new ColumnGroup(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 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 propertyIds = new ArrayList(); @@ -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 true 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"); + + } } -- cgit v1.2.3