Revision: 10473
Author:   [email protected]
Date:     Wed Jul 27 04:19:13 2011
Log: 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.

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

Review by: [email protected]
http://code.google.com/p/google-web-toolkit/source/detail?r=10473

Modified:
/trunk/samples/showcase/src/com/google/gwt/sample/showcase/client/content/cell/CwCellTable.java
 /trunk/user/src/com/google/gwt/user/cellview/client/Column.java
 /trunk/user/src/com/google/gwt/user/cellview/client/ColumnSortList.java
/trunk/user/test/com/google/gwt/user/cellview/client/ColumnSortListTest.java

=======================================
--- /trunk/samples/showcase/src/com/google/gwt/sample/showcase/client/content/cell/CwCellTable.java Fri Jan 7 10:19:06 2011 +++ /trunk/samples/showcase/src/com/google/gwt/sample/showcase/client/content/cell/CwCellTable.java Wed Jul 27 04:19:13 2011
@@ -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());
=======================================
--- /trunk/user/src/com/google/gwt/user/cellview/client/Column.java Thu Jun 9 09:55:08 2011 +++ /trunk/user/src/com/google/gwt/user/cellview/client/Column.java Wed Jul 27 04:19:13 2011
@@ -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;
@@ -110,6 +111,15 @@
   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;
+  }

   /**
    * Check if the column is sortable.
@@ -164,6 +174,16 @@
   public void setCellStyleNames(String styleNames) {
     this.cellStyleNames = styleNames;
   }
+
+  /**
+   * 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.
=======================================
--- /trunk/user/src/com/google/gwt/user/cellview/client/ColumnSortList.java Wed Jan 5 04:42:33 2011 +++ /trunk/user/src/com/google/gwt/user/cellview/client/ColumnSortList.java Wed Jul 27 04:19:13 2011
@@ -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();
     }
=======================================
--- /trunk/user/test/com/google/gwt/user/cellview/client/ColumnSortListTest.java Wed Jan 5 04:42:33 2011 +++ /trunk/user/test/com/google/gwt/user/cellview/client/ColumnSortListTest.java Wed Jul 27 04:19:13 2011
@@ -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));
@@ -177,6 +176,30 @@
     assertEquals(col1, list.get(2).getColumn());
     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();

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

Reply via email to