Subversion Repository Public Repository

litesoft

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
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
/*
 * 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 java.util.*;

import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.*;
import com.google.gwt.user.client.ui.HasVerticalAlignment.*;
import com.google.gwt.user.client.ui.*;

/**
 * This class should replace the actual class of the same name.
 * <p/>
 * TODO: Incorporate changes into actual class.
 * <p/>
 * Steps to incorporate: 1. Replace "OverrideDOM." with "DOM." 2. Copy contents
 * to actual HTMLTable class
 */
public abstract class HTMLTable extends Gen2TablePanel implements SourcesTableEvents
{
    /**
     * This class contains methods used to format a table's cells.
     */
    public class CellFormatter
    {
        /**
         * Adds a style to the specified cell.
         *
         * @param row       the cell's row
         * @param column    the cell's column
         * @param styleName the style name to be added
         *
         * @see UIObject#addStyleName(String)
         */
        public void addStyleName( int row, int column, String styleName )
        {
            prepareCell( row, column );
            Element td = getRawElement( row, column );
            UIObject.setStyleName( td, styleName, true );
        }

        /**
         * Gets the TD element representing the specified cell.
         *
         * @param row    the row of the cell to be retrieved
         * @param column the column of the cell to be retrieved
         *
         * @return the column's TD element
         *
         * @throws IndexOutOfBoundsException
         */
        public Element getElement( int row, int column )
        {
            checkCellBounds( row, column );
            return getRawElement( row, column );
        }

        /**
         * Gets the style of a specified cell.
         *
         * @param row    the cell's row
         * @param column the cell's column
         *
         * @return returns the style name
         *
         * @throws IndexOutOfBoundsException
         * @see UIObject#getStyleName()
         */
        public String getStyleName( int row, int column )
        {
            return UIObject.getStyleName( getElement( row, column ) );
        }

        /**
         * Gets the primary style of a specified cell.
         *
         * @param row    the cell's row
         * @param column the cell's column
         *
         * @return returns the style name
         *
         * @throws IndexOutOfBoundsException
         * @see UIObject#getStylePrimaryName()
         */
        public String getStylePrimaryName( int row, int column )
        {
            return UIObject.getStylePrimaryName( getElement( row, column ) );
        }

        /**
         * Determines whether or not this cell is visible.
         *
         * @param row    the row of the cell whose visibility is to be set
         * @param column the column of the cell whose visibility is to be set
         *
         * @return <code>true</code> if the object is visible
         */
        public boolean isVisible( int row, int column )
        {
            Element e = getElement( row, column );
            return UIObject.isVisible( e );
        }

        /**
         * Removes a style from the specified cell.
         *
         * @param row       the cell's row
         * @param column    the cell's column
         * @param styleName the style name to be removed
         *
         * @throws IndexOutOfBoundsException
         * @see UIObject#removeStyleName(String)
         */
        public void removeStyleName( int row, int column, String styleName )
        {
            checkCellBounds( row, column );
            Element td = getRawElement( row, column );
            UIObject.setStyleName( td, styleName, false );
        }

        /**
         * Sets the horizontal and vertical alignment of the specified cell's
         * contents.
         *
         * @param row    the row of the cell whose alignment is to be set
         * @param column the cell whose alignment is to be set
         * @param hAlign the cell's new horizontal alignment as specified in
         *               {@link com.google.gwt.user.client.ui.HasHorizontalAlignment}
         * @param vAlign the cell's new vertical alignment as specified in
         *               {@link com.google.gwt.user.client.ui.HasVerticalAlignment}
         *
         * @throws IndexOutOfBoundsException
         */
        public void setAlignment( int row, int column, HorizontalAlignmentConstant hAlign, VerticalAlignmentConstant vAlign )
        {
            setHorizontalAlignment( row, column, hAlign );
            setVerticalAlignment( row, column, vAlign );
        }

        /**
         * Sets the height of the specified cell.
         *
         * @param row    the row of the cell whose height is to be set
         * @param column the cell whose height is to be set
         * @param height the cell's new height, in CSS units
         *
         * @throws IndexOutOfBoundsException
         */
        public void setHeight( int row, int column, String height )
        {
            prepareCell( row, column );
            Element elem = getRawElement( row, column );
            DOM.setElementProperty( elem, "height", height );
        }

        /**
         * Sets the horizontal alignment of the specified cell.
         *
         * @param row    the row of the cell whose alignment is to be set
         * @param column the cell whose alignment is to be set
         * @param align  the cell's new horizontal alignment as specified in
         *               {@link com.google.gwt.user.client.ui.HasHorizontalAlignment}.
         *
         * @throws IndexOutOfBoundsException
         */
        public void setHorizontalAlignment( int row, int column, HorizontalAlignmentConstant align )
        {
            prepareCell( row, column );
            Element elem = getRawElement( row, column );
            DOM.setElementProperty( elem, "align", align.getTextAlignString() );
        }

        /**
         * Sets the style name associated with the specified cell.
         *
         * @param row       the row of the cell whose style name is to be set
         * @param column    the column of the cell whose style name is to be set
         * @param styleName the new style name
         *
         * @throws IndexOutOfBoundsException
         * @see UIObject#setStyleName(String)
         */
        public void setStyleName( int row, int column, String styleName )
        {
            prepareCell( row, column );
            UIObject.setStyleName( getRawElement( row, column ), styleName );
        }

        /**
         * Sets the primary style name associated with the specified cell.
         *
         * @param row       the row of the cell whose style name is to be set
         * @param column    the column of the cell whose style name is to be set
         * @param styleName the new style name
         *
         * @throws IndexOutOfBoundsException
         * @see UIObject#setStylePrimaryName(String)
         */
        public void setStylePrimaryName( int row, int column, String styleName )
        {
            UIObject.setStylePrimaryName( getRawElement( row, column ), styleName );
        }

