From: Mika Murtojarvi Date: Tue, 21 Apr 2015 11:15:20 +0000 (+0300) Subject: Add a method for setting visible Grid columns (#17080) X-Git-Tag: 7.5.0.beta1~72 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0efb58c22e3be4254de07badc0d7e4695baa7210;p=vaadin-framework.git Add a method for setting visible Grid columns (#17080) Change-Id: I7ad63af87ae44021bf161d7eadc92ccf33bbd2bb --- diff --git a/server/src/com/vaadin/ui/Grid.java b/server/src/com/vaadin/ui/Grid.java index a2487411c2..22d6f009e4 100644 --- a/server/src/com/vaadin/ui/Grid.java +++ b/server/src/com/vaadin/ui/Grid.java @@ -3861,6 +3861,29 @@ public class Grid extends AbstractComponent implements SelectionNotifier, removeExtension(column.getRenderer()); } + /** + * Sets the columns and their order for the grid. Current columns whose + * property id is not in propertyIds are removed. Similarly, a column is + * added for any property id in propertyIds that has no corresponding column + * in this Grid. + * + * @param propertyIds + * properties in the desired column order + */ + public void setColumns(Object... propertyIds) { + Set removePids = new HashSet(columns.keySet()); + removePids.removeAll(Arrays.asList(propertyIds)); + for (Object removePid : removePids) { + removeColumn(removePid); + } + Set addPids = new HashSet(Arrays.asList(propertyIds)); + addPids.removeAll(columns.keySet()); + for (Object propertyId : addPids) { + addColumn(propertyId); + } + setColumnOrder(propertyIds); + } + /** * Sets a new column order for the grid. All columns which are not ordered * here will remain in the order they were before as the last columns of diff --git a/server/tests/src/com/vaadin/tests/server/component/grid/GridColumns.java b/server/tests/src/com/vaadin/tests/server/component/grid/GridColumns.java index dbc8cfff3c..35553bb406 100644 --- a/server/tests/src/com/vaadin/tests/server/component/grid/GridColumns.java +++ b/server/tests/src/com/vaadin/tests/server/component/grid/GridColumns.java @@ -25,6 +25,7 @@ import static org.junit.Assert.fail; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Set; @@ -311,4 +312,30 @@ public class GridColumns { assertEquals("Column sortability changed when re-adding", sortable, grid.getColumn("column1").isSortable()); } + + @Test + public void testSetColumns() { + grid.setColumns("column7", "column0", "column9"); + Iterator it = grid.getColumns().iterator(); + assertEquals(it.next().getPropertyId(), "column7"); + assertEquals(it.next().getPropertyId(), "column0"); + assertEquals(it.next().getPropertyId(), "column9"); + assertFalse(it.hasNext()); + } + + @Test + public void testAddingColumnsWithSetColumns() { + Grid g = new Grid(); + g.setColumns("c1", "c2", "c3"); + Iterator it = g.getColumns().iterator(); + assertEquals(it.next().getPropertyId(), "c1"); + assertEquals(it.next().getPropertyId(), "c2"); + assertEquals(it.next().getPropertyId(), "c3"); + assertFalse(it.hasNext()); + } + + @Test(expected = IllegalStateException.class) + public void testAddingColumnsWithSetColumnsNonDefaultContainer() { + grid.setColumns("column1", "column2", "column50"); + } }