aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeif Ã…strand <leif@vaadin.com>2014-07-10 08:57:21 +0300
committerPatrik Lindström <patrik@vaadin.com>2014-07-10 12:34:32 +0000
commitf8d87110461bee9b7056858750d1fd6a19e2a5d6 (patch)
tree6453674b1088272c016dc3091e2950482d82ff5f
parentb36c0fc7e01def912430be5d9a42d344d8202046 (diff)
downloadvaadin-framework-f8d87110461bee9b7056858750d1fd6a19e2a5d6.tar.gz
vaadin-framework-f8d87110461bee9b7056858750d1fd6a19e2a5d6.zip
Add server-side SortOrderChangeEvent support (#13334)
Change-Id: Ia250909edccf9a838414994ee24eb5326f49478a
-rw-r--r--server/src/com/vaadin/ui/components/grid/Grid.java31
-rw-r--r--server/src/com/vaadin/ui/components/grid/SortOrderChangeEvent.java57
-rw-r--r--server/src/com/vaadin/ui/components/grid/SortOrderChangeListener.java34
-rw-r--r--server/src/com/vaadin/ui/components/grid/sort/SortOrder.java41
-rw-r--r--server/tests/src/com/vaadin/tests/server/component/grid/sort/SortTest.java49
-rw-r--r--uitest/src/com/vaadin/tests/components/grid/GridBasicFeatures.java9
-rw-r--r--uitest/src/com/vaadin/tests/components/grid/GridBasicFeaturesTest.java10
7 files changed, 226 insertions, 5 deletions
diff --git a/server/src/com/vaadin/ui/components/grid/Grid.java b/server/src/com/vaadin/ui/components/grid/Grid.java
index cc284841a1..79effadab9 100644
--- a/server/src/com/vaadin/ui/components/grid/Grid.java
+++ b/server/src/com/vaadin/ui/components/grid/Grid.java
@@ -211,6 +211,10 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier {
.findMethod(SelectionChangeListener.class, "selectionChange",
SelectionChangeEvent.class);
+ private static final Method SORT_ORDER_CHANGE_METHOD = ReflectTools
+ .findMethod(SortOrderChangeListener.class, "sortOrderChange",
+ SortOrderChangeEvent.class);
+
/**
* Creates a new Grid using the given datasource.
*
@@ -1252,9 +1256,36 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier {
}
cs.sort(propertyIds, directions);
+
+ fireEvent(new SortOrderChangeEvent(this, new ArrayList<SortOrder>(
+ sortOrder)));
} else {
throw new IllegalStateException(
"Container is not sortable (does not implement Container.Sortable)");
}
}
+
+ /**
+ * Adds a sort order change listener that gets notified when the sort order
+ * changes.
+ *
+ * @param listener
+ * the sort order change listener to add
+ */
+ public void addSortOrderChangeListener(SortOrderChangeListener listener) {
+ addListener(SortOrderChangeEvent.class, listener,
+ SORT_ORDER_CHANGE_METHOD);
+ }
+
+ /**
+ * Removes a sort order change listener previously added using
+ * {@link #addSortOrderChangeListener(SortOrderChangeListener)}.
+ *
+ * @param listener
+ * the sort order change listener to remove
+ */
+ public void removeSortOrderChangeListener(SortOrderChangeListener listener) {
+ removeListener(SortOrderChangeEvent.class, listener,
+ SORT_ORDER_CHANGE_METHOD);
+ }
}
diff --git a/server/src/com/vaadin/ui/components/grid/SortOrderChangeEvent.java b/server/src/com/vaadin/ui/components/grid/SortOrderChangeEvent.java
new file mode 100644
index 0000000000..71afa10a9b
--- /dev/null
+++ b/server/src/com/vaadin/ui/components/grid/SortOrderChangeEvent.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2000-2014 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.ui.components.grid;
+
+import java.util.List;
+
+import com.vaadin.ui.Component;
+import com.vaadin.ui.components.grid.sort.SortOrder;
+
+/**
+ * Event fired by {@link Grid} when the sort order has changed.
+ *
+ * @see SortOrderChangeListener
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class SortOrderChangeEvent extends Component.Event {
+
+ private final List<SortOrder> sortOrder;
+
+ /**
+ * Creates a new sort order change event for a grid and a sort order list.
+ *
+ * @param grid
+ * the grid from which the event originates
+ * @param sortOrder
+ * the new sort order list
+ */
+ public SortOrderChangeEvent(Grid grid, List<SortOrder> sortOrder) {
+ super(grid);
+ this.sortOrder = sortOrder;
+ }
+
+ /**
+ * Gets the sort order list.
+ *
+ * @return the sort order list
+ */
+ public List<SortOrder> getSortOrder() {
+ return sortOrder;
+ }
+
+}
diff --git a/server/src/com/vaadin/ui/components/grid/SortOrderChangeListener.java b/server/src/com/vaadin/ui/components/grid/SortOrderChangeListener.java
new file mode 100644
index 0000000000..82d7ba3108
--- /dev/null
+++ b/server/src/com/vaadin/ui/components/grid/SortOrderChangeListener.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2000-2014 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.ui.components.grid;
+
+import java.io.Serializable;
+
+/**
+ * Listener for sort order change events from {@link Grid}.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface SortOrderChangeListener extends Serializable {
+ /**
+ * Called when the sort order has changed.
+ *
+ * @param event
+ * the sort order change event
+ */
+ public void sortOrderChange(SortOrderChangeEvent event);
+}
diff --git a/server/src/com/vaadin/ui/components/grid/sort/SortOrder.java b/server/src/com/vaadin/ui/components/grid/sort/SortOrder.java
index f186333e0a..a76148fe0c 100644
--- a/server/src/com/vaadin/ui/components/grid/sort/SortOrder.java
+++ b/server/src/com/vaadin/ui/components/grid/sort/SortOrder.java
@@ -22,7 +22,7 @@ import com.vaadin.shared.ui.grid.SortDirection;
/**
* Sort order descriptor. Links together a {@link SortDirection} value and a
* Vaadin container property ID.
- *
+ *
* @since 7.4
* @author Vaadin Ltd
*/
@@ -33,7 +33,7 @@ public class SortOrder implements Serializable {
/**
* Create a SortOrder object. Both arguments must be non-null.
- *
+ *
* @param propertyId
* id of the data source property to sort by
* @param direction
@@ -54,7 +54,7 @@ public class SortOrder implements Serializable {
/**
* Returns the property ID.
- *
+ *
* @return a property ID
*/
public Object getPropertyId() {
@@ -63,11 +63,44 @@ public class SortOrder implements Serializable {
/**
* Returns the {@link SortDirection} value.
- *
+ *
* @return a sort direction value
*/
public SortDirection getDirection() {
return direction;
}
+ @Override
+ public String toString() {
+ return propertyId + " " + direction;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + direction.hashCode();
+ result = prime * result + propertyId.hashCode();
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ } else if (obj == null) {
+ return false;
+ } else if (getClass() != obj.getClass()) {
+ return false;
+ }
+
+ SortOrder other = (SortOrder) obj;
+ if (direction != other.direction) {
+ return false;
+ } else if (!propertyId.equals(other.propertyId)) {
+ return false;
+ }
+ return true;
+ }
+
}
diff --git a/server/tests/src/com/vaadin/tests/server/component/grid/sort/SortTest.java b/server/tests/src/com/vaadin/tests/server/component/grid/sort/SortTest.java
index 844292265d..d3a9315e20 100644
--- a/server/tests/src/com/vaadin/tests/server/component/grid/sort/SortTest.java
+++ b/server/tests/src/com/vaadin/tests/server/component/grid/sort/SortTest.java
@@ -15,6 +15,9 @@
*/
package com.vaadin.tests.server.component.grid.sort;
+import java.util.Arrays;
+import java.util.List;
+
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@@ -23,7 +26,10 @@ import org.junit.Test;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.shared.ui.grid.SortDirection;
import com.vaadin.ui.components.grid.Grid;
+import com.vaadin.ui.components.grid.SortOrderChangeEvent;
+import com.vaadin.ui.components.grid.SortOrderChangeListener;
import com.vaadin.ui.components.grid.sort.Sort;
+import com.vaadin.ui.components.grid.sort.SortOrder;
public class SortTest {
@@ -65,14 +71,38 @@ public class SortTest {
}
}
+ class RegisteringSortChangeListener implements SortOrderChangeListener {
+ private List<SortOrder> order;
+
+ @Override
+ public void sortOrderChange(SortOrderChangeEvent event) {
+ assert order == null : "The same listener was notified multipe times without checking";
+
+ order = event.getSortOrder();
+ }
+
+ public void assertEventFired(SortOrder... expectedOrder) {
+ Assert.assertEquals(Arrays.asList(expectedOrder), order);
+
+ // Reset for nest test
+ order = null;
+ }
+
+ }
+
private DummySortingIndexedContainer container;
+ private RegisteringSortChangeListener listener;
private Grid grid;
@Before
public void setUp() {
container = createContainer();
container.expectedSort(new Object[] {}, new SortDirection[] {});
+
+ listener = new RegisteringSortChangeListener();
+
grid = new Grid(container);
+ grid.addSortOrderChangeListener(listener);
}
@After
@@ -107,6 +137,8 @@ public class SortTest {
container.expectedSort(new Object[] { "foo" },
new SortDirection[] { SortDirection.ASCENDING });
grid.sort("foo");
+
+ listener.assertEventFired(new SortOrder("foo", SortDirection.ASCENDING));
}
@Test
@@ -114,6 +146,8 @@ public class SortTest {
container.expectedSort(new Object[] { "foo" },
new SortDirection[] { SortDirection.DESCENDING });
grid.sort("foo", SortDirection.DESCENDING);
+
+ listener.assertEventFired(new SortOrder("foo", SortDirection.DESCENDING));
}
@Test
@@ -123,6 +157,12 @@ public class SortTest {
SortDirection.ASCENDING, SortDirection.DESCENDING });
grid.sort(Sort.by("foo").then("bar")
.then("baz", SortDirection.DESCENDING));
+
+ listener.assertEventFired(
+ new SortOrder("foo", SortDirection.ASCENDING), new SortOrder(
+ "bar", SortDirection.ASCENDING), new SortOrder("baz",
+ SortDirection.DESCENDING));
+
}
@Test
@@ -132,11 +172,20 @@ public class SortTest {
SortDirection.ASCENDING, SortDirection.DESCENDING });
grid.sort(Sort.by("foo").then("bar")
.then("baz", SortDirection.DESCENDING));
+
+ listener.assertEventFired(
+ new SortOrder("foo", SortDirection.ASCENDING), new SortOrder(
+ "bar", SortDirection.ASCENDING), new SortOrder("baz",
+ SortDirection.DESCENDING));
+
container = new DummySortingIndexedContainer();
container.addContainerProperty("baz", String.class, "");
container.expectedSort(new Object[] { "baz" },
new SortDirection[] { SortDirection.DESCENDING });
grid.setContainerDataSource(container);
+
+ listener.assertEventFired(new SortOrder("baz", SortDirection.DESCENDING));
+
}
private DummySortingIndexedContainer createContainer() {
diff --git a/uitest/src/com/vaadin/tests/components/grid/GridBasicFeatures.java b/uitest/src/com/vaadin/tests/components/grid/GridBasicFeatures.java
index a7dad4795f..6e4bafc797 100644
--- a/uitest/src/com/vaadin/tests/components/grid/GridBasicFeatures.java
+++ b/uitest/src/com/vaadin/tests/components/grid/GridBasicFeatures.java
@@ -36,6 +36,8 @@ import com.vaadin.ui.components.grid.ColumnGroupRow;
import com.vaadin.ui.components.grid.Grid;
import com.vaadin.ui.components.grid.Grid.SelectionMode;
import com.vaadin.ui.components.grid.GridColumn;
+import com.vaadin.ui.components.grid.SortOrderChangeEvent;
+import com.vaadin.ui.components.grid.SortOrderChangeListener;
import com.vaadin.ui.components.grid.renderers.DateRenderer;
import com.vaadin.ui.components.grid.renderers.HtmlRenderer;
import com.vaadin.ui.components.grid.renderers.NumberRenderer;
@@ -143,6 +145,13 @@ public class GridBasicFeatures extends AbstractComponentTest<Grid> {
grid.getColumn("Column" + col).setWidth(100 + col * 50);
}
+ grid.addSortOrderChangeListener(new SortOrderChangeListener() {
+ @Override
+ public void sortOrderChange(SortOrderChangeEvent event) {
+ log("Sort order: " + event.getSortOrder());
+ }
+ });
+
grid.setSelectionMode(SelectionMode.NONE);
createGridActions();
diff --git a/uitest/src/com/vaadin/tests/components/grid/GridBasicFeaturesTest.java b/uitest/src/com/vaadin/tests/components/grid/GridBasicFeaturesTest.java
index 59f6311995..f15f45f97a 100644
--- a/uitest/src/com/vaadin/tests/components/grid/GridBasicFeaturesTest.java
+++ b/uitest/src/com/vaadin/tests/components/grid/GridBasicFeaturesTest.java
@@ -419,7 +419,7 @@ public class GridBasicFeaturesTest extends MultiBrowserTest {
}
@Test
- public void testUserSorting() {
+ public void testUserSorting() throws InterruptedException {
openTestURL();
GridElement grid = getGridElement();
@@ -439,10 +439,15 @@ public class GridBasicFeaturesTest extends MultiBrowserTest {
"(" + row + ", 0)", grid.getCell(i, 0).getText());
}
+ assertEquals("2. Sort order: [Column9 ASCENDING]", getLogRow(2));
+ assertEquals("4. Sort order: [Column9 DESCENDING]", getLogRow(0));
+
// Column 10 is random numbers from Random with seed 13334
// Click header to sort ascending
grid.getHeaderCell(0, 10).click();
+ assertEquals("6. Sort order: [Column10 ASCENDING]", getLogRow(0));
+
// Not cleaning up correctly causes exceptions when scrolling.
grid.scrollToRow(50);
assertFalse("Scrolling caused and exception when shuffled.",
@@ -467,6 +472,9 @@ public class GridBasicFeaturesTest extends MultiBrowserTest {
"(" + i + ", 0)",
grid.getCell(GridBasicFeatures.ROWS - (i + 1), 0).getText());
}
+
+ assertEquals("9. Sort order: [Column7 ASCENDING]", getLogRow(3));
+ assertEquals("11. Sort order: [Column7 DESCENDING]", getLogRow(1));
}
private void sortBy(String column) {