View Javadoc

1   package org.appfuse.webapp.client.application.utils.tables;
2   
3   import java.util.ArrayList;
4   import java.util.Collections;
5   import java.util.Comparator;
6   import java.util.HashMap;
7   import java.util.List;
8   import java.util.Map;
9   
10  import com.google.gwt.user.cellview.client.CellTable;
11  import com.google.gwt.user.cellview.client.Column;
12  import com.google.gwt.user.cellview.client.ColumnSortEvent;
13  import com.google.gwt.user.cellview.client.ColumnSortEvent.Handler;
14  import com.google.gwt.view.client.HasData;
15  
16  /**
17   * 
18   * @author ivangsa
19   *
20   * @param <T>
21   */
22  public abstract class LocalColumnSortHandler<T> implements Handler {
23  
24      private final HasData<T> hasData;
25      private final Map<Column<?, ?>, Comparator<T>> comparators = new HashMap<Column<?, ?>, Comparator<T>>();
26  
27      /**
28       * @param cellTable
29       */
30      public LocalColumnSortHandler(HasData<T> hasData) {
31          super();
32          this.hasData = hasData;
33      }
34  
35      /**
36       * Returns the comparator that has been set for the specified column, or
37       * null if no comparator has been set.
38       * 
39       * @param column
40       *            the {@link Column}
41       */
42      public Comparator<T> getComparator(final Column<T, ?> column) {
43          if (!comparators.containsKey(column)) {
44              setComparator(column, new Comparator<T>() {
45                  @Override
46                  public int compare(T o1, T o2) {
47                      Object value1 = column.getValue(o1);
48                      Object value2 = column.getValue(o2);
49                      if (value1 == null) {
50                          return -1;
51                      }
52                      if (value2 == null) {
53                          return 1;
54                      }
55                      if (value1 instanceof Number || value2 instanceof Number) {
56                          // numeric comparison
57                          return ((Number) value1).intValue() - ((Number) value2).intValue();
58                      } else {
59                          // string comparison
60                          return value1.toString().compareTo(value2.toString());
61                      }
62                  }
63              });
64          }
65  
66          return comparators.get(column);
67      }
68  
69      public abstract List<T> getList();
70  
71      public void onColumnSort(ColumnSortEvent event) {
72          // Get the sorted column.
73          Column<T, ?> column = (Column<T, ?>) event.getColumn();
74          if (column == null) {
75              return;
76          }
77  
78          // Get the comparator.
79          final Comparator<T> comparator = getComparator(column);
80          if (comparator == null) {
81              return;
82          }
83  
84          // Sort using the comparator.
85          List<T> sortedList = new ArrayList<T>(getList());
86          if (event.isSortAscending()) {
87              Collections.sort(sortedList, comparator);
88          } else {
89              Collections.sort(sortedList, new Comparator<T>() {
90                  public int compare(T o1, T o2) {
91                      return -comparator.compare(o1, o2);
92                  }
93              });
94          }
95          hasData.setRowData(0, sortedList);
96      }
97  
98      /**
99       * Set the comparator used to sort the specified column in ascending order.
100      * 
101      * @param column
102      *            the {@link Column}
103      * @param comparator
104      *            the {@link Comparator} to use for the {@link Column}
105      */
106     public void setComparator(Column<T, ?> column, Comparator<T> comparator) {
107         comparators.put(column, comparator);
108     }
109 }