        /**
         * Sets the vertical alignment of the specified cell.
         *
         * @param row    the row of the cell whose alignment is to be set
         * @param column the cell whose alignment is to be set
         * @param align  the cell's new vertical alignment as specified in
         *               {@link com.google.gwt.user.client.ui.HasVerticalAlignment}.
         *
         * @throws IndexOutOfBoundsException
         */
        public void setVerticalAlignment( int row, int column, VerticalAlignmentConstant align )
        {
            prepareCell( row, column );
            DOM.setStyleAttribute( getRawElement( row, column ), "verticalAlign", align.getVerticalAlignString() );
        }

        /**
         * Sets whether this cell is visible via the display style property. The
         * other cells in the row will all shift left to fill the cell's space. So,
         * for example a table with (0,1,2) will become (1,2) if cell 1 is hidden.
         *
         * @param row     the row of the cell whose visibility is to be set
         * @param column  the column of the cell whose visibility is to be set
         * @param visible <code>true</code> to show the cell, <code>false</code>
         *                to hide it
         */
        public void setVisible( int row, int column, boolean visible )
        {
            Element e = ensureElement( row, column );
            UIObject.setVisible( e, visible );
        }

        /**
         * Sets the width of the specified cell.
         *
         * @param row    the row of the cell whose width is to be set
         * @param column the cell whose width is to be set
         * @param width  the cell's new width, in CSS units
         *
         * @throws IndexOutOfBoundsException
         */
        public void setWidth( int row, int column, String width )
        {
            // Give the subclass a chance to prepare the cell.
            prepareCell( row, column );
            DOM.setElementProperty( getRawElement( row, column ), "width", width );
        }

        /**
         * Sets whether the specified cell will allow word wrapping of its contents.
         *
         * @param row    the row of the cell whose word-wrap is to be set
         * @param column the cell whose word-wrap is to be set
         * @param wrap   <code>false </code> to disable word wrapping in this cell
         *
         * @throws IndexOutOfBoundsException
         */
        public void setWordWrap( int row, int column, boolean wrap )
        {
            prepareCell( row, column );
            String wrapValue = wrap ? "" : "nowrap";
            DOM.setStyleAttribute( getElement( row, column ), "whiteSpace", wrapValue );
        }

        /**
         * Gets the element associated with a cell. If it does not exist and the
         * subtype allows creation of elements, creates it.
         *
         * @param row    the cell's row
         * @param column the cell's column
         *
         * @return the cell's element
         *
         * @throws IndexOutOfBoundsException
         */
        protected Element ensureElement( int row, int column )
        {
            prepareCell( row, column );
            return getRawElement( row, column );
        }

        /**
         * Convenience methods to get an attribute on a cell.
         *
         * @param row    cell's row
         * @param column cell's column
         * @param attr   attribute to get
         *
         * @return the attribute's value
         *
         * @throws IndexOutOfBoundsException
         */
        protected String getAttr( int row, int column, String attr )
        {
            Element elem = getElement( row, column );
            return DOM.getElementAttribute( elem, attr );
        }

        /**
         * Gets the TD element representing the specified cell unsafely (meaning
         * that it doesn't ensure that the row and column are valid).
         *
         * @param row    the row of the cell to be retrieved
         * @param column the column of the cell to be retrieved
         *
         * @return the column's TD element
         */
        protected Element getRawElement( int row, int column )
        {
            return getCellElement( bodyElem, row, column );
        }

        /**
         * Convenience methods to set an attribute on a cell.
         *
         * @param row      cell's row
         * @param column   cell's column
         * @param attrName attribute to set
         * @param value    value to set
         *
         * @throws IndexOutOfBoundsException
         */
        protected void setAttr( int row, int column, String attrName, String value )
        {
            Element elem = ensureElement( row, column );
            DOM.setElementAttribute( elem, attrName, value );
        }

        /**
         * Native method to get a cell's element.
         *
         * @param table the table element
         * @param row   the row of the cell
         * @param col   the column of the cell
         *
         * @return the element
         */
        private native Element getCellElement( Element table, int row, int col ) /*-{
            var out = table.rows[row].cells[col];
            return (out == null ? null : out);
        }-*/;
    }

    /**
     * This class contains methods used to format a table's columns. It is limited
     * by the support cross-browser HTML support for column formatting.
     */
    public class ColumnFormatter
    {
        protected Element columnGroup;

        /**
         * Adds a style to the specified column.
         *
         * @param col       the col to which the style will be added
         * @param styleName the style name to be added
         *
         * @throws IndexOutOfBoundsException
         * @see UIObject#addStyleName(String)
         */
        public void addStyleName( int col, String styleName )
        {
            UIObject.setStyleName( ensureColumn( col ), styleName, true );
        }

        /**
         * Gets the style of the specified column.
         *
         * @param column the column to be queried
         *
         * @return the style name
         *
         * @throws IndexOutOfBoundsException
         * @see UIObject#getStyleName()
         */
        public String getStyleName( int column )
        {
            return UIObject.getStyleName( ensureColumn( column ) );
        }

        /**
         * Gets the primary style of the specified column.
         *
         * @param column the column to be queried
         *
         * @return the style name
         *
         * @throws IndexOutOfBoundsException
         * @see UIObject#getStylePrimaryName()
         */
        public String getStylePrimaryName( int column )
        {
            return UIObject.getStylePrimaryName( ensureColumn( column ) );
        }

