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 +++++++++++++++++++++- 1 file changed, 55 insertions(+), 2 deletions(-) (limited to 'client/src') 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(); -- cgit v1.2.3