diff options
11 files changed, 441 insertions, 38 deletions
diff --git a/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java b/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java index b50b5c7fc8..13694010c7 100644 --- a/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java +++ b/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java @@ -17,6 +17,7 @@ package com.vaadin.data.provider; import java.util.Collections; import java.util.List; +import java.util.Objects; /** * A data provider that lazy loads items from a back end. @@ -45,6 +46,22 @@ public interface BackEndDataProvider<T, F> extends DataProvider<T, F> { void setSortOrders(List<QuerySortOrder> sortOrders); /** + * Sets the sort order to use, given a {@link QuerySortOrderBuilder}. + * Shorthand for {@code setSortOrders(builder.build())}. + * + * @see QuerySortOrderBuilder + * + * @param builder + * the sort builder to retrieve the sort order from + * @throws NullPointerException + * if builder is null + */ + default void setSortOrders(QuerySortOrderBuilder builder) { + Objects.requireNonNull("Sort builder cannot be null."); + setSortOrders(builder.build()); + } + + /** * Sets a single sort order to use as the default sorting for this data * provider. This overrides the sorting set by any other method that * manipulates the default sorting of this data provider. diff --git a/server/src/main/java/com/vaadin/data/provider/GridSortOrder.java b/server/src/main/java/com/vaadin/data/provider/GridSortOrder.java new file mode 100644 index 0000000000..ffb17d301d --- /dev/null +++ b/server/src/main/java/com/vaadin/data/provider/GridSortOrder.java @@ -0,0 +1,80 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.data.provider; + +import com.vaadin.shared.data.sort.SortDirection; +import com.vaadin.ui.Grid.Column; + +/** + * Sorting information for {@link Grid}. + * + * @param <T> + * the grid type + */ +public class GridSortOrder<T> extends SortOrder<Column<T, ?>> { + + /** + * Construct sorting information for usage in a {@link Grid}. + * + * @param column + * the column to be sorted + * @param direction + * sorting direction + */ + public GridSortOrder(Column<T, ?> column, SortDirection direction) { + super(column, direction); + } + + /** + * Gets the column this sorting information is attached to. + * + * @return the column being sorted + */ + @Override + public Column<T, ?> getSorted() { + return super.getSorted(); + } + + /** + * Creates a new grid sort builder with given sorting using ascending sort + * direction. + * + * @param by + * the column to sort by + * @param <T> + * the grid type + * + * @return the grid sort builder + */ + public static <T> GridSortOrderBuilder<T> asc(Column<T, ?> by) { + return new GridSortOrderBuilder<T>().thenAsc(by); + } + + /** + * Creates a new grid sort builder with given sorting using descending sort + * direction. + * + * @param by + * the column to sort by + * @param <T> + * the grid type + * + * @return the grid sort builder + */ + public static <T> GridSortOrderBuilder<T> desc(Column<T, ?> by) { + return new GridSortOrderBuilder<T>().thenDesc(by); + } +} diff --git a/server/src/main/java/com/vaadin/data/provider/GridSortOrderBuilder.java b/server/src/main/java/com/vaadin/data/provider/GridSortOrderBuilder.java new file mode 100644 index 0000000000..3aba2eba6b --- /dev/null +++ b/server/src/main/java/com/vaadin/data/provider/GridSortOrderBuilder.java @@ -0,0 +1,52 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.data.provider; + +import com.vaadin.shared.data.sort.SortDirection; +import com.vaadin.ui.Grid.Column; + +/** + * Helper classes with fluent API for constructing {@link GridSortOrder} lists. + * When the sort order is ready to be passed on, calling {@link #build()} will + * create the list of sort orders. + * + * @see GridSortOrder + * @see #thenAsc(Column) + * @see #thenDesc(Column) + * @see #build() + * + * @param <T> + * the type of the grid + */ +public class GridSortOrderBuilder<T> + extends SortOrderBuilder<GridSortOrder<T>, Column<T, ?>> { + + @Override + public GridSortOrderBuilder<T> thenAsc(Column<T, ?> by) { + return (GridSortOrderBuilder<T>) super.thenAsc(by); + } + + @Override + public GridSortOrderBuilder<T> thenDesc(Column<T, ?> by) { + return (GridSortOrderBuilder<T>) super.thenDesc(by); + } + + @Override + protected GridSortOrder<T> createSortOrder(Column<T, ?> by, + SortDirection direction) { + return new GridSortOrder<>(by, direction); + } +} diff --git a/server/src/main/java/com/vaadin/data/provider/QuerySortOrder.java b/server/src/main/java/com/vaadin/data/provider/QuerySortOrder.java index ca8d743fae..7f0a76bf91 100644 --- a/server/src/main/java/com/vaadin/data/provider/QuerySortOrder.java +++ b/server/src/main/java/com/vaadin/data/provider/QuerySortOrder.java @@ -45,4 +45,30 @@ public class QuerySortOrder extends SortOrder<String> { public String getSorted() { return super.getSorted(); } + |