        /**
         * Removes a style from the specified column.
         *
         * @param column    the column from which the style will be removed
         * @param styleName the style name to be removed
         *
         * @throws IndexOutOfBoundsException
         * @see UIObject#removeStyleName(String)
         */
        public void removeStyleName( int column, String styleName )
        {
            UIObject.setStyleName( ensureColumn( column ), styleName, false );
        }

        /**
         * Sets the style name associated with the specified column.
         *
         * @param column    the column whose style name is to be set
         * @param styleName the new style name
         *
         * @throws IndexOutOfBoundsException
         * @see UIObject#setStyleName(String)
         */
        public void setStyleName( int column, String styleName )
        {
            UIObject.setStyleName( ensureColumn( column ), styleName );
        }

        /**
         * Sets the primary style name associated with the specified column.
         *
         * @param column    the column whose style name is to be set
         * @param styleName the new style name
         *
         * @throws IndexOutOfBoundsException
         * @see UIObject#setStylePrimaryName(String)
         */
        public void setStylePrimaryName( int column, String styleName )
        {
            UIObject.setStylePrimaryName( ensureColumn( column ), styleName );
        }

        /**
         * Sets the width of the specified column.
         *
         * @param column the column of the cell whose width is to be set
         * @param width  the cell's new width, in percentage or pixel units
         *
         * @throws IndexOutOfBoundsException
         */
        public void setWidth( int column, String width )
        {
            DOM.setElementProperty( ensureColumn( column ), "width", width );
        }

        private Element ensureColumn( int col )
        {
            prepareColumn( col );
            prepareColumnGroup();

            int num = DOM.getChildCount( columnGroup );
            if ( num <= col )
            {
                Element colElement = null;
                for ( int i = num; i <= col; i++ )
                {
                    colElement = DOM.createElement( "col" );
                    DOM.appendChild( columnGroup, colElement );
                }
                return colElement;
            }
            return DOM.getChild( columnGroup, col );
        }

        /**
         * Prepare the colgroup tag for the first time, guarenteeing that it exists
         * and has at least one col tag in it. This method corrects a Mozilla issue
         * where the col tag will affect the wrong column if a col tag doesn't exist
         * when the element is attached to the page.
         */
        private void prepareColumnGroup()
        {
            if ( columnGroup == null )
            {
                columnGroup = DOM.createElement( "colgroup" );
                DOM.insertChild( tableElem, columnGroup, 0 );
                DOM.appendChild( columnGroup, DOM.createElement( "col" ) );
            }
        }
    }

    /**
     * This class contains methods used to format a table's rows.
     */
    public class RowFormatter
    {

        /**
         * Adds a style to the specified row.
         *
         * @param row       the row to which the style will be added
         * @param styleName the style name to be added
         *
         * @throws IndexOutOfBoundsException
         * @see UIObject#addStyleName(String)
         */
        public void addStyleName( int row, String styleName )
        {
            UIObject.setStyleName( ensureElement( row ), styleName, true );
        }

        /**
         * Gets the TR element representing the specified row.
         *
         * @param row the row whose TR element is to be retrieved
         *
         * @return the row's TR element
         *
         * @throws IndexOutOfBoundsException
         */
        public Element getElement( int row )
        {
            checkRowBounds( row );
            return getRawElement( row );
        }

        /**
         * Gets the style of the specified row.
         *
         * @param row the row to be queried
         *
         * @return the style name
         *
         * @throws IndexOutOfBoundsException
         * @see UIObject#getStyleName()
         */
        public String getStyleName( int row )
        {
            return UIObject.getStyleName( getElement( row ) );
        }

        /**
         * Gets the primary style of the specified row.
         *
         * @param row the row to be queried
         *
         * @return the style name
         *
         * @throws IndexOutOfBoundsException
         * @see UIObject#getStylePrimaryName()
         */
        public String getStylePrimaryName( int row )
        {
            return UIObject.getStylePrimaryName( getElement( row ) );
        }

        /**
         * Determines whether or not this row is visible via the display style
         * attribute.
         *
         * @param row the row whose visibility is to be set
         *
         * @return <code>true</code> if the row is visible
         */
        public boolean isVisible( int row )
        {
            Element e = getElement( row );
            return UIObject.isVisible( e );
        }

        /**
         * Removes a style from the specified row.
         *
         * @param row       the row from which the style will be removed
         * @param styleName the style name to be removed
         *
         * @throws IndexOutOfBoundsException
         * @see UIObject#removeStyleName(String)
         */
        public void removeStyleName( int row, String styleName )
        {
            UIObject.setStyleName( ensureElement( row ), styleName, false );
        }

        /**
         * Sets the style name associated with the specified row.
         *
         * @param row       the row whose style name is to be set
         * @param styleName the new style name
         *
         * @throws IndexOutOfBoundsException
         * @see UIObject#setStyleName(String)
         */
        public void setStyleName( int row, String styleName )
        {
            UIObject.setStyleName( ensureElement( row ), styleName );
        }

        /**
         * Sets the primary style name associated with the specified row.
         *
         * @param row       the row whose style name is to be set
         * @param styleName the new style name
         *
         * @throws IndexOutOfBoundsException
         * @see UIObject#setStylePrimaryName(String)
         */
        public void setStylePrimaryName( int row, String styleName )
        {
            UIObject.setStylePrimaryName( ensureElement( row ), styleName );
        }

