]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add Grid.addColumn(String, Renderer) (#8470)
authorLeif Åstrand <legioth@gmail.com>
Tue, 7 Feb 2017 13:33:20 +0000 (15:33 +0200)
committerPekka Hyvönen <pekka@vaadin.com>
Tue, 7 Feb 2017 13:33:20 +0000 (15:33 +0200)
* Add Grid.addColumn(String, Renderer)

server/src/main/java/com/vaadin/ui/Grid.java
server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java

index fc45ffe5397f52ed21327f77bb90df98b3b9ca16..7842faf5038c532cd2a22e3bf99655901d77c0e5 100644 (file)
@@ -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;
     }
 
     /**
index 57c4107a6b31e82c60304bcab6ad5862d7763418..9762b7235e89caec55cd22ac97982aa9c3ce37cd 100644 (file)
@@ -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()) {