litesoft
@ 965
litesoft / trunk / GWT_Sandbox / FormEngine / src / com / temp / foundation / client / widget / table / ResizableCellTable.java
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 |
package com.temp.foundation.client.widget.table; import com.google.gwt.cell.client.*; import com.google.gwt.cell.client.Cell.*; import com.google.gwt.core.client.*; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.*; import com.google.gwt.dom.client.Style.*; import com.google.gwt.safehtml.client.*; import com.google.gwt.safehtml.shared.*; import com.google.gwt.user.cellview.client.*; import com.google.gwt.user.client.*; import com.google.gwt.user.client.ui.*; import com.google.gwt.view.client.*; import java.util.*; /** * 2011-07-18: this was TOTALLY CRIBBED from the DataTable.java * in the SQSAWSConsoleWebsiteContent package * <p/> * Cribbed javadoc * ===================== * This widget is derived from CellTable, and behaves very similar to it, * except that it adds the following two features. * <p/> * <p><dl> * <p/> * <dt>Interactive Resizable Colums</dt> * <dd> * - Users can drag the right edge of columns to interactively adjust * column widths. * </dd> * <p/> * <dt>Scollable Table Body</dt> * <dd> * - If the rows in the table body overflow the space available for the table, * then a scroll bar will be added to the table body only, so the body rows * can be scrolled independent of the table header and footer. * </dd> * a * </dl> * <p/> * <p/> * Although this widget is functionally equivalent to a CellTable, it can't * be used in exactly the same way as a CellTable can be used. The following * additional rules must be followed for this table to work correctly. * <p/> * <p><ol> * <li> * This class adds the {@link RequiresResize} interface, because it needs to adjust * body column widths and body height whenever the widget holding the table * changes size. This means that this widget needs to be added to a widget * that implements {@link ProvidesResize} in order to work correctly. * <li> * The implementation of this class requires that the widget be placed inside an * absolute-positioned element that defines the amount of space available for the * table. The recommended usage is to place this table in a {@link ScrollPanel} * whose immediate parent is some sort of {@link LayoutPanel}, such as * {@link DockLayoutPanel}. * <li> * For this table to function correctly, developers should not set the width of * columns directly using one of the setColumnWidth methods. Instead, they should * call setFixedColumnWidth to set the width of fixed-width columns, or * setRelativeColumnWidth to set the relative width of columns that should expand * to fill whatever space is not used by fixed-width columns. These functions * should generally only be used once to set the initial width of columns when * they are added to the table, or to show a column that was previously hidden by * calling clearColumnWidth, because otherwise user's column width adjustments * could be lost. * <li> * This widget is designed to work with fixed layout tables, and calls * setTableLayoutFixed(true) in its constructors. A good layout strategy is * to call setWidth("100%"), and to have at least one relative width column * that will expand to fill any space not used by fixed-width columns. NOTE: * this means that you *must* call setFixedColumnWidth or * setRelativeColumnWidth, or a column will not be visible. * </ol> * <p/> * <p/> * In addition to the features mentioned above, this table also overrides the * CellTable styles to look more like a standard Amazon DataTable widget. * * @param <T> Type of data object stored in the table. * ===================== * * @author Stephen Saville (saville@amazon.com) */ public class ResizableCellTable<T> extends CellTable<T> implements RequiresResize { public static final int DEFAULT_PAGE_SIZE = 15; public interface ResizableCellTableStyle extends Style { int cellBorderWidth(); int vCellPadding(); int hCellPadding(); String resizableCellTableResizeHandle(); String resizableCellTableHeader(); String scrollableTableBody(); } public interface ResizableCellTableResources extends Resources { @Override @Source("ResizableCellTable.css") public ResizableCellTableStyle cellTableStyle(); } public static final String RESIZE_HANDLE_ATTRIBUTE = "resize-col"; interface Templates extends SafeHtmlTemplates { @Template("<div class=\"{0}\">") SafeHtml resizableHeaderCellPrefix( String headerClassName ); @Template("<div " + RESIZE_HANDLE_ATTRIBUTE + "=\"{0}\" class=\"{1}\"> </div></div>") SafeHtml resizableHeaderCellSuffix( int col, String resizeHandleClassName ); } private static final Templates TEMPLATES = GWT.create( Templates.class ); public class ResizableCellTableOverlayHeaderCell<V> implements Cell<V> { private final Cell<V> delegate; public ResizableCellTableOverlayHeaderCell( Cell<V> delegate ) { this.delegate = delegate; } public boolean dependsOnSelection() { return delegate.dependsOnSelection(); } public Set<String> getConsumedEvents() { return delegate.getConsumedEvents(); } public boolean handlesSelection() { return delegate.handlesSelection(); } public boolean isEditing( com.google.gwt.cell.client.Cell.Context context, Element parent, V value ) { return delegate.isEditing( context, parent, value ); } public void onBrowserEvent( com.google.gwt.cell.client.Cell.Context context, Element parent, V value, NativeEvent event, ValueUpdater<V> valueUpdater ) { delegate.onBrowserEvent( context, parent, value, event, valueUpdater ); } public void render( com.google.gwt.cell.client.Cell.Context context, V value, SafeHtmlBuilder sb ) { sb.append( TEMPLATES.resizableHeaderCellPrefix( styles.resizableCellTableHeader() ) ); delegate.render( context, value, sb ); sb.append( TEMPLATES.resizableHeaderCellSuffix( context.getColumn(), styles.resizableCellTableResizeHandle() ) ); } public boolean resetFocus( com.google.gwt.cell.client.Cell.Context context, Element parent, V value ) { return delegate.resetFocus( context, parent, value ); } public void setValue( com.google.gwt.cell.client.Cell.Context context, Element parent, V value ) { delegate.setValue( context, parent, value ); } } public class ResizableCellTableOverlayHeader<V> extends Header<V> { private final Header<V> delegate; public ResizableCellTableOverlayHeader( Header<V> delegate ) { super( new ResizableCellTableOverlayHeaderCell<V>( delegate.getCell() ) ); this.delegate = delegate; } public Object getKey() { return delegate.getKey(); } public V getValue() { return delegate.getValue(); } public void onBrowserEvent( Context context, Element elem, NativeEvent event ) { delegate.onBrowserEvent( context, elem, event ); } public void setUpdater( ValueUpdater<V> updater ) { delegate.setUpdater( updater ); } } public <V> ResizableCellTableOverlayHeader<V> overlayHeader( Header<V> header ) { return new ResizableCellTableOverlayHeader<V>( header ); } public class ResizableCellTableSafeHtmlHeaderCell extends AbstractCell<SafeHtml> { @Override public void render( com.google.gwt.cell.client.Cell.Context context, SafeHtml value, SafeHtmlBuilder sb ) { sb.append( TEMPLATES.resizableHeaderCellPrefix( styles.resizableCellTableHeader() ) ); sb.append( value ); sb.append( TEMPLATES.resizableHeaderCellSuffix( context.getColumn(), styles.resizableCellTableResizeHandle() ) ); } } public class ResizableCellTableSafeHtmlHeader extends Header<SafeHtml> { private SafeHtml value; public ResizableCellTableSafeHtmlHeader( SafeHtml html ) { super( new ResizableCellTableSafeHtmlHeaderCell() ); this.value = html; } public ResizableCellTableSafeHtmlHeader( String text ) { this( SafeHtmlUtils.fromString( text ) ); } @Override public SafeHtml getValue() { return value; } } private final Map<Column<T, ?>, Double> relativeWidthColumns = new HashMap<Column<T, ?>, Double>(); private final ResizableCellTableResources resources; private final ResizableCellTableStyle styles; public ResizableCellTable() { this( DEFAULT_PAGE_SIZE ); } public ResizableCellTable( int pageSize, ResizableCellTableResources resources, ProvidesKey<T> keyProvider ) { super( pageSize, resources, keyProvider ); this.resources = resources; this.styles = resources.cellTableStyle(); sinkEvents( Event.ONMOUSEUP ); setTableLayoutFixed( true ); } public ResizableCellTable( int pageSize, ProvidesKey<T> keyProvider ) { this( pageSize, GWT.<ResizableCellTableResources>create( ResizableCellTableResources.class ), keyProvider ); } public ResizableCellTable( int pageSize ) { this( pageSize, new SimpleKeyProvider<T>() ); } public ResizableCellTable( ProvidesKey<T> keyProvider ) { this( DEFAULT_PAGE_SIZE, keyProvider ); } @Override public void addColumn( Column<T, ?> col ) { super.addColumn( col, new ResizableCellTableSafeHtmlHeader( SafeHtmlUtils.EMPTY_SAFE_HTML ) ); } @Override public void addColumn( Column<T, ?> col, SafeHtml headerHtml ) { super.addColumn( col, new ResizableCellTableSafeHtmlHeader( headerHtml ) ); } @Override public void addColumn( Column<T, ?> col, String headerString ) { super.addColumn( col, new ResizableCellTableSafeHtmlHeader( headerString ) ); } @Override public void addColumn( Column<T, ?> col, Header<?> header ) { super.addColumn( col, overlayHeader( header ) ); } @Override public void addColumn( Column<T, ?> col, Header<?> header, Header<?> footer ) { super.addColumn( col, overlayHeader( header ), footer ); } @Override public void addColumn( Column<T, ?> col, String headerString, String footerString ) { super.addColumn( col, new ResizableCellTableSafeHtmlHeader( headerString ), new TextHeader( footerString ) ); } @Override public void addColumn( Column<T, ?> col, SafeHtml headerHtml, SafeHtml footerHtml ) { super.addColumn( col, new ResizableCellTableSafeHtmlHeader( headerHtml ), new SafeHtmlHeader( footerHtml ) ); } @Override public void removeColumn( Column<T, ?> col ) { super.removeColumn( col ); clearRelativeColumnWidth( col ); } @Override public void removeColumn( int index ) { Column<T, ?> col = getColumn( index ); super.removeColumn( index ); clearRelativeColumnWidth( col ); } private double adjustRelativeColumnWidths( double totalWidthAdjustment ) { // add up relative column widths for normalization double total = totalWidthAdjustment; for ( Double value : relativeWidthColumns.values() ) { total += value; } // update the widths of existing relative width columns for ( Map.Entry<Column<T, ?>, Double> entry : relativeWidthColumns.entrySet() ) { setColumnWidth( entry.getKey(), entry.getValue() * 100.0 / total, Unit.PCT ); } return total; } /** * Set the relative width of a column that should expand to take up any * space not used by fixed-width columns. Relative widths are normalized * across relative width columns, so, for example, if you add two columns * with relatives widths of 1 and 2, then the first column will take up * 33% of the available space, and the second column will take up 66% of * the available space. If you were to add a third column with a relative * width of 1, then the columns would take up 25%, 50% and 25% of the * available space, respectively. * * @param column Column to set the relative width of. * @param relativeWidth Relative column width. */ public void setRelativeColumnWidth( Column<T, ?> column, double relativeWidth ) { // check if column already had a relative width Double existingRelativeWidth = relativeWidthColumns.get( column ); if ( existingRelativeWidth != null ) { if ( existingRelativeWidth == relativeWidth ) { // existing is same as new relative width // no change is required return; } else { // existing width is different // start by removing the existing width relativeWidthColumns.remove( column ); } } // reduce the widths of existing columns double totalWidth = adjustRelativeColumnWidths( relativeWidth ); // add new column and set its width relativeWidthColumns.put( column, relativeWidth ); setColumnWidth( column, relativeWidth * 100.0 / totalWidth, Unit.PCT ); } private void clearRelativeColumnWidth( Column<T, ?> column ) { Double relativeWidth = relativeWidthColumns.remove( column ); if ( relativeWidth != null ) { adjustRelativeColumnWidths( 0 ); } } /** * Set the width of a fixed-width column. * * @param column Column to set the width of. * @param fixedWidth Fixed width in the given units. * @param unit Fixed width unit, such as {@link Unit#PX}. Must <it>not</it> be {@link Unit#PCT}. */ public void setFixedColumnWidth( Column<T, ?> column, double fixedWidth, Unit unit ) { assert (unit != Unit.PCT); // set new fixed column width setColumnWidth( column, fixedWidth, unit ); // update relative width columnds if this column used to have a relative width clearRelativeColumnWidth( column ); } /** * Clear the width of a column. This will make the column disappear. * * @param column Column to clear the width of. */ @Override public void clearColumnWidth( Column<T, ?> column ) { // clear the width of this column super.clearColumnWidth( column ); // update relative width columns if this column used to have a relative width clearRelativeColumnWidth( column ); } private static final int MIN_RESIZED_WIDTH = 10; private boolean resizingColumns = false; private Column<T, ?> resizeLeftColumn, resizeRightColumn; private int resizeLeftX, resizeRightX; private static class TableElements { private Element table; private Element tHead; private Element tFoot; private Element colGroup; private List<Element> tBodies; private Element tableContainer; public TableElements( Element table ) { this.table = table; this.tBodies = new ArrayList<Element>(); NodeList<Node> tableChildren = table.getChildNodes(); int childCount = tableChildren.getLength(); for ( int i = 0; i < childCount; i++ ) { Node child = tableChildren.getItem( i ); if ( child == null ) { break; } if ( Element.is( child ) ) { Element childElement = Element.as( child ); String childTag = childElement.getTagName(); if ( "thead".equalsIgnoreCase( childTag ) ) { tHead = childElement; } else if ( "tfoot".equalsIgnoreCase( childTag ) ) { tFoot = childElement; } else if ( "colgroup".equalsIgnoreCase( childTag ) ) { colGroup = childElement; } else if ( "tbody".equalsIgnoreCase( childTag ) ) { tBodies.add( childElement ); } } } tableContainer = table.getParentElement(); while ( tableContainer != null ) { if ( "absolute".equals( tableContainer.getStyle().getPosition() ) ) { break; } else { Element parent = tableContainer.getParentElement(); if ( parent == null ) { break; } tableContainer = parent; } } } public Element getTable() { return table; } public Element getTHead() { return tHead; } public Element getTFoot() { return tFoot; } public Element getColGroup() { return colGroup; } public List<Element> getTBodies() { return tBodies; } public int getTableContainerWidth() { return tableContainer != null ? tableContainer.getClientWidth() : table.getClientWidth(); } public int getTableContainerHeight() { return tableContainer != null ? tableContainer.getClientHeight() : table.getClientHeight(); } } private void adjustBodyColumnWidths() { adjustBodyColumnWidths( new TableElements( getElement() ) ); } private void adjustBodyColumnWidths( TableElements tableElements ) { /*Element table = tableElements.getTable(); Element thead = tableElements.getTHead(); Element tfoot = tableElements.getTFoot(); Element colgroup = tableElements.getColGroup(); Element tbody; try { tbody = tableElements.getTBodies().get(0); } catch (IndexOutOfBoundsException e) { tbody = null; } if (colgroup != null && tbody != null) { Element firstBodyRow = tbody.getFirstChildElement(); if (firstBodyRow != null) { Element column = colgroup.getFirstChildElement(); Element cell = firstBodyRow.getFirstChildElement(); // calculate the difference between column width and cell width int cellWidthAdjustment; Element cellChild = cell.getFirstChildElement(); if (cellChild != null) { cellWidthAdjustment = cell.getOffsetWidth() - cellChild.getOffsetWidth(); } else { cellWidthAdjustment = resources.cellTableStyle().hCellPadding() * 2 + resources.cellTableStyle().cellBorderWidth(); } while (column != null && cell != null) { // calculate cell width based on column width int cellWidth = column.getClientWidth() - cellWidthAdjustment; // set cell's width style GWT.log("Setting width: " + cellWidth + " on cell: " + cell.getTagName()); cell.setAttribute("width", cellWidth + "px"); cell.getStyle().setWidth(cellWidth, Unit.PX); // subtract scroll bar width from last cell's width Element nextCell = cell.getNextSiblingElement(); if (nextCell == null) { // last cell in the row cellWidth += tbody.getClientWidth() - tbody.getOffsetWidth(); cell.getStyle().setWidth(cellWidth, Unit.PX); } // go to next column and cell column = column.getNextSiblingElement(); cell = nextCell; } } // find out how much space is available for the table body, in pixels int bodyHeight = tableElements.getTableContainerHeight(); if (thead != null) bodyHeight -= thead.getClientHeight(); if (tfoot != null) bodyHeight -= tfoot.getClientHeight(); tbody.getStyle().setWidth(table.getClientWidth(), Unit.PX); tbody.getStyle().setHeight(bodyHeight, Unit.PX); tbody.setClassName(resources.cellTableStyle().scrollableTableBody()); }*/ } @Override protected void replaceAllChildren( List<T> values, SafeHtml html ) { super.replaceAllChildren( values, html ); adjustBodyColumnWidths(); } @Override protected void replaceChildren( List<T> values, int start, SafeHtml html ) { super.replaceChildren( values, start, html ); adjustBodyColumnWidths(); } @Override public void onResize() { // find the width of all fixed-width columns, in pixels TableElements tableElements = new TableElements( getElement() ); Map<Column<T, ?>, Integer> fixedColumnWidths = new HashMap<Column<T, ?>, Integer>(); Column<T, ?> lastFixedWidthColumn = null; Element lastFixedWidthColumnElement = null; int totalWidthOfFixedColumns = 0; int relativeWidthColumnCount = 0; NodeList<Node> colGroupChildren = tableElements.getColGroup().getChildNodes(); int colGroupChildCount = colGroupChildren.getLength(); for ( int i = 0, col = 0; i < colGroupChildCount; i++ ) { Node child = colGroupChildren.getItem( i ); if ( Element.is( child ) ) { Element childElement = Element.as( child ); Column<T, ?> column = getColumn( col++ ); if ( !relativeWidthColumns.containsKey( column ) ) { int columnWidth = childElement.getClientWidth(); fixedColumnWidths.put( column, columnWidth ); totalWidthOfFixedColumns += columnWidth; lastFixedWidthColumn = column; lastFixedWidthColumnElement = childElement; } else { relativeWidthColumnCount++; } } } // adjust fixed-width columns if they don't fit after a resize, or if // all columns are fixed width, so they all resize proportionally. int availableWidth = tableElements.getTableContainerWidth(); if ( totalWidthOfFixedColumns + MIN_RESIZED_WIDTH * relativeWidthColumnCount > availableWidth || 0 == relativeWidthColumnCount && totalWidthOfFixedColumns != availableWidth ) { float adjustmentRatio = (float) availableWidth / totalWidthOfFixedColumns; for ( Map.Entry<Column<T, ?>, Integer> entry : fixedColumnWidths.entrySet() ) { int newColumnWidth = Math.round( entry.getValue() * adjustmentRatio ); setColumnWidth( entry.getKey(), newColumnWidth, Unit.PX ); availableWidth -= newColumnWidth; } if ( availableWidth != 0 && lastFixedWidthColumn != null ) { // adjust the last fixed width column so that all columns fit // perfectly into the available space, which prevents columns // from jumping around when you resize them later. setColumnWidth( lastFixedWidthColumn, lastFixedWidthColumnElement.getClientWidth() + availableWidth, Unit.PX ); } } // finally adjust child column widths redraw(); adjustBodyColumnWidths(); } private void applyColumnResize( int clientX ) { if ( clientX > resizeLeftX + MIN_RESIZED_WIDTH && clientX < resizeRightX - MIN_RESIZED_WIDTH ) { int leftWidth = clientX - resizeLeftX; setFixedColumnWidth( resizeLeftColumn, leftWidth, Unit.PX ); int rightWidth = resizeRightX - clientX; setFixedColumnWidth( resizeRightColumn, rightWidth, Unit.PX ); } redraw(); } @Override protected void onBrowserEvent2( Event event ) { if ( resizingColumns ) { applyColumnResize( event.getClientX() ); if ( "mouseup".equals( event.getType() ) ) { resizingColumns = false; adjustBodyColumnWidths(); } } else { EventTarget eventTarget = event.getEventTarget(); if ( !Element.is( eventTarget ) ) { return; } final Element target = Element.as( eventTarget ); if ( target.hasAttribute( RESIZE_HANDLE_ATTRIBUTE ) ) { // figure out which column this event is for int col = Integer.parseInt( target.getAttribute( RESIZE_HANDLE_ATTRIBUTE ) ); if ( col < getColumnCount() - 1 && "mousedown".equals( event.getType() ) ) { resizeLeftColumn = getColumn( col ); resizeRightColumn = getColumn( col + 1 ); Element leftTableHeader = target.getParentElement().getParentElement(); assert (leftTableHeader != null); Element rightTableHeader = leftTableHeader.getNextSiblingElement(); assert (rightTableHeader != null); int clientX = event.getClientX(); resizeLeftX = clientX - leftTableHeader.getOffsetWidth(); resizeRightX = clientX + rightTableHeader.getOffsetWidth(); resizingColumns = true; } } else { // defer to CellTable's event handler super.onBrowserEvent2( event ); } } } } |
Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/foundation/client/widget/table/ResizableCellTable.java
Revision | Author | Commited | Message |
---|---|---|---|
965 | GeorgeS | Fri 01 Aug, 2014 03:20:35 +0000 | ! |