diff options
author | elmot <elmot@vaadin.com> | 2016-11-22 12:49:37 +0200 |
---|---|---|
committer | Ilia Motornyi <elmot@vaadin.com> | 2016-11-24 12:01:59 +0000 |
commit | 4e8eb29c548128a50a000699f60259243e4695ed (patch) | |
tree | d16cbc9319785721924ad9455f99a1fb64c229a6 /server/src/main | |
parent | 159d413602380497b189e5cabbdd9ecf6431c725 (diff) | |
download | vaadin-framework-4e8eb29c548128a50a000699f60259243e4695ed.tar.gz vaadin-framework-4e8eb29c548128a50a000699f60259243e4695ed.zip |
Grid merging header cells
Change-Id: Ia52bbef412fc8701f6b862960dfed9c08c17ff7a
Diffstat (limited to 'server/src/main')
3 files changed, 110 insertions, 13 deletions
diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index b9f5f5afbb..ee1547bfce 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -1540,6 +1540,25 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents { public default HeaderCell getCell(Column<?, ?> column) { return getCell(column.getId()); } + + /** + * Merges columns cells in a row + * + * @param cells + * The cells to merge. Must be from the same row. + * @return The remaining visible cell after the merge + */ + HeaderCell join(Set<HeaderCell> cellsToMerge); + + /** + * Merges columns cells in a row + * + * @param cells + * The cells to merge. Must be from the same row. + * @return The remaining visible cell after the merge + */ + HeaderCell join(HeaderCell ... cellsToMerge); + } /** @@ -1599,6 +1618,13 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents { * @return cell content type */ public GridStaticCellType getCellType(); + + /** + * Gets the column id where this cell is. + * + * @return column id for this cell + */ + public String getColumnId(); } /** @@ -1894,11 +1920,12 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents { */ public void removeColumn(Column<T, ?> column) { if (columnSet.remove(column)) { - columnKeys.remove(column.getId()); - removeDataGenerator(column); - getHeader().removeColumn(column.getId()); + String columnId = column.getId(); + columnKeys.remove(columnId); column.remove(); - + getHeader().removeColumn(columnId); + getFooter().removeColumn(columnId); + getState(true).columnOrder.remove(columnId); } } diff --git a/server/src/main/java/com/vaadin/ui/components/grid/Header.java b/server/src/main/java/com/vaadin/ui/components/grid/Header.java index 31f80a9add..6a4b72c5f9 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/Header.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/Header.java @@ -17,11 +17,15 @@ package com.vaadin.ui.components.grid; import com.vaadin.ui.Grid; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + /** * Represents the header section of a Grid. - * + * * @author Vaadin Ltd. - * + * * @since 8.0 */ public abstract class Header extends StaticSection<Header.Row> { @@ -64,7 +68,7 @@ public abstract class Header extends StaticSection<Header.Row> { /** * Returns whether this row is the default header row. - * + * * @return {@code true} if this row is the default row, {@code false} * otherwise. */ @@ -74,13 +78,52 @@ public abstract class Header extends StaticSection<Header.Row> { /** * Sets whether this row is the default header row. - * + * * @param defaultHeader * {@code true} to set to default, {@code false} otherwise. */ protected void setDefault(boolean defaultHeader) { getRowState().defaultHeader = defaultHeader; } + + /** + * Merges columns cells in a row + * + * @param cellsToMerge + * the cells which should be merged + * @return the remaining visible cell after the merge + */ + @Override + public Grid.HeaderCell join(Set<Grid.HeaderCell> cellsToMerge) { + for (Grid.HeaderCell cell : cellsToMerge) { + checkIfAlreadyMerged(cell.getColumnId()); + } + + // Create new cell data for the group + Cell newCell = createCell(); + + Set<String> columnGroup = new HashSet<>(); + for (Grid.HeaderCell cell : cellsToMerge) { + columnGroup.add(cell.getColumnId()); + } + addMergedCell(newCell, columnGroup); + markAsDirty(); + return newCell; + } + + /** + * Merges columns cells in a row + * + * @param cellsToMerge + * the cells which should be merged + * @return the remaining visible cell after the merge + */ + @Override + public Grid.HeaderCell join(Grid.HeaderCell... cellsToMerge) { + Set<Grid.HeaderCell> headerCells = new HashSet<>(Arrays.asList(cellsToMerge)); + return join(headerCells); + } + } @Override @@ -99,7 +142,7 @@ public abstract class Header extends StaticSection<Header.Row> { /** * Returns the default row of this header. The default row displays column * captions and sort indicators. - * + * * @return the default row, or {@code null} if there is no default row */ public Row getDefaultRow() { diff --git a/server/src/main/java/com/vaadin/ui/components/grid/StaticSection.java b/server/src/main/java/com/vaadin/ui/components/grid/StaticSection.java index 678ab2a935..5c7d682b17 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/StaticSection.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/StaticSection.java @@ -19,10 +19,12 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Set; import com.vaadin.shared.ui.grid.GridStaticCellType; import com.vaadin.shared.ui.grid.SectionState; @@ -56,7 +58,7 @@ public abstract class StaticSection<ROW extends StaticSection.StaticRow<?>> private final RowState rowState = new RowState(); private final StaticSection<?> section; - private final Map<Object, CELL> cells = new LinkedHashMap<>(); + private final Map<String, CELL> cells = new LinkedHashMap<>(); /** * Creates a new row belonging to the given section. @@ -102,10 +104,17 @@ public abstract class StaticSection<ROW extends StaticSection.StaticRow<?>> * @param columnId * the id of the column from which to remove the cell */ - protected void removeCell(Object columnId) { + protected void removeCell(String columnId) { CELL cell = cells.remove(columnId); if (cell != null) { - rowState.cells.remove(cell.getCellState()); + rowState.cells.remove(columnId); + for (Iterator<Set<String>> iterator = rowState.cellGroups.values().iterator(); iterator.hasNext(); ) { + Set<String> group = iterator.next(); + group.remove(columnId); + if(group.size() < 2) { + iterator.remove(); + } + } } } @@ -143,6 +152,23 @@ public abstract class StaticSection<ROW extends StaticSection.StaticRow<?>> cell.detach(); } } + + void checkIfAlreadyMerged(String columnId) { + for (Set<String> cellGroup : getRowState().cellGroups.values()) { + if (cellGroup.contains(columnId)) { + throw new IllegalArgumentException( + "Cell " + columnId + " is already merged"); + } + } + if (!cells.containsKey(columnId)) { + throw new IllegalArgumentException( + "Cell " + columnId + " does not exist on this row"); + } + } + + void addMergedCell(CELL newCell, Set<String> columnGroup) { + rowState.cellGroups.put(newCell.getCellState(), columnGroup); + } } /** @@ -161,7 +187,7 @@ public abstract class StaticSection<ROW extends StaticSection.StaticRow<?>> cellState.columnId = id; } - String getColumnId() { + public String getColumnId() { return cellState.columnId; } @@ -411,6 +437,7 @@ public abstract class StaticSection<ROW extends StaticSection.StaticRow<?>> for (ROW row : rows) { row.removeCell(columnId); } + markAsDirty(); } /** |