        /**
         * Sets the vertical alignment of the specified row.
         *
         * @param row   the row whose alignment is to be set
         * @param align the row's new vertical alignment as specified in
         *              {@link com.google.gwt.user.client.ui.HasVerticalAlignment}
         *
         * @throws IndexOutOfBoundsException
         */
        public void setVerticalAlign( int row, VerticalAlignmentConstant align )
        {
            DOM.setStyleAttribute( ensureElement( row ), "verticalAlign", align.getVerticalAlignString() );
        }

        /**
         * Sets whether this row is visible.
         *
         * @param row     the row whose visibility is to be set
         * @param visible <code>true</code> to show the row, <code>false</code>
         *                to hide it
         */
        public void setVisible( int row, boolean visible )
        {
            Element e = ensureElement( row );
            UIObject.setVisible( e, visible );
        }

        /**
         * Ensure the TR element representing the specified row exists for
         * subclasses that allow dynamic addition of elements.
         *
         * @param row the row whose TR element is to be retrieved
         *
         * @return the row's TR element
         *
         * @throws IndexOutOfBoundsException
         */
        protected Element ensureElement( int row )
        {
            prepareRow( row );
            return getRawElement( row );
        }

        /**
         * Unsafe method to get a row element.
         *
         * @param row the row to get
         *
         * @return the row element
         */
        protected Element getRawElement( int row )
        {
            return getRow( bodyElem, row );
        }

        protected native Element getRow( Element elem, int row ) /*-{
            return elem.rows[row] || null;
        }-*/;

        /**
         * Convenience methods to set an attribute on a row.
         *
         * @param row      cell's row
         * @param attrName attribute to set
         * @param value    value to set
         *
         * @throws IndexOutOfBoundsException
         */
        protected void setAttr( int row, String attrName, String value )
        {
            Element elem = ensureElement( row );
            DOM.setElementAttribute( elem, attrName, value );
        }
    }

    /**
     * Creates a mapping from elements to their associated widgets.
     */
    protected static class WidgetMapper
    {

        private static class FreeNode
        {
            int index;
            FreeNode next;

            public FreeNode( int index, FreeNode next )
            {
                this.index = index;
                this.next = next;
            }
        }

        private static native void clearWidgetIndex( Element elem ) /*-{
            elem["__widgetID"] = null;
        }-*/;

        private static native int getWidgetIndex( Element elem ) /*-{
            var index = elem["__widgetID"];
            return (index == null) ? -1 : index;
        }-*/;

        private static native void setWidgetIndex( Element elem, int index ) /*-{
            elem["__widgetID"] = index;
        }-*/;

        private FreeNode freeList = null;

        private final ArrayList widgetList = new ArrayList();

        /**
         * Returns the widget associated with the given element.
         *
         * @param elem widget's element
         *
         * @return the widget
         */
        public Widget getWidget( Element elem )
        {
            int index = getWidgetIndex( elem );
            if ( index < 0 )
            {
                return null;
            }
            return (Widget) widgetList.get( index );
        }

        /**
         * Adds the Widget.
         *
         * @param widget widget to add
         */
        public void putWidget( Widget widget )
        {
            int index;
            if ( freeList == null )
            {
                index = widgetList.size();
                widgetList.add( widget );
            }
            else
            {
                index = freeList.index;
                widgetList.set( index, widget );
                freeList = freeList.next;
            }
            setWidgetIndex( widget.getElement(), index );
        }

        /**
         * Remove the widget associated with the given element.
         *
         * @param elem the widget's element
         */
        public void removeWidgetByElement( Element elem )
        {
            int index = getWidgetIndex( elem );
            removeImpl( elem, index );
        }

        /**
         * Creates an iterator of widgets.
         *
         * @return the iterator
         */
        public Iterator widgetIterator()
        {
            // TODO: look at using the WidgetIterators class!
            return new Iterator()
            {
                int lastIndex = -1;
                int nextIndex = -1;

                {
                    findNext();
                }

                public boolean hasNext()
                {
                    return nextIndex < widgetList.size();
                }

                public Object next()
                {
                    if ( !hasNext() )
                    {
                        throw new NoSuchElementException();
                    }
                    Object result = widgetList.get( nextIndex );
                    lastIndex = nextIndex;
                    findNext();
                    return result;
                }

                public void remove()
                {
                    if ( lastIndex < 0 )
                    {
                        throw new IllegalStateException();
                    }
                    Widget w = (Widget) widgetList.get( lastIndex );
                    assert (w.getParent() instanceof HTMLTable);
                    w.removeFromParent();
                    lastIndex = -1;
                }

                private void findNext()
                {
                    while ( ++nextIndex < widgetList.size() )
                    {
                        if ( widgetList.get( nextIndex ) != null )
                        {
                            return;
                        }
                    }
                }
            };
        }

        private void removeImpl( Element elem, int index )
        {
            clearWidgetIndex( elem );
            widgetList.set( index, null );
            freeList = new FreeNode( index, freeList );
        }
    }

    /**
     * Table's body.
     */
    private Element bodyElem;

    /**
     * Current cell formatter.
     */
    private CellFormatter cellFormatter;

    /**
     * The text to insert into cleared cells.
     */
    private String clearText = "";

    /**
     * Column Formatter.
     */
    private ColumnFormatter columnFormatter;

    /**
     * Current row formatter.
     */
    private RowFormatter rowFormatter;

    /**
     * Table element.
     */
    private final Element tableElem;

    /**
     * Current table listener.
     */
    private TableListenerCollection tableListeners;

    private WidgetMapper widgetMap = new WidgetMapper();

    /**
     * Create a new empty HTML Table.
     */
    public HTMLTable()
    {
        super();
        tableElem = DOM.createTable();
        bodyElem = DOM.createTBody();
        DOM.appendChild( tableElem, bodyElem );
        setElement( tableElem );
        sinkEvents( Event.ONCLICK );
    }

