aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeif Åstrand <leif@vaadin.com>2014-07-10 15:41:24 +0300
committerLeif Åstrand <leif@vaadin.com>2014-07-10 15:41:24 +0300
commitf27a00b7b9a487a66dd23a4d28aa56fe60a1ad4e (patch)
tree057e2843430c7d0669fece9277af683e04ce8c0a
parentf8d87110461bee9b7056858750d1fd6a19e2a5d6 (diff)
downloadvaadin-framework-f27a00b7b9a487a66dd23a4d28aa56fe60a1ad4e.tar.gz
vaadin-framework-f27a00b7b9a487a66dd23a4d28aa56fe60a1ad4e.zip
Server-side programmatic sort updates sort indicators in the UI (#13334)
Change-Id: Ia4bae5d88d265250d1bf4daa8d23561edb757872
-rw-r--r--client/src/com/vaadin/client/ui/grid/GridConnector.java23
-rw-r--r--server/src/com/vaadin/ui/components/grid/Grid.java10
-rw-r--r--shared/src/com/vaadin/shared/ui/grid/GridState.java6
-rw-r--r--uitest/src/com/vaadin/tests/components/grid/GridBasicFeaturesTest.java22
4 files changed, 60 insertions, 1 deletions
diff --git a/client/src/com/vaadin/client/ui/grid/GridConnector.java b/client/src/com/vaadin/client/ui/grid/GridConnector.java
index 095b049506..ca66ccc3d5 100644
--- a/client/src/com/vaadin/client/ui/grid/GridConnector.java
+++ b/client/src/com/vaadin/client/ui/grid/GridConnector.java
@@ -17,6 +17,7 @@
package com.vaadin.client.ui.grid;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
@@ -276,7 +277,12 @@ public class GridConnector extends AbstractComponentConnector {
directions[i] = sortOrder.getDirection();
}
- getRpcProxy(GridServerRpc.class).sort(columnIds, directions);
+ if (!Arrays.equals(columnIds, getState().sortColumns)
+ || !Arrays.equals(directions, getState().sortDirs)) {
+ // Report back to server if changed
+ getRpcProxy(GridServerRpc.class)
+ .sort(columnIds, directions);
+ }
}
});
}
@@ -522,6 +528,21 @@ public class GridConnector extends AbstractComponentConnector {
}
+ @OnStateChange({ "sortColumns", "sortDirs" })
+ void onSortStateChange() {
+ List<SortOrder> sortOrder = new ArrayList<SortOrder>();
+
+ String[] sortColumns = getState().sortColumns;
+ SortDirection[] sortDirs = getState().sortDirs;
+
+ for (int i = 0; i < sortColumns.length; i++) {
+ sortOrder.add(new SortOrder(columnIdToColumn.get(sortColumns[i]),
+ sortDirs[i]));
+ }
+
+ getWidget().setSortOrder(sortOrder);
+ }
+
private Logger getLogger() {
return Logger.getLogger(getClass().getName());
}
diff --git a/server/src/com/vaadin/ui/components/grid/Grid.java b/server/src/com/vaadin/ui/components/grid/Grid.java
index 79effadab9..f18ca6045e 100644
--- a/server/src/com/vaadin/ui/components/grid/Grid.java
+++ b/server/src/com/vaadin/ui/components/grid/Grid.java
@@ -1239,8 +1239,15 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier {
Object[] propertyIds = new Object[items];
boolean[] directions = new boolean[items];
+ String[] columnKeys = new String[items];
+ SortDirection[] stateDirs = new SortDirection[items];
+
for (int i = 0; i < items; ++i) {
SortOrder order = sortOrder.get(i);
+
+ columnKeys[i] = this.columnKeys.key(order.getPropertyId());
+ stateDirs[i] = order.getDirection();
+
propertyIds[i] = order.getPropertyId();
switch (order.getDirection()) {
case ASCENDING:
@@ -1259,6 +1266,9 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier {
fireEvent(new SortOrderChangeEvent(this, new ArrayList<SortOrder>(
sortOrder)));
+
+ getState().sortColumns = columnKeys;
+ getState(false).sortDirs = stateDirs;
} else {
throw new IllegalStateException(
"Container is not sortable (does not implement Container.Sortable)");
diff --git a/shared/src/com/vaadin/shared/ui/grid/GridState.java b/shared/src/com/vaadin/shared/ui/grid/GridState.java
index f71fe0929a..ef1b9a806a 100644
--- a/shared/src/com/vaadin/shared/ui/grid/GridState.java
+++ b/shared/src/com/vaadin/shared/ui/grid/GridState.java
@@ -132,4 +132,10 @@ public class GridState extends AbstractComponentState {
public List<String> selectedKeys = new ArrayList<String>();
public SharedSelectionMode selectionMode;
+
+ /** Keys of the currently sorted columns */
+ public String[] sortColumns = new String[0];
+
+ /** Directions for each sorted column */
+ public SortDirection[] sortDirs = new SortDirection[0];
}
diff --git a/uitest/src/com/vaadin/tests/components/grid/GridBasicFeaturesTest.java b/uitest/src/com/vaadin/tests/components/grid/GridBasicFeaturesTest.java
index f15f45f97a..a94550721e 100644
--- a/uitest/src/com/vaadin/tests/components/grid/GridBasicFeaturesTest.java
+++ b/uitest/src/com/vaadin/tests/components/grid/GridBasicFeaturesTest.java
@@ -383,6 +383,11 @@ public class GridBasicFeaturesTest extends MultiBrowserTest {
// String.
// First cells for first 3 rows are (9, 0), (99, 0) and (999, 0)
sortBy("Column9, DESC");
+
+ assertTrue("Column 9 should have the sort-desc stylename", grid
+ .getHeaderCell(0, 9).getAttribute("class")
+ .contains("sort-desc"));
+
String row = "";
for (int i = 0; i < 3; ++i) {
row += "9";
@@ -394,6 +399,14 @@ public class GridBasicFeaturesTest extends MultiBrowserTest {
// Column 10 is random numbers from Random with seed 13334
sortBy("Column10, ASC");
+ assertFalse(
+ "Column 9 should no longer have the sort-desc stylename",
+ grid.getHeaderCell(0, 9).getAttribute("class")
+ .contains("sort-desc"));
+ assertTrue("Column 10 should have the sort-asc stylename", grid
+ .getHeaderCell(0, 10).getAttribute("class")
+ .contains("sort-asc"));
+
// Not cleaning up correctly causes exceptions when scrolling.
grid.scrollToRow(50);
assertFalse("Scrolling caused and exception when shuffled.",
@@ -416,6 +429,15 @@ public class GridBasicFeaturesTest extends MultiBrowserTest {
"(" + i + ", 0)",
grid.getCell(GridBasicFeatures.ROWS - (i + 1), 0).getText());
}
+
+ assertFalse(
+ "Column 10 should no longer have the sort-asc stylename",
+ grid.getHeaderCell(0, 10).getAttribute("class")
+ .contains("sort-asc"));
+ assertTrue("Column 7 should have the sort-desc stylename", grid
+ .getHeaderCell(0, 7).getAttribute("class")
+ .contains("sort-desc"));
+
}
@Test