Subversion Repository Public Repository

litesoft

Diff Revisions 947 vs 948 for /trunk/Java/GWT/Client/src/com/google/gwt/gen2/table/client/FixedWidthGrid.java

Diff revisions: vs.
  @@ -15,34 +15,30 @@
15 15 */
16 16 package com.google.gwt.gen2.table.client;
17 17
18 - import java.util.*;
19 -
20 18 import org.litesoft.commonfoundation.html.*;
21 19
22 20 import com.google.gwt.gen2.table.client.FixedWidthTableImpl.*;
23 21 import com.google.gwt.user.client.*;
24 22 import com.google.gwt.user.client.ui.*;
25 23
24 + import java.util.*;
25 +
26 26 /**
27 27 * A variation of the {@link Grid}
28 28 * that resizes columns using a fixed table width.
29 29 */
30 - public class FixedWidthGrid extends SortableGrid
31 - {
30 + public class FixedWidthGrid extends SortableGrid {
32 31 /**
33 32 * This class contains methods used to format a table's cells.
34 33 */
35 - public class FixedWidthGridCellFormatter extends SelectionGridCellFormatter
36 - {
34 + public class FixedWidthGridCellFormatter extends SelectionGridCellFormatter {
37 35 @Override
38 - public void setWidth( int row, int column, String width )
39 - {
36 + public void setWidth( int row, int column, String width ) {
40 37 throw new UnsupportedOperationException( "setWidth is not supported. " + "Use FixedWidthGrid.setColumnWidth(int, int) instead." );
41 38 }
42 39
43 40 @Override
44 - protected Element getRawElement( int row, int column )
45 - {
41 + protected Element getRawElement( int row, int column ) {
46 42 return super.getRawElement( row + 1, column );
47 43 }
48 44 }
  @@ -50,11 +46,9 @@
50 46 /**
51 47 * This class contains methods used to format a table's columns.
52 48 */
53 - public class FixedWidthGridColumnFormatter extends ColumnFormatter
54 - {
49 + public class FixedWidthGridColumnFormatter extends ColumnFormatter {
55 50 @Override
56 - public void setWidth( int column, String width )
57 - {
51 + public void setWidth( int column, String width ) {
58 52 throw new UnsupportedOperationException( "setWidth is not supported. " + "Use FixedWidthdGrid.setColumnWidth(int, int) instead." );
59 53 }
60 54 }
  @@ -62,11 +56,9 @@
62 56 /**
63 57 * This class contains methods used to format a table's rows.
64 58 */
65 - public class FixedWidthGridRowFormatter extends SelectionGridRowFormatter
66 - {
59 + public class FixedWidthGridRowFormatter extends SelectionGridRowFormatter {
67 60 @Override
68 - protected Element getRawElement( int row )
69 - {
61 + protected Element getRawElement( int row ) {
70 62 return super.getRawElement( row + 1 );
71 63 }
72 64 }
  @@ -104,8 +96,7 @@
104 96 /**
105 97 * Constructor.
106 98 */
107 - public FixedWidthGrid()
108 - {
99 + public FixedWidthGrid() {
109 100 super();
110 101 setClearText( HTMLConstants.NBSP );
111 102
  @@ -135,15 +126,13 @@
135 126 *
136 127 * @throws IndexOutOfBoundsException
137 128 */
138 - public FixedWidthGrid( int rows, int columns )
139 - {
129 + public FixedWidthGrid( int rows, int columns ) {
140 130 this();
141 131 resize( rows, columns );
142 132 }
143 133
144 134 @Override
145 - public void clear()
146 - {
135 + public void clear() {
147 136 super.clear();
148 137 clearIdealWidths();
149 138 }
  @@ -156,15 +145,11 @@
156 145 *
157 146 * @return the column width in pixels
158 147 */
159 - public int getColumnWidth( int column )
160 - {
148 + public int getColumnWidth( int column ) {
161 149 Integer colWidth = colWidths.get( new Integer( column ) );
162 - if ( colWidth == null )
163 - {
150 + if ( colWidth == null ) {
164 151 return DEFAULT_COLUMN_WIDTH;
165 - }
166 - else
167 - {
152 + } else {
168 153 return colWidth.intValue();
169 154 }
170 155 }
  @@ -183,21 +168,17 @@
183 168 *
184 169 * @return the ideal column width, or -1 if it is not applicable
185 170 */
186 - public int getIdealColumnWidth( int column )
187 - {
171 + public int getIdealColumnWidth( int column ) {
188 172 maybeRecalculateIdealColumnWidths();
189 - if ( idealWidths.length > column )
190 - {
173 + if ( idealWidths.length > column ) {
191 174 return idealWidths[column];
192 175 }
193 176 return -1;
194 177 }
195 178
196 179 @Override
197 - public boolean remove( Widget widget )
198 - {
199 - if ( super.remove( widget ) )
200 - {
180 + public boolean remove( Widget widget ) {
181 + if ( super.remove( widget ) ) {
201 182 clearIdealWidths();
202 183 return true;
203 184 }
  @@ -205,55 +186,46 @@
205 186 }
206 187
207 188 @Override
208 - public void removeRow( int row )
209 - {
189 + public void removeRow( int row ) {
210 190 super.removeRow( row );
211 191 clearIdealWidths();
212 192 }
213 193
214 194 @Override
215 - public void resizeColumns( int columns )
216 - {
195 + public void resizeColumns( int columns ) {
217 196 super.resizeColumns( columns );
218 197 updateGhostRow();
219 198 clearIdealWidths();
220 199 }
221 200
222 201 @Override
223 - public void resizeRows( int rows )
224 - {
202 + public void resizeRows( int rows ) {
225 203 super.resizeRows( rows );
226 204 clearIdealWidths();
227 205 }
228 206
229 207 @Override
230 - public void setCellPadding( int padding )
231 - {
208 + public void setCellPadding( int padding ) {
232 209 super.setCellPadding( padding );
233 210
234 211 // Reset the width of all columns
235 - for ( Map.Entry<Integer, Integer> entry : colWidths.entrySet() )
236 - {
212 + for ( Map.Entry<Integer, Integer> entry : colWidths.entrySet() ) {
237 213 setColumnWidth( entry.getKey(), entry.getValue() );
238 214 }
239 - if ( getSelectionPolicy().hasInputColumn() )
240 - {
215 + if ( getSelectionPolicy().hasInputColumn() ) {
241 216 setColumnWidthImpl( -1, getInputColumnWidth() );
242 217 }
243 218 }
244 219
245 220 @Override
246 - public void setCellSpacing( int spacing )
247 - {
221 + public void setCellSpacing( int spacing ) {
248 222 super.setCellSpacing( spacing );
249 223
250 224 // Reset the width of all columns
251 - for ( Map.Entry<Integer, Integer> entry : colWidths.entrySet() )
252 - {
225 + for ( Map.Entry<Integer, Integer> entry : colWidths.entrySet() ) {
253 226 setColumnWidth( entry.getKey(), entry.getValue() );
254 227 }
255 - if ( getSelectionPolicy().hasInputColumn() )
256 - {
228 + if ( getSelectionPolicy().hasInputColumn() ) {
257 229 setColumnWidthImpl( -1, getInputColumnWidth() );
258 230 }
259 231 }
  @@ -266,11 +238,9 @@
266 238 *
267 239 * @throws IndexOutOfBoundsException
268 240 */
269 - public void setColumnWidth( int column, int width )
270 - {
241 + public void setColumnWidth( int column, int width ) {
271 242 // Ensure that the indices are not negative.
272 - if ( column < 0 )
273 - {
243 + if ( column < 0 ) {
274 244 throw new IndexOutOfBoundsException( "Cannot access a column with a negative index: " + column );
275 245 }
276 246
  @@ -279,8 +249,7 @@
279 249 colWidths.put( new Integer( column ), new Integer( width ) );
280 250
281 251 // Update the cell width if possible
282 - if ( column >= numColumns )
283 - {
252 + if ( column >= numColumns ) {
284 253 return;
285 254 }
286 255
  @@ -289,61 +258,50 @@
289 258 }
290 259
291 260 @Override
292 - public void setHTML( int row, int column, String html )
293 - {
261 + public void setHTML( int row, int column, String html ) {
294 262 super.setHTML( row, column, html );
295 263 clearIdealWidths();
296 264 }
297 265
298 266 @Override
299 - public void setSelectionPolicy( SelectionPolicy selectionPolicy )
300 - {
267 + public void setSelectionPolicy( SelectionPolicy selectionPolicy ) {
301 268 // Update the input column in the ghost row
302 - if ( selectionPolicy.hasInputColumn() && !getSelectionPolicy().hasInputColumn() )
303 - {
269 + if ( selectionPolicy.hasInputColumn() && !getSelectionPolicy().hasInputColumn() ) {
304 270 // Add ghost input column
305 271 Element tr = getGhostRow();
306 272 Element td = FixedWidthTableImpl.get().createGhostCell( null );
307 273 tr.insertBefore( td, tr.getFirstChildElement() );
308 274 super.setSelectionPolicy( selectionPolicy );
309 275 setColumnWidthImpl( -1, getInputColumnWidth() );
310 - }
311 - else if ( !selectionPolicy.hasInputColumn() && getSelectionPolicy().hasInputColumn() )
312 - {
276 + } else if ( !selectionPolicy.hasInputColumn() && getSelectionPolicy().hasInputColumn() ) {
313 277 // Remove ghost input column
314 278 Element tr = getGhostRow();
315 279 tr.removeChild( tr.getFirstChildElement() );
316 280 super.setSelectionPolicy( selectionPolicy );
317 - }
318 - else
319 - {
281 + } else {
320 282 super.setSelectionPolicy( selectionPolicy );
321 283 }
322 284 }
323 285
324 286 @Override
325 - public void setText( int row, int column, String text )
326 - {
287 + public void setText( int row, int column, String text ) {
327 288 super.setText( row, column, text );
328 289 clearIdealWidths();
329 290 }
330 291
331 292 @Override
332 - public void setWidget( int row, int column, Widget widget )
333 - {
293 + public void setWidget( int row, int column, Widget widget ) {
334 294 super.setWidget( row, column, widget );
335 295 clearIdealWidths();
336 296 }
337 297
338 298 @Override
339 - protected int getDOMCellCount( int row )
340 - {
299 + protected int getDOMCellCount( int row ) {
341 300 return super.getDOMCellCount( row + 1 );
342 301 }
343 302
344 303 @Override
345 - protected int getDOMRowCount()
346 - {
304 + protected int getDOMRowCount() {
347 305 return super.getDOMRowCount() - 1;
348 306 }
349 307
  @@ -354,8 +312,7 @@
354 312 *
355 313 * @return the {@link FixedWidthGrid}'s cell formatter
356 314 */
357 - protected FixedWidthGridCellFormatter getFixedWidthGridCellFormatter()
358 - {
315 + protected FixedWidthGridCellFormatter getFixedWidthGridCellFormatter() {
359 316 return (FixedWidthGridCellFormatter) getCellFormatter();
360 317 }
361 318
  @@ -366,24 +323,21 @@
366 323 *
367 324 * @return the {@link FixedWidthGrid}'s cell formatter
368 325 */
369 - protected FixedWidthGridRowFormatter getFixedWidthGridRowFormatter()
370 - {
326 + protected FixedWidthGridRowFormatter getFixedWidthGridRowFormatter() {
371 327 return (FixedWidthGridRowFormatter) getRowFormatter();
372 328 }
373 329
374 330 /**
375 331 * @return the number of columns in the ghost row
376 332 */
377 - protected int getGhostColumnCount()
378 - {
333 + protected int getGhostColumnCount() {
379 334 return super.getDOMCellCount( 0 );
380 335 }
381 336
382 337 /**
383 338 * @return the ghost row element
384 339 */
385 - protected Element getGhostRow()
386 - {
340 + protected Element getGhostRow() {
387 341 return ghostRow;
388 342 }
389 343
  @@ -393,32 +347,27 @@
393 347 *
394 348 * @return the width of the input element
395 349 */
396 - protected int getInputColumnWidth()
397 - {
350 + protected int getInputColumnWidth() {
398 351 return 30;
399 352 }
400 353
401 354 @Override
402 - protected int getRowIndex( Element rowElem )
403 - {
355 + protected int getRowIndex( Element rowElem ) {
404 356 int rowIndex = super.getRowIndex( rowElem );
405 - if ( rowIndex < 0 )
406 - {
357 + if ( rowIndex < 0 ) {
407 358 return rowIndex;
408 359 }
409 360 return rowIndex - 1;
410 361 }
411 362
412 363 @Override
413 - protected boolean internalClearCell( Element td, boolean clearInnerHTML )
414 - {
364 + protected boolean internalClearCell( Element td, boolean clearInnerHTML ) {
415 365 clearIdealWidths();
416 366 return super.internalClearCell( td, clearInnerHTML );
417 367 }
418 368
419 369 @Override
420 - protected void onAttach()
421 - {
370 + protected void onAttach() {
422 371 super.onAttach();
423 372 clearIdealWidths();
424 373 }
  @@ -426,12 +375,10 @@
426 375 /**
427 376 * Recalculate the ideal column widths of each column in the data table.
428 377 */
429 - protected void recalculateIdealColumnWidths()
430 - {
378 + protected void recalculateIdealColumnWidths() {
431 379 // We need at least one cell to do any calculations
432 380 int columnCount = getColumnCount();
433 - if ( !isAttached() || getRowCount() == 0 || columnCount < 1 )
434 - {
381 + if ( !isAttached() || getRowCount() == 0 || columnCount < 1 ) {
435 382 idealWidths = new int[0];
436 383 return;
437 384 }
  @@ -447,46 +394,37 @@
447 394 *
448 395 * @param ghostRow the new ghost row
449 396 */
450 - protected void setGhostRow( Element ghostRow )
451 - {
397 + protected void setGhostRow( Element ghostRow ) {
452 398 this.ghostRow = ghostRow;
453 399 }
454 400
455 401 /**
456 402 * Add or remove ghost cells when the table size changes.
457 403 */
458 - protected void updateGhostRow()
459 - {
404 + protected void updateGhostRow() {
460 405 int numGhosts = getGhostColumnCount();
461 - if ( numColumns > numGhosts )
462 - {
406 + if ( numColumns > numGhosts ) {
463 407 // Add ghosts as needed
464 - for ( int i = numGhosts; i < numColumns; i++ )
465 - {
408 + for ( int i = numGhosts; i < numColumns; i++ ) {
466 409 Element td = FixedWidthTableImpl.get().createGhostCell( null );
467 410 DOM.appendChild( ghostRow, td );
468 411 setColumnWidth( i, getColumnWidth( i ) );
469 412 }
470 - }
471 - else if ( numColumns < numGhosts )
472 - {
413 + } else if ( numColumns < numGhosts ) {
473 414 int cellsToRemove = numGhosts - numColumns;
474 - for ( int i = 0; i < cellsToRemove; i++ )
475 - {
415 + for ( int i = 0; i < cellsToRemove; i++ ) {
476 416 Element td = getGhostCellElement( numColumns );
477 417 DOM.removeChild( ghostRow, td );
478 418 }
479 419 }
480 420 }
481 421
482 - @Override void applySort( Element[] trElems )
483 - {
422 + @Override
423 + void applySort( Element[] trElems ) {
484 424 // Move the rows to their new positions
485 425 Element bodyElem = getBodyElement();
486 - for ( int i = trElems.length - 1; i >= 0; i-- )
487 - {
488 - if ( trElems[i] != null )
489 - {
426 + for ( int i = trElems.length - 1; i >= 0; i-- ) {
427 + if ( trElems[i] != null ) {
490 428 DOM.removeChild( bodyElem, trElems[i] );
491 429 // Need to insert below the ghost row
492 430 DOM.insertChild( bodyElem, trElems[i], 1 );
  @@ -497,16 +435,14 @@
497 435 /**
498 436 * Clear the idealWidths field when the ideal widths change.
499 437 */
500 - void clearIdealWidths()
501 - {
438 + void clearIdealWidths() {
502 439 idealWidths = null;
503 440 }
504 441
505 442 /**
506 443 * @return true if the ideal column widths have already been calculated
507 444 */
508 - boolean isIdealColumnWidthsCalculated()
509 - {
445 + boolean isIdealColumnWidthsCalculated() {
510 446 return idealWidths != null;
511 447 }
512 448
  @@ -514,19 +450,16 @@
514 450 * Recalculate the ideal column widths of each column in the data table. This
515 451 * method assumes that the tableLayout has already been changed.
516 452 */
517 - void recalculateIdealColumnWidthsImpl()
518 - {
453 + void recalculateIdealColumnWidthsImpl() {
519 454 idealWidths = FixedWidthTableImpl.get().recalculateIdealColumnWidths( idealColumnWidthInfo );
520 455 }
521 456
522 457 /**
523 458 * Setup to recalculate column widths.
524 459 */
525 - void recalculateIdealColumnWidthsSetup()
526 - {
460 + void recalculateIdealColumnWidthsSetup() {
527 461 int offset = 0;
528 - if ( getSelectionPolicy().hasInputColumn() )
529 - {
462 + if ( getSelectionPolicy().hasInputColumn() ) {
530 463 offset++;
531 464 }
532 465 idealColumnWidthInfo = FixedWidthTableImpl.get().recalculateIdealColumnWidthsSetup( this, getColumnCount(), offset );
  @@ -535,8 +468,7 @@
535 468 /**
536 469 * Tear down after recalculating column widths.
537 470 */
538 - void recalculateIdealColumnWidthsTeardown()
539 - {
471 + void recalculateIdealColumnWidthsTeardown() {
540 472 FixedWidthTableImpl.get().recalculateIdealColumnWidthsTeardown( idealColumnWidthInfo );
541 473 idealColumnWidthInfo = null;
542 474 }
  @@ -548,10 +480,8 @@
548 480 *
549 481 * @return the ghost cell
550 482 */
551 - private Element getGhostCellElement( int column )
552 - {
553 - if ( getSelectionPolicy().hasInputColumn() )
554 - {
483 + private Element getGhostCellElement( int column ) {
484 + if ( getSelectionPolicy().hasInputColumn() ) {
555 485 column++;
556 486 }
557 487 return FixedWidthTableImpl.get().getGhostCell( ghostRow, column );
  @@ -561,10 +491,8 @@
561 491 * Recalculate the ideal column widths of each column in the data table if
562 492 * they have changed since the last calculation.
563 493 */
564 - private void maybeRecalculateIdealColumnWidths()
565 - {
566 - if ( idealWidths == null )
567 - {
494 + private void maybeRecalculateIdealColumnWidths() {
495 + if ( idealWidths == null ) {
568 496 recalculateIdealColumnWidths();
569 497 }
570 498 }
  @@ -575,10 +503,8 @@
575 503 * @param column the index of the column
576 504 * @param width the width in pixels
577 505 */
578 - private void setColumnWidthImpl( int column, int width )
579 - {
580 - if ( getSelectionPolicy().hasInputColumn() )
581 - {
506 + private void setColumnWidthImpl( int column, int width ) {
507 + if ( getSelectionPolicy().hasInputColumn() ) {
582 508 column++;
583 509 }
584 510 FixedWidthTableImpl.get().setColumnWidth( this, ghostRow, column, width );