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 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 |
/* * 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 org.litesoft.commonfoundation.html.*; import com.google.gwt.event.dom.client.*; import com.google.gwt.gen2.event.shared.*; import com.google.gwt.gen2.table.client.CellEditor.*; import com.google.gwt.gen2.table.client.FlexTable.*; import com.google.gwt.gen2.table.client.SelectionGrid.*; import com.google.gwt.gen2.table.client.SortableGrid.*; import com.google.gwt.gen2.table.client.TableDefinition.*; import com.google.gwt.gen2.table.client.TableModel.Callback; import com.google.gwt.gen2.table.client.TableModelHelper.*; import com.google.gwt.gen2.table.client.property.*; import com.google.gwt.gen2.table.event.client.*; import com.google.gwt.gen2.table.event.client.TableEvent.*; import com.google.gwt.user.client.ui.*; import com.google.gwt.user.client.ui.HasHorizontalAlignment.*; import com.google.gwt.user.client.ui.HasVerticalAlignment.*; import java.util.*; /** * An {@link AbstractScrollTable} that acts as a view for an underlying * {@link MutableTableModel}. * * @param <RowType> the data type of the row values */ public class PagingScrollTable<RowType> extends AbstractScrollTable implements HasTableDefinition<RowType>, HasPageCountChangeHandlers, HasPageLoadHandlers, HasPageChangeHandlers, HasPagingFailureHandlers { /** * A custom {@link AbstractCellView} used by the {@link PagingScrollTable}. * * @param <RowType> the type of the row values */ protected static class PagingScrollTableCellView<RowType> extends AbstractCellView<RowType> { private PagingScrollTable<RowType> table; public PagingScrollTableCellView( PagingScrollTable<RowType> table ) { super( table ); this.table = table; } @Override public void setHorizontalAlignment( HorizontalAlignmentConstant align ) { table.getDataTable().getCellFormatter().setHorizontalAlignment( getRowIndex(), getCellIndex(), align ); } @Override public void setHTML( String html ) { table.getDataTable().setHTML( getRowIndex(), getCellIndex(), html ); } @Override public void setStyleAttribute( String attr, String value ) { table.getDataTable().getFixedWidthGridCellFormatter().getRawElement( getRowIndex(), getCellIndex() ).getStyle().setProperty( attr, value ); } @Override public void setStyleName( String stylename ) { table.getDataTable().getCellFormatter().setStyleName( getRowIndex(), getCellIndex(), stylename ); } @Override public void setText( String text ) { table.getDataTable().setText( getRowIndex(), getCellIndex(), text ); } @Override public void setVerticalAlignment( VerticalAlignmentConstant align ) { table.getDataTable().getCellFormatter().setVerticalAlignment( getRowIndex(), getCellIndex(), align ); } @Override public void setWidget( Widget widget ) { table.getDataTable().setWidget( getRowIndex(), getCellIndex(), widget ); } } /** * A custom {@link AbstractRowView} used by the {@link PagingScrollTable}. * * @param <RowType> the type of the row values */ protected static class PagingScrollTableRowView<RowType> extends AbstractRowView<RowType> { private PagingScrollTable<RowType> table; public PagingScrollTableRowView( PagingScrollTable<RowType> table ) { super( new PagingScrollTableCellView<RowType>( table ) ); this.table = table; } @Override public void setStyleAttribute( String attr, String value ) { table.getDataTable().getFixedWidthGridRowFormatter().getRawElement( getRowIndex() ).getStyle().setProperty( attr, value ); } @Override public void setStyleName( String stylename ) { // If the row is selected, add the selected style name back if ( table.getDataTable().isRowSelected( getRowIndex() ) ) { stylename += " selected"; } table.getDataTable().getRowFormatter().setStyleName( getRowIndex(), stylename ); } } /** * Information about a column header. */ private static class ColumnHeaderInfo { private int rowSpan = 1; private Object header; public ColumnHeaderInfo( Object header ) { this.header = (header == null) ? "" : header; } public ColumnHeaderInfo( Object header, int rowSpan ) { this.header = (header == null) ? HTMLConstants.NBSP : header; this.rowSpan = rowSpan; } @Override public boolean equals( Object o ) { if ( o == null ) { return false; } if ( o instanceof ColumnHeaderInfo ) { ColumnHeaderInfo info = (ColumnHeaderInfo) o; return (rowSpan == info.rowSpan) && header.equals( info.header ); } return false; } public Object getHeader() { return header; } public int getRowSpan() { return rowSpan; } public void incrementRowSpan() { rowSpan++; } } /** * An iterator over the visible rows in an iterator over many rows. */ private class VisibleRowsIterator implements Iterator<RowType> { /** * The iterator of row data. */ private Iterator<RowType> rows; /** * The current row of the rows iterator. */ private int curRow; /** * The last visible row in the grid. */ private int lastVisibleRow; /** * Constructor. * * @param rows the iterator over row data * @param firstRow the first absolute row of the rows iterator * @param firstVisibleRow the first visible row in this grid * @param lastVisibleRow the last visible row in this grid */ public VisibleRowsIterator( Iterator<RowType> rows, int firstRow, int firstVisibleRow, int lastVisibleRow ) { this.curRow = firstRow; this.lastVisibleRow = lastVisibleRow; // Iterate up to the first row while ( curRow < firstVisibleRow && rows.hasNext() ) { rows.next(); curRow++; } this.rows = rows; } public boolean hasNext() { return (curRow <= lastVisibleRow && rows.hasNext()); } public RowType next() { // Check that the next row exists if ( !hasNext() ) { throw new NoSuchElementException(); } return rows.next(); } public void remove() { throw new UnsupportedOperationException( "Remove not supported" ); } } /** * The bulk render used to render the contents of this table. */ private FixedWidthGridBulkRenderer<RowType> bulkRenderer = null; /** * The wrapper around the empty table widget. */ private SimplePanel emptyTableWidgetWrapper = new SimplePanel(); /** * The definition of the columns in the table. */ private TableDefinition<RowType> tableDefinition = null; /** * The current visible page. */ private int currentPage = -1; /** * The last request that was sent to the {@link TableModel}. */ private Request lastRequest = null; /** * A boolean indicating that cross page selection is enabled. */ private boolean isCrossPageSelectionEnabled; /** * The set of selected row values. */ private Set<RowType> selectedRowValues = new HashSet<RowType>(); /** * A boolean indicating that the footer should be generated automatically. */ private boolean isFooterGenerated; /** * A boolean indicating that the header should be generated automatically. */ private boolean isHeaderGenerated; /** * A boolean indicating that the page is currently being loaded. */ private boolean isPageLoading; /** * The old page count, used to detect when the number of pages changes. */ private int oldPageCount; /** * The number of rows per page. If the number of rows per page is equal to the * number of rows, paging is disabled because only one page exists. */ private int pageSize = 0; /** * The callback that handles page requests. */ private Callback<RowType> pagingCallback = new Callback<RowType>() { public void onFailure( Throwable caught ) { isPageLoading = false; fireEvent( new PagingFailureEvent( caught ) ); } public void onRowsReady( Request request, Response<RowType> response ) { if ( lastRequest == request ) { setData( request.getStartRow(), response.getRowValues() ); lastRequest = null; } } }; /** * The values associated with each row. This is an optional list of data that * ties the visible content in each row to an underlying object. */ private List<RowType> rowValues = new ArrayList<RowType>(); /** * The view of this table. */ private AbstractRowView<RowType> rowView = new PagingScrollTableRowView<RowType>( this ); /** * The {@link CheckBox} used to select all rows. */ private Widget selectAllWidget; /** * The underlying table model. */ private TableModel<RowType> tableModel; /** * The {@link RendererCallback} used when table rendering completes. */ private RendererCallback tableRendererCallback = new RendererCallback() { public void onRendered() { onDataTableRendered(); } }; /** * The columns that are currently visible. */ private List<ColumnDefinition<RowType, ?>> visibleColumns = new ArrayList<ColumnDefinition<RowType, ?>>(); /** * The boolean indicating that the header tables are obsolete. */ private boolean headersObsolete; /** * Construct a new {@link PagingScrollTable}. * * @param tableModel the underlying table model * @param tableDefinition the column definitions */ public PagingScrollTable( TableModel<RowType> tableModel, TableDefinition<RowType> tableDefinition ) { this( tableModel, new FixedWidthGrid(), new FixedWidthFlexTable(), tableDefinition ); isHeaderGenerated = true; isFooterGenerated = true; } /** * Construct a new {@link PagingScrollTable}. * * @param tableModel the underlying table model * @param dataTable the table used to display data * @param headerTable the header table * @param tableDefinition the column definitions */ public PagingScrollTable( TableModel<RowType> tableModel, FixedWidthGrid dataTable, FixedWidthFlexTable headerTable, TableDefinition<RowType> tableDefinition ) { super( dataTable, headerTable ); this.tableModel = tableModel; setTableDefinition( tableDefinition ); refreshVisibleColumnDefinitions(); oldPageCount = getPageCount(); // Setup the empty table widget wrapper emptyTableWidgetWrapper.getElement().getStyle().setProperty( "width", "100%" ); emptyTableWidgetWrapper.getElement().getStyle().setProperty( "overflow", "hidden" ); emptyTableWidgetWrapper.getElement().getStyle().setPropertyPx( "border", 0 ); emptyTableWidgetWrapper.getElement().getStyle().setPropertyPx( "margin", 0 ); emptyTableWidgetWrapper.getElement().getStyle().setPropertyPx( "padding", 0 ); insert( emptyTableWidgetWrapper, getAbsoluteElement(), 2, true ); setEmptyTableWidgetVisible( false ); // Listen to table model events tableModel.addRowCountChangeHandler( new RowCountChangeHandler() { public void onRowCountChange( RowCountChangeEvent event ) { int pageCount = getPageCount(); if ( pageCount != oldPageCount ) { fireEvent( new PageCountChangeEvent( oldPageCount, pageCount ) ); oldPageCount = pageCount; } } } ); if ( tableModel instanceof HasRowInsertionHandlers ) { ((HasRowInsertionHandlers) tableModel).addRowInsertionHandler( new RowInsertionHandler() { public void onRowInsertion( RowInsertionEvent event ) { insertAbsoluteRow( event.getRowIndex() ); } } ); } if ( tableModel instanceof HasRowRemovalHandlers ) { ((HasRowRemovalHandlers) tableModel).addRowRemovalHandler( new RowRemovalHandler() { public void onRowRemoval( RowRemovalEvent event ) { removeAbsoluteRow( event.getRowIndex() ); } } ); } if ( tableModel instanceof HasRowValueChangeHandlers ) { ((HasRowValueChangeHandlers<RowType>) tableModel).addRowValueChangeHandler( new RowValueChangeHandler<RowType>() { public void onRowValueChange( RowValueChangeEvent<RowType> event ) { int rowIndex = event.getRowIndex(); if ( rowIndex < getAbsoluteFirstRowIndex() || rowIndex > getAbsoluteLastRowIndex() ) { return; } setRowValue( rowIndex - getAbsoluteFirstRowIndex(), event.getRowValue() ); } } ); } // Listen for cell click events dataTable.addTableListener( new TableListener() { public void onCellClicked( SourcesTableEvents sender, int row, int cell ) { editCell( row, cell ); } } ); // Override the column sorter if ( dataTable.getColumnSorter() == null ) { ColumnSorter sorter = new ColumnSorter() { @Override public void onSortColumn( SortableGrid grid, ColumnSortList sortList, ColumnSorterCallback callback ) { reloadPage(); callback.onSortingComplete(); } }; dataTable.setColumnSorter( sorter ); } // Listen for selection events dataTable.addRowSelectionHandler( new RowSelectionHandler() { public void onRowSelection( RowSelectionEvent event ) { if ( isPageLoading ) { return; } Set<Row> deselected = event.getDeselectedRows(); for ( Row row : deselected ) { selectedRowValues.remove( getRowValue( row.getRowIndex() ) ); } Set<Row> selected = event.getSelectedRows(); for ( Row row : selected ) { selectedRowValues.add( getRowValue( row.getRowIndex() ) ); } } } ); } public HandlerRegistration addPageChangeHandler( PageChangeHandler handler ) { return addHandler( PageChangeEvent.TYPE, handler ); } public HandlerRegistration addPageCountChangeHandler( PageCountChangeHandler handler ) { return addHandler( PageCountChangeEvent.TYPE, handler ); } public HandlerRegistration addPageLoadHandler( PageLoadHandler handler ) { return addHandler( PageLoadEvent.TYPE, handler ); } public HandlerRegistration addPagingFailureHandler( PagingFailureHandler handler ) { return addHandler( PagingFailureEvent.TYPE, handler ); } /** * @return the absolute index of the first visible row */ public int getAbsoluteFirstRowIndex() { return currentPage * pageSize; } /** * @return the absolute index of the last visible row */ public int getAbsoluteLastRowIndex() { if ( tableModel.getRowCount() < 0 ) { // Unknown row count, so just return based on current page return (currentPage + 1) * pageSize - 1; } else if ( pageSize == 0 ) { // Only one page, so return row count return tableModel.getRowCount() - 1; } return Math.min( tableModel.getRowCount(), (currentPage + 1) * pageSize ) - 1; } /** * @return the current page */ public int getCurrentPage() { return currentPage; } /** * @return the widget displayed when the data table is empty */ public Widget getEmptyTableWidget() { return emptyTableWidgetWrapper.getWidget(); } @Override public int getMaximumColumnWidth( int column ) { ColumnDefinition<RowType, ?> colDef = getColumnDefinition( column ); if ( colDef == null ) { return -1; } return colDef.getColumnProperty( MaximumWidthProperty.TYPE ).getMaximumColumnWidth(); } @Override public int getMinimumColumnWidth( int column ) { ColumnDefinition<RowType, ?> colDef = getColumnDefinition( column ); if ( colDef == null ) { return FixedWidthGrid.MIN_COLUMN_WIDTH; } int minWidth = colDef.getColumnProperty( MinimumWidthProperty.TYPE ).getMinimumColumnWidth(); return Math.max( FixedWidthGrid.MIN_COLUMN_WIDTH, minWidth ); } /** * @return the number of pages, or -1 if not known */ public int getPageCount() { if ( pageSize < 1 ) { return 1; } else { int numDataRows = tableModel.getRowCount(); if ( numDataRows < 0 ) { return -1; } return (int) Math.ceil( numDataRows / (pageSize + 0.0) ); } } /** * @return the number of rows per page */ public int getPageSize() { return pageSize; } @Override public int getPreferredColumnWidth( int column ) { ColumnDefinition<RowType, ?> colDef = getColumnDefinition( column ); if ( colDef == null ) { return FixedWidthGrid.DEFAULT_COLUMN_WIDTH; } return colDef.getColumnProperty( PreferredWidthProperty.TYPE ).getPreferredColumnWidth(); } /** * Get the value associated with a row. * * @param row the row index * * @return the value associated with the row */ public RowType getRowValue( int row ) { if ( rowValues.size() <= row ) { return null; } return rowValues.get( row ); } /** * Get the selected row values. If cross page selection is enabled, this will * include row values selected on all pages. * * @return the selected row values * * @see #setCrossPageSelectionEnabled(boolean) */ public Set<RowType> getSelectedRowValues() { return selectedRowValues; } public TableDefinition<RowType> getTableDefinition() { return tableDefinition; } /** * @return the table model */ public TableModel<RowType> getTableModel() { return tableModel; } /** * Go to the first page. */ public void gotoFirstPage() { gotoPage( 0, false ); } /** * Go to the last page. If the number of pages is not known, this method is * ignored. */ public void gotoLastPage() { if ( getPageCount() >= 0 ) { gotoPage( getPageCount(), false ); } } /** * Go to the next page. */ public void gotoNextPage() { gotoPage( currentPage + 1, false ); } /** * Set the current page. If the page is out of bounds, it will be * automatically set to zero or the last page without throwing any errors. * * @param page the page * @param forced reload the page even if it is already loaded */ public void gotoPage( int page, boolean forced ) { int oldPage = currentPage; int numPages = getPageCount(); if ( numPages >= 0 ) { currentPage = Math.max( 0, Math.min( page, numPages - 1 ) ); } else { currentPage = page; } if ( currentPage != oldPage || forced ) { isPageLoading = true; // Deselect rows when switching pages FixedWidthGrid dataTable = getDataTable(); dataTable.deselectAllRows(); if ( !isCrossPageSelectionEnabled ) { selectedRowValues = new HashSet<RowType>(); } // Fire listeners fireEvent( new PageChangeEvent( oldPage, currentPage ) ); // Clear out existing data if we aren't bulk rendering if ( bulkRenderer == null ) { int rowCount = getAbsoluteLastRowIndex() - getAbsoluteFirstRowIndex() + 1; if ( rowCount != dataTable.getRowCount() ) { dataTable.resizeRows( rowCount ); } dataTable.clearAll(); } // Request the new data from the table model int firstRow = getAbsoluteFirstRowIndex(); int lastRow = pageSize == 0 ? tableModel.getRowCount() : pageSize; lastRequest = new Request( firstRow, lastRow, dataTable.getColumnSortList() ); tableModel.requestRows( lastRequest, pagingCallback ); } } /** * Go to the previous page. */ public void gotoPreviousPage() { gotoPage( currentPage - 1, false ); } @Override public boolean isColumnSortable( int column ) { ColumnDefinition<RowType, ?> colDef = getColumnDefinition( column ); if ( colDef == null ) { return true; } if ( getSortPolicy() == SortPolicy.DISABLED ) { return false; } return colDef.getColumnProperty( SortableProperty.TYPE ).isColumnSortable(); } @Override public boolean isColumnTruncatable( int column ) { ColumnDefinition<RowType, ?> colDef = getColumnDefinition( column ); if ( colDef == null ) { return true; } return colDef.getColumnProperty( TruncationProperty.TYPE ).isColumnTruncatable(); } /** * @return true if cross page selection is enabled */ public boolean isCrossPageSelectionEnabled() { return isCrossPageSelectionEnabled; } @Override public boolean isFooterColumnTruncatable( int column ) { ColumnDefinition<RowType, ?> colDef = getColumnDefinition( column ); if ( colDef == null ) { return true; } return colDef.getColumnProperty( TruncationProperty.TYPE ).isFooterTruncatable(); } /** * @return true if the footer table is automatically generated */ public boolean isFooterGenerated() { return isFooterGenerated; } @Override public boolean isHeaderColumnTruncatable( int column ) { ColumnDefinition<RowType, ?> colDef = getColumnDefinition( column ); if ( colDef == null ) { return true; } return colDef.getColumnProperty( TruncationProperty.TYPE ).isHeaderTruncatable(); } /** * @return true if the header table is automatically generated */ public boolean isHeaderGenerated() { return isHeaderGenerated; } /** * @return true if a page load is pending */ public boolean isPageLoading() { return isPageLoading; } /** * Reload the current page. */ public void reloadPage() { if ( currentPage >= 0 ) { gotoPage( currentPage, true ); } else { gotoPage( 0, true ); } } /** * Set the {@link FixedWidthGridBulkRenderer} used to render the data table. * * @param bulkRenderer the table renderer */ public void setBulkRenderer( FixedWidthGridBulkRenderer<RowType> bulkRenderer ) { this.bulkRenderer = bulkRenderer; } /** * Enable or disable cross page selection. When enabled, row value selections * are maintained across page loads. Selections are remembered by type (not by * row index), so row values can move around and still maintain their * selection. * * @param enabled true to enable, false to disable */ public void setCrossPageSelectionEnabled( boolean enabled ) { if ( isCrossPageSelectionEnabled != enabled ) { this.isCrossPageSelectionEnabled = enabled; // Reselected only the rows on this page if ( !enabled ) { selectedRowValues = new HashSet<RowType>(); Set<Integer> selectedRows = getDataTable().getSelectedRows(); for ( Integer selectedRow : selectedRows ) { selectedRowValues.add( getRowValue( selectedRow ) ); } } } } /** * Set the {@link Widget} that will be displayed in place of the data table * when the data table has no data to display. * * @param emptyTableWidget the widget to display when the data table is empty */ public void setEmptyTableWidget( Widget emptyTableWidget ) { emptyTableWidgetWrapper.setWidget( emptyTableWidget ); } /** * Set whether or not the footer table should be automatically generated. * * @param isGenerated true to enable, false to disable */ public void setFooterGenerated( boolean isGenerated ) { this.isFooterGenerated = isGenerated; if ( isGenerated ) { refreshFooterTable(); } } /** * Set whether or not the header table should be automatically generated. * * @param isGenerated true to enable, false to disable */ public void setHeaderGenerated( boolean isGenerated ) { this.isHeaderGenerated = isGenerated; if ( isGenerated ) { refreshHeaderTable(); } } /** * Set the number of rows per page. * <p/> * By default, the page size is zero, which indicates that all rows should be * shown on the page. * * @param pageSize the number of rows per page */ public void setPageSize( int pageSize ) { pageSize = Math.max( 0, pageSize ); this.pageSize = pageSize; int pageCount = getPageCount(); if ( pageCount != oldPageCount ) { fireEvent( new PageCountChangeEvent( oldPageCount, pageCount ) ); oldPageCount = pageCount; } // Reset the page if ( currentPage >= 0 ) { gotoPage( currentPage, true ); } } /** * Associate a row in the table with a value. * * @param row the row index * @param value the value to associate */ public void setRowValue( int row, RowType value ) { // Make sure the list can fit the row for ( int i = rowValues.size(); i <= row; i++ ) { rowValues.add( null ); } // Set the row value rowValues.set( row, value ); // Render the new row value refreshRow( row ); } /** * Set the {@link TableDefinition} used to define the columns. * * @param tableDefinition the new table definition. */ public void setTableDefinition( TableDefinition<RowType> tableDefinition ) { assert tableDefinition != null : "tableDefinition cannot be null"; this.tableDefinition = tableDefinition; } /** * Invoke the cell editor on a cell, if one is set. If a cell editor is not * specified, this method has no effect. */ protected void editCell( int row, int column ) { // Get the cell editor final ColumnDefinition colDef = getColumnDefinition( column ); if ( colDef == null ) { return; } CellEditor cellEditor = colDef.getCellEditor(); if ( cellEditor == null ) { return; } // Forward the request to the cell editor final RowType rowValue = getRowValue( row ); CellEditInfo editInfo = new CellEditInfo( getDataTable(), row, column ); cellEditor.editCell( editInfo, colDef.getCellValue( rowValue ), new CellEditor.Callback() { public void onCancel( CellEditInfo cellEditInfo ) { } public void onComplete( CellEditInfo cellEditInfo, Object cellValue ) { colDef.setCellValue( rowValue, cellValue ); if ( tableModel instanceof MutableTableModel ) { int row = getAbsoluteFirstRowIndex() + cellEditInfo.getRowIndex(); ((MutableTableModel<RowType>) tableModel).setRowValue( row, rowValue ); } else { refreshRow( cellEditInfo.getRowIndex() ); } } } ); } /** * Get the {@link ColumnDefinition} currently associated with a column. * * @param colIndex the index of the column * * @return the {@link ColumnDefinition} associated with the column, or null */ protected ColumnDefinition<RowType, ?> getColumnDefinition( int colIndex ) { if ( colIndex < visibleColumns.size() ) { return visibleColumns.get( colIndex ); } return null; } /** * @return the index of the first visible row * * @deprecated use {@link #getAbsoluteFirstRowIndex()} instead */ @Deprecated protected int getFirstRow() { return getAbsoluteFirstRowIndex(); } /** * @return the index of the last visible row * * @deprecated use {@link #getAbsoluteLastRowIndex()} instead */ @Deprecated protected int getLastRow() { return getAbsoluteLastRowIndex(); } /** * Get the list of row values associated with the table. * * @return the list of row value */ protected List<RowType> getRowValues() { return rowValues; } /** * @return the header widget used to select all rows */ protected Widget getSelectAllWidget() { if ( selectAllWidget == null ) { final CheckBox box = new CheckBox(); selectAllWidget = box; box.addClickHandler( new ClickHandler() { public void onClick( ClickEvent event ) { if ( box.getValue() ) { getDataTable().selectAllRows(); } else { getDataTable().deselectAllRows(); } } } ); } return selectAllWidget; } /** * @return the list of current visible column definitions */ protected List<ColumnDefinition<RowType, ?>> getVisibleColumnDefinitions() { return visibleColumns; } /** * Insert a row into the table relative to the total number of rows. * * @param beforeRow the row index */ protected void insertAbsoluteRow( int beforeRow ) { // Physically insert the row int lastRow = getAbsoluteLastRowIndex() + 1; if ( beforeRow <= lastRow ) { int firstRow = getAbsoluteFirstRowIndex(); if ( beforeRow >= firstRow ) { // Insert row in the middle of the page getDataTable().insertRow( beforeRow - firstRow ); } else { // Insert zero row because row is before this page getDataTable().insertRow( 0 ); } if ( getDataTable().getRowCount() > pageSize ) { getDataTable().removeRow( pageSize ); } } } /** * Called when the data table has finished rendering. */ protected void onDataTableRendered() { // Refresh the headers if needed if ( headersObsolete ) { refreshHeaderTable(); refreshFooterTable(); headersObsolete = false; } // Select rows FixedWidthGrid dataTable = getDataTable(); int rowCount = dataTable.getRowCount(); for ( int i = 0; i < rowCount; i++ ) { if ( selectedRowValues.contains( getRowValue( i ) ) ) { dataTable.selectRow( i, false ); } } // Update the UI of the table dataTable.clearIdealWidths(); redraw(); isPageLoading = false; fireEvent( new PageLoadEvent( currentPage ) ); } /** * Update the footer table based on the new {@link ColumnDefinition}. */ protected void refreshFooterTable() { if ( !isFooterGenerated ) { return; } // Generate the list of lists of ColumnHeaderInfo. List<List<ColumnHeaderInfo>> allInfos = new ArrayList<List<ColumnHeaderInfo>>(); int columnCount = visibleColumns.size(); int footerCounts[] = new int[columnCount]; int maxFooterCount = 0; for ( int col = 0; col < columnCount; col++ ) { // Get the header property. ColumnDefinition<RowType, ?> colDef = visibleColumns.get( col ); FooterProperty prop = colDef.getColumnProperty( FooterProperty.TYPE ); int footerCount = prop.getFooterCount(); footerCounts[col] = footerCount; maxFooterCount = Math.max( maxFooterCount, footerCount ); // Add each ColumnHeaderInfo List<ColumnHeaderInfo> infos = new ArrayList<ColumnHeaderInfo>(); ColumnHeaderInfo prev = null; for ( int row = 0; row < footerCount; row++ ) { Object footer = prop.getFooter( row, col ); if ( prev != null && prev.header.equals( footer ) ) { prev.incrementRowSpan(); } else { prev = new ColumnHeaderInfo( footer ); infos.add( prev ); } } allInfos.add( infos ); } // Return early if there is no footer if ( maxFooterCount == 0 ) { return; } // Fill in missing rows for ( int col = 0; col < columnCount; col++ ) { int footerCount = footerCounts[col]; if ( footerCount < maxFooterCount ) { allInfos.get( col ).add( new ColumnHeaderInfo( null, maxFooterCount - footerCount ) ); } } // Ensure that we have a footer table if ( getFooterTable() == null ) { setFooterTable( new FixedWidthFlexTable() ); } // Refresh the table refreshHeaderTable( getFooterTable(), allInfos, false ); } /** * Update the header table based on the new {@link ColumnDefinition}. */ protected void refreshHeaderTable() { if ( !isHeaderGenerated ) { return; } // Generate the list of lists of ColumnHeaderInfo. List<List<ColumnHeaderInfo>> allInfos = new ArrayList<List<ColumnHeaderInfo>>(); int columnCount = visibleColumns.size(); int headerCounts[] = new int[columnCount]; int maxHeaderCount = 0; for ( int col = 0; col < columnCount; col++ ) { // Get the header property. ColumnDefinition<RowType, ?> colDef = visibleColumns.get( col ); HeaderProperty prop = colDef.getColumnProperty( HeaderProperty.TYPE ); int headerCount = prop.getHeaderCount(); headerCounts[col] = headerCount; maxHeaderCount = Math.max( maxHeaderCount, headerCount ); // Add each ColumnHeaderInfo List<ColumnHeaderInfo> infos = new ArrayList<ColumnHeaderInfo>(); ColumnHeaderInfo prev = null; for ( int row = 0; row < headerCount; row++ ) { Object header = prop.getHeader( row, col ); if ( prev != null && prev.header.equals( header ) ) { prev.incrementRowSpan(); } else { prev = new ColumnHeaderInfo( header ); infos.add( 0, prev ); } } allInfos.add( infos ); } // Return early if there is no header if ( maxHeaderCount == 0 ) { return; } // Fill in missing rows for ( int col = 0; col < columnCount; col++ ) { int headerCount = headerCounts[col]; if ( headerCount < maxHeaderCount ) { allInfos.get( col ).add( 0, new ColumnHeaderInfo( null, maxHeaderCount - headerCount ) ); } } // Refresh the table refreshHeaderTable( getHeaderTable(), allInfos, true ); } /** * Refresh the list of the currently visible column definitions based on the * {@link TableDefinition}. */ protected void refreshVisibleColumnDefinitions() { List<ColumnDefinition<RowType, ?>> colDefs = new ArrayList<ColumnDefinition<RowType, ?>>( tableDefinition.getVisibleColumnDefinitions() ); if ( !colDefs.equals( visibleColumns ) ) { visibleColumns = colDefs; headersObsolete = true; } else { // Check if any of the headers are dynamic for ( ColumnDefinition<RowType, ?> colDef : colDefs ) { if ( colDef.getColumnProperty( HeaderProperty.TYPE ).isDynamic() || colDef.getColumnProperty( FooterProperty.TYPE ).isDynamic() ) { headersObsolete = true; return; } } } } /** * Remove a row from the table relative to the total number of rows. * * @param row the row index */ protected void removeAbsoluteRow( int row ) { // Physically remove the row if it is in the middle of the data table int firstRow = getAbsoluteFirstRowIndex(); int lastRow = getAbsoluteLastRowIndex(); if ( row <= lastRow && row >= firstRow ) { FixedWidthGrid dataTable = getDataTable(); int relativeRow = row - firstRow; if ( relativeRow < dataTable.getRowCount() ) { dataTable.removeRow( relativeRow ); } } } /** * Set a block of data. This method is used when responding to data requests. * <p/> * This method takes an iterator of iterators, where each iterator represents * one row of data starting with the first row. * * @param firstRow the row index that the rows iterator starts with * @param rows the values associated with each row */ protected void setData( int firstRow, Iterator<RowType> rows ) { getDataTable().deselectAllRows(); rowValues = new ArrayList<RowType>(); if ( rows != null && rows.hasNext() ) { setEmptyTableWidgetVisible( false ); // Get an iterator over the visible rows int firstVisibleRow = getAbsoluteFirstRowIndex(); int lastVisibleRow = getAbsoluteLastRowIndex(); Iterator<RowType> visibleIter = new VisibleRowsIterator( rows, firstRow, firstVisibleRow, lastVisibleRow ); // Set the row values while ( visibleIter.hasNext() ) { rowValues.add( visibleIter.next() ); } // Copy the visible column definitions refreshVisibleColumnDefinitions(); // Render using the bulk renderer if ( bulkRenderer != null ) { bulkRenderer.renderRows( rowValues.iterator(), tableRendererCallback ); return; } // Get rid of unneeded rows and columns int rowCount = rowValues.size(); int colCount = visibleColumns.size(); getDataTable().resize( rowCount, colCount ); // Render the rows tableDefinition.renderRows( 0, rowValues.iterator(), rowView ); } else { setEmptyTableWidgetVisible( true ); } // Fire page loaded event onDataTableRendered(); } /** * Set whether or not the empty table widget is visible. * * @param visible true to show the empty table widget */ protected void setEmptyTableWidgetVisible( boolean visible ) { emptyTableWidgetWrapper.setVisible( visible ); if ( visible ) { getDataWrapper().getStyle().setProperty( "display", "none" ); } else { getDataWrapper().getStyle().setProperty( "display", "" ); } } /** * Update the header or footer tables based on the new * {@link ColumnDefinition}. * * @param table the header or footer table * @param allInfos the header info * @param isHeader false if refreshing the footer table */ private void refreshHeaderTable( FixedWidthFlexTable table, List<List<ColumnHeaderInfo>> allInfos, boolean isHeader ) { // Return if we have no column definitions. if ( visibleColumns == null ) { return; } // Reset the header table. int rowCount = table.getRowCount(); for ( int i = 0; i < rowCount; i++ ) { table.removeRow( 0 ); } // Generate the header table int columnCount = allInfos.size(); FlexCellFormatter formatter = table.getFlexCellFormatter(); List<ColumnHeaderInfo> prevInfos = null; for ( int col = 0; col < columnCount; col++ ) { List<ColumnHeaderInfo> infos = allInfos.get( col ); int row = 0; for ( ColumnHeaderInfo info : infos ) { // Get the actual row and cell index int rowSpan = info.getRowSpan(); int cell = 0; if ( table.getRowCount() > row ) { cell = table.getCellCount( row ); } // Compare to the cell in the previous column if ( prevInfos != null ) { boolean headerAdded = false; int prevRow = 0; for ( ColumnHeaderInfo prevInfo : prevInfos ) { // Increase the colSpan of the previous cell if ( prevRow == row && info.equals( prevInfo ) ) { int colSpan = formatter.getColSpan( row, cell - 1 ); formatter.setColSpan( row, cell - 1, colSpan + 1 ); headerAdded = true; break; } prevRow += prevInfo.getRowSpan(); } if ( headerAdded ) { row += rowSpan; continue; } } // Set the new header Object header = info.getHeader(); if ( header instanceof Widget ) { table.setWidget( row, cell, (Widget) header ); } else { table.setHTML( row, cell, header.toString() ); } // Update the rowSpan if ( rowSpan > 1 ) { formatter.setRowSpan( row, cell, rowSpan ); } // Increment the row row += rowSpan; } // Increment the previous info prevInfos = infos; } // Insert the checkbox column SelectionPolicy selectionPolicy = getDataTable().getSelectionPolicy(); if ( selectionPolicy.hasInputColumn() ) { // Get the select all box Widget box = null; if ( isHeader && getDataTable().getSelectionPolicy() == SelectionPolicy.CHECKBOX ) { box = getSelectAllWidget(); } // Add the offset column table.insertCell( 0, 0 ); if ( box != null ) { table.setWidget( 0, 0, box ); } else { table.setHTML( 0, 0, HTMLConstants.NBSP ); } formatter.setRowSpan( 0, 0, table.getRowCount() ); formatter.setHorizontalAlignment( 0, 0, HasHorizontalAlignment.ALIGN_CENTER ); table.setColumnWidth( 0, getDataTable().getInputColumnWidth() ); } } /** * Refresh a single row in the table. * * @param rowIndex the index of the row */ private void refreshRow( int rowIndex ) { final RowType rowValue = getRowValue( rowIndex ); Iterator<RowType> singleIterator = new Iterator<RowType>() { private boolean nextCalled = false; public boolean hasNext() { return !nextCalled; } public RowType next() { if ( !hasNext() ) { throw new NoSuchElementException(); } nextCalled = true; return rowValue; } public void remove() { throw new UnsupportedOperationException(); } }; tableDefinition.renderRows( rowIndex, singleIterator, rowView ); } } |
Commits for litesoft/trunk/Java/GWT/Client/src/com/google/gwt/gen2/table/client/PagingScrollTable.java
Revision | Author | Commited | Message |
---|---|---|---|
948 Diff | GeorgeS | Sat 07 Jun, 2014 23:42:39 +0000 | Jusefuls Formatter Updated to New Code Format |
942 Diff | GeorgeS | Mon 02 Jun, 2014 23:41:46 +0000 | Extracting commonfoundation |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
760 Diff | GeorgeS | Wed 11 Jul, 2012 05:10:55 +0000 | |
712 Diff | GeorgeS | Sat 09 Jun, 2012 22:46:04 +0000 | Move PAV stuff into LiteSoft |
613 Diff | GeorgeS | Thu 15 Mar, 2012 13:38:15 +0000 | Table Fix |
475 Diff | GeorgeS | Sat 03 Sep, 2011 13:54:51 +0000 | |
282 | GeorgeS | Fri 17 Jun, 2011 13:54:39 +0000 |