]> source.dussan.org Git - vaadin-framework.git/commitdiff
Use column id as the default sort order property (#8529)
authorLeif Åstrand <legioth@gmail.com>
Fri, 10 Feb 2017 07:47:50 +0000 (09:47 +0200)
committerPekka Hyvönen <pekka@vaadin.com>
Fri, 10 Feb 2017 07:47:50 +0000 (09:47 +0200)
server/src/main/java/com/vaadin/ui/Grid.java
server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java

index fb5eeb6428ae2ab73e7fba45fcbec977ab476bca..b11bef7eed56baa21b0f6535b264199176d480ca 100644 (file)
@@ -818,7 +818,15 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
 
         private final SerializableFunction<T, ? extends V> valueProvider;
 
-        private SortOrderProvider sortOrderProvider;
+        private SortOrderProvider sortOrderProvider = direction -> {
+            String id = getId();
+            if (id == null) {
+                return Stream.empty();
+            } else {
+                return Stream.of(new QuerySortOrder(id, direction));
+            }
+        };
+
         private SerializableComparator<T> comparator;
         private StyleGenerator<T> styleGenerator = item -> null;
         private DescriptionGenerator<T> descriptionGenerator;
@@ -849,7 +857,6 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
             state.renderer = renderer;
 
             state.caption = "";
-            sortOrderProvider = d -> Stream.of();
 
             // Add the renderer as a child extension of this extension, thus
             // ensuring the renderer will be unregistered when this column is
@@ -1040,9 +1047,17 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
         /**
          * Sets the user-defined identifier to map this column. The identifier
          * can be used for example in {@link Grid#getColumn(String)}.
+         * <p>
+         * The id is also used as the {@link #setSortProperty(String...) backend
+         * sort property} for this column if no sort property or sort order
+         * provider has been set for this column.
+         *
+         * @see #setSortProperty(String...)
+         * @see #setSortOrderProvider(SortOrderProvider)
          *
          * @param id
          *            the identifier string
+         * @return this column
          */
         public Column<T, V> setId(String id) {
             Objects.requireNonNull(id, "Column identifier cannot be null");
@@ -1052,6 +1067,7 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
             }
             this.userId = id;
             getGrid().setColumnId(id, this);
+
             return this;
         }
 
@@ -1143,6 +1159,9 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
         /**
          * Sets strings describing back end properties to be used when sorting
          * this column.
+         * <p>
+         * By default, the {@link #setId(String) column id} will be used as the
+         * sort property.
          *
          * @param properties
          *            the array of strings describing backend properties
@@ -1159,6 +1178,9 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
          * Sets the sort orders when sorting this column. The sort order
          * provider is a function which provides {@link QuerySortOrder} objects
          * to describe how to sort by this column.
+         * <p>
+         * By default, the {@link #setId(String) column id} will be used as the
+         * sort property.
          *
          * @param provider
          *            the function to use when generating sort orders with the
@@ -1176,6 +1198,10 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
          * Gets the sort orders to use with back-end sorting for this column
          * when sorting in the given direction.
          *
+         * @see #setSortProperty(String...)
+         * @see #setId(String)
+         * @see #setSortOrderProvider(SortOrderProvider)
+         *
          * @param direction
          *            the sorting direction
          * @return stream of sort orders
index 9762b7235e89caec55cd22ac97982aa9c3ce37cd..fafbd4c90009a0000ef96b5ad101b9a3482e4653 100644 (file)
@@ -27,6 +27,7 @@ import com.vaadin.data.Binder.Binding;
 import com.vaadin.data.ValidationException;
 import com.vaadin.data.ValueProvider;
 import com.vaadin.data.provider.GridSortOrder;
+import com.vaadin.data.provider.QuerySortOrder;
 import com.vaadin.data.provider.bov.Person;
 import com.vaadin.event.selection.SelectionEvent;
 import com.vaadin.server.SerializableComparator;
@@ -306,6 +307,9 @@ public class GridTest {
 
         Assert.assertEquals(new HashSet<>(Arrays.asList("Lorem", "2000")),
                 values);
+
+        assertSingleSortProperty(nameColumn, "name");
+        assertSingleSortProperty(bornColumn, "born");
     }
 
     @Test
@@ -520,6 +524,37 @@ public class GridTest {
         grid.addColumn("name", new NumberRenderer());
     }
 
+    @Test
+    public void columnId_sortProperty() {
+        assertSingleSortProperty(lengthColumn, "length");
+    }
+
+    @Test
+    public void columnId_sortProperty_noId() {
+        Assert.assertEquals(0,
+                objectColumn.getSortOrder(SortDirection.ASCENDING).count());
+    }
+
+    @Test
+    public void sortProperty_setId_doesntOverride() {
+        objectColumn.setSortProperty("foo");
+        objectColumn.setId("bar");
+
+        assertSingleSortProperty(objectColumn, "foo");
+    }
+
+    private static void assertSingleSortProperty(Column<?, ?> column,
+            String expectedProperty) {
+        QuerySortOrder[] sortOrders = column
+                .getSortOrder(SortDirection.ASCENDING)
+                .toArray(QuerySortOrder[]::new);
+
+        Assert.assertEquals(1, sortOrders.length);
+        Assert.assertEquals(SortDirection.ASCENDING,
+                sortOrders[0].getDirection());
+        Assert.assertEquals(expectedProperty, sortOrders[0].getSorted());
+    }
+
     private static <T> JsonObject getRowData(Grid<T> grid, T row) {
         JsonObject json = Json.createObject();
         if (grid.getColumns().isEmpty()) {