Revision: 9500
Author: jlaba...@google.com
Date: Thu Jan 6 05:35:04 2011
Log: 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: p...@google.com
http://code.google.com/p/google-web-toolkit/source/detail?r=9500
Added:
/trunk/user/test/com/google/gwt/user/cellview/client/ColumnSortEventTest.java
Modified:
/trunk/user/src/com/google/gwt/user/cellview/client/ColumnSortEvent.java
/trunk/user/test/com/google/gwt/user/cellview/CellViewSuite.java
=======================================
--- /dev/null
+++
/trunk/user/test/com/google/gwt/user/cellview/client/ColumnSortEventTest.java
Thu Jan 6 05:35:04 2011
@@ -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));
+ }
+}
=======================================
---
/trunk/user/src/com/google/gwt/user/cellview/client/ColumnSortEvent.java
Wed Jan 5 04:42:33 2011
+++
/trunk/user/src/com/google/gwt/user/cellview/client/ColumnSortEvent.java
Thu Jan 6 05:35:04 2011
@@ -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;
@@ -42,6 +43,25 @@
*/
void onColumnSort(ColumnSortEvent event);
}
+
+ /**
+ * 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>
=======================================
--- /trunk/user/test/com/google/gwt/user/cellview/CellViewSuite.java Wed
Jan 5 04:42:33 2011
+++ /trunk/user/test/com/google/gwt/user/cellview/CellViewSuite.java Thu
Jan 6 05:35:04 2011
@@ -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);
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors