summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis <denis@vaadin.com>2016-12-15 17:41:34 +0200
committerTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>2016-12-15 17:41:34 +0200
commit80a406c075ecdc91c1c439a04796e72a7642bf23 (patch)
treed1012c72b7984ba87525a52b18c098aa1c07ac9c
parentdc01a9a4d85f78dba63c310fb9dc478cf1d40f8d (diff)
downloadvaadin-framework-80a406c075ecdc91c1c439a04796e72a7642bf23.tar.gz
vaadin-framework-80a406c075ecdc91c1c439a04796e72a7642bf23.zip
Add a SortListener to a Grid (#8008)8.0.0.alpha10
Fixes vaadin/framework8-issues#568
-rw-r--r--server/src/main/java/com/vaadin/event/SortEvent.java117
-rw-r--r--server/src/main/java/com/vaadin/ui/Grid.java27
-rw-r--r--server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java42
-rw-r--r--uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java7
-rw-r--r--uitest/src/main/java/com/vaadin/v7/tests/components/grid/basicfeatures/GridBasicFeatures.java4
-rw-r--r--uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridSortingTest.java13
6 files changed, 204 insertions, 6 deletions
diff --git a/server/src/main/java/com/vaadin/event/SortEvent.java b/server/src/main/java/com/vaadin/event/SortEvent.java
new file mode 100644
index 0000000000..496e5863f9
--- /dev/null
+++ b/server/src/main/java/com/vaadin/event/SortEvent.java
@@ -0,0 +1,117 @@
+/*
+ * 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.event;
+
+import java.io.Serializable;
+import java.util.List;
+
+import com.vaadin.data.provider.SortOrder;
+import com.vaadin.shared.Registration;
+import com.vaadin.ui.Component;
+
+/**
+ * Event describing a change in sorting of a {@link Container}. Fired by
+ * {@link SortNotifier SortNotifiers}.
+ *
+ * @see SortListener
+ * @see SortOrder
+ * @param <T>
+ * the type of the sorting information, usually a String (field id)
+ * or a {@link java.util.Comparator}.
+ *
+ * @since 8.0
+ * @author Vaadin Ltd
+ */
+public class SortEvent<T> extends Component.Event {
+
+ private final List<SortOrder<T>> sortOrder;
+ private final boolean userOriginated;
+
+ /**
+ * Creates a new sort order change event with a sort order list.
+ *
+ * @param source
+ * the component from which the event originates
+ * @param sortOrder
+ * the new sort order list
+ * @param userOriginated
+ * <code>true</code> if event is a result of user interaction,
+ * <code>false</code> if from API call
+ */
+ public SortEvent(Component source, List<SortOrder<T>> sortOrder,
+ boolean userOriginated) {
+ super(source);
+ this.sortOrder = sortOrder;
+ this.userOriginated = userOriginated;
+ }
+
+ /**
+ * Gets the sort order list.
+ *
+ * @return the sort order list
+ */
+ public List<SortOrder<T>> getSortOrder() {
+ return sortOrder;
+ }
+
+ /**
+ * Returns whether this event originated from actions done by the user.
+ *
+ * @return true if sort event originated from user interaction
+ */
+ public boolean isUserOriginated() {
+ return userOriginated;
+ }
+
+ /**
+ * Listener for sort order change events.
+ *
+ * @param <T>
+ * the type of the sorting information, usually a String (field
+ * id) or a {@link java.util.Comparator}.
+ */
+ @FunctionalInterface
+ public interface SortListener<T> extends Serializable {
+ /**
+ * Called when the sort order has changed.
+ *
+ * @param event
+ * the sort order change event
+ */
+ public void sort(SortEvent<T> event);
+ }
+
+ /**
+ * The interface for adding and removing listeners for {@link SortEvent
+ * SortEvents}.
+ *
+ * @param <T>
+ * the type of the sorting information, usually a String (field
+ * id) or a {@link java.util.Comparator}.
+ */
+ public interface SortNotifier<T> extends Serializable {
+ /**
+ * Adds a sort order change listener that gets notified when the sort
+ * order changes.
+ *
+ * @param listener
+ * the sort order change listener to add
+ * @return a registration object for removing the listener
+ */
+ public Registration addSortListener(SortListener<T> listener);
+
+ }
+}
diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java
index bb48ac56a4..b7a3690f03 100644
--- a/server/src/main/java/com/vaadin/ui/Grid.java
+++ b/server/src/main/java/com/vaadin/ui/Grid.java
@@ -51,6 +51,9 @@ import com.vaadin.data.provider.Query;
import com.vaadin.data.provider.SortOrder;
import com.vaadin.event.ConnectorEvent;
import com.vaadin.event.ContextClickEvent;
+import com.vaadin.event.SortEvent;
+import com.vaadin.event.SortEvent.SortListener;
+import com.vaadin.event.SortEvent.SortNotifier;
import com.vaadin.event.selection.MultiSelectionListener;
import com.vaadin.event.selection.SelectionListener;
import com.vaadin.event.selection.SingleSelectionListener;
@@ -74,7 +77,6 @@ import com.vaadin.shared.ui.grid.GridState;
import com.vaadin.shared.ui.grid.GridStaticCellType;
import com.vaadin.shared.ui.grid.HeightMode;
import com.vaadin.shared.ui.grid.SectionState;
-import com.vaadin.ui.Grid.SelectionMode;
import com.vaadin.ui.components.grid.ColumnReorderListener;
import com.vaadin.ui.components.grid.ColumnResizeListener;
import com.vaadin.ui.components.grid.ColumnVisibilityChangeListener;
@@ -121,14 +123,17 @@ import elemental.json.JsonValue;
* @param <T>
* the grid bean type
*/
-public class Grid<T> extends AbstractListing<T>
- implements HasComponents, Listing<T, DataProvider<T, ?>> {
+public class Grid<T> extends AbstractListing<T> implements HasComponents,
+ Listing<T, DataProvider<T, ?>>, SortNotifier<Grid.Column<T, ?>> {
@Deprecated
private static final Method COLUMN_REORDER_METHOD = ReflectTools.findMethod(
ColumnReorderListener.class, "columnReorder",
ColumnReorderEvent.class);
+ private static final Method SORT_ORDER_CHANGE_METHOD = ReflectTools
+ .findMethod(SortListener.class, "sort", SortEvent.class);
+
@Deprecated
private static final Method COLUMN_RESIZE_METHOD = ReflectTools.findMethod(
ColumnResizeListener.class, "columnResize",
@@ -2809,6 +2814,18 @@ public class Grid<T> extends AbstractListing<T>
}
/**
+ * Adds a sort order change listener that gets notified when the sort order
+ * changes.
+ *
+ * @param listener
+ * the sort order change listener to add
+ */
+ @Override
+ public Registration addSortListener(SortListener<Column<T, ?>> listener) {
+ return addListener(SortEvent.class, listener, SORT_ORDER_CHANGE_METHOD);
+ }
+
+ /**
* Get the current sort order list.
*
* @return a sort order list
@@ -3164,6 +3181,8 @@ public class Grid<T> extends AbstractListing<T>
// Grid is not sorted anymore.
getDataCommunicator().setBackEndSorting(Collections.emptyList());
getDataCommunicator().setInMemorySorting(null);
+ fireEvent(new SortEvent<>(this, new ArrayList<>(sortOrder),
+ userOriginated));
return;
}
sortOrder.addAll(order);
@@ -3193,6 +3212,8 @@ public class Grid<T> extends AbstractListing<T>
if (getEditor().isOpen()) {
getEditor().cancel();
}
+ fireEvent(new SortEvent<>(this, new ArrayList<>(sortOrder),
+ userOriginated));
}
}
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 26eab6c911..5dc49c5e8f 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,9 +4,11 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
+import java.util.concurrent.atomic.AtomicReference;
import org.easymock.Capture;
import org.junit.Assert;
@@ -218,4 +220,44 @@ public class GridTest {
assertEquals(0, grid.getSortOrder().size());
}
+
+ @Test
+ public void sortListener_eventIsFired() {
+ Column<String, ?> column1 = grid.getColumns().get(1);
+ Column<String, ?> column2 = grid.getColumns().get(2);
+
+ List<SortOrder<Column<String, ?>>> list = new ArrayList<>();
+ AtomicReference<Boolean> fired = new AtomicReference<>();
+ grid.addSortListener(event -> {
+ Assert.assertTrue(list.isEmpty());
+ fired.set(true);
+ list.addAll(event.getSortOrder());
+ });
+ grid.sort(column1, SortDirection.DESCENDING);
+
+ Assert.assertEquals(column1, list.get(0).getSorted());
+ Assert.assertEquals(SortDirection.DESCENDING,
+ list.get(0).getDirection());
+
+ List<SortOrder<Column<String, ?>>> order = Arrays.asList(
+ new SortOrder<>(column2, SortDirection.DESCENDING),
+ new SortOrder<>(column1, SortDirection.ASCENDING));
+ list.clear();
+
+ grid.setSortOrder(order);
+
+ Assert.assertEquals(column2, list.get(0).getSorted());
+ Assert.assertEquals(SortDirection.DESCENDING,
+ list.get(0).getDirection());
+
+ Assert.assertEquals(column1, list.get(1).getSorted());
+ Assert.assertEquals(SortDirection.ASCENDING,
+ list.get(1).getDirection());
+
+ list.clear();
+ fired.set(false);
+ grid.clearSortOrder();
+ Assert.assertEquals(0, list.size());
+ Assert.assertTrue(fired.get());
+ }
}
diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java b/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java
index 63ef591246..19867f9559 100644
--- a/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java
+++ b/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java
@@ -240,7 +240,12 @@ public class GridBasics extends AbstractTestUIWithLog {
event -> log("ColumnResizeEvent: isUserOriginated? "
+ event.isUserOriginated()));
- layout.addComponent(createMenu());
+ grid.addSortListener(event -> log(
+ "SortEvent: isUserOriginated? " + event.isUserOriginated()));
+
+ layout.addComponent(
+
+ createMenu());
layout.addComponent(grid);
addComponent(layout);
}
diff --git a/uitest/src/main/java/com/vaadin/v7/tests/components/grid/basicfeatures/GridBasicFeatures.java b/uitest/src/main/java/com/vaadin/v7/tests/components/grid/basicfeatures/GridBasicFeatures.java
index fa56567f19..49ee5c008a 100644
--- a/uitest/src/main/java/com/vaadin/v7/tests/components/grid/basicfeatures/GridBasicFeatures.java
+++ b/uitest/src/main/java/com/vaadin/v7/tests/components/grid/basicfeatures/GridBasicFeatures.java
@@ -53,7 +53,6 @@ import com.vaadin.v7.event.ItemClickEvent.ItemClickListener;
import com.vaadin.v7.event.SelectionEvent;
import com.vaadin.v7.event.SelectionEvent.SelectionListener;
import com.vaadin.v7.event.SortEvent;
-import com.vaadin.v7.event.SortEvent.SortListener;
import com.vaadin.v7.shared.ui.grid.ColumnResizeMode;
import com.vaadin.v7.shared.ui.grid.GridStaticCellType;
import com.vaadin.v7.shared.ui.grid.HeightMode;
@@ -357,7 +356,8 @@ public class GridBasicFeatures extends AbstractComponentTest<Grid> {
column.setHidable(isColumnHidableByDefault(col));
}
- grid.addSortListener(new SortListener() {
+ grid.addSortListener(new SortEvent.SortListener() {
+
@Override
public void sort(SortEvent event) {
log("SortEvent: isUserOriginated? " + event.isUserOriginated());
diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridSortingTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridSortingTest.java
index c172f53f3b..4a6ddc4f61 100644
--- a/uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridSortingTest.java
+++ b/uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridSortingTest.java
@@ -63,6 +63,9 @@ public class GridSortingTest extends GridBasicsTest {
public void serverSideOrderByColumn0() {
selectMenuPath("Component", "Columns", "Column 0", "Sort ASC");
+ Assert.assertEquals("1. SortEvent: isUserOriginated? false",
+ getLogRow(0));
+
Comparator<String> comparator = Comparator.naturalOrder();
int i = 0;
@@ -76,6 +79,8 @@ public class GridSortingTest extends GridBasicsTest {
Assert.assertTrue(i > 0);
selectMenuPath("Component", "Columns", "Column 0", "Sort DESC");
+ Assert.assertEquals("2. SortEvent: isUserOriginated? false",
+ getLogRow(0));
i = 0;
for (String coord : getTestData().map(DataObject::getCoordinates)
@@ -91,6 +96,9 @@ public class GridSortingTest extends GridBasicsTest {
public void serverSideOrderByDate() {
selectMenuPath("Component", "Columns", "Date", "Sort ASC");
+ Assert.assertEquals("1. SortEvent: isUserOriginated? false",
+ getLogRow(0));
+
Comparator<Date> comparator = Comparator.naturalOrder();
int i = 0;
@@ -105,6 +113,8 @@ public class GridSortingTest extends GridBasicsTest {
Assert.assertTrue(i > 0);
selectMenuPath("Component", "Columns", "Date", "Sort DESC");
+ Assert.assertEquals("2. SortEvent: isUserOriginated? false",
+ getLogRow(0));
i = 0;
for (Date date : getTestData().map(DataObject::getDate)
@@ -122,6 +132,9 @@ public class GridSortingTest extends GridBasicsTest {
selectMenuPath("Component", "Columns", "Column 0", "Sort ASC");
selectMenuPath("Component", "Columns", "Clear sort");
+ Assert.assertEquals("2. SortEvent: isUserOriginated? false",
+ getLogRow(0));
+
int i = 0;
for (String coord : getTestData().map(DataObject::getCoordinates)
.limit(5).collect(Collectors.toList())) {