    /**
     * Adds a listener to the current table.
     *
     * @param listener listener to add
     */
    public void addTableListener( TableListener listener )
    {
        if ( tableListeners == null )
        {
            tableListeners = new TableListenerCollection();
        }
        tableListeners.add( listener );
    }

    /**
     * Removes all widgets from this table, but does not remove other HTML or text
     * contents of cells.
     */
    public void clear()
    {
        for ( int row = 0; row < getRowCount(); ++row )
        {
            for ( int col = 0; col < getCellCount( row ); ++col )
            {
                Widget child = getWidgetImpl( row, col );
                if ( child != null )
                {
                    remove( child );
                }
            }
        }
    }

    /**
     * Removes all widgets from this table, including other HTML or text contents
     * of cells.
     */
    public void clearAll()
    {
        for ( int row = 0; row < getRowCount(); row++ )
        {
            for ( int col = 0; col < getCellCount( row ); col++ )
            {
                Element td = cellFormatter.getRawElement( row, col );
                internalClearCell( td, true );
            }
        }
    }

    /**
     * Clears the given row and column. If it contains a Widget, it will be
     * removed from the table. If not, its contents will simply be cleared.
     *
     * @param row    the widget's column
     * @param column the widget's column
     *
     * @return true if a widget was removed
     *
     * @throws IndexOutOfBoundsException
     */
    public boolean clearCell( int row, int column )
    {
        Element td = getCellFormatter().getElement( row, column );
        return internalClearCell( td, true );
    }

    /**
     * Gets the number of cells in a given row.
     *
     * @param row the row whose cells are to be counted
     *
     * @return the number of cells present in the row
     */
    public abstract int getCellCount( int row );

    /**
     * Gets the {@link CellFormatter} associated with this table. Use casting to
     * get subclass-specific functionality
     *
     * @return this table's cell formatter
     */
    public CellFormatter getCellFormatter()
    {
        return cellFormatter;
    }

    /**
     * Gets the amount of padding that is added around all cells.
     *
     * @return the cell padding, in pixels
     */
    public int getCellPadding()
    {
        return DOM.getElementPropertyInt( tableElem, "cellPadding" );
    }

    /**
     * Gets the amount of spacing that is added around all cells.
     *
     * @return the cell spacing, in pixels
     */
    public int getCellSpacing()
    {
        return DOM.getElementPropertyInt( tableElem, "cellSpacing" );
    }

    /**
     * Gets the column formatter.
     *
     * @return the column formatter
     */
    public ColumnFormatter getColumnFormatter()
    {
        return columnFormatter;
    }

    /**
     * Determines the TD associated with the specified event.
     *
     * @param event the event to be queried
     *
     * @return the TD associated with the event, or <code>null</code> if none is
     *         found.
     */
    public Element getEventTargetCell( Event event )
    {
        Element td = DOM.eventGetTarget( event );
        for (; td != null; td = DOM.getParent( td ) )
        {
            // If it's a TD, it might be the one we're looking for.
            if ( "td".equalsIgnoreCase( td.getPropertyString( "tagName" ) ) )
            {
                // Make sure it's directly a part of this table before returning
                // it.
                Element tr = DOM.getParent( td );
                Element body = DOM.getParent( tr );
                if ( DOM.compare( body, bodyElem ) )
                {
                    return td;
                }
            }
            // If we run into this table's body, we're out of options.
            if ( DOM.compare( td, bodyElem ) )
            {
                return null;
            }
        }
        return null;
    }

    /**
     * Determines the TR associated with the specified event.
     *
     * @param event the event to be queried
     *
     * @return the TR associated with the event, or <code>null</code> if none is
     *         found.
     */
    public Element getEventTargetRow( Event event )
    {
        Element tr = DOM.eventGetTarget( event );
        for (; tr != null; tr = DOM.getParent( tr ) )
        {
            // If it's a TD, it might be the one we're looking for.
            if ( "tr".equalsIgnoreCase( tr.getPropertyString( "tagName" ) ) )
            {
                // Make sure it's directly a part of this table before returning
                // it.
                Element body = DOM.getParent( tr );
                if ( DOM.compare( body, bodyElem ) )
                {
                    return tr;
                }
            }
            // If we run into this table's body, we're out of options.
            if ( DOM.compare( tr, bodyElem ) )
            {
                return null;
            }
        }
        return null;
    }

    /**
     * Gets the HTML contents of the specified cell.
     *
     * @param row    the cell's row
     * @param column the cell's column
     *
     * @return the cell's HTML contents
     *
     * @throws IndexOutOfBoundsException
     */
    public String getHTML( int row, int column )
    {
        return DOM.getInnerHTML( cellFormatter.getElement( row, column ) );
    }

    /**
     * Gets the number of rows present in this table.
     *
     * @return the table's row count
     */
    public abstract int getRowCount();

    /**
     * Gets the RowFormatter associated with this table.
     *
     * @return the table's row formatter
     */
    public RowFormatter getRowFormatter()
    {
        return rowFormatter;
    }

    /**
     * Gets the text within the specified cell.
     *
     * @param row    the cell's row
     * @param column the cell's column
     *
     * @return the cell's text contents
     *
     * @throws IndexOutOfBoundsException
     */
    public String getText( int row, int column )
    {
        return DOM.getInnerText( cellFormatter.getElement( row, column ) );
    }

    /**
     * Gets the widget in the specified cell.
     *
     * @param row    the cell's row
     * @param column the cell's column
     *
     * @return the widget in the specified cell, or <code>null</code> if none is
     *         present
     *
     * @throws IndexOutOfBoundsException
     */
    public Widget getWidget( int row, int column )
    {
        checkCellBounds( row, column );
        return getWidgetImpl( row, column );
    }

    /**
     * Determines whether the specified cell exists.
     *
     * @param row    the cell's row
     * @param column the cell's column
     *
     * @return <code>true</code> if the specified cell exists
     */
    public boolean isCellPresent( int row, int column )
    {
        if ( (row >= getRowCount()) || (row < 0) )
        {
            return false;
        }
        if ( (column < 0) || (column >= getCellCount( row )) )
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    /**
     * Returns an iterator containing all the widgets in this table.
     *
     * @return the iterator
     */
    public Iterator<Widget> iterator()
    {
        return widgetMap.widgetIterator();
    }

    /**
     * Method to process events generated from the browser.
     *
     * @param event the generated event
     */
    public void onBrowserEvent( Event event )
    {
        switch ( DOM.eventGetType( event ) )
        {
            case Event.ONCLICK:
            {
                if ( tableListeners != null )
                {
                    // Find out which cell was actually clicked.
                    Element td = getEventTargetCell( event );
                    if ( td == null )
                    {
                        return;
                    }
                    Element tr = DOM.getParent( td );
                    int row = getRowIndex( tr );
                    int column = getCellIndex( tr, td );
                    if ( column >= 0 )
                    {
                        // Fire the event.
                        tableListeners.fireCellClicked( this, row, column );
                    }
                }
                break;
            }
            default:
            {
                // Do nothing
            }
        }
    }

    /**
     * Remove the specified widget from the table.
     *
     * @param widget widget to remove
     *
     * @return was the widget removed from the table.
     */
    public boolean remove( Widget widget )
    {
        // Validate.
        if ( widget.getParent() != this )
        {
            return false;
        }

        // Orphan.
        orphan( widget );

        // Physical detach.
        Element elem = widget.getElement();
        DOM.removeChild( DOM.getParent( elem ), elem );

        // Logical detach.
        widgetMap.removeWidgetByElement( elem );
        return true;
    }

    /**
     * Removes the specified table listener.
     *
     * @param listener listener to remove
     */
    public void removeTableListener( TableListener listener )
    {
        if ( tableListeners != null )
        {
            tableListeners.remove( listener );
        }
    }

    /**
     * Sets the width of the table's border. This border is displayed around all
     * cells in the table.
     *
     * @param width the width of the border, in pixels
     */
    public void setBorderWidth( int width )
    {
        DOM.setElementProperty( tableElem, "border", "" + width );
    }

    /**
     * Sets the amount of padding to be added around all cells.
     *
     * @param padding the cell padding, in pixels
     */
    public void setCellPadding( int padding )
    {
        DOM.setElementPropertyInt( tableElem, "cellPadding", padding );
    }

    /**
     * Sets the amount of spacing to be added around all cells.
     *
     * @param spacing the cell spacing, in pixels
     */
    public void setCellSpacing( int spacing )
    {
        DOM.setElementPropertyInt( tableElem, "cellSpacing", spacing );
    }

    /**
     * Sets the element within the specified cell.
     * <p>
     * Inherited implementations may either throw IndexOutOfBounds exception if
     * the cell does not exist, or allocate a new cell to store the content.
     * </p>
     * <p>
     * FlexTable will automatically allocate the cell at the correct location and
     * then set the widget. Grid will set the widget if and only if the cell is
     * within the Grid's bounding box.
     * </p>
     *
     * @param row    the cell's row
     * @param column the cell's column
     *
     * @throws IndexOutOfBoundsException
     */
    public void setElement( int row, int column, Element element )
    {
        prepareCell( row, column );
        if ( element != null )
        {

            // Removes any existing widget.
            Element td = cleanCell( row, column, true );

            // Physical attach.
            DOM.appendChild( td, element );
        }
    }

    /**
     * Sets the HTML contents of the specified cell.
     *
     * @param row    the cell's row
     * @param column the cell's column
     * @param html   the cell's HTML contents
     *
     * @throws IndexOutOfBoundsException
     */
    public void setHTML( int row, int column, String html )
    {
        prepareCell( row, column );
        Element td = cleanCell( row, column, html == null );
        if ( html != null )
        {
            DOM.setInnerHTML( td, html );
        }
    }

    /**
     * Sets the text within the specified cell.
     *
     * @param row    the cell's row
     * @param column cell's column
     * @param text   the cell's text contents
     *
     * @throws IndexOutOfBoundsException
     */
    public void setText( int row, int column, String text )
    {
        prepareCell( row, column );
        Element td = cleanCell( row, column, text == null );
        if ( text != null )
        {
            DOM.setInnerText( td, text );
        }
    }

    /**
     * Sets the widget within the specified cell.
     * <p>
     * Inherited implementations may either throw IndexOutOfBounds exception if
     * the cell does not exist, or allocate a new cell to store the content.
     * </p>
     * <p>
     * FlexTable will automatically allocate the cell at the correct location and
     * then set the widget. Grid will set the widget if and only if the cell is
     * within the Grid's bounding box.
     * </p>
     *
     * @param widget The widget to be added
     * @param row    the cell's row
     * @param column the cell's column
     *
     * @throws IndexOutOfBoundsException
     */
    public void setWidget( int row, int column, Widget widget )
    {
        prepareCell( row, column );
        if ( widget != null )
        {
            widget.removeFromParent();

            // Removes any existing widget.
            Element td = cleanCell( row, column, true );
            DOM.setInnerHTML( td, "" );

            // Logical attach.
            widgetMap.putWidget( widget );

            // Physical attach.
            DOM.appendChild( td, widget.getElement() );

            adopt( widget );
        }
    }

    /**
     * Bounds checks that the cell exists at the specified location.
     *
     * @param row    cell's row
     * @param column cell's column
     *
     * @throws IndexOutOfBoundsException
     */
    protected void checkCellBounds( int row, int column )
    {
        checkRowBounds( row );
        if ( column < 0 )
        {
            throw new IndexOutOfBoundsException( "Column " + column + " must be non-negative: " + column );
        }
        int cellSize = getCellCount( row );
        if ( cellSize <= column )
        {
            throw new IndexOutOfBoundsException( "Column index: " + column + ", Column size: " + getCellCount( row ) );
        }
    }

    /**
     * Checks that the row is within the correct bounds.
     *
     * @param row row index to check
     *
     * @throws IndexOutOfBoundsException
     */
    protected void checkRowBounds( int row )
    {
        int rowSize = getRowCount();
        if ( (row >= rowSize) || (row < 0) )
        {
            throw new IndexOutOfBoundsException( "Row index: " + row + ", Row size: " + rowSize );
        }
    }

    /**
     * Removes any widgets, text, and HTML within the cell. This method assumes
     * that the requested cell already exists.
     *
     * @param row            the cell's row
     * @param column         the cell's column
     * @param clearInnerHTML should the cell's inner html be cleared?
     *
     * @return element that has been cleaned
     */
    protected Element cleanCell( int row, int column, boolean clearInnerHTML )
    {
        // Clear whatever is in the cell.
        Element td = getCellFormatter().getRawElement( row, column );
        internalClearCell( td, clearInnerHTML );
        return td;
    }

    /**
     * Creates a new cell. Override this method if the cell should have initial
     * contents.
     *
     * @return the newly created TD
     */
    protected Element createCell()
    {
        return OverrideDOM.createTD();
    }

    /**
     * Creates a new row. Override this method if the row should have initial
     * contents.
     *
     * @return the newly created TD
     */
    protected Element createRow()
    {
        return DOM.createTR();
    }

    /**
     * Gets the table's TBODY element.
     *
     * @return the TBODY element
     */
    protected Element getBodyElement()
    {
        return bodyElem;
    }

    /**
     * @param rowElem  the row element
     * @param cellElem the cell element
     *
     * @return the index of a cell in the row
     */
    protected int getCellIndex( Element rowElem, Element cellElem )
    {
        return DOM.getChildIndex( rowElem, cellElem );
    }

    /**
     * Directly ask the underlying DOM what the cell count on the given row is.
     *
     * @param tableBody the element
     * @param row       the row
     *
     * @return number of columns in the row
     */
    protected native int getDOMCellCount( Element tableBody, int row ) /*-{
        return tableBody.rows[row].cells.length;
    }-*/;

    /**
     * Directly ask the underlying DOM what the cell count on the given row is.
     *
     * @param row the row
     *
     * @return number of columns in the row
     */
    protected int getDOMCellCount( int row )
    {
        return getDOMCellCount( bodyElem, row );
    }

    /**
     * Directly ask the underlying DOM what the row count is.
     *
     * @return Returns the number of rows in the table
     */
    protected int getDOMRowCount()
    {
        return getDOMRowCount( bodyElem );
    }

    protected native int getDOMRowCount( Element elem ) /*-{
        return elem.rows.length;
    }-*/;

    /**
     * @param rowElem the row element
     *
     * @return the index of a row
     */
    protected int getRowIndex( Element rowElem )
    {
        return OverrideDOM.getRowIndex( rowElem );
    }

    /**
     * Returns the widgetMap.
     *
     * @return the widget map
     */
    protected WidgetMapper getWidgetMap()
    {
        return widgetMap;
    }

    /**
     * Inserts a new cell into the specified row.
     *
     * @param row    the row into which the new cell will be inserted
     * @param column the column before which the cell will be inserted
     *
     * @return the new element
     *
     * @throws IndexOutOfBoundsException
     */
    protected Element insertCell( int row, int column )
    {
        Element tr = rowFormatter.getElement( row );
        Element td = createCell();
        Element beforeTd = cellFormatter.getRawElement( row, column );
        tr.insertBefore( td, beforeTd );
        return td;
    }

    /**
     * Inserts a number of cells before the specified cell.
     *
     * @param row    the row into which the new cells will be inserted
     * @param column the column before which the new cells will be inserted
     * @param count  number of cells to be inserted
     *
     * @throws IndexOutOfBoundsException
     */
    protected void insertCells( int row, int column, int count )
    {
        Element tr = rowFormatter.getRawElement( row );
        Element beforeTd = cellFormatter.getRawElement( row, column );
        for ( int i = column; i < column + count; i++ )
        {
            Element td = createCell();
            tr.insertBefore( td, beforeTd );
        }
    }

    /**
     * Inserts a new row into the table.
     *
     * @param beforeRow the index before which the new row will be inserted
     *
     * @return the index of the newly-created row
     *
     * @throws IndexOutOfBoundsException
     */
    protected int insertRow( int beforeRow )
    {
        // Specifically allow the row count as an insert position.
        if ( beforeRow != getRowCount() )
        {
            checkRowBounds( beforeRow );
        }
        Element tr = createRow();
        Element beforeRowElem = rowFormatter.getRawElement( beforeRow );
        bodyElem.insertBefore( tr, beforeRowElem );
        return beforeRow;
    }

    /**
     * Does actual clearing, used by clearCell and cleanCell. All HTMLTable
     * methods should use internalClearCell rather than clearCell, as clearCell
     * may be overridden in subclasses to format an empty cell.
     *
     * @param td             element to clear
     * @param clearInnerHTML should the cell's inner html be cleared?
     *
     * @return returns whether a widget was cleared
     */
    protected boolean internalClearCell( Element td, boolean clearInnerHTML )
    {
        Element maybeChild = DOM.getFirstChild( td );
        Widget widget = null;
        if ( maybeChild != null )
        {
            widget = widgetMap.getWidget( maybeChild );
        }
        if ( widget != null )
        {
            // If there is a widget, remove it.
            remove( widget );
            return true;
        }
        else
        {
            // Otherwise, simply clear whatever text and/or HTML may be there.
            if ( clearInnerHTML )
            {
                DOM.setInnerHTML( td, clearText );
            }
            return false;
        }
    }

    /**
     * Subclasses must implement this method. It allows them to decide what to do
     * just before a cell is accessed. If the cell already exists, this method
     * must do nothing. Otherwise, a subclass must either ensure that the cell
     * exists or throw an {@link IndexOutOfBoundsException}.
     *
     * @param row    the cell's row
     * @param column the cell's column
     */
    protected abstract void prepareCell( int row, int column );

    /**
     * Subclasses can implement this method. It allows them to decide what to do
     * just before a column is accessed. For classes, such as
     * <code>FlexTable</code>, that do not have a concept of a global column
     * length can ignore this method.
     *
     * @param column the cell's column
     *
     * @throws IndexOutOfBoundsException
     */
    protected void prepareColumn( int column )
    {
        // By default, do nothing.
    }

    /**
     * Subclasses must implement this method. If the row already exists, this
     * method must do nothing. Otherwise, a subclass must either ensure that the
     * row exists or throw an {@link IndexOutOfBoundsException}.
     *
     * @param row the cell's row
     */
    protected abstract void prepareRow( int row );

    /**
     * Removes the specified cell from the table.
     *
     * @param row    the row of the cell to remove
     * @param column the column of cell to remove
     *
     * @throws IndexOutOfBoundsException
     */
    protected void removeCell( int row, int column )
    {
        checkCellBounds( row, column );
        Element td = cleanCell( row, column, false );
        Element tr = rowFormatter.getRawElement( row );
        DOM.removeChild( tr, td );
    }

    /**
     * Removes the specified row from the table.
     *
     * @param row the index of the row to be removed
     *
     * @throws IndexOutOfBoundsException
     */
    protected void removeRow( int row )
    {
        checkRowBounds( row );
        int columnCount = getCellCount( row );
        for ( int column = 0; column < columnCount; ++column )
        {
            cleanCell( row, column, false );
        }
        DOM.removeChild( bodyElem, rowFormatter.getRawElement( row ) );
    }

    protected void setBodyElement( Element element )
    {
        // Must free any existing widgets from the current DOM first.
        if ( this.bodyElem != null )
        {
            clearOnlyWidgets();
        }
        this.bodyElem = element;
    }

    /**
     * Sets the table's CellFormatter.
     *
     * @param cellFormatter the table's cell formatter
     */
    protected void setCellFormatter( CellFormatter cellFormatter )
    {
        this.cellFormatter = cellFormatter;
    }

    /**
     * Set the string to insert into cells when they are cleared. By default, an
     * empty string is used, but a &nbsp; or line break may also be used.
     *
     * @param clearText the default clear text
     */
    protected void setClearText( String clearText )
    {
        this.clearText = clearText;
    }

    protected void setColumnFormatter( ColumnFormatter formatter )
    {
        columnFormatter = formatter;
        columnFormatter.prepareColumnGroup();
    }

    /**
     * Sets the table's RowFormatter.
     *
     * @param rowFormatter the table's row formatter
     */
    protected void setRowFormatter( RowFormatter rowFormatter )
    {
        this.rowFormatter = rowFormatter;
    }

    /**
     * Clears the widgets from the table without actually modifying the underlying
     * HTMLTable.
     */
    private void clearOnlyWidgets()
    {
        Iterator widgets = iterator();
        while ( widgets.hasNext() )
        {
            orphan( (Widget) widgets.next() );
        }
        widgetMap = new WidgetMapper();
    }

    /**
     * Gets the Widget associated with the given cell.
     *
     * @param row    the cell's row
     * @param column the cell's column
     *
     * @return the widget
     */
    private Widget getWidgetImpl( int row, int column )
    {
        Element e = cellFormatter.getRawElement( row, column );
        Element child = DOM.getFirstChild( e );
        if ( child == null )
        {
            return null;
        }
        else
        {
            return widgetMap.getWidget( child );
        }
    }

    /**
     * Sets the widget of a cell without clearing the cell.
     *
     * @param widget The widget to be added
     * @param row    the cell's row
     * @param column the cell's column
     *
     * @throws IndexOutOfBoundsException
     */
    @SuppressWarnings("unused")
    private void setWidgetRaw( int row, int column, Widget widget )
    {
        if ( widget != null )
        {
            widget.removeFromParent();

            // Logical attach.
            widgetMap.putWidget( widget );

            // Physical attach.
            Element td = getCellFormatter().getRawElement( row, column );
            DOM.appendChild( td, widget.getElement() );

            adopt( widget );
        }
    }
}

Commits for litesoft/trunk/Java/GWT/Client/src/com/google/gwt/gen2/table/client/HTMLTable.java

Diff revisions: vs.
Revision Author Commited Message
939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

475 Diff Diff GeorgeS picture GeorgeS Sat 03 Sep, 2011 13:54:51 +0000
282 GeorgeS picture GeorgeS Fri 17 Jun, 2011 13:54:39 +0000