litesoft
@ 947
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 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.GWT.client.widgets.datatables; import java.util.*; import org.litesoft.GWT.client.*; import org.litesoft.GWT.client.event.*; import org.litesoft.GWT.client.widgets.datatables.RowSelectionPolicy.*; import org.litesoft.GWT.client.widgets.nonpublic.*; import org.litesoft.core.*; import org.litesoft.core.util.*; import com.google.gwt.dom.client.*; import com.google.gwt.dom.client.Element; import com.google.gwt.event.dom.client.*; import com.google.gwt.event.logical.shared.*; import com.google.gwt.event.shared.*; import com.google.gwt.gen2.table.client.AbstractScrollTable.*; import com.google.gwt.gen2.table.client.*; import com.google.gwt.gen2.table.client.FlexTable.*; import com.google.gwt.gen2.table.client.SelectionGrid.*; import com.google.gwt.gen2.table.client.property.*; import com.google.gwt.gen2.table.event.client.*; import com.google.gwt.user.client.*; import com.google.gwt.user.client.ui.*; import com.google.gwt.user.client.ui.HasHorizontalAlignment.*; import com.google.gwt.widgetideas.client.*; public class Table<RowType> extends Composite implements HasSelectionHandlers<RowType> { private final FixedWidthFlexTable mHeaderTable = new FixedWidthFlexTable(); private final DataTable mDataTable = new DataTable(); private final TableImpl<RowType> mTable; private final TableClickCommand<RowType> mRowClickCommand; protected SelectAllCheckBox mSelectAllCheckBox = null; private RowSelectionPolicy mSelectionPolicy = RowSelectionPolicy.MULTI; // important: initialized to match PagingScrollingTable's default value public Table( ListTableModel<RowType> pTableModel, TableDefinitionPlus<RowType> pTableDefinition, TableClickCommand<RowType> pRowClickCommand ) { mRowClickCommand = pRowClickCommand; FlexCellFormatter headerCellFormatter = mHeaderTable.getFlexCellFormatter(); final List<ColumnDefinition<RowType, ?>> colDefs = pTableDefinition.getVisibleColumnDefinitions(); int colCount = colDefs.size(); mDataTable.resizeColumns( colCount ); for ( int col = 0; col < colCount; col++ ) { ColumnDefinition<RowType, ?> colDef = colDefs.get( col ); mHeaderTable.setHTML( 0, col, (String) colDef.getColumnProperty( HeaderProperty.TYPE ).getHeader( 0 ) ); Style style = headerCellFormatter.getElement( 0, col ).getStyle(); HorizontalAlignmentConstant alignment = colDef.getColumnProperty( HorizontalAlignmentProperty.TYPE ).getValue(); headerCellFormatter.setHorizontalAlignment( 0, col, alignment ); style.setPropertyPx( "paddingLeft", colDef.getColumnProperty( PaddingProperty.LEFT ).getValue() ); style.setPropertyPx( "paddingRight", colDef.getColumnProperty( PaddingProperty.RIGHT ).getValue() ); style.setPropertyPx( "paddingTop", colDef.getColumnProperty( PaddingProperty.TOP ).getValue() ); style.setPropertyPx( "paddingBottom", colDef.getColumnProperty( PaddingProperty.BOTTOM ).getValue() ); String colStyleName = colDef.getColumnProperty( ColStyleNameProperty.TYPE ).getValue(); if ( colStyleName != null ) { mHeaderTable.getColumnFormatter().addStyleName( col, colStyleName ); mDataTable.getColumnFormatter().addStyleName( col, colStyleName ); } } mTable = new TableImpl<RowType>( pTableModel, mDataTable, mHeaderTable, pTableDefinition ); mTable.setPageSize( 5 ); mTable.setResizePolicy( ResizePolicy.FILL_WIDTH ); mTable.setSortPolicy( SortPolicy.DISABLED ); setSelectionPolicy( RowSelectionPolicy.SINGLE ); if ( mRowClickCommand != null ) { setClickCommand(); } final TableHelper.GridHelper gridHelper = new TableHelper.GridHelper( mDataTable ) { @Override protected int getRowIndex( com.google.gwt.user.client.Element pRowElement ) { return mDataTable.getRowIndex( pRowElement ); } @Override protected int getCellIndex( com.google.gwt.user.client.Element pRowElement, com.google.gwt.user.client.Element pCellElement ) { return mDataTable.getCellIndex( pRowElement, pCellElement ); } }; mDataTable.addClickHandler( new TableHelper.GridClickHandler<RowType>( 0, gridHelper, colDefs ) { @Override public void onClick( ClickEvent pEvent ) { super.onClick( pEvent ); boolean checkBox = mSelectionPolicy == RowSelectionPolicy.CHECKBOX; boolean radio = mSelectionPolicy == RowSelectionPolicy.RADIO; if ( checkBox || radio ) { TableHelper.Cell cell = gridHelper.getCellForEvent( Event.as( pEvent.getNativeEvent() ) ); if ( cell != null ) { /* * ctrlKey = false for RADIO to workaround table bug * where deselection of old row does not update UI */ boolean ctrlKey = !radio; mDataTable.selectRow( cell.getRow(), ctrlKey, false ); } } } @Override protected RowType getRowValue( int pRow ) { return mTable.getRowValue( pRow ); } } ); initWidget( mTable ); } public TableModel<RowType> getModel() { return mTable.getTableModel(); } public ImmutableArrayList<RowType> getAllRows() { return new ImmutableArrayList<RowType>( mTable.getRowValues() ); } public void selectAll() { mTable.getDataTable().selectAllRows(); } public void deselectAll() { mTable.getDataTable().deselectAllRows(); } /** * @return true if pPossibleRowValue is in the table (and is now selected) */ public boolean selectRow( RowType pPossibleRowValue ) { return mTable.selectRow( pPossibleRowValue ); } public RowSelectionPolicy getSelectionPolicy() { return mSelectionPolicy; } public Set<RowType> getSelectedValues() { return mTable.getSelectedRowValues(); } public void setSelectionPolicy( RowSelectionPolicy pSelectionPolicy ) { if ( mSelectionPolicy != pSelectionPolicy ) { mSelectionPolicy = pSelectionPolicy; SelectionPolicy oldSelectionPolicy = mDataTable.getSelectionPolicy(); SelectionPolicy newSelectionPolicy = pSelectionPolicy.getSelectionPolicy(); boolean hasSelectionColumn = oldSelectionPolicy == SelectionPolicy.CHECKBOX || oldSelectionPolicy == SelectionPolicy.RADIO; boolean needsSelectionColumn = newSelectionPolicy == SelectionPolicy.CHECKBOX || newSelectionPolicy == SelectionPolicy.RADIO; if ( oldSelectionPolicy == newSelectionPolicy && !hasSelectionColumn ) { return; } mDataTable.setSelectionEnabled( pSelectionPolicy.isSelectionEnabled() ); mDataTable.setSelectionPolicy( (newSelectionPolicy != null) ? newSelectionPolicy : SelectionPolicy.ONE_ROW ); if ( hasSelectionColumn && !needsSelectionColumn ) { mHeaderTable.removeCell( 0, 0 ); Element colGroup = mHeaderTable.getElement().getFirstChildElement(); colGroup.removeChild( colGroup.getFirstChildElement() ); } else if ( needsSelectionColumn && !hasSelectionColumn ) { Element colGroup = mHeaderTable.getElement().getFirstChildElement(); colGroup.insertBefore( Document.get().createColElement(), colGroup.getFirstChildElement() ); mHeaderTable.insertCell( 0, 0 ); mHeaderTable.getCellFormatter().getElement( 0, 0 ).getStyle().setProperty( "textAlign", HasHorizontalAlignment.ALIGN_CENTER.getTextAlignString() ); mHeaderTable.setColumnWidth( 0, mDataTable.getInputColumnWidth() ); } if ( needsSelectionColumn ) { // can be called if hasSelectionColumn=true but the label has changed Widget colLabel = ((RowSelectionPolicy.Column) pSelectionPolicy).getColLabel(); /* * If no selection col label is specified, set a "select all" * checkbox in the header. */ if ( colLabel == null && pSelectionPolicy.getType() == Type.CheckBox ) { colLabel = mSelectAllCheckBox = new SelectAllCheckBox(); } mHeaderTable.setWidget( 0, 0, colLabel ); } } } /** * Note: currently only notifies w/ selected item w/ the lowest index. If we * need multiple items we can roll a custom event or create a method to get * selected items on this class. */ @Override public HandlerRegistration addSelectionHandler( SelectionHandler<RowType> pHandler ) { return new IncubatorHandlerRegistrationAdaptor2( addHandler( pHandler, SelectionEvent.getType() ), mDataTable.addRowSelectionHandler( new RowSelectionHandler() { @Override public void onRowSelection( RowSelectionEvent pEvent ) { Set<Integer> selectionSet = mDataTable.getSelectedRows(); RowType selection; if ( selectionSet.size() > 0 ) { int selectedIndex = selectionSet.iterator().next(); selection = mTable.getRowValue( selectedIndex ); } else { selection = null; } SelectionEvent.fire( Table.this, selection ); } } ) ); } public boolean isScrollVertically() { return mTable.isScrollVertically(); } /** * Must be set before loading data to ensure that fetched rows do not * increase viewport size. Default is true. * * @throws IllegalStateException if called after {@link #loadData()} */ public void setScrollVertically( boolean pScrollVertically ) { mTable.setScrollVertically( pScrollVertically ); } public TableRowCountCallback getRowCountCallback() { return mTable.getRowCountCallback(); } public void setRowCountCallback( TableRowCountCallback pRowCountCallback ) { mTable.setRowCountCallback( pRowCountCallback ); } public boolean isHeaderVisible() { return mHeaderTable.isVisible(); } public void setHeaderVisible( boolean pVisible ) { mHeaderTable.setVisible( pVisible ); } public Widget getEmptyTableWidget() { return mTable.getEmptyTableWidget(); } public void setEmptyTableWidget( Widget pEmptyTableWidget ) { mTable.setEmptyTableWidget( pEmptyTableWidget ); } public void loadData() { mTable.loadPage(); } @SuppressWarnings({"deprecation"}) private void setClickCommand() { mDataTable.addTableListener( new com.google.gwt.user.client.ui.TableListener() { @Override public void onCellClicked( com.google.gwt.user.client.ui.SourcesTableEvents pSender, int pRow, int pCol ) { mRowClickCommand.execute( ((ListTableModel<RowType>) mTable.getTableModel()).get( pRow ) ); } } ); } public void simulateUserClickHeader( int pCol, KeyboardKeyModifier pModifiers ) { pModifiers = KeyboardKeyModifier.deNull( pModifiers ); NativeEvent event = Document.get().createClickEvent( 0, 0, 0, 0, 0, pModifiers.isCtrl(), pModifiers.isAlt(), pModifiers.isShift(), pModifiers.isMeta() ); com.google.gwt.user.client.Element zElement = mHeaderTable.getCellFormatter().getElement( 0, pCol ); zElement.dispatchEvent( event ); } public void simulateUserClickDataGrid( int pRow, int pCol, KeyboardKeyModifier pModifiers ) { pModifiers = KeyboardKeyModifier.deNull( pModifiers ); NativeEvent event = Document.get().createClickEvent( 0, 0, 0, 0, 0, pModifiers.isCtrl(), pModifiers.isAlt(), pModifiers.isShift(), pModifiers.isMeta() ); com.google.gwt.user.client.Element zElement = mDataTable.getCellFormatter().getElement( pRow, pCol ); zElement.dispatchEvent( event ); } private static class TableImpl<RowType> extends PagingScrollTable<RowType> { private boolean mScrollVertically = true; // important: initialized to match PagingScrollingTable's default value private TableRowCountCallback mRowCountCallback = null; private TableRowElement mGridRow; private int mRowCount = 0; private int mMaxVisibleRows = 0; private boolean mFetchDataWaitingForSize = false; private int mViewportHeight = 0; private int mPrototypeRowHeight = 0; private boolean mLoadPageCalled = false; public TableImpl( TableModel<RowType> pTableModel, FixedWidthGrid pDataTable, FixedWidthFlexTable pHeaderTable, TableDefinitionPlus<RowType> pTableDefinition ) { super( pTableModel, pDataTable, pHeaderTable, pTableDefinition ); if ( BrowserFamily.IE.equals( UserAgent.getInstance().getFamily() ) ) { /* * Hack the header spacer so it does not prevent interaction w/ the column resize handles in IE (Issue 66). * * DOM structure of table for reference: * mainElem:div > absoluteElem:div > headerWrapper:div [> headerSpacer:div, IE only] * > headerTable:table * * http://code.google.com/p/google-web-toolkit-incubator/issues/detail?id=313 */ Element headerWrapper = pHeaderTable.getElement().getParentElement(); Element headerSpacer = headerWrapper.getFirstChildElement(); CommonElementHelper.hide( headerSpacer ); // Almost fixes, but 1px div still gets in the way. IE will not honor 0px. // CommonElementHelper.setOverflowHidden( headerSpacer ); } setCellPadding( 0 ); setCellSpacing( 0 ); setHeight( "200px" ); MockTable.CreateResult mockTableResult = MockTable.createFrom( this, pTableDefinition ); mGridRow = mockTableResult.mPrototypeRow; getElement().insertBefore( mockTableResult.mTable, getElement().getFirstChild() ); ResizableWidgetCollection.get().add( new ResizableWidgetAdaptor( this, getDataWrapper() ) { @Override public void onResize( int pViewportWidth, int pViewportHeight ) { mViewportHeight = pViewportHeight; updateMaxVisibleRows(); } } ); ResizableWidgetCollection.get().add( new TemporaryResizableWidgetAdaptor( this, mGridRow.<com.google.gwt.user.client.Element>cast() ) { @Override protected void onSizeInitialized( int pRowWidth, int pRowHeight ) { mPrototypeRowHeight = pRowHeight; updateMaxVisibleRows(); } } ); } public boolean isScrollVertically() { return mScrollVertically; } /** * Must be set before loading data to ensure that fetched rows do not * increase viewport size. Default is true. * * @throws IllegalStateException if called after {@link #loadData()} */ public void setScrollVertically( boolean pScrollVertically ) { if ( mLoadPageCalled ) { throw new IllegalStateException( "Table: must call set setScrollVertically() before loadData()" ); } mScrollVertically = pScrollVertically; } @Override public void setScrollPolicy( ScrollPolicy pScrollPolicy ) { throw new UnsupportedOperationException(); } public void loadPage() { mLoadPageCalled = true; if ( isSizeInitialized() ) { gotoPage( 0, true ); } else { mFetchDataWaitingForSize = true; } } public TableRowCountCallback getRowCountCallback() { return mRowCountCallback; } public void setRowCountCallback( TableRowCountCallback pRowCountCallback ) { mRowCountCallback = pRowCountCallback; } /** * @return true if pPossibleRowValue is in the table (and is now selected) */ public boolean selectRow( RowType pPossibleRowValue ) { if ( pPossibleRowValue != null ) { List<RowType> zRows = getRowValues(); for ( int i = 0; i < zRows.size(); i++ ) { if ( pPossibleRowValue.equals( zRows.get( i ) ) ) { getDataTable().selectRow( i, false ); // todo: Do we need to do some kind of page adjustment? return true; } } } return false; } /** * Override to gain access */ @Override protected List<RowType> getRowValues() { return super.getRowValues(); } /** * Override to gain access */ @Override protected void setEmptyTableWidgetVisible( boolean pVisible ) { super.setEmptyTableWidgetVisible( pVisible ); } /** * Called by superclass after {@link TableModel} calls callback w/ data. */ @Override protected void setData( int pFirstRow, Iterator<RowType> pRows ) { super.setData( pFirstRow, (mScrollVertically) ? pRows : new LimitingIteratorWrapper<RowType>( pRows, mMaxVisibleRows ) ); int rowCount = getTableModel().getRowCount(); if ( mRowCount != rowCount ) { mRowCount = rowCount; fireRowCountChanged(); } } @Override public void onResize( int pTableWidth, int pTableHeight ) { super.onResize( pTableWidth, pTableHeight ); updateMaxVisibleRows(); } private boolean isSizeInitialized() { return mViewportHeight > 0 && mPrototypeRowHeight > 0; } private void updateMaxVisibleRows() { if ( isSizeInitialized() ) { /* * Require a minimum of 1 row. Otherwise outer element * (AbstractScrollTable.absoluteElem) can get stuck at header * size and will not expand to show rows when more space becomes * available. */ int maxVisibleRows = Math.max( 1, mViewportHeight / mPrototypeRowHeight ); if ( mMaxVisibleRows != maxVisibleRows ) { mMaxVisibleRows = maxVisibleRows; setPageSize( mMaxVisibleRows ); if ( mFetchDataWaitingForSize ) { mFetchDataWaitingForSize = false; super.gotoFirstPage(); } else { fireRowCountChanged(); } } } } private void fireRowCountChanged() { if ( mRowCountCallback != null ) { int visibleRows = Math.min( mRowCount, mMaxVisibleRows ); mRowCountCallback.rowCountChanged( visibleRows, mRowCount, mMaxVisibleRows ); } } } private static class DataTable extends FixedWidthGrid { public HandlerRegistration addClickHandler( ClickHandler pHandler ) { return addDomHandler( pHandler, ClickEvent.getType() ); } /** * Override to gain access */ @Override protected int getInputColumnWidth() { return super.getInputColumnWidth(); } /** * Override to gain access */ @Override protected int getRowIndex( com.google.gwt.user.client.Element pRowElem ) { return super.getRowIndex( pRowElem ); } /** * Override to gain access */ @Override protected int getCellIndex( com.google.gwt.user.client.Element pRowElem, com.google.gwt.user.client.Element pCellElem ) { return super.getCellIndex( pRowElem, pCellElem ); } @Override public void onBrowserEvent( Event pEvent ) { if ( pEvent.getTypeInt() == Event.ONCLICK ) { // the current incubator HTMLTable does not call super() DomEvent.fireNativeEvent( pEvent, this, getElement() ); } super.onBrowserEvent( pEvent ); } } public class SelectAllCheckBox extends CheckBox { private HandlerRegistration mSelectionHandlerRegistration; public SelectAllCheckBox() { addValueChangeHandler( new ValueChangeHandler<Boolean>() { @Override public void onValueChange( ValueChangeEvent<Boolean> pEvent ) { if ( pEvent.getValue() ) { selectAll(); } else { deselectAll(); } } } ); update(); } public void update() { int selectionCount = getSelectedValues().size(); setValue( selectionCount > 0 && getSelectedValues().size() == getModel().getRowCount() ); } @Override protected void onLoad() { super.onLoad(); mSelectionHandlerRegistration = addSelectionHandler( new SelectionHandler<RowType>() { @Override public void onSelection( SelectionEvent<RowType> pEvent ) { update(); } } ); } @Override protected void onUnload() { mSelectionHandlerRegistration.removeHandler(); } } } |
Commits for litesoft/trunk/Java/GWT/Client/src/org/litesoft/GWT/client/widgets/datatables/Table.java
Revision | Author | Commited | Message |
---|---|---|---|
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
475 Diff | GeorgeS | Sat 03 Sep, 2011 13:54:51 +0000 | |
282 Diff | GeorgeS | Fri 17 Jun, 2011 13:54:39 +0000 | |
278 Diff | GeorgeS | Tue 14 Jun, 2011 20:21:50 +0000 | |
243 Diff | GeorgeS | Sun 05 Jun, 2011 20:29:12 +0000 | |
151 Diff | GeorgeS | Thu 17 Mar, 2011 04:16:22 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
23 Diff | GeorgeS | Wed 24 Feb, 2010 00:34:32 +0000 | |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |