diff options
7 files changed, 180 insertions, 11 deletions
diff --git a/client/src/com/vaadin/client/ui/grid/FlyweightCell.java b/client/src/com/vaadin/client/ui/grid/FlyweightCell.java index de003f865c..b82fccfbe0 100644 --- a/client/src/com/vaadin/client/ui/grid/FlyweightCell.java +++ b/client/src/com/vaadin/client/ui/grid/FlyweightCell.java @@ -144,6 +144,11 @@ public class FlyweightCell { } public void setColSpan(final int numberOfCells) { + if (numberOfCells < 1) { + throw new IllegalArgumentException( + "Number of cells should be more than 0"); + } + /*- * This will default to 1 if unset, as per DOM specifications: * http://www.w3.org/TR/html5/tabular-data.html#attributes-common-to-td-and-th-elements diff --git a/client/src/com/vaadin/client/ui/grid/Grid.java b/client/src/com/vaadin/client/ui/grid/Grid.java index cff9f68454..580cf41165 100644 --- a/client/src/com/vaadin/client/ui/grid/Grid.java +++ b/client/src/com/vaadin/client/ui/grid/Grid.java @@ -45,6 +45,7 @@ import com.vaadin.client.Util; import com.vaadin.client.data.DataChangeHandler; import com.vaadin.client.data.DataSource; import com.vaadin.client.ui.SubPartAware; +import com.vaadin.client.ui.grid.GridFooter.FooterRow; import com.vaadin.client.ui.grid.GridHeader.HeaderRow; import com.vaadin.client.ui.grid.GridStaticSection.StaticCell; import com.vaadin.client.ui.grid.renderers.ComplexRenderer; @@ -923,7 +924,6 @@ public class Grid<T> extends Composite implements this.visible = visible; - // Remove column if (grid != null) { int index = findIndexOfColumn(); ColumnConfiguration conf = grid.escalator @@ -934,8 +934,15 @@ public class Grid<T> extends Composite implements } else { conf.removeColumns(index, 1); } - } + for (HeaderRow row : grid.getHeader().getRows()) { + row.calculateColspans(); + } + + for (FooterRow row : grid.getFooter().getRows()) { + row.calculateColspans(); + } + } } /** diff --git a/client/src/com/vaadin/client/ui/grid/GridFooter.java b/client/src/com/vaadin/client/ui/grid/GridFooter.java index eba6ad81cb..4470bbf6b9 100644 --- a/client/src/com/vaadin/client/ui/grid/GridFooter.java +++ b/client/src/com/vaadin/client/ui/grid/GridFooter.java @@ -15,6 +15,8 @@ */ package com.vaadin.client.ui.grid; +import com.google.gwt.core.client.Scheduler; + /** * Represents the footer section of a Grid. The footer is always empty. * @@ -50,6 +52,8 @@ public class GridFooter extends GridStaticSection<GridFooter.FooterRow> { public class FooterCell extends GridStaticSection.StaticCell { } + private boolean markAsDirty = false; + @Override protected FooterRow createRow() { return new FooterRow(); @@ -57,6 +61,22 @@ public class GridFooter extends GridStaticSection<GridFooter.FooterRow> { @Override protected void refreshSection() { - getGrid().refreshFooter(); + markAsDirty = true; + + /* + * Defer the refresh so if we multiple times call refreshSection() (for + * example when updating cell values) we only get one actual refresh in + * the end. + */ + Scheduler.get().scheduleFinally(new Scheduler.ScheduledCommand() { + + @Override + public void execute() { + if (markAsDirty) { + markAsDirty = false; + getGrid().refreshFooter(); + } + } + }); } } diff --git a/client/src/com/vaadin/client/ui/grid/GridHeader.java b/client/src/com/vaadin/client/ui/grid/GridHeader.java index 4e046873f4..e139d7b946 100644 --- a/client/src/com/vaadin/client/ui/grid/GridHeader.java +++ b/client/src/com/vaadin/client/ui/grid/GridHeader.java @@ -15,6 +15,7 @@ */ package com.vaadin.client.ui.grid; +import com.google.gwt.core.client.Scheduler; import com.vaadin.client.ui.grid.Grid.AbstractGridColumn.SortableColumnHeaderRenderer; /** @@ -68,6 +69,8 @@ public class GridHeader extends GridStaticSection<GridHeader.HeaderRow> { private HeaderRow defaultRow; + private boolean markAsDirty = false; + @Override public void removeRow(int index) { HeaderRow removedRow = getRow(index); @@ -134,6 +137,22 @@ public class GridHeader extends GridStaticSection<GridHeader.HeaderRow> { @Override protected void refreshSection() { - getGrid().refreshHeader(); + markAsDirty = true; + + /* + * Defer the refresh so if we multiple times call refreshSection() (for + * example when updating cell values) we only get one actual refresh in + * the end. + */ + Scheduler.get().scheduleFinally(new Scheduler.ScheduledCommand() { + + @Override + public void execute() { + if (markAsDirty) { + markAsDirty = false; + getGrid().refreshHeader(); + } + } + }); } } diff --git a/client/src/com/vaadin/client/ui/grid/GridStaticSection.java b/client/src/com/vaadin/client/ui/grid/GridStaticSection.java index fa4f3e5ea0..c40c41594c 100644 --- a/client/src/com/vaadin/client/ui/grid/GridStaticSection.java +++ b/client/src/com/vaadin/client/ui/grid/GridStaticSection.java @@ -85,11 +85,18 @@ abstract class GridStaticSection<ROWTYPE extends GridStaticSection.StaticRow<?>> } /** + * Sets the colspan for the cell + * * @param colspan * the colspan to set */ public void setColspan(int colspan) { + if (colspan < 1) { + throw new IllegalArgumentException( + "Colspan cannot be less than 1"); + } this.colspan = colspan; + section.refreshSection(); } } @@ -201,7 +208,8 @@ abstract class GridStaticSection<ROWTYPE extends GridStaticSection.StaticRow<?>> return null; } - private void calculateColspans() { + void calculateColspans() { + // Reset all cells for (CELLTYPE cell : cells) { cell.setColspan(1); @@ -209,17 +217,52 @@ abstract class GridStaticSection<ROWTYPE extends GridStaticSection.StaticRow<?>> // Set colspan for grouped cells for (List<CELLTYPE> group : cellGroups) { + + int firstVisibleColumnInGroup = -1; + int lastVisibleColumnInGroup = -1; + int hiddenInsideGroup = 0; + + /* + * To be able to calculate the colspan correctly we need to two + * things; find the first visible cell in the group which will + * get the colspan assigned to and find the amount of columns + * which should be spanned. + * + * To do that we iterate through all cells, marking into memory + * when we find the first visible cell, when we find the last + * visible cell and how many cells are hidden in between. + */ for (int i = 0; i < group.size(); i++) { CELLTYPE cell = group.get(i); - if (i == 0) { - // Assign full colspan to first cell - cell.setColspan(group.size()); - } else { - // Hide other cells - cell.setColspan(0); + int cellIndex = this.cells.indexOf(cell); + boolean columnVisible = getSection().getGrid() + .getColumn(cellIndex).isVisible(); + if (columnVisible) { + lastVisibleColumnInGroup = i; + if (firstVisibleColumnInGroup == -1) { + firstVisibleColumnInGroup = i; + } + } else if (firstVisibleColumnInGroup != -1) { + hiddenInsideGroup++; } } + + if (firstVisibleColumnInGroup == -1 + || lastVisibleColumnInGroup == -1 + || firstVisibleColumnInGroup == lastVisibleColumnInGroup) { + // No cells in group + continue; + } + + /* + * Assign colspan to first cell in group. + */ + CELLTYPE firstVisibleCell = group + .get(firstVisibleColumnInGroup); + firstVisibleCell.setColspan(lastVisibleColumnInGroup + - firstVisibleColumnInGroup - hiddenInsideGroup + 1); } + } protected void addCell(int index) { diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridHeaderTest.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridHeaderTest.java index 43d5aa47df..2665d5c669 100644 --- a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridHeaderTest.java +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridHeaderTest.java @@ -211,6 +211,71 @@ public class GridHeaderTest extends GridStaticSectionTest { } } + @Test + public void hideFirstColumnInColspan() throws Exception { + openTestURL(); + + selectMenuPath("Component", "Header", "Append row"); + + selectMenuPath("Component", "Header", "Row 2", "Join all columns"); + + int visibleColumns = GridBasicFeatures.COLUMNS; + + GridCellElement spannedCell = getGridElement().getHeaderCell(1, 0); + assertTrue(spannedCell.isDisplayed()); + assertEquals("" + visibleColumns, spannedCell.getAttribute("colspan")); + + selectMenuPath("Component", "Columns", "Column 0", "Visible"); + visibleColumns--; + + spannedCell = getGridElement().getHeaderCell(1, 0); + assertTrue(spannedCell.isDisplayed()); + assertEquals("" + visibleColumns, spannedCell.getAttribute("colspan")); + } + + @Test + public void multipleColspanAndMultipleHiddenColumns() throws Exception { + openTestURL(); + + selectMenuPath("Component", "Header", "Append row"); + + // Join columns [1,2] and [3,4,5] + selectMenuPath("Component", "Header", "Row 2", "Join columns 1, 2"); + GridCellElement spannedCell = getGridElement().getHeaderCell(1, 1); + assertEquals("2", spannedCell.getAttribute("colspan")); + + selectMenuPath("Component", "Header", "Row 2", "Join columns 3, 4, 5"); + spannedCell = getGridElement().getHeaderCell(1, 3); + assertEquals("3", spannedCell.getAttribute("colspan")); + + selectMenuPath("Component", "Columns", "Column 2", "Visible"); + spannedCell = getGridElement().getHeaderCell(1, 1); + assertEquals("1", spannedCell.getAttribute("colspan")); + + // Ensure the second colspan is preserved (shifts one index to the left) + spannedCell = getGridElement().getHeaderCell(1, 2); + assertEquals("3", spannedCell.getAttribute("colspan")); + + selectMenuPath("Component", "Columns", "Column 4", "Visible"); + + // First reduced colspan is reduced + spannedCell = getGridElement().getHeaderCell(1, 1); + assertEquals("1", spannedCell.getAttribute("colspan")); + + // Second colspan is also now reduced + spannedCell = getGridElement().getHeaderCell(1, 2); + assertEquals("2", spannedCell.getAttribute("colspan")); + + // Show columns again + selectMenuPath("Component", "Columns", "Column 2", "Visible"); + selectMenuPath("Component", "Columns", "Column 4", "Visible"); + + spannedCell = getGridElement().getHeaderCell(1, 1); + assertEquals("2", spannedCell.getAttribute("colspan")); + spannedCell = getGridElement().getHeaderCell(1, 3); + assertEquals("3", spannedCell.getAttribute("colspan")); + } + private void assertHeaderCount(int count) { assertEquals("header count", count, getGridElement().getHeaderCount()); } diff --git a/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeatures.java b/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeatures.java index 1c9758b669..24cfe49239 100644 --- a/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeatures.java +++ b/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeatures.java @@ -396,6 +396,16 @@ public class GridBasicClientFeatures extends } }, menuPath); + addMenuCommand("Join columns 3, 4, 5", new ScheduledCommand() { + + @Override + public void execute() { + row.join(grid.getColumn(3), grid.getColumn(4), + grid.getColumn(5)); + + } + }, menuPath); + addMenuCommand("Join all columns", new ScheduledCommand() { @Override |