diff options
author | Mika Murtojarvi <mika@vaadin.com> | 2015-04-21 14:15:20 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2015-04-23 11:14:01 +0000 |
commit | 0efb58c22e3be4254de07badc0d7e4695baa7210 (patch) | |
tree | 420221e380cf997528248a5ffd4dda314f15b141 | |
parent | 443744ff578545fb57cf77875097ea50f8af4000 (diff) | |
download | vaadin-framework-0efb58c22e3be4254de07badc0d7e4695baa7210.tar.gz vaadin-framework-0efb58c22e3be4254de07badc0d7e4695baa7210.zip |
Add a method for setting visible Grid columns (#17080)
Change-Id: I7ad63af87ae44021bf161d7eadc92ccf33bbd2bb
-rw-r--r-- | server/src/com/vaadin/ui/Grid.java | 23 | ||||
-rw-r--r-- | server/tests/src/com/vaadin/tests/server/component/grid/GridColumns.java | 27 |
2 files changed, 50 insertions, 0 deletions
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 @@ -3862,6 +3862,29 @@ public class Grid extends AbstractComponent implements SelectionNotifier, } /** + * 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<Object>(columns.keySet()); + removePids.removeAll(Arrays.asList(propertyIds)); + for (Object removePid : removePids) { + removeColumn(removePid); + } + Set<?> addPids = new HashSet<Object>(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 * grid. 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<Column> 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<Column> 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"); + } } |