Reviewers: pengzhuang_google.com,
Description:
Adding API method Column#setDefaultSortOrder(boolean isAscending) to
control whether the column should be sorted in ascending or descending
order the first time it is clicked. Currently, the first time a column
is clicked, it is sorted in ascending order.
Please review this at http://gwt-code-reviews.appspot.com/1498806/
Affected files:
M
samples/showcase/src/com/google/gwt/sample/showcase/client/content/cell/CwCellTable.java
M user/src/com/google/gwt/user/cellview/client/Column.java
M user/src/com/google/gwt/user/cellview/client/ColumnSortList.java
M user/test/com/google/gwt/user/cellview/client/ColumnSortListTest.java
Index:
samples/showcase/src/com/google/gwt/sample/showcase/client/content/cell/CwCellTable.java
===================================================================
---
samples/showcase/src/com/google/gwt/sample/showcase/client/content/cell/CwCellTable.java
(revision 10462)
+++
samples/showcase/src/com/google/gwt/sample/showcase/client/content/cell/CwCellTable.java
(working copy)
@@ -278,6 +278,7 @@
}
};
addressColumn.setSortable(true);
+ addressColumn.setDefaultSortAscending(false);
sortHandler.setComparator(addressColumn, new Comparator<ContactInfo>()
{
public int compare(ContactInfo o1, ContactInfo o2) {
return o1.getAddress().compareTo(o2.getAddress());
Index: user/src/com/google/gwt/user/cellview/client/Column.java
===================================================================
--- user/src/com/google/gwt/user/cellview/client/Column.java (revision
10462)
+++ user/src/com/google/gwt/user/cellview/client/Column.java (working copy)
@@ -48,6 +48,7 @@
*/
private FieldUpdater<T, C> fieldUpdater;
+ private boolean isDefaultSortAscending = true;
private boolean isSortable = false;
private HorizontalAlignmentConstant hAlign = null;
private VerticalAlignmentConstant vAlign = null;
@@ -109,6 +110,15 @@
@Override
public VerticalAlignmentConstant getVerticalAlignment() {
return vAlign;
+ }
+
+ /**
+ * Check if the default sort order of the column is ascending or
descending.
+ *
+ * @return true if default sort is ascending, false if not
+ */
+ public boolean isDefaultSortAscending() {
+ return isDefaultSortAscending;
}
/**
@@ -166,6 +176,16 @@
}
/**
+ * Set whether or not the default sort order is ascending.
+ *
+ * @param isAscending true to set the default order to ascending, false
for
+ * descending
+ */
+ public void setDefaultSortAscending(boolean isAscending) {
+ this.isDefaultSortAscending = isAscending;
+ }
+
+ /**
* Set the {@link FieldUpdater} used for updating values in the column.
*
* @param fieldUpdater the field updater
Index: user/src/com/google/gwt/user/cellview/client/ColumnSortList.java
===================================================================
--- user/src/com/google/gwt/user/cellview/client/ColumnSortList.java
(revision 10462)
+++ user/src/com/google/gwt/user/cellview/client/ColumnSortList.java
(working copy)
@@ -82,8 +82,7 @@
@Override
public int hashCode() {
- return 31 * (column == null ? 0 : column.hashCode())
- + (ascending ? 1 : 0);
+ return 31 * (column == null ? 0 : column.hashCode()) + (ascending ?
1 : 0);
}
/**
@@ -220,7 +219,7 @@
*/
public ColumnSortInfo push(Column<?, ?> column) {
// If the column matches the primary column, toggle the order.
- boolean ascending = true;
+ boolean ascending = (column == null) ? true :
column.isDefaultSortAscending();
if (size() > 0 && get(0).getColumn() == column) {
ascending = !get(0).isAscending();
}
Index: user/test/com/google/gwt/user/cellview/client/ColumnSortListTest.java
===================================================================
--- user/test/com/google/gwt/user/cellview/client/ColumnSortListTest.java
(revision 10462)
+++ user/test/com/google/gwt/user/cellview/client/ColumnSortListTest.java
(working copy)
@@ -63,8 +63,7 @@
// Compare with different items that equals each other.
ColumnSortInfo info2a = createColumnSortInfo();
- ColumnSortInfo info2b = new ColumnSortInfo(info2a.getColumn(),
- info2a.isAscending());
+ ColumnSortInfo info2b = new ColumnSortInfo(info2a.getColumn(),
info2a.isAscending());
list0.push(info2a);
list1.push(info2b);
assertTrue(list0.equals(list1));
@@ -178,6 +177,30 @@
assertTrue(list.get(2).isAscending());
}
+ /**
+ * Test pushing a column with a default sort order of descending.
+ */
+ public void testPushColumnDescending() {
+ ColumnSortList list = new ColumnSortList();
+ assertEquals(0, list.size());
+
+ // Push a column.
+ Column<String, String> col0 = new IdentityColumn<String>(new
TextCell());
+ col0.setDefaultSortAscending(false);
+ ColumnSortInfo item0 = list.push(col0);
+ assertEquals(1, list.size());
+ assertEquals(item0, list.get(0));
+ assertEquals(col0, list.get(0).getColumn());
+ assertFalse(list.get(0).isAscending());
+
+ // Push the same item. Should change sort order.
+ ColumnSortInfo item0desc = list.push(col0);
+ assertEquals(1, list.size());
+ assertEquals(item0desc, list.get(0));
+ assertEquals(col0, list.get(0).getColumn());
+ assertTrue(list.get(0).isAscending());
+ }
+
public void testPushColumnSortInfo() {
ColumnSortList list = new ColumnSortList();
assertEquals(0, list.size());
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors