Adding an Async SortHandler that can be used with AsyncDataProvider.  When a table is sorted, it called the setVisibleRangeAndClear() method to clear the current data and send an event to the AsyncDataProvider to provide new information.

Review at http://gwt-code-reviews.appspot.com/1258801

Review by: pdr@google.com

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@9500 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 7e5c524..b5015f3 100644
--- a/user/src/com/google/gwt/user/cellview/client/ColumnSortEvent.java
+++ b/user/src/com/google/gwt/user/cellview/client/ColumnSortEvent.java
@@ -18,6 +18,7 @@
 import com.google.gwt.event.shared.EventHandler;
 import com.google.gwt.event.shared.GwtEvent;
 import com.google.gwt.event.shared.HasHandlers;
+import com.google.gwt.view.client.HasData;
 
 import java.util.Collections;
 import java.util.Comparator;
@@ -44,6 +45,25 @@
   }
 
   /**
+   * A default handler used with views attached to asynchronous data providers
+   * such as {@link AsyncDataProvider}. This handler calls
+   * {@link HasData#setVisibleRangeAndClearData(com.google.gwt.view.client.Range, boolean)},
+   * which clears the current data and triggers the data provider's range change
+   * handler.
+   */
+  public static class AsyncHandler implements Handler {
+    private final HasData<?> hasData;
+
+    public AsyncHandler(HasData<?> hasData) {
+      this.hasData = hasData;
+    }
+
+    public void onColumnSort(ColumnSortEvent event) {
+      hasData.setVisibleRangeAndClearData(hasData.getVisibleRange(), true);
+    }
+  }
+
+  /**
    * <p>
    * A default handler used to sort a {@link List} backing a table. If the
    * sorted column has an associated {@link Comparator}, the list is sorted
diff --git a/user/test/com/google/gwt/user/cellview/CellViewSuite.java b/user/test/com/google/gwt/user/cellview/CellViewSuite.java
index 3dd2f6e..c08762d 100644
--- a/user/test/com/google/gwt/user/cellview/CellViewSuite.java
+++ b/user/test/com/google/gwt/user/cellview/CellViewSuite.java
@@ -23,6 +23,7 @@
 import com.google.gwt.user.cellview.client.CellTableTest;
 import com.google.gwt.user.cellview.client.CellTreeTest;
 import com.google.gwt.user.cellview.client.CellWidgetTest;
+import com.google.gwt.user.cellview.client.ColumnSortEventTest;
 import com.google.gwt.user.cellview.client.ColumnSortInfoTest;
 import com.google.gwt.user.cellview.client.ColumnSortListTest;
 import com.google.gwt.user.cellview.client.ColumnTest;
@@ -46,6 +47,7 @@
     suite.addTestSuite(CellTableTest.class);
     suite.addTestSuite(CellTreeTest.class);
     suite.addTestSuite(CellWidgetTest.class);
+    suite.addTestSuite(ColumnSortEventTest.class);
     suite.addTestSuite(ColumnSortInfoTest.class);
     suite.addTestSuite(ColumnSortListTest.class);
     suite.addTestSuite(ColumnTest.class);
diff --git a/user/test/com/google/gwt/user/cellview/client/ColumnSortEventTest.java b/user/test/com/google/gwt/user/cellview/client/ColumnSortEventTest.java
new file mode 100644
index 0000000..c9dea08
--- /dev/null
+++ b/user/test/com/google/gwt/user/cellview/client/ColumnSortEventTest.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2010 Google Inc.
+ * 
+ * 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.google.gwt.user.cellview.client;
+
+import com.google.gwt.cell.client.TextCell;
+import com.google.gwt.user.cellview.client.ColumnSortEvent.AsyncHandler;
+import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
+import com.google.gwt.user.cellview.client.ColumnSortList.ColumnSortInfo;
+import com.google.gwt.view.client.MockHasData;
+import com.google.gwt.view.client.Range;
+import com.google.gwt.view.client.RangeChangeEvent;
+
+import junit.framework.TestCase;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+
+/**
+ * Tests for {@link ColumnSortEvent}.
+ */
+public class ColumnSortEventTest extends TestCase {
+
+  public void testAccessors() {
+    ColumnSortList sortList = new ColumnSortList();
+    IdentityColumn<String> col0 = new IdentityColumn<String>(new TextCell());
+    IdentityColumn<String> col1 = new IdentityColumn<String>(new TextCell());
+    sortList.push(new ColumnSortInfo(col0, true));
+    sortList.push(new ColumnSortInfo(col1, false));
+
+    ColumnSortEvent event = new ColumnSortEvent(sortList);
+    assertEquals(sortList, event.getColumnSortList());
+    assertEquals(col1, event.getColumn());
+    assertFalse(event.isSortAcsending());
+  }
+
+  public void testAsyncHandler() {
+    MockHasData<String> hasData = new MockHasData<String>();
+    final List<Range> events = new ArrayList<Range>();
+    hasData.addRangeChangeHandler(new RangeChangeEvent.Handler() {
+      public void onRangeChange(RangeChangeEvent event) {
+        events.add(event.getNewRange());
+      }
+    });
+    AsyncHandler handler = new AsyncHandler(hasData);
+    assertEquals(0, events.size());
+
+    // Fire an event to the handler.
+    ColumnSortList sortList = new ColumnSortList();
+    handler.onColumnSort(new ColumnSortEvent(sortList));
+    assertEquals(1, events.size());
+  }
+
+  public void testListHandler() {
+    // Create some unsorted values.
+    List<String> values = new ArrayList<String>();
+    values.add("b");
+    values.add("a");
+    values.add("c");
+
+    // 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>() {
+      public int compare(String o1, String o2) {
+        return o1.compareTo(o2);
+      }
+    });
+    IdentityColumn<String> col1 = new IdentityColumn<String>(new TextCell());
+    handler.setComparator(col1, null);
+
+    // Sort ascending.
+    ColumnSortList sortList = new ColumnSortList();
+    sortList.push(col0);
+    handler.onColumnSort(new ColumnSortEvent(sortList));
+    assertEquals("a", values.get(0));
+    assertEquals("b", values.get(1));
+    assertEquals("c", values.get(2));
+
+    // Sort descending.
+    sortList.push(col0); // Switches sort to descending.
+    handler.onColumnSort(new ColumnSortEvent(sortList));
+    assertEquals("c", values.get(0));
+    assertEquals("b", values.get(1));
+    assertEquals("a", values.get(2));
+
+    // Null comparator.
+    sortList.push(col1);
+    assertEquals("c", values.get(0));
+    assertEquals("b", values.get(1));
+    assertEquals("a", values.get(2));
+  }
+}