| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.student.common.ui.client.widgets.pagetable; | 
  | 17 |  |   | 
  | 18 |  |  import java.util.ArrayList; | 
  | 19 |  |  import java.util.Collections; | 
  | 20 |  |  import java.util.Comparator; | 
  | 21 |  |  import java.util.Iterator; | 
  | 22 |  |  import java.util.List; | 
  | 23 |  |   | 
  | 24 |  |  import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow; | 
  | 25 |  |  import org.kuali.student.common.ui.client.widgets.searchtable.SearchColumnDefinition; | 
  | 26 |  |  import org.kuali.student.core.dto.Idable; | 
  | 27 |  |   | 
  | 28 |  |  import com.google.gwt.gen2.table.client.AbstractColumnDefinition; | 
  | 29 |  |  import com.google.gwt.gen2.table.client.CachedTableModel; | 
  | 30 |  |  import com.google.gwt.gen2.table.client.MutableTableModel; | 
  | 31 |  |  import com.google.gwt.gen2.table.client.TableModelHelper.Request; | 
  | 32 |  |  import com.google.gwt.gen2.table.client.TableModelHelper.Response; | 
  | 33 |  |  import com.google.gwt.gen2.table.client.TableModelHelper.SerializableResponse; | 
  | 34 |  |   | 
  | 35 |  |   | 
  | 36 |  |   | 
  | 37 |  |   | 
  | 38 |  |   | 
  | 39 |  |   | 
  | 40 |  |   | 
  | 41 |  |   | 
  | 42 |  |   | 
  | 43 | 0 |  public class GenericTableModel<RowType> extends MutableTableModel<RowType> { | 
  | 44 | 0 |      private List<RowType> rowDTOs = null; | 
  | 45 | 0 |      private int fromIndex = 0; | 
  | 46 | 0 |      private int lastSortedColumn = -1; | 
  | 47 | 0 |      private int lastSortDirection = 1; | 
  | 48 |  |   | 
  | 49 |  |   | 
  | 50 |  |      private List<AbstractColumnDefinition<ResultRow, ?>> columnDefs; | 
  | 51 |  |   | 
  | 52 |  |       | 
  | 53 |  |   | 
  | 54 |  |   | 
  | 55 | 0 |      private boolean errorMode = false; | 
  | 56 |  |       | 
  | 57 |  |   | 
  | 58 |  |   | 
  | 59 | 0 |      private boolean zeroMode = false; | 
  | 60 |  |       | 
  | 61 |  |   | 
  | 62 |  |   | 
  | 63 |  |   | 
  | 64 |  |      public GenericTableModel() { | 
  | 65 | 0 |          super(); | 
  | 66 | 0 |      } | 
  | 67 |  |       | 
  | 68 |  |   | 
  | 69 |  |   | 
  | 70 |  |   | 
  | 71 |  |      public GenericTableModel(List<RowType>rows) { | 
  | 72 | 0 |          super(); | 
  | 73 | 0 |          setRows(rows); | 
  | 74 | 0 |      } | 
  | 75 |  |       | 
  | 76 |  |   | 
  | 77 |  |   | 
  | 78 |  |   | 
  | 79 |  |      public CachedTableModel<RowType>createCachedTableModel(int pageSize,int pages) { | 
  | 80 | 0 |          CachedTableModel<RowType>cachedTableModel = new CachedTableModel<RowType>(this); | 
  | 81 | 0 |          cachedTableModel.setPreCachedRowCount(pageSize); | 
  | 82 | 0 |          cachedTableModel.setPostCachedRowCount(pageSize); | 
  | 83 | 0 |          cachedTableModel.setRowCount(pageSize * pages); | 
  | 84 | 0 |          return cachedTableModel; | 
  | 85 |  |      } | 
  | 86 |  |       | 
  | 87 |  |   | 
  | 88 |  |   | 
  | 89 |  |   | 
  | 90 |  |   | 
  | 91 |  |      @Override | 
  | 92 |  |      protected boolean onRowInserted(int beforeRow) { | 
  | 93 | 0 |          return true; | 
  | 94 |  |      } | 
  | 95 |  |   | 
  | 96 |  |       | 
  | 97 |  |   | 
  | 98 |  |   | 
  | 99 |  |   | 
  | 100 |  |   | 
  | 101 |  |      @Override | 
  | 102 |  |      protected boolean onRowRemoved(int row) { | 
  | 103 | 0 |          return true; | 
  | 104 |  |      } | 
  | 105 |  |   | 
  | 106 |  |       | 
  | 107 |  |   | 
  | 108 |  |   | 
  | 109 |  |   | 
  | 110 |  |   | 
  | 111 |  |      @Override | 
  | 112 |  |      protected boolean onSetRowValue(int row, RowType rowValue) { | 
  | 113 | 0 |          return true; | 
  | 114 |  |      } | 
  | 115 |  |   | 
  | 116 |  |       | 
  | 117 |  |   | 
  | 118 |  |   | 
  | 119 |  |   | 
  | 120 |  |   | 
  | 121 |  |   | 
  | 122 |  |   | 
  | 123 |  |      @Override | 
  | 124 |  |      public void requestRows(final Request request, final Callback<RowType> callback) { | 
  | 125 | 0 |          if (errorMode) { | 
  | 126 |  |               | 
  | 127 | 0 |              callback.onFailure(new Exception("An error has occured.")); | 
  | 128 | 0 |          } else if (zeroMode) { | 
  | 129 |  |               | 
  | 130 | 0 |              List<RowType> selectedRows = new ArrayList<RowType>(0); | 
  | 131 | 0 |              callback.onRowsReady(request, new SerializableResponse(selectedRows)); | 
  | 132 | 0 |          } else { | 
  | 133 | 0 |              callback.onRowsReady(request, new Response<RowType>() { | 
  | 134 |  |   | 
  | 135 |  |                  @SuppressWarnings("unchecked") | 
  | 136 |  |                  @Override | 
  | 137 |  |                  public Iterator<RowType> getRowValues() { | 
  | 138 |  |                       | 
  | 139 |  |   | 
  | 140 | 0 |                      final int sortColumn = request.getColumnSortList().getPrimaryColumn(); | 
  | 141 | 0 |                      final int sortDirection = (request.getColumnSortList().isPrimaryAscending() ? 1 : -1); | 
  | 142 | 0 |                      if (sortColumn != lastSortedColumn || sortDirection != lastSortDirection) { | 
  | 143 | 0 |                          SearchColumnDefinition rowDef = (SearchColumnDefinition) columnDefs.get(sortColumn); | 
  | 144 | 0 |                          final String sortColumKey = rowDef.getColumnKey(); | 
  | 145 | 0 |                          Collections.sort((List<ResultRow>)rowDTOs, new Comparator<ResultRow>() { | 
  | 146 |  |   | 
  | 147 |  |                              @Override | 
  | 148 |  |                              public int compare(ResultRow o1, ResultRow o2) { | 
  | 149 | 0 |                                  return o1.getValue(sortColumKey).compareToIgnoreCase(o2.getValue(sortColumKey)) * sortDirection; | 
  | 150 |  |                              }}); | 
  | 151 | 0 |                          lastSortedColumn = sortColumn; | 
  | 152 | 0 |                          lastSortDirection = sortDirection; | 
  | 153 |  |                      } | 
  | 154 | 0 |                      int numRows = request.getNumRows(); | 
  | 155 |  |                       | 
  | 156 | 0 |                      if((fromIndex + numRows) >= rowDTOs.size()) { | 
  | 157 | 0 |                          fromIndex = 0; | 
  | 158 |  |                      } | 
  | 159 | 0 |                      int toIndex = fromIndex + numRows; | 
  | 160 | 0 |                      List<RowType> selectedRows = new ArrayList<RowType>(numRows); | 
  | 161 | 0 |                      for(int i = fromIndex; i < toIndex; i++) { | 
  | 162 | 0 |                          RowType e = rowDTOs.get(i); | 
  | 163 | 0 |                          selectedRows.add(e); | 
  | 164 |  |                      } | 
  | 165 |  |   | 
  | 166 | 0 |                      fromIndex = toIndex; | 
  | 167 | 0 |                      return new SerializableResponse(selectedRows).getRowValues(); | 
  | 168 |  |                  } | 
  | 169 |  |              }); | 
  | 170 |  |          } | 
  | 171 | 0 |      } | 
  | 172 |  |   | 
  | 173 |  |       | 
  | 174 |  |   | 
  | 175 |  |   | 
  | 176 |  |      public void setRows(List<RowType> rows) { | 
  | 177 |  |   | 
  | 178 | 0 |              List<Idable> ids = (List<Idable>)rows; | 
  | 179 | 0 |              this.rowDTOs = rows; | 
  | 180 | 0 |      } | 
  | 181 |  |       | 
  | 182 |  |   | 
  | 183 |  |   | 
  | 184 |  |   | 
  | 185 |  |   | 
  | 186 |  |      @Override | 
  | 187 |  |      public int getRowCount() { | 
  | 188 |  |           | 
  | 189 | 0 |          return rowDTOs.size(); | 
  | 190 |  |      } | 
  | 191 |  |       | 
  | 192 |  |   | 
  | 193 |  |   | 
  | 194 |  |      public boolean isErrorMode() { | 
  | 195 | 0 |          return errorMode; | 
  | 196 |  |      } | 
  | 197 |  |       | 
  | 198 |  |   | 
  | 199 |  |   | 
  | 200 |  |      public void setErrorMode(boolean errorMode) { | 
  | 201 | 0 |          this.errorMode = errorMode; | 
  | 202 | 0 |      } | 
  | 203 |  |       | 
  | 204 |  |   | 
  | 205 |  |   | 
  | 206 |  |      public boolean isZeroMode() { | 
  | 207 | 0 |          return zeroMode; | 
  | 208 |  |      } | 
  | 209 |  |       | 
  | 210 |  |   | 
  | 211 |  |   | 
  | 212 |  |      public void setZeroMode(boolean zeroMode) { | 
  | 213 | 0 |          this.zeroMode = zeroMode; | 
  | 214 | 0 |      } | 
  | 215 |  |   | 
  | 216 |  |      public void setColumnDefs(List<AbstractColumnDefinition<ResultRow, ?>> columnDefs) { | 
  | 217 | 0 |          this.columnDefs = columnDefs; | 
  | 218 | 0 |      } | 
  | 219 |  |  } |