diff options
author | Leif Åstrand <legioth@gmail.com> | 2017-02-07 15:33:20 +0200 |
---|---|---|
committer | Pekka Hyvönen <pekka@vaadin.com> | 2017-02-07 15:33:20 +0200 |
commit | b57a9cbd6e2d6eeed83e7d9ddd0e61561b3cd557 (patch) | |
tree | a52a38b1b31ce1c2d917890254c0db8c5b855bde | |
parent | 19fdb91d4b8c926431995e157bdf440436209c2d (diff) | |
download | vaadin-framework-b57a9cbd6e2d6eeed83e7d9ddd0e61561b3cd557.tar.gz vaadin-framework-b57a9cbd6e2d6eeed83e7d9ddd0e61561b3cd557.zip |
Add Grid.addColumn(String, Renderer) (#8470)
* Add Grid.addColumn(String, Renderer)
-rw-r--r-- | server/src/main/java/com/vaadin/ui/Grid.java | 43 | ||||
-rw-r--r-- | server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java | 26 |
2 files changed, 64 insertions, 5 deletions
diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index fc45ffe539..7842faf503 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -2096,8 +2096,26 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, } /** - * Adds a new column with the given property name. The property name will be - * used as the {@link Column#getId() column id} and the + * Adds a new column with the given property name. The column will use a + * {@link TextRenderer}. The value is converted to a String using + * {@link Object#toString()}. The property name will be used as the + * {@link Column#getId() column id} and the {@link Column#getCaption() + * column caption} will be set based on the property definition. + * <p> + * This method can only be used for a <code>Grid</code> created using + * {@link Grid#Grid(Class)} or {@link #withPropertySet(PropertySet)}. + * + * @param propertyName + * the property name of the new column, not <code>null</code> + * @return the newly added column, not <code>null</code> + */ + public Column<T, ?> addColumn(String propertyName) { + return addColumn(propertyName, new TextRenderer()); + } + + /** + * Adds a new column with the given property name and renderer. The property + * name will be used as the {@link Column#getId() column id} and the * {@link Column#getCaption() column caption} will be set based on the * property definition. * <p> @@ -2106,10 +2124,14 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, * * @param propertyName * the property name of the new column, not <code>null</code> + * @param renderer + * the renderer to use, not <code>null</code> * @return the newly added column, not <code>null</code> */ - public Column<T, ?> addColumn(String propertyName) { + public Column<T, ?> addColumn(String propertyName, + AbstractRenderer<? super T, ?> renderer) { Objects.requireNonNull(propertyName, "Property name cannot be null"); + Objects.requireNonNull(renderer, "Renderer cannot be null"); if (getColumn(propertyName) != null) { throw new IllegalStateException( @@ -2122,8 +2144,19 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, "Could not resolve property name " + propertyName + " from " + propertySet)); - return addColumn(definition.getGetter()).setId(definition.getName()) - .setCaption(definition.getCaption()); + if (!renderer.getPresentationType() + .isAssignableFrom(definition.getType())) { + throw new IllegalArgumentException(renderer.toString() + + " cannot be used with a property of type " + + definition.getType().getName()); + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + Column<T, ?> column = addColumn(definition.getGetter(), + (AbstractRenderer) renderer).setId(definition.getName()) + .setCaption(definition.getCaption()); + + return column; } /** diff --git a/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java b/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java index 57c4107a6b..9762b7235e 100644 --- a/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java +++ b/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -494,6 +495,31 @@ public class GridTest { values.stream().sorted(comparator).toArray()); } + @Test + public void addBeanColumn_validRenderer() { + Grid<Person> grid = new Grid<>(Person.class); + + grid.removeColumn("born"); + grid.addColumn("born", new NumberRenderer(new DecimalFormat("#,###"))); + + Person person = new Person("Name", 2017); + + JsonObject rowData = getRowData(grid, person); + + String formattedValue = Stream.of(rowData.keys()) + .map(rowData::getString).filter(value -> !value.equals("Name")) + .findFirst().orElse(null); + Assert.assertEquals(formattedValue, "2,017"); + } + + @Test(expected = IllegalArgumentException.class) + public void addBeanColumn_invalidRenderer() { + Grid<Person> grid = new Grid<>(Person.class); + + grid.removeColumn("name"); + grid.addColumn("name", new NumberRenderer()); + } + private static <T> JsonObject getRowData(Grid<T> grid, T row) { JsonObject json = Json.createObject(); if (grid.getColumns().isEmpty()) { |