Revision: 10520
Author:   [email protected]
Date:     Thu Aug 11 09:26:57 2011
Log: 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: [email protected]
http://code.google.com/p/google-web-toolkit/source/detail?r=10520

Modified:
 /trunk/user/src/com/google/gwt/user/cellview/client/ColumnSortEvent.java
/trunk/user/test/com/google/gwt/user/cellview/client/ColumnSortEventTest.java

=======================================
--- /trunk/user/src/com/google/gwt/user/cellview/client/ColumnSortEvent.java Mon Mar 21 12:22:19 2011 +++ /trunk/user/src/com/google/gwt/user/cellview/client/ColumnSortEvent.java Thu Aug 11 09:26:57 2011
@@ -80,11 +80,21 @@
    */
   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;
+    }
   }

   /**
=======================================
--- /trunk/user/test/com/google/gwt/user/cellview/client/ColumnSortEventTest.java Wed Jan 12 14:44:03 2011 +++ /trunk/user/test/com/google/gwt/user/cellview/client/ColumnSortEventTest.java Thu Aug 11 09:26:57 2011
@@ -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));
   }
 }

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to