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 |
/* * 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.gen2.table.client.FixedWidthTableImpl.*; import com.google.gwt.user.client.*; import com.google.gwt.user.client.ui.*; import java.util.*; /** * A variation of the {@link Grid} * that resizes columns using a fixed table width. */ public class FixedWidthGrid extends SortableGrid { /** * This class contains methods used to format a table's cells. */ public class FixedWidthGridCellFormatter extends SelectionGridCellFormatter { @Override public void setWidth( int row, int column, String width ) { throw new UnsupportedOperationException( "setWidth is not supported. " + "Use FixedWidthGrid.setColumnWidth(int, int) instead." ); } @Override protected Element getRawElement( int row, int column ) { return super.getRawElement( row + 1, column ); } } /** * This class contains methods used to format a table's columns. */ public class FixedWidthGridColumnFormatter extends ColumnFormatter { @Override public void setWidth( int column, String width ) { throw new UnsupportedOperationException( "setWidth is not supported. " + "Use FixedWidthdGrid.setColumnWidth(int, int) instead." ); } } /** * This class contains methods used to format a table's rows. */ public class FixedWidthGridRowFormatter extends SelectionGridRowFormatter { @Override protected Element getRawElement( int row ) { return super.getRawElement( row + 1 ); } } /** * The default width of a column in pixels. */ public static final int DEFAULT_COLUMN_WIDTH = 80; /** * The minimum width of any column. */ public static final int MIN_COLUMN_WIDTH = 1; /** * A mapping of column indexes to their widths in pixels. */ private Map<Integer, Integer> colWidths = new HashMap<Integer, Integer>(); /** * The hidden, ghost row used for sizing the columns. */ private Element ghostRow = null; /** * The ideal widths of all columns (that are available). */ private int[] idealWidths; /** * Info used to calculate ideal column width. */ private IdealColumnWidthInfo idealColumnWidthInfo; /** * Constructor. */ public FixedWidthGrid() { super(); setClearText( HTMLConstants.NBSP ); // Setup the table element Element tableElem = getElement(); DOM.setStyleAttribute( tableElem, "tableLayout", "fixed" ); DOM.setStyleAttribute( tableElem, "width", "0px" ); // Replace the formatters setRowFormatter( new FixedWidthGridRowFormatter() ); setCellFormatter( new FixedWidthGridCellFormatter() ); setColumnFormatter( new FixedWidthGridColumnFormatter() ); // Create the ghost row for sizing ghostRow = FixedWidthTableImpl.get().createGhostRow(); DOM.insertChild( getBodyElement(), ghostRow, 0 ); // Sink highlight and selection events sinkEvents( Event.ONMOUSEOVER | Event.ONMOUSEDOWN | Event.ONCLICK ); } /** * Constructs a {@link FixedWidthGrid} with the requested size. * * @param rows the number of rows * @param columns the number of columns * * @throws IndexOutOfBoundsException */ public FixedWidthGrid( int rows, int columns ) { this(); resize( rows, columns ); } @Override public void clear() { super.clear(); clearIdealWidths(); } /** * Return the column width for a given column index. If a width has not been * assigned, the default width is returned. * * @param column the column index * * @return the column width in pixels */ public int getColumnWidth( int column ) { Integer colWidth = colWidths.get( new Integer( column ) ); if ( colWidth == null ) { return DEFAULT_COLUMN_WIDTH; } else { return colWidth.intValue(); } } /** * <p> * Calculate the ideal width required to tightly wrap the specified column. If * the ideal column width cannot be calculated (eg. if the table is not * attached), -1 is returned. * </p> * <p> * Note that this method requires an expensive operation whenever the content * of the table is changed, so you should only call it after you've completely * modified the contents of your table. * </p> * * @return the ideal column width, or -1 if it is not applicable */ public int getIdealColumnWidth( int column ) { maybeRecalculateIdealColumnWidths(); if ( idealWidths.length > column ) { return idealWidths[column]; } return -1; } @Override public boolean remove( Widget widget ) { if ( super.remove( widget ) ) { clearIdealWidths(); return true; } return false; } @Override public void removeRow( int row ) { super.removeRow( row ); clearIdealWidths(); } @Override public void resizeColumns( int columns ) { super.resizeColumns( columns ); updateGhostRow(); clearIdealWidths(); } @Override public void resizeRows( int rows ) { super.resizeRows( rows ); clearIdealWidths(); } @Override public void setCellPadding( int padding ) { super.setCellPadding( padding ); // Reset the width of all columns for ( Map.Entry<Integer, Integer> entry : colWidths.entrySet() ) { setColumnWidth( entry.getKey(), entry.getValue() ); } if ( getSelectionPolicy().hasInputColumn() ) { setColumnWidthImpl( -1, getInputColumnWidth() ); } } @Override public void setCellSpacing( int spacing ) { super.setCellSpacing( spacing ); // Reset the width of all columns for ( Map.Entry<Integer, Integer> entry : colWidths.entrySet() ) { setColumnWidth( entry.getKey(), entry.getValue() ); } if ( getSelectionPolicy().hasInputColumn() ) { setColumnWidthImpl( -1, getInputColumnWidth() ); } } /** * Set the width of a column. * * @param column the index of the column * @param width the width in pixels * * @throws IndexOutOfBoundsException */ public void setColumnWidth( int column, int width ) { // Ensure that the indices are not negative. if ( column < 0 ) { throw new IndexOutOfBoundsException( "Cannot access a column with a negative index: " + column ); } // Add the width to the map width = Math.max( MIN_COLUMN_WIDTH, width ); colWidths.put( new Integer( column ), new Integer( width ) ); // Update the cell width if possible if ( column >= numColumns ) { return; } // Set the actual column width setColumnWidthImpl( column, width ); } @Override public void setHTML( int row, int column, String html ) { super.setHTML( row, column, html ); clearIdealWidths(); } @Override public void setSelectionPolicy( SelectionPolicy selectionPolicy ) { // Update the input column in the ghost row if ( selectionPolicy.hasInputColumn() && !getSelectionPolicy().hasInputColumn() ) { // Add ghost input column Element tr = getGhostRow(); Element td = FixedWidthTableImpl.get().createGhostCell( null ); tr.insertBefore( td, tr.getFirstChildElement() ); super.setSelectionPolicy( selectionPolicy ); setColumnWidthImpl( -1, getInputColumnWidth() ); } else if ( !selectionPolicy.hasInputColumn() && getSelectionPolicy().hasInputColumn() ) { // Remove ghost input column Element tr = getGhostRow(); tr.removeChild( tr.getFirstChildElement() ); super.setSelectionPolicy( selectionPolicy ); } else { super.setSelectionPolicy( selectionPolicy ); } } @Override public void setText( int row, int column, String text ) { super.setText( row, column, text ); clearIdealWidths(); } @Override public void setWidget( int row, int column, Widget widget ) { super.setWidget( row, column, widget ); clearIdealWidths(); } @Override protected int getDOMCellCount( int row ) { return super.getDOMCellCount( row + 1 ); } @Override protected int getDOMRowCount() { return super.getDOMRowCount() - 1; } /** * Explicitly gets the {@link FixedWidthGridCellFormatter}. The results of * {@link com.google.gwt.user.client.ui.HTMLTable#getCellFormatter()} may also * be downcast to a {@link FixedWidthGridCellFormatter}. * * @return the {@link FixedWidthGrid}'s cell formatter */ protected FixedWidthGridCellFormatter getFixedWidthGridCellFormatter() { return (FixedWidthGridCellFormatter) getCellFormatter(); } /** * Explicitly gets the {@link FixedWidthGridRowFormatter}. The results of * {@link com.google.gwt.user.client.ui.HTMLTable#getCellFormatter()} may also * be downcast to a {@link FixedWidthGridRowFormatter}. * * @return the {@link FixedWidthGrid}'s cell formatter */ protected FixedWidthGridRowFormatter getFixedWidthGridRowFormatter() { return (FixedWidthGridRowFormatter) getRowFormatter(); } /** * @return the number of columns in the ghost row */ protected int getGhostColumnCount() { return super.getDOMCellCount( 0 ); } /** * @return the ghost row element */ protected Element getGhostRow() { return ghostRow; } /** * Get the width of the input column used in the current * {@link SelectionGrid.SelectionPolicy}. * * @return the width of the input element */ protected int getInputColumnWidth() { return 30; } @Override protected int getRowIndex( Element rowElem ) { int rowIndex = super.getRowIndex( rowElem ); if ( rowIndex < 0 ) { return rowIndex; } return rowIndex - 1; } @Override protected boolean internalClearCell( Element td, boolean clearInnerHTML ) { clearIdealWidths(); return super.internalClearCell( td, clearInnerHTML ); } @Override protected void onAttach() { super.onAttach(); clearIdealWidths(); } /** * Recalculate the ideal column widths of each column in the data table. */ protected void recalculateIdealColumnWidths() { // We need at least one cell to do any calculations int columnCount = getColumnCount(); if ( !isAttached() || getRowCount() == 0 || columnCount < 1 ) { idealWidths = new int[0]; return; } recalculateIdealColumnWidthsSetup(); recalculateIdealColumnWidthsImpl(); recalculateIdealColumnWidthsTeardown(); } /** * Sets the ghost row variable. This does not change the underlying structure * of the table. * * @param ghostRow the new ghost row */ protected void setGhostRow( Element ghostRow ) { this.ghostRow = ghostRow; } /** * Add or remove ghost cells when the table size changes. */ protected void updateGhostRow() { int numGhosts = getGhostColumnCount(); if ( numColumns > numGhosts ) { // Add ghosts as needed for ( int i = numGhosts; i < numColumns; i++ ) { Element td = FixedWidthTableImpl.get().createGhostCell( null ); DOM.appendChild( ghostRow, td ); setColumnWidth( i, getColumnWidth( i ) ); } } else if ( numColumns < numGhosts ) { int cellsToRemove = numGhosts - numColumns; for ( int i = 0; i < cellsToRemove; i++ ) { Element td = getGhostCellElement( numColumns ); DOM.removeChild( ghostRow, td ); } } } @Override 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] ); // Need to insert below the ghost row DOM.insertChild( bodyElem, trElems[i], 1 ); } } } /** * Clear the idealWidths field when the ideal widths change. */ void clearIdealWidths() { idealWidths = null; } /** * @return true if the ideal column widths have already been calculated */ boolean isIdealColumnWidthsCalculated() { return idealWidths != null; } /** * Recalculate the ideal column widths of each column in the data table. This * method assumes that the tableLayout has already been changed. */ void recalculateIdealColumnWidthsImpl() { idealWidths = FixedWidthTableImpl.get().recalculateIdealColumnWidths( idealColumnWidthInfo ); } /** * Setup to recalculate column widths. */ void recalculateIdealColumnWidthsSetup() { int offset = 0; if ( getSelectionPolicy().hasInputColumn() ) { offset++; } idealColumnWidthInfo = FixedWidthTableImpl.get().recalculateIdealColumnWidthsSetup( this, getColumnCount(), offset ); } /** * Tear down after recalculating column widths. */ void recalculateIdealColumnWidthsTeardown() { FixedWidthTableImpl.get().recalculateIdealColumnWidthsTeardown( idealColumnWidthInfo ); idealColumnWidthInfo = null; } /** * Returns a cell in the ghost row. * * @param column the cell's column * * @return the ghost cell */ private Element getGhostCellElement( int column ) { if ( getSelectionPolicy().hasInputColumn() ) { column++; } return FixedWidthTableImpl.get().getGhostCell( ghostRow, column ); } /** * Recalculate the ideal column widths of each column in the data table if * they have changed since the last calculation. */ private void maybeRecalculateIdealColumnWidths() { if ( idealWidths == null ) { recalculateIdealColumnWidths(); } } /** * Set the width of a column. * * @param column the index of the column * @param width the width in pixels */ private void setColumnWidthImpl( int column, int width ) { if ( getSelectionPolicy().hasInputColumn() ) { column++; } FixedWidthTableImpl.get().setColumnWidth( this, ghostRow, column, width ); } } |
Commits for litesoft/trunk/Java/GWT/Client/src/com/google/gwt/gen2/table/client/FixedWidthGrid.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 |
475 Diff | GeorgeS | Sat 03 Sep, 2011 13:54:51 +0000 | |
282 | GeorgeS | Fri 17 Jun, 2011 13:54:39 +0000 |