Added methods to ColumnSortEvent.ListHandler to set the Handler's list and to retrieve sorting comparators that have been registered to Columns.
Review at http://gwt-code-reviews.appspot.com/1519803
Review by: jlabanca@google.com
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@10520 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/cellview/client/ColumnSortEvent.java b/user/src/com/google/gwt/user/cellview/client/ColumnSortEvent.java
index 099767a..d4fa1fb 100644
--- a/user/src/com/google/gwt/user/cellview/client/ColumnSortEvent.java
+++ b/user/src/com/google/gwt/user/cellview/client/ColumnSortEvent.java
@@ -80,12 +80,22 @@
*/
public static class ListHandler<T> implements Handler {
private final Map<Column<?, ?>, Comparator<T>> comparators = new HashMap<Column<?, ?>, Comparator<T>>();
- private final List<T> list;
+ private List<T> list;
public ListHandler(List<T> list) {
this.list = list;
}
+ /**
+ * Returns the comparator that has been set for the specified column, or
+ * null if no comparator has been set.
+ *
+ * @param column the {@link Column}
+ */
+ public Comparator<T> getComparator(Column<T, ?> column) {
+ return comparators.get(column);
+ }
+
public List<T> getList() {
return list;
}
@@ -124,6 +134,11 @@
public void setComparator(Column<T, ?> column, Comparator<T> comparator) {
comparators.put(column, comparator);
}
+
+ public void setList(List<T> list) {
+ assert list != null : "list cannot be null";
+ this.list = list;
+ }
}
/**
diff --git a/user/test/com/google/gwt/user/cellview/client/ColumnSortEventTest.java b/user/test/com/google/gwt/user/cellview/client/ColumnSortEventTest.java
index 0cf4fe8..e0d6b94 100644
--- a/user/test/com/google/gwt/user/cellview/client/ColumnSortEventTest.java
+++ b/user/test/com/google/gwt/user/cellview/client/ColumnSortEventTest.java
@@ -74,11 +74,12 @@
// Create a handler for the list of values.
ListHandler<String> handler = new ListHandler<String>(values);
IdentityColumn<String> col0 = new IdentityColumn<String>(new TextCell());
- handler.setComparator(col0, new Comparator<String>() {
+ Comparator<String> col0Comparator = new Comparator<String>() {
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
- });
+ };
+ handler.setComparator(col0, col0Comparator);
IdentityColumn<String> col1 = new IdentityColumn<String>(new TextCell());
handler.setComparator(col1, null);
@@ -102,5 +103,34 @@
assertEquals("c", values.get(0));
assertEquals("b", values.get(1));
assertEquals("a", values.get(2));
+
+ // Retrieve the comparators.
+ assertEquals(col0Comparator, handler.getComparator(col0));
+ assertNull(handler.getComparator(col1));
+ assertNull(handler.getComparator(new IdentityColumn<String>(
+ new TextCell())));
+
+ // Create some new unsorted values.
+ List<String> newValues = new ArrayList<String>();
+ newValues.add("e");
+ newValues.add("d");
+ newValues.add("f");
+
+ // Update the handler to be for the new list of values.
+ handler.setList(newValues);
+
+ // Sort the new list in ascending order.
+ sortList.push(col0);
+ handler.onColumnSort(new ColumnSortEvent(sortList));
+
+ // The new values, sorted in ascending order.
+ assertEquals("d", newValues.get(0));
+ assertEquals("e", newValues.get(1));
+ assertEquals("f", newValues.get(2));
+
+ // The old values, still sorted in descending order.
+ assertEquals("c", values.get(0));
+ assertEquals("b", values.get(1));
+ assertEquals("a", values.get(2));
}
}