litesoft
@ 948
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
/* * Copyright 2008 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.gen2.table.client; import com.google.gwt.gen2.event.shared.*; import com.google.gwt.gen2.table.client.TableModelHelper.*; import com.google.gwt.gen2.table.event.client.*; import com.google.gwt.user.client.*; import java.util.*; /** * A variation of the {@link Grid} * that supports sorting and row movement. */ public class SortableGrid extends SelectionGrid implements HasColumnSortHandlers { /** * The column sorter defines an algorithm to sort columns. */ public abstract static class ColumnSorter { /** * Override this method to implement a custom column sorting algorithm. * * @param grid the grid that the sorting will be applied to * @param sortList the list of columns to sort by * @param callback the callback object when sorting is complete */ public abstract void onSortColumn( SortableGrid grid, ColumnSortList sortList, SortableGrid.ColumnSorterCallback callback ); } /** * The default {@link ColumnSorter} used if no other {@link ColumnSorter} is * specified. This column sorted uses a quicksort algorithm to sort columns. */ private static class DefaultColumnSorter extends ColumnSorter { @Override public void onSortColumn( SortableGrid grid, ColumnSortList sortList, SortableGrid.ColumnSorterCallback callback ) { // Get the primary column and sort order int column = sortList.getPrimaryColumn(); boolean ascending = sortList.isPrimaryAscending(); // Get all of the cell elements SelectionGridCellFormatter formatter = grid.getSelectionGridCellFormatter(); int rowCount = grid.getRowCount(); List<Element> tdElems = new ArrayList<Element>( rowCount ); for ( int i = 0; i < rowCount; i++ ) { tdElems.add( formatter.getRawElement( i, column ) ); } // Sort the cell elements if ( ascending ) { Collections.sort( tdElems, new Comparator<Element>() { @Override public int compare( Element o1, Element o2 ) { return o1.getInnerText().compareTo( o2.getInnerText() ); } } ); } else { Collections.sort( tdElems, new Comparator<Element>() { @Override public int compare( Element o1, Element o2 ) { return o2.getInnerText().compareTo( o1.getInnerText() ); } } ); } // Convert tdElems to trElems, reversing if needed Element[] trElems = new Element[rowCount]; for ( int i = 0; i < rowCount; i++ ) { trElems[i] = DOM.getParent( tdElems.get( i ) ); } // Use the callback to complete the sorting callback.onSortingComplete( trElems ); } } /** * Callback that is called when the row sorting is complete. */ public class ColumnSorterCallback { /** * An array of the tr elements that should be reselected. */ private Element[] selectedRows; /** * Construct a new {@link ColumnSorterCallback}. */ protected ColumnSorterCallback( Element[] selectedRows ) { this.selectedRows = selectedRows; } /** * Set the order of all rows after a column sort request. * <p/> * This method takes an array of the row indexes in this Grid, ordered * according to their new positions in the Grid. * * @param trIndexes the row index in their new order */ public void onSortingComplete( int[] trIndexes ) { // Convert indexes to row elements SelectionGridRowFormatter formatter = getSelectionGridRowFormatter(); Element[] trElems = new Element[trIndexes.length]; for ( int i = 0; i < trElems.length; i++ ) { trElems[i] = formatter.getRawElement( trIndexes[i] ); } // Call main callback method onSortingComplete( trElems ); } /** * Set the order of all rows after a column sort request. * <p/> * This method takes an array of the row elements in this Grid, ordered * according to their new positions in the Grid. * * @param trElems the row elements in their new order */ public void onSortingComplete( Element[] trElems ) { // Move the rows to their new positions applySort( trElems ); // Fire the listeners onSortingComplete(); } /** * Trigger the callback, but do not change the ordering of the rows. * <p/> * Use this method if your {@link ColumnSorter} rearranges the columns * manually. */ public void onSortingComplete() { // Reselect things that need reselecting for ( int i = 0; i < selectedRows.length; i++ ) { int rowIndex = getRowIndex( selectedRows[i] ); if ( rowIndex >= 0 ) { selectRow( rowIndex, false ); } } fireColumnSorted(); } } /** * The class used to do column sorting. */ private ColumnSorter columnSorter = null; /** * Information about the sorted columns. */ private ColumnSortList columnSortList = new ColumnSortList(); /** * Constructor. */ public SortableGrid() { super(); } /** * Constructs a {@link SortableGrid} with the requested size. * * @param rows the number of rows * @param columns the number of columns * * @throws IndexOutOfBoundsException */ public SortableGrid( int rows, int columns ) { this(); resize( rows, columns ); } @Override public HandlerRegistration addColumnSortHandler( ColumnSortHandler handler ) { return addHandler( ColumnSortEvent.TYPE, handler ); } /** * @return the column sorter used to sort columns */ public ColumnSorter getColumnSorter() { return getColumnSorter( false ); } /** * @return the {@link ColumnSortList} of previously sorted columns */ public ColumnSortList getColumnSortList() { return columnSortList; } /** * Move a row up (relative to the screen) one index to a lesser index. * * @param row the row index to move * * @throws IndexOutOfBoundsException */ public void moveRowDown( int row ) { swapRows( row, row + 1 ); } /** * Move a row down (relative to the screen) one index to a greater index. * * @param row the row index to move * * @throws IndexOutOfBoundsException */ public void moveRowUp( int row ) { swapRows( row, row - 1 ); } /** * Completely reverse the order of all rows in the table. */ public void reverseRows() { int lastRow = numRows - 1; for ( int i = 0; i < numRows / 2; i++ ) { swapRowsRaw( i, lastRow ); lastRow--; } // Set the column sorting as reversed for ( ColumnSortInfo sortInfo : columnSortList ) { sortInfo.setAscending( !sortInfo.isAscending() ); } fireColumnSorted(); } /** * Set the {@link ColumnSorter}. * * @param sorter the new {@link ColumnSorter} */ public void setColumnSorter( ColumnSorter sorter ) { this.columnSorter = sorter; } /** * Set the current {@link ColumnSortList} and fire an event. * * @param columnSortList the new {@link ColumnSortList} */ public void setColumnSortList( ColumnSortList columnSortList ) { setColumnSortList( columnSortList, true ); } /** * Set the current {@link ColumnSortList} and optionally fire an event. * * @param columnSortList the new {@link ColumnSortList} * @param fireEvents true to trigger the onSort event */ public void setColumnSortList( ColumnSortList columnSortList, boolean fireEvents ) { assert columnSortList != null : "columnSortList cannot be null"; this.columnSortList = columnSortList; if ( fireEvents ) { fireColumnSorted(); } } /** * Sort the grid according to the specified column. If the column is already * sorted, reverse sort it. * * @param column the column to sort * * @throws IndexOutOfBoundsException */ public void sortColumn( int column ) { if ( column == columnSortList.getPrimaryColumn() ) { sortColumn( column, !columnSortList.isPrimaryAscending() ); } else { sortColumn( column, true ); } } /** * Sort the grid according to the specified column. * * @param column the column to sort * @param ascending sort the column in ascending order * * @throws IndexOutOfBoundsException */ public void sortColumn( int column, boolean ascending ) { // Verify the column bounds if ( column < 0 ) { throw new IndexOutOfBoundsException( "Cannot access a column with a negative index: " + column ); } else if ( column >= numColumns ) { throw new IndexOutOfBoundsException( "Column index: " + column + ", Column size: " + numColumns ); } // Add the sorting to the list of sorted columns columnSortList.add( new ColumnSortInfo( column, ascending ) ); // Use the onSort method to actually sort the column Element[] selectedRows = getSelectedRowsMap().values().toArray( new Element[0] ); deselectAllRows(); getColumnSorter( true ).onSortColumn( this, columnSortList, new SortableGrid.ColumnSorterCallback( selectedRows ) ); } /** * Swap the positions of two rows. * * @param row1 the first row to swap * @param row2 the second row to swap * * @throws IndexOutOfBoundsException */ public void swapRows( int row1, int row2 ) { checkRowBounds( row1 ); checkRowBounds( row2 ); swapRowsRaw( row1, row2 ); } /** * Fire column sorted event to listeners. */ protected void fireColumnSorted() { fireEvent( new ColumnSortEvent( columnSortList ) ); } /** * Get the {@link ColumnSorter}. Optionally create a * {@link DefaultColumnSorter} if the user hasn't specified one already. * * @param createAsNeeded create a default sorter if needed * * @return the column sorter */ protected ColumnSorter getColumnSorter( boolean createAsNeeded ) { if ( (columnSorter == null) && createAsNeeded ) { columnSorter = new DefaultColumnSorter(); } return columnSorter; } /** * Swap two rows without checking the cell bounds. * * @param row1 the first row to swap * @param row2 the second row to swap */ protected void swapRowsRaw( int row1, int row2 ) { Element tbody = getBodyElement(); if ( row1 == row2 + 1 ) { // Just move row1 up one Element tr = getSelectionGridRowFormatter().getRawElement( row1 ); int index = OverrideDOM.getRowIndex( tr ); DOM.removeChild( tbody, tr ); DOM.insertChild( tbody, tr, index - 1 ); } else if ( row2 == row1 + 1 ) { // Just move row2 up one Element tr = getSelectionGridRowFormatter().getRawElement( row2 ); int index = OverrideDOM.getRowIndex( tr ); DOM.removeChild( tbody, tr ); DOM.insertChild( tbody, tr, index - 1 ); } else if ( row1 == row2 ) { // Do nothing if rows are the same return; } else { // Remove both rows Element tr1 = getSelectionGridRowFormatter().getRawElement( row1 ); Element tr2 = getSelectionGridRowFormatter().getRawElement( row2 ); int index1 = OverrideDOM.getRowIndex( tr1 ); int index2 = OverrideDOM.getRowIndex( tr2 ); DOM.removeChild( tbody, tr1 ); DOM.removeChild( tbody, tr2 ); // Reinsert them into the table if ( row1 > row2 ) { DOM.insertChild( tbody, tr1, index2 ); DOM.insertChild( tbody, tr2, index1 ); } else if ( row1 < row2 ) { DOM.insertChild( tbody, tr2, index1 ); DOM.insertChild( tbody, tr1, index2 ); } } // Update the selected rows table Map<Integer, Element> selectedRows = getSelectedRowsMap(); Element tr1 = selectedRows.remove( new Integer( row1 ) ); Element tr2 = selectedRows.remove( new Integer( row2 ) ); if ( tr1 != null ) { selectedRows.put( new Integer( row2 ), tr1 ); } if ( tr2 != null ) { selectedRows.put( new Integer( row1 ), tr2 ); } } /** * Set the order of all rows after a column sort request. * <p/> * This method takes an array of the row elements in this Grid, ordered * according to their new positions in the Grid. * * @param trElems the row elements in their new order */ void applySort( Element[] trElems ) { // Move the rows to their new positions Element bodyElem = getBodyElement(); for ( int i = trElems.length - 1; i >= 0; i-- ) { if ( trElems[i] != null ) { DOM.removeChild( bodyElem, trElems[i] ); DOM.insertChild( bodyElem, trElems[i], 0 ); } } } } |