litesoft
@ 950
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 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 |
/* * 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.core.client.*; import com.google.gwt.gen2.event.dom.client.*; import com.google.gwt.gen2.event.shared.*; import com.google.gwt.gen2.table.client.ColumnResizer.*; import com.google.gwt.gen2.table.client.HTMLTable.*; import com.google.gwt.gen2.table.client.TableModelHelper.*; import com.google.gwt.gen2.table.client.property.*; import com.google.gwt.gen2.table.event.client.*; import com.google.gwt.i18n.client.*; import com.google.gwt.user.client.*; import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.ui.*; import com.google.gwt.widgetideas.client.*; import java.util.*; /** * <p> * A ScrollTable consists of a fixed header and footer (optional) that remain * visible and a scrollable body that contains the data. * </p> * <p/> * <p> * In order for the columns in the header table and data table to line up, the * two table must have the same margin, padding, and border widths. You can use * CSS style sheets to manipulate the colors and styles of the cell's, but you * must keep the actual sizes consistent (especially with respect to the left * and right side of the cells). * </p> * <p/> * <p> * NOTE: AbstractScrollTable does not resize correctly in older versions of * Mozilla (specifically, Linux hosted mode). In use, the PagingScrollTable will * expand horizontally, but it will not contract when you reduce the screen * size. However, the AbstractScrollTable resizes naturally (you can set width * in percentages) on all modern browsers including IE6+, FF2+, Safari2+, * Chrome, Opera 9.6. * </p> * <p/> * <h3>CSS Style Rules</h3> * <p/> * <dl> * <dt>.gwt-ScrollTable</dt> * <dd>applied to the entire widget</dd> * <dt>.gwt-ScrollTable .headerTable</dt> * <dd>applied to the header table</dd> * <dt>.gwt-ScrollTable .dataTable</dt> * <dd>applied to the data table</dd> * <dt>.gwt-ScrollTable .footerTable</dt> * <dd>applied to the footer table</dd> * <dt>.gwt-ScrollTable .headerWrapper</dt> * <dd>wrapper around the header table</dd> * <dt>.gwt-ScrollTable .dataWrapper</dt> * <dd>wrapper around the data table</dd> * <dt>.gwt-ScrollTable .footerWrapper</dt> * <dd>wrapper around the footer table</dd> * </dl> */ public abstract class AbstractScrollTable extends Gen2TableComplexPanel implements ResizableWidget, HasScrollHandlers { /** * Browser specific implementation class for {@link AbstractScrollTable}. */ private static class Impl { /** * Create a spacer element that allows the header table to scroll over the * vertical scroll bar of the data table. * * @param wrapper the wrapper that contains the header table * * @return the spacer element */ public Element createSpacer( FixedWidthFlexTable table, Element wrapper ) { resizeSpacer( table, null, 15 ); return null; } /** * Returns the width of a table, minus any padding, in pixels. * * @param table the table * @param includeSpacer true to include the spacer width * * @return the width */ public int getTableWidth( FixedWidthFlexTable table, boolean includeSpacer ) { int scrollWidth = table.getElement().getScrollWidth(); if ( !includeSpacer ) { int spacerWidth = getSpacerWidth( table ); if ( spacerWidth > 0 ) { scrollWidth -= spacerWidth; } } return scrollWidth; } /** * Recalculate the ideal widths of columns. * * @param scrollTable the scroll table * @param command an optional command to execute while recalculating */ public void recalculateIdealColumnWidths( AbstractScrollTable scrollTable, Command command ) { FixedWidthFlexTable headerTable = scrollTable.getHeaderTable(); FixedWidthFlexTable footerTable = scrollTable.getFooterTable(); FixedWidthGrid dataTable = scrollTable.getDataTable(); // Setup all inner tables dataTable.recalculateIdealColumnWidthsSetup(); headerTable.recalculateIdealColumnWidthsSetup(); if ( footerTable != null ) { footerTable.recalculateIdealColumnWidthsSetup(); } // Perform operations dataTable.recalculateIdealColumnWidthsImpl(); headerTable.recalculateIdealColumnWidthsImpl(); if ( footerTable != null ) { footerTable.recalculateIdealColumnWidthsImpl(); } // Execute the optional command if ( command != null ) { command.execute(); } // Teardown all inner tables dataTable.recalculateIdealColumnWidthsTeardown(); headerTable.recalculateIdealColumnWidthsTeardown(); if ( footerTable != null ) { footerTable.recalculateIdealColumnWidthsTeardown(); } } /** * Reposition the header spacer as needed. * * @param scrollTable the scroll table * @param force if true, ignore the scroll policy */ public void repositionSpacer( AbstractScrollTable scrollTable, boolean force ) { // Only ScrollPolicy.BOTH has a vertical scroll bar if ( !force && scrollTable.scrollPolicy != ScrollPolicy.BOTH ) { return; } Element dataWrapper = scrollTable.dataWrapper; int spacerWidth = dataWrapper.getOffsetWidth() - dataWrapper.getPropertyInt( "clientWidth" ); resizeSpacer( scrollTable.headerTable, scrollTable.headerSpacer, spacerWidth ); if ( scrollTable.footerTable != null ) { resizeSpacer( scrollTable.footerTable, scrollTable.footerSpacer, spacerWidth ); } } /** * @return true if the scroll bar is on the right */ boolean isScrollBarOnRight() { return true; } void resizeSpacer( FixedWidthFlexTable table, Element spacer, int spacerWidth ) { // Exit early if the spacer is already the correct size if ( spacerWidth == getSpacerWidth( table ) ) { return; } if ( isScrollBarOnRight() ) { table.getElement().getStyle().setPropertyPx( "paddingRight", spacerWidth ); } else { table.getElement().getStyle().setPropertyPx( "paddingLeft", spacerWidth ); } } /** * Get the current width of the spacer element. * * @param table the table to check * * @return the current width */ private int getSpacerWidth( FixedWidthFlexTable table ) { // Get the padding string String paddingStr; if ( isScrollBarOnRight() ) { paddingStr = table.getElement().getStyle().getProperty( "paddingRight" ); } else { paddingStr = table.getElement().getStyle().getProperty( "paddingLeft" ); } // Check the padding string if ( paddingStr == null || paddingStr.length() < 3 ) { return -1; } // Parse the int from the padding try { return Integer.parseInt( paddingStr.substring( 0, paddingStr.length() - 2 ) ); } catch ( NumberFormatException e ) { return -1; } } } /** * Opera and Old Mozilla put the scroll bar on the left side in RTL mode. */ private static class ImplLeftScrollBar extends Impl { @Override boolean isScrollBarOnRight() { return !LocaleInfo.getCurrentLocale().isRTL(); } } /** * IE puts the scroll bar on the left side in RTL mode. The padding trick * doesn't work, so we use a separate element. */ @SuppressWarnings("unused") private static class ImplIE6 extends ImplLeftScrollBar { /** * Adding padding to a table in IE will mess up the layout, so we use an * absolutely positioned div to add padding. In RTL mode, the div needs to * be exactly the right width and position or scrollLeft will be affected. * In LTR mode, we can position it anywhere and set the width to a high * number, improving performance. */ @Override public Element createSpacer( FixedWidthFlexTable table, Element wrapper ) { Element spacer = DOM.createDiv(); spacer.getStyle().setPropertyPx( "height", 1 ); spacer.getStyle().setPropertyPx( "top", 1 ); spacer.getStyle().setProperty( "position", "absolute" ); if ( !LocaleInfo.getCurrentLocale().isRTL() ) { spacer.getStyle().setPropertyPx( "left", 1 ); spacer.getStyle().setPropertyPx( "width", 10000 ); } wrapper.appendChild( spacer ); return spacer; } @Override public int getTableWidth( FixedWidthFlexTable table, boolean includeSpacer ) { return table.getElement().getScrollWidth(); } /** * IE allows the table to resize as widely as needed unless we restrict the * width of a parent element. */ @Override public void recalculateIdealColumnWidths( AbstractScrollTable scrollTable, Command command ) { scrollTable.getAbsoluteElement().getStyle().setPropertyPx( "width", 1 ); super.recalculateIdealColumnWidths( scrollTable, command ); scrollTable.getAbsoluteElement().getStyle().setProperty( "width", "100%" ); } @Override void resizeSpacer( FixedWidthFlexTable table, Element spacer, int width ) { if ( LocaleInfo.getCurrentLocale().isRTL() ) { int headerWidth = table.getOffsetWidth(); spacer.getStyle().setPropertyPx( "width", width ); spacer.getStyle().setPropertyPx( "right", headerWidth ); } } } /** * A helper class that handles some of the mouse events associated with * resizing columns. */ private static class MouseResizeWorker { /** * The width of the area that is available for resize. */ private static final int RESIZE_CURSOR_WIDTH = 15; /** * The current header cell that the mouse is affecting. */ private Element curCell = null; /** * The columns under the colSpan of the current cell. */ private List<ColumnWidthInfo> curCells = new ArrayList<ColumnWidthInfo>(); /** * The index of the current header cell. */ private int curCellIndex = 0; /** * The current x position of the mouse. */ private int mouseXCurrent = 0; /** * The last x position of the mouse when we resized. */ private int mouseXLast = 0; /** * The starting x position of the mouse when resizing a column. */ private int mouseXStart = 0; /** * A timer used to resize the columns. As long as the timer is active, it * will poll for the new row size and resize the columns. */ private Timer resizeTimer = new Timer() { @Override public void run() { resizeColumn(); schedule( 100 ); } }; /** * A boolean indicating that we are resizing the current header cell. */ private boolean resizing = false; /** * The index of the first column that will be sacrificed. */ private int sacrificeCellIndex = -1; /** * The cells that will be sacrificed so the current cells can be resized. */ private List<ColumnWidthInfo> sacrificeCells = new ArrayList<ColumnWidthInfo>(); /** * The table that this worker affects. */ private AbstractScrollTable table = null; /** * @return the current cell */ public Element getCurrentCell() { return curCell; } /** * @return true if a header is currently being resized */ public boolean isResizing() { return resizing; } /** * Resize the column on a mouse event. This method also marks the client as * busy so we do not try to change the size repeatedly. * * @param event the mouse event */ public void resizeColumn( Event event ) { mouseXCurrent = DOM.eventGetClientX( event ); } /** * Set the current cell that will be resized based on the mouse event. * * @param event the event that triggered the new cell * * @return true if the cell was actually changed */ public boolean setCurrentCell( Event event ) { // Check the resize policy of the table Element cell = null; if ( table.columnResizePolicy == ColumnResizePolicy.MULTI_CELL ) { cell = table.headerTable.getEventTargetCell( event ); } else if ( table.columnResizePolicy == ColumnResizePolicy.SINGLE_CELL ) { cell = table.headerTable.getEventTargetCell( event ); if ( cell != null && cell.getPropertyInt( "colSpan" ) > 1 ) { cell = null; } } // See if we are near the edge of the cell int clientX = event.getClientX(); if ( cell != null ) { int absLeft = cell.getAbsoluteLeft() - Window.getScrollLeft(); if ( LocaleInfo.getCurrentLocale().isRTL() ) { if ( clientX < absLeft || clientX > absLeft + RESIZE_CURSOR_WIDTH ) { cell = null; } } else { int absRight = absLeft + cell.getOffsetWidth(); if ( clientX < absRight - RESIZE_CURSOR_WIDTH || clientX > absRight ) { cell = null; } } } // Change out the current cell if ( cell != curCell ) { // Clear the old cell if ( curCell != null ) { curCell.getStyle().setProperty( "cursor", "" ); } // Set the new cell curCell = cell; if ( curCell != null ) { // Check the cell index curCellIndex = getCellIndex( curCell ); if ( curCellIndex < 0 ) { curCell = null; return false; } // Check for resizable columns within one of the cells in the colspan boolean resizable = false; int colSpan = cell.getPropertyInt( "colSpan" ); curCells = table.getColumnWidthInfo( curCellIndex, colSpan ); for ( ColumnWidthInfo info : curCells ) { if ( !info.hasMaximumWidth() || !info.hasMinimumWidth() || info.getMaximumWidth() != info.getMinimumWidth() ) { resizable = true; } } if ( !resizable ) { curCell = null; curCells = null; return false; } // Update the cursor on the cell curCell.getStyle().setProperty( "cursor", "e-resize" ); } return true; } // The cell did not change return false; } /** * Set the ScrollTable table that this worker affects. * * @param table the scroll table */ public void setScrollTable( AbstractScrollTable table ) { this.table = table; } /** * Start resizing the current cell when the user clicks on the right edge of * the cell. * * @param event the mouse event */ public void startResizing( Event event ) { if ( curCell != null ) { resizing = true; mouseXStart = event.getClientX(); mouseXLast = mouseXStart; mouseXCurrent = mouseXStart; // Add the sacrifice cells int numColumns = table.getDataTable().getColumnCount(); int colSpan = curCell.getPropertyInt( "colSpan" ); sacrificeCellIndex = curCellIndex + colSpan; sacrificeCells = table.getColumnWidthInfo( sacrificeCellIndex, numColumns - sacrificeCellIndex ); // Start the timer and listen for changes DOM.setCapture( table.headerWrapper ); resizeTimer.schedule( 20 ); } } /** * Stop resizing the current cell. * * @param event the mouse event */ public void stopResizing( Event event ) { if ( curCell != null && resizing ) { curCell.getStyle().setProperty( "cursor", "" ); curCell = null; resizing = false; DOM.releaseCapture( table.headerWrapper ); resizeTimer.cancel(); resizeColumn(); curCells = null; sacrificeCells = null; table.resizeTablesVertically(); } } /** * Get the scroll table. * * @return the scroll table */ protected AbstractScrollTable getScrollTable() { return table; } /** * Get the actual cell index of a cell in the header table. * * @param cell the cell element * * @return the cell index */ private int getCellIndex( Element cell ) { int row = OverrideDOM.getRowIndex( DOM.getParent( cell ) ) - 1; int column = OverrideDOM.getCellIndex( cell ); return table.headerTable.getColumnIndex( row, column ) - table.getHeaderOffset(); } /** * Helper method that actually sets the column size. This method is called * periodically by a timer. */ private void resizeColumn() { if ( mouseXLast != mouseXCurrent ) { mouseXLast = mouseXCurrent; // Distribute to the cells being resized int totalDelta = mouseXCurrent - mouseXStart; if ( LocaleInfo.getCurrentLocale().isRTL() ) { totalDelta *= -1; } totalDelta -= table.columnResizer.distributeWidth( curCells, totalDelta ); // Distribute to the sacrifice cells if ( table.resizePolicy.isSacrificial() ) { int remaining = table.columnResizer.distributeWidth( sacrificeCells, -totalDelta ); // We don't have enough to sacrifice, redistribute the width if ( remaining != 0 && table.resizePolicy.isFixedWidth() ) { totalDelta += remaining; table.columnResizer.distributeWidth( curCells, totalDelta ); } // Apply the widths to the sacrifice column table.applyNewColumnWidths( sacrificeCellIndex, sacrificeCells, true ); } // Set the new widths table.applyNewColumnWidths( curCellIndex, curCells, true ); // Scroll to table back into alignment table.scrollTables( false ); } } } /** * The Opera version of the mouse worker fixes an Opera bug where the cursor * isn't updated if the mouse is hovering over an element DOM object when its * cursor style is changed. */ @SuppressWarnings("unused") private static class MouseResizeWorkerOpera extends MouseResizeWorker { /** * A div used to force the cursor to update. */ private Element cursorUpdateDiv; /** * Constructor. */ public MouseResizeWorkerOpera() { cursorUpdateDiv = DOM.createDiv(); DOM.setStyleAttribute( cursorUpdateDiv, "position", "absolute" ); } /** * Set the current cell that will be resized based on the mouse event. * * @param event the event that triggered the new cell * * @return true if the cell was actually changed */ @Override public boolean setCurrentCell( Event event ) { // Check if cursor update div is active if ( DOM.eventGetTarget( event ) == cursorUpdateDiv ) { removeCursorUpdateDiv(); return false; } // Use the parent method boolean cellChanged = super.setCurrentCell( event ); // Position a div that forces a cursor redraw in Opera if ( cellChanged ) { DOM.setCapture( getScrollTable().headerWrapper ); DOM.setStyleAttribute( cursorUpdateDiv, "height", (Window.getClientHeight() - 1) + "px" ); DOM.setStyleAttribute( cursorUpdateDiv, "width", (Window.getClientWidth() - 1) + "px" ); DOM.setStyleAttribute( cursorUpdateDiv, "left", "0px" ); DOM.setStyleAttribute( cursorUpdateDiv, "top", "0px" ); DOM.appendChild( RootPanel.getBodyElement(), cursorUpdateDiv ); } return cellChanged; } /** * Start resizing the current cell. * * @param event the mouse event */ @Override public void startResizing( Event event ) { removeCursorUpdateDiv(); super.startResizing( event ); } /** * Remove the cursor update div from the page. */ private void removeCursorUpdateDiv() { if ( DOM.getCaptureElement() != null ) { DOM.removeChild( RootPanel.getBodyElement(), cursorUpdateDiv ); DOM.releaseCapture( getScrollTable().headerWrapper ); } } } /** * Information about the height of the inner tables. */ private class TableHeightInfo { private int headerTableHeight; private int dataTableHeight; private int footerTableHeight; /** * Construct a new {@link TableHeightInfo}. */ public TableHeightInfo() { int totalHeight = DOM.getElementPropertyInt( getElement(), "clientHeight" ); headerTableHeight = headerTable.getOffsetHeight(); if ( footerTable != null ) { footerTableHeight = footerTable.getOffsetHeight(); } dataTableHeight = totalHeight - headerTableHeight - footerTableHeight; } } /** * Information about the width of the inner tables. */ private class TableWidthInfo { private int headerTableWidth; private int dataTableWidth; private int footerTableWidth; private int availableWidth; /** * Construct a new {@link TableWidthInfo}. * * @param includeSpacer true to include spacer in calculations */ public TableWidthInfo( boolean includeSpacer ) { availableWidth = getAvailableWidth(); headerTableWidth = impl.getTableWidth( headerTable, includeSpacer ); dataTableWidth = dataTable.getElement().getScrollWidth(); if ( footerTable != null ) { footerTableWidth = impl.getTableWidth( footerTable, includeSpacer ); } } } // /** // * An {@link ImageBundle} that provides images for {@link AbstractScrollTable} // * . // */ // public static interface ScrollTableImages extends ImageBundle // { // /** // * An image used to fill the available width. // * // * @return a prototype of this image // */ // AbstractImagePrototype scrollTableFillWidth(); // // /** // * An image indicating that a column is sorted in ascending order. // * // * @return a prototype of this image // */ // AbstractImagePrototype scrollTableAscending(); // // /** // * An image indicating a column is sorted in descending order. // * // * @return a prototype of this image // */ // AbstractImagePrototype scrollTableDescending(); // } /** * The default style name. */ public static final String DEFAULT_STYLE_NAME = "gwt-ScrollTable"; /** * The resize policies related to user resizing. * <p/> * <ul> * <li>DISABLED - Columns cannot be resized by the user</li> * <li>SINGLE_CELL - Only cells with a colspan of 1 can be resized</li> * <li>MULTI_CELL - All cells can be resized by the user</li> * </ul> */ public static enum ColumnResizePolicy { DISABLED, SINGLE_CELL, MULTI_CELL } /** * The resize policies of table cells. * <p/> * <ul> * <li>UNCONSTRAINED - Columns shrink and expand independently of each other</li> * <li>FLOW - As one column expands or shrinks, the columns to the right will * do the opposite, trying to maintain the same size. The user can still * expand the grid if there is no more space to take from the columns on the * right.</li> * <li>FIXED_WIDTH - As one column expands or shrinks, the columns to the * right will do the opposite, trying to maintain the same size. The width of * the grid will remain constant, ignoring column resizing that would result * in the grid growing in size.</li> * <li>FILL_WIDTH - Same as FIXED_WIDTH, but the grid will always fill the * available width, even if the widget is resized.</li> * </ul> */ public static enum ResizePolicy { UNCONSTRAINED( false, false ), FLOW( false, true ), FIXED_WIDTH( true, true ), FILL_WIDTH( true, true ); private boolean isSacrificial; private boolean isFixedWidth; private ResizePolicy( boolean isFixedWidth, boolean isSacrificial ) { this.isFixedWidth = isFixedWidth; this.isSacrificial = isSacrificial; } private boolean isFixedWidth() { return isFixedWidth; } private boolean isSacrificial() { return isSacrificial; } } /** * The scroll policy of the table. * <p/> * <ul> * <li>HORIZONTAL - Only a horizontal scrollbar will be present.</li> * <li>BOTH - Both a vertical and horizontal scrollbar will be present.</li> * <li>DISABLED - Scrollbars will not appear, even if content doesn't fit</li> * </ul> */ public static enum ScrollPolicy { HORIZONTAL, BOTH, DISABLED } /** * The sorting policies related to user column sorting. * <p/> * <ul> * <li>DISABLED - Columns cannot be sorted by the user</li> * <li>SINGLE_CELL - Only cells with a colspan of 1 can be sorted</li> * <li>MULTI_CELL - All cells can be sorted by the user</li> * </ul> */ public static enum SortPolicy { DISABLED, SINGLE_CELL, MULTI_CELL } /** * The div that wraps the table wrappers. */ private Element absoluteElem; /** * The helper class used to resize columns. */ private ColumnResizer columnResizer = new ColumnResizer(); /** * The policy applied to user actions that resize columns. */ private ColumnResizePolicy columnResizePolicy = ColumnResizePolicy.MULTI_CELL; /** * The data table. */ private FixedWidthGrid dataTable; /** * The scrollable wrapper div around the data table. */ private Element dataWrapper; /** * An image used to show a fill width button. */ private Image fillWidthImage; /** * A spacer used to stretch the footerTable area so we can scroll past the * edge of the footer table. */ private Element footerSpacer = null; /** * The footer table. */ private FixedWidthFlexTable footerTable = null; /** * The non-scrollable wrapper div around the footer table. */ private Element footerWrapper = null; /** * A spacer used to stretch the headerTable area so we can scroll past the * edge of the header table. */ private Element headerSpacer; /** * The header table. */ private FixedWidthFlexTable headerTable = null; /** * The non-scrollable wrapper div around the header table. */ private Element headerWrapper; // /** // * The images applied to the table. // */ // private ScrollTableImages images; /** * The implementation class for this widget. */ private Impl impl = GWT.create( Impl.class ); /** * The last known height of this widget that the user set. */ private String lastHeight = null; /** * The last scrollLeft position. */ private int lastScrollLeft; /** * An element used to determine the width of the scroll bar. */ private com.google.gwt.dom.client.Element mockScrollable; /** * A boolean indicating whether or not the grid should try to maintain its * width as much as possible. */ private ResizePolicy resizePolicy = ResizePolicy.FLOW; /** * The worker that helps with mouse resize events. */ private MouseResizeWorker resizeWorker = GWT.create( MouseResizeWorker.class ); /** * The scrolling policy. */ private ScrollPolicy scrollPolicy = ScrollPolicy.BOTH; /** * The current {@link SortPolicy}. */ private SortPolicy sortPolicy = SortPolicy.SINGLE_CELL; /** * The cell index of the TD cell that initiated a column sort operation. */ private int sortedCellIndex = -1; /** * The row index of the TD cell that initiated a column sort operation. */ private int sortedRowIndex = -1; /** * The wrapper around the image indicator. */ private Element sortedColumnWrapper = null; /** * Constructor. * * @param dataTable the data table * @param headerTable the header table */ public AbstractScrollTable( FixedWidthGrid dataTable, final FixedWidthFlexTable headerTable ) { this.dataTable = dataTable; this.headerTable = headerTable; resizeWorker.setScrollTable( this ); // Prepare the header and data tables prepareTable( dataTable, "dataTable" ); prepareTable( headerTable, "headerTable" ); if ( dataTable.getSelectionPolicy().hasInputColumn() ) { headerTable.setColumnWidth( 0, dataTable.getInputColumnWidth() ); } // Create the main div container Element mainElem = DOM.createDiv(); setElement( mainElem ); setStylePrimaryName( DEFAULT_STYLE_NAME ); DOM.setStyleAttribute( mainElem, "padding", "0px" ); DOM.setStyleAttribute( mainElem, "overflow", "hidden" ); DOM.setStyleAttribute( mainElem, "position", "relative" ); // Wrap the table wrappers in another div absoluteElem = DOM.createDiv(); absoluteElem.getStyle().setProperty( "position", "absolute" ); absoluteElem.getStyle().setProperty( "top", "0px" ); absoluteElem.getStyle().setProperty( "left", "0px" ); absoluteElem.getStyle().setProperty( "width", "100%" ); absoluteElem.getStyle().setProperty( "padding", "0px" ); absoluteElem.getStyle().setProperty( "margin", "0px" ); absoluteElem.getStyle().setProperty( "border", "0px" ); absoluteElem.getStyle().setProperty( "overflow", "hidden" ); mainElem.appendChild( absoluteElem ); // Create the table wrapper and spacer headerWrapper = createWrapper( "headerWrapper" ); headerSpacer = impl.createSpacer( headerTable, headerWrapper ); dataWrapper = createWrapper( "dataWrapper" ); // Create an element to determine the scroll bar width mockScrollable = com.google.gwt.dom.client.Element.as( dataWrapper.cloneNode( false ) ); mockScrollable.getStyle().setProperty( "position", "absolute" ); mockScrollable.getStyle().setProperty( "top", "0px" ); mockScrollable.getStyle().setProperty( "left", "0px" ); mockScrollable.getStyle().setProperty( "width", "100px" ); mockScrollable.getStyle().setProperty( "height", "100px" ); mockScrollable.getStyle().setProperty( "visibility", "hidden" ); mockScrollable.getStyle().setProperty( "overflow", "scroll" ); mockScrollable.getStyle().setProperty( "zIndex", "-1" ); absoluteElem.appendChild( mockScrollable ); // Create image to fill width fillWidthImage = new Image() { @Override public void onBrowserEvent( Event event ) { super.onBrowserEvent( event ); if ( DOM.eventGetType( event ) == Event.ONCLICK ) { fillWidth(); } } }; fillWidthImage.setTitle( "Shrink/Expand to fill visible area" ); // TODO: images.scrollTableFillWidth().applyTo( fillWidthImage ); Element fillWidthImageElem = fillWidthImage.getElement(); DOM.setStyleAttribute( fillWidthImageElem, "cursor", "pointer" ); DOM.setStyleAttribute( fillWidthImageElem, "position", "absolute" ); DOM.setStyleAttribute( fillWidthImageElem, "top", "0px" ); DOM.setStyleAttribute( fillWidthImageElem, "right", "0px" ); DOM.setStyleAttribute( fillWidthImageElem, "zIndex", "1" ); add( fillWidthImage, getElement() ); // Adopt the header and data tables into the panel adoptTable( headerTable, headerWrapper, 0 ); adoptTable( dataTable, dataWrapper, 1 ); // Create the sort indicator Image sortedColumnWrapper = DOM.createSpan(); // Add some event handling sinkEvents( Event.ONMOUSEOUT ); DOM.setEventListener( dataWrapper, this ); DOM.sinkEvents( dataWrapper, Event.ONSCROLL ); DOM.setEventListener( headerWrapper, this ); DOM.sinkEvents( headerWrapper, Event.ONMOUSEMOVE | Event.ONMOUSEDOWN | Event.ONMOUSEUP | Event.ONCLICK ); // Listen for sorting events in the data table dataTable.addColumnSortHandler( new ColumnSortHandler() { public void onColumnSorted( ColumnSortEvent event ) { // Get the primary column and sort order int column = -1; boolean ascending = true; ColumnSortList sortList = event.getColumnSortList(); if ( sortList != null ) { column = sortList.getPrimaryColumn(); ascending = sortList.isPrimaryAscending(); } // Remove the sorted column indicator if ( isColumnSortable( column ) ) { Element parent = DOM.getParent( sortedColumnWrapper ); if ( parent != null ) { parent.removeChild( sortedColumnWrapper ); } // Re-add the sorted column indicator if ( column < 0 ) { sortedCellIndex = -1; sortedRowIndex = -1; } else if ( sortedCellIndex >= 0 && sortedRowIndex >= 0 && headerTable.getRowCount() > sortedRowIndex && headerTable.getCellCount( sortedRowIndex ) > sortedCellIndex ) { CellFormatter formatter = headerTable.getCellFormatter(); Element td = formatter.getElement( sortedRowIndex, sortedCellIndex ); applySortedColumnIndicator( td, ascending ); } } } } ); } public HandlerRegistration addScrollHandler( ScrollHandler handler ) { return addHandler( ScrollEvent.TYPE, handler ); } /** * Adjust all column widths so they take up the maximum amount of space * without needing a horizontal scroll bar. The distribution will be * proportional to the current width of each column. * <p/> * The {@link AbstractScrollTable} must be visible on the page for this method * to work. */ public void fillWidth() { List<ColumnWidthInfo> colWidths = getFillColumnWidths( null ); applyNewColumnWidths( 0, colWidths, false ); scrollTables( false ); } /** * @return the cell padding of the tables, in pixels */ public int getCellPadding() { return dataTable.getCellPadding(); } /** * @return the cell spacing of the tables, in pixels */ public int getCellSpacing() { return dataTable.getCellSpacing(); } /** * @return the column resize policy */ public ColumnResizePolicy getColumnResizePolicy() { return columnResizePolicy; } /** * Return the column width for a given column index. * * @param column the column index * * @return the column width in pixels */ public int getColumnWidth( int column ) { return dataTable.getColumnWidth( column ); } /** * @return the data table */ public FixedWidthGrid getDataTable() { return dataTable; } /** * @return the footer table */ public FixedWidthFlexTable getFooterTable() { return footerTable; } /** * @return the header table */ public FixedWidthFlexTable getHeaderTable() { return headerTable; } /** * Get the absolute maximum width of a column. * * @param column the column index * * @return the maximum allowable width of the column */ public abstract int getMaximumColumnWidth( int column ); /** * Get the absolute minimum width of a column. * * @param column the column index * * @return the minimum allowable width of the column */ public abstract int getMinimumColumnWidth( int column ); /** * Get the minimum offset width of the largest inner table given the * constraints on the minimum and ideal column widths. Note that this does not * account for the vertical scroll bar. * * @return the tables minimum offset width, or -1 if it cannot be calculated */ public int getMinimumOffsetWidth() { if ( !isAttached() ) { return -1; } // Determine the width and column count of the largest table TableWidthInfo redrawInfo = new TableWidthInfo( true ); maybeRecalculateIdealColumnWidths( null ); if ( redrawInfo.availableWidth < 1 ) { return -1; } int scrollWidth = 0; int numColumns = 0; { int numHeaderCols = headerTable.getColumnCount() - getHeaderOffset(); int numDataCols = dataTable.getColumnCount(); int numFooterCols = (footerTable == null) ? -1 : footerTable.getColumnCount() - getHeaderOffset(); if ( numHeaderCols >= numDataCols && numHeaderCols >= numFooterCols ) { numColumns = numHeaderCols; scrollWidth = redrawInfo.headerTableWidth; } else if ( numFooterCols >= numDataCols && numFooterCols >= numHeaderCols ) { numColumns = numFooterCols; scrollWidth = redrawInfo.footerTableWidth; } else if ( numDataCols > 0 ) { numColumns = numDataCols; scrollWidth = redrawInfo.dataTableWidth; } } if ( numColumns <= 0 ) { return -1; } // Calculate the available diff List<ColumnWidthInfo> colWidthInfos = getColumnWidthInfo( 0, numColumns ); return -columnResizer.distributeWidth( colWidthInfos, -scrollWidth ); } /** * Get the preferred width of a column. * * @param column the column index * * @return the preferred width of the column */ public abstract int getPreferredColumnWidth( int column ); /** * @return the resize policy */ public ResizePolicy getResizePolicy() { return resizePolicy; } /** * @return the current scroll policy */ public ScrollPolicy getScrollPolicy() { return scrollPolicy; } /** * @return the current sort policy */ public SortPolicy getSortPolicy() { return sortPolicy; } /** * Returns true if the specified column is sortable. * * @param column the column index * * @return true if the column is sortable, false if it is not sortable */ public abstract boolean isColumnSortable( int column ); /** * Returns true if the specified column can be truncated. If it cannot be * truncated, its minimum width will be adjusted to ensure the cell content is * visible. * * @param column the column index * * @return true if the column is truncatable, false if it is not */ public abstract boolean isColumnTruncatable( int column ); /** * Returns true if the specified column in the footer table can be truncated. * If it cannot be truncated, its minimum width will be adjusted to ensure the * cell content is visible. * * @param column the column index * * @return true if the column is truncatable, false if it is not */ public abstract boolean isFooterColumnTruncatable( int column ); /** * Returns true if the specified column in the header table can be truncated. * If it cannot be truncated, its minimum width will be adjusted to ensure the * cell content is visible. * * @param column the column index * * @return true if the column is truncatable, false if it is not */ public abstract boolean isHeaderColumnTruncatable( int column ); @Override public void onBrowserEvent( Event event ) { super.onBrowserEvent( event ); Element target = DOM.eventGetTarget( event ); switch ( DOM.eventGetType( event ) ) { case Event.ONSCROLL: // Reposition the tables on scroll lastScrollLeft = dataWrapper.getScrollLeft(); scrollTables( false ); if ( dataWrapper.isOrHasChild( target ) ) { fireEvent( new ScrollEvent( event ) ); } break; case Event.ONMOUSEDOWN: // Start resizing a header column if ( DOM.eventGetButton( event ) != Event.BUTTON_LEFT ) { return; } if ( resizeWorker.getCurrentCell() != null ) { DOM.eventPreventDefault( event ); DOM.eventCancelBubble( event, true ); resizeWorker.startResizing( event ); } break; case Event.ONMOUSEUP: if ( DOM.eventGetButton( event ) != Event.BUTTON_LEFT ) { return; } // Stop resizing the header column if ( resizeWorker.isResizing() ) { resizeWorker.stopResizing( event ); } else { // Scroll tables if needed if ( DOM.isOrHasChild( headerWrapper, target ) ) { scrollTables( true ); } else { scrollTables( false ); } // Get the actual column index Element cellElem = headerTable.getEventTargetCell( event ); if ( cellElem != null ) { // Sorting is disabled if ( sortPolicy == SortPolicy.DISABLED ) { return; } // Check the colSpan int colSpan = cellElem.getPropertyInt( "colSpan" ); if ( colSpan > 1 && getSortPolicy() != SortPolicy.MULTI_CELL ) { return; } // Sort the column sortedRowIndex = OverrideDOM.getRowIndex( DOM.getParent( cellElem ) ) - 1; sortedCellIndex = OverrideDOM.getCellIndex( cellElem ); int column = headerTable.getColumnIndex( sortedRowIndex, sortedCellIndex ) - getHeaderOffset(); if ( column >= 0 && isColumnSortable( column ) ) { if ( dataTable.getColumnCount() > column && onHeaderSort( sortedRowIndex, column ) ) { dataTable.sortColumn( column ); } } } } break; case Event.ONMOUSEMOVE: // Resize the header column if ( resizeWorker.isResizing() ) { resizeWorker.resizeColumn( event ); } else { resizeWorker.setCurrentCell( event ); } break; case Event.ONMOUSEOUT: // Unhighlight if the mouse leaves the table Element toElem = DOM.eventGetToElement( event ); if ( toElem == null || !dataWrapper.isOrHasChild( toElem ) ) { // Check that the coordinates are not directly over the table int clientX = event.getClientX() + Window.getScrollLeft(); int clientY = event.getClientY() + Window.getScrollTop(); int tableLeft = dataWrapper.getAbsoluteLeft(); int tableTop = dataWrapper.getAbsoluteTop(); int tableWidth = dataWrapper.getOffsetWidth(); int tableHeight = dataWrapper.getOffsetHeight(); int tableBottom = tableTop + tableHeight; int tableRight = tableLeft + tableWidth; if ( clientX > tableLeft && clientX < tableRight && clientY > tableTop && clientY < tableBottom ) { return; } dataTable.highlightCell( null ); } break; } } /** * This method is called when the dimensions of the parent element change. * Subclasses should override this method as needed. * * @param width the new client width of the element * @param height the new client height of the element */ public void onResize( int width, int height ) { redraw(); } /** * Redraw the table. */ public void redraw() { if ( !isAttached() ) { return; } // Create a command to execute while recalculating widths. Using this // command prevents an extra browser layout by grouping read operations. TableWidthInfo redrawInfo = new TableWidthInfo( false ); Command command = new Command() { public void execute() { // We update the ResizableWidgetCollection before changing the size of // the ScrollTable, because change the size of the scroll table could // require an additional layout (ex. if window scroll bars show up). ResizableWidgetCollection.get().updateWidgetSize( AbstractScrollTable.this ); } }; // Recalculate the ideal table widths of each column. maybeRecalculateIdealColumnWidths( command ); // Calculate the new widths of the columns List<ColumnWidthInfo> colWidths = null; if ( resizePolicy == ResizePolicy.FILL_WIDTH ) { colWidths = getFillColumnWidths( redrawInfo ); } else { colWidths = getBoundedColumnWidths( true ); } applyNewColumnWidths( 0, colWidths, true ); // Update the overall height of the scroll table. This can only happen // after the widths have been set because setting the width of cells can // cause word wrap, which increases the height of the inner tables. resizeTablesVertically(); // Reset the scroll position, which might be lost when we change the layout. scrollTables( false ); } /** * Unsupported. * * @param child the widget to be removed * * @return false * * @throws UnsupportedOperationException */ @Override public boolean remove( Widget child ) { throw new UnsupportedOperationException( "This panel does not support remove()" ); } /** * Reset the widths of all columns to their preferred sizes. */ public void resetColumnWidths() { applyNewColumnWidths( 0, getBoundedColumnWidths( false ), false ); scrollTables( false ); } /** * Sets the amount of padding to be added around all cells. * * @param padding the cell padding, in pixels */ public void setCellPadding( int padding ) { headerTable.setCellPadding( padding ); dataTable.setCellPadding( padding ); if ( footerTable != null ) { footerTable.setCellPadding( padding ); } redraw(); } /** * Sets the amount of spacing to be added around all cells. * * @param spacing the cell spacing, in pixels */ public void setCellSpacing( int spacing ) { headerTable.setCellSpacing( spacing ); dataTable.setCellSpacing( spacing ); if ( footerTable != null ) { footerTable.setCellSpacing( spacing ); } redraw(); } /** * Set the resize policy applied to user actions that resize columns. * * @param columnResizePolicy the resize policy */ public void setColumnResizePolicy( ColumnResizePolicy columnResizePolicy ) { this.columnResizePolicy = columnResizePolicy; updateFillWidthImage(); } /** * Set the width of a column. * * @param column the index of the column * @param width the width in pixels * * @return the new column width */ public int setColumnWidth( int column, int width ) { // Constrain the size of the column ColumnWidthInfo info = getColumnWidthInfo( column ); if ( info.hasMaximumWidth() ) { width = Math.min( width, info.getMaximumWidth() ); } if ( info.hasMinimumWidth() ) { width = Math.max( width, info.getMinimumWidth() ); } // Try to constrain the size of the grid if ( resizePolicy.isSacrificial() ) { // Get the sacrifice columns int sacrificeColumn = column + 1; int numColumns = dataTable.getColumnCount(); int remainingColumns = numColumns - sacrificeColumn; List<ColumnWidthInfo> infos = getColumnWidthInfo( sacrificeColumn, remainingColumns ); // Distribute the width over the sacrifice columns int diff = width - getColumnWidth( column ); int undistributed = columnResizer.distributeWidth( infos, -diff ); // Set the new column widths applyNewColumnWidths( sacrificeColumn, infos, false ); // Prevent over resizing if ( resizePolicy.isFixedWidth() ) { width += undistributed; } } // Resize the column int offset = getHeaderOffset(); dataTable.setColumnWidth( column, width ); headerTable.setColumnWidth( column + offset, width ); if ( footerTable != null ) { footerTable.setColumnWidth( column + offset, width ); } // Reposition things as needed impl.repositionSpacer( this, false ); resizeTablesVertically(); scrollTables( false ); return width; } /** * Set the footer table that appears under the data table. If set to null, the * footer table will not be shown. * * @param footerTable the table to use in the footer */ public void setFooterTable( FixedWidthFlexTable footerTable ) { // Disown the old footer table if ( this.footerTable != null ) { super.remove( this.footerTable ); DOM.removeChild( absoluteElem, footerWrapper ); } // Set the new footer table this.footerTable = footerTable; if ( footerTable != null ) { footerTable.setCellSpacing( getCellSpacing() ); footerTable.setCellPadding( getCellPadding() ); prepareTable( footerTable, "footerTable" ); if ( dataTable.getSelectionPolicy().hasInputColumn() ) { footerTable.setColumnWidth( 0, dataTable.getInputColumnWidth() ); } // Create the footer wrapper and spacer if ( footerWrapper == null ) { footerWrapper = createWrapper( "footerWrapper" ); footerSpacer = impl.createSpacer( footerTable, footerWrapper ); DOM.setEventListener( footerWrapper, this ); DOM.sinkEvents( footerWrapper, Event.ONMOUSEUP ); } // Adopt the header table into the panel adoptTable( footerTable, footerWrapper, absoluteElem.getChildNodes().getLength() ); } redraw(); } @Override public void setHeight( String height ) { this.lastHeight = height; super.setHeight( height ); resizeTablesVertically(); } /** * Set the resize policy of the table. * * @param resizePolicy the resize policy */ public void setResizePolicy( ResizePolicy resizePolicy ) { this.resizePolicy = resizePolicy; updateFillWidthImage(); redraw(); } /** * Set the scroll policy of the table. * * @param scrollPolicy the new scroll policy */ public void setScrollPolicy( ScrollPolicy scrollPolicy ) { if ( scrollPolicy == this.scrollPolicy ) { return; } this.scrollPolicy = scrollPolicy; // Clear the heights of the wrappers headerWrapper.getStyle().setProperty( "height", "" ); dataWrapper.getStyle().setProperty( "height", "" ); if ( footerWrapper != null ) { footerWrapper.getStyle().setProperty( "height", "" ); } if ( scrollPolicy == ScrollPolicy.DISABLED ) { // Disabled scroll bars dataWrapper.getStyle().setProperty( "height", "auto" ); dataWrapper.getStyle().setProperty( "overflow", "" ); } else if ( scrollPolicy == ScrollPolicy.HORIZONTAL ) { // Only show horizontal scroll bar dataWrapper.getStyle().setProperty( "height", "auto" ); dataWrapper.getStyle().setProperty( "overflow", "auto" ); } else if ( scrollPolicy == ScrollPolicy.BOTH ) { // Show both scroll bars if ( lastHeight != null ) { super.setHeight( lastHeight ); } else { super.setHeight( "" ); } dataWrapper.getStyle().setProperty( "overflow", "auto" ); } // Resize the tables impl.repositionSpacer( this, true ); redraw(); } /** * Set the {@link SortPolicy} that defines what columns users can sort. * * @param sortPolicy the {@link SortPolicy} */ public void setSortPolicy( SortPolicy sortPolicy ) { this.sortPolicy = sortPolicy; // Remove the sorted indicator image applySortedColumnIndicator( null, true ); } /** * Apply the sorted column indicator to a specific table cell in the header * table. * * @param tdElem the cell in the header table, or null to remove it * @param ascending true to apply the ascending indicator, false for * descending */ protected void applySortedColumnIndicator( Element tdElem, boolean ascending ) { // Remove the sort indicator if ( tdElem == null ) { Element parent = DOM.getParent( sortedColumnWrapper ); if ( parent != null ) { parent.removeChild( sortedColumnWrapper ); headerTable.clearIdealWidths(); } return; } tdElem.appendChild( sortedColumnWrapper ); if ( ascending ) { sortedColumnWrapper.setInnerHTML( HtmlEntity.NBSP ); // TODO: + images.scrollTableAscending().getHTML() ); } else { sortedColumnWrapper.setInnerHTML( HtmlEntity.NBSP ); // TODO: + images.scrollTableDescending().getHTML() ); } sortedRowIndex = -1; sortedCellIndex = -1; // The column with the indicator now has a new ideal width headerTable.clearIdealWidths(); redraw(); } /** * Create a wrapper element that will hold a table. * * @param cssName the style name added to the base name * * @return a new wrapper element */ protected Element createWrapper( String cssName ) { Element wrapper = DOM.createDiv(); wrapper.getStyle().setProperty( "width", "100%" ); wrapper.getStyle().setProperty( "overflow", "hidden" ); wrapper.getStyle().setPropertyPx( "padding", 0 ); wrapper.getStyle().setPropertyPx( "margin", 0 ); wrapper.getStyle().setPropertyPx( "border", 0 ); if ( cssName != null ) { setStyleName( wrapper, cssName ); } return wrapper; } /** * @return the wrapper element around the data table */ protected Element getDataWrapper() { return dataWrapper; } /** * Extend the columns to exactly fill the available space, if the current * {@link ResizePolicy} requires it. * * @deprecated use {@link #redraw()} instead */ @Deprecated protected void maybeFillWidth() { redraw(); } /** * Called just before a column is sorted because of a user click on the header * row. * * @param row the row index that was clicked * @param column the column index that was clicked * * @return true to sort, false to ignore */ protected boolean onHeaderSort( int row, int column ) { return true; } @Override protected void onLoad() { ResizableWidgetCollection.get().add( this ); redraw(); } @Override protected void onUnload() { ResizableWidgetCollection.get().remove( this ); } /** * Fixes the table heights so the header is visible and the data takes up the * remaining vertical space. */ protected void resizeTablesVertically() { if ( scrollPolicy == ScrollPolicy.DISABLED ) { dataWrapper.getStyle().setProperty( "overflow", "auto" ); dataWrapper.getStyle().setProperty( "overflow", "" ); int height = Math.max( 1, absoluteElem.getOffsetHeight() ); super.setHeight( height + "px" ); } else if ( scrollPolicy == ScrollPolicy.HORIZONTAL ) { dataWrapper.getStyle().setProperty( "overflow", "hidden" ); dataWrapper.getStyle().setProperty( "overflow", "auto" ); int height = Math.max( 1, absoluteElem.getOffsetHeight() ); super.setHeight( height + "px" ); } else { applyTableWrapperSizes( getTableWrapperSizes() ); dataWrapper.getStyle().setProperty( "width", "100%" ); } } /** * Helper method that actually performs the vertical resizing. * * @deprecated use {@link #redraw()} instead */ @Deprecated protected void resizeTablesVerticallyNow() { redraw(); } /** * Sets the scroll property of the header and footers wrappers when scrolling * so that the header, footer, and data tables line up. * * @param baseHeader true to scroll the data table as well */ protected void scrollTables( boolean baseHeader ) { if ( scrollPolicy == ScrollPolicy.DISABLED ) { return; } if ( lastScrollLeft >= 0 ) { headerWrapper.setScrollLeft( lastScrollLeft ); if ( baseHeader ) { dataWrapper.setScrollLeft( lastScrollLeft ); } if ( footerWrapper != null ) { footerWrapper.setScrollLeft( lastScrollLeft ); } } } /** * @return the absolutely positioned wrapper element */ Element getAbsoluteElement() { return absoluteElem; } /** * Adopt a table into this {@link AbstractScrollTable} within its wrapper. * * @param table the table to adopt * @param wrapper the wrapper element * @param index the index to insert the wrapper in the main element */ private void adoptTable( Widget table, Element wrapper, int index ) { DOM.insertChild( absoluteElem, wrapper, index ); add( table, wrapper ); } /** * Apply the new widths to a list of columns. * * @param startIndex the index of the first column * @param infos the new column width info * @param forced if false, only set column widths that have changed */ private void applyNewColumnWidths( int startIndex, List<ColumnWidthInfo> infos, boolean forced ) { // Infos can be null if the widths cannot be calculated if ( infos == null ) { return; } int offset = getHeaderOffset(); int numColumns = infos.size(); for ( int i = 0; i < numColumns; i++ ) { ColumnWidthInfo info = infos.get( i ); int newWidth = info.getNewWidth(); if ( forced || info.getCurrentWidth() != newWidth ) { dataTable.setColumnWidth( startIndex + i, newWidth ); headerTable.setColumnWidth( startIndex + i + offset, newWidth ); if ( footerTable != null ) { footerTable.setColumnWidth( startIndex + i + offset, newWidth ); } } } impl.repositionSpacer( this, false ); } /** * Apply the new sizes to the table wrappers. * * @param sizes the sizes to apply */ private void applyTableWrapperSizes( TableHeightInfo sizes ) { if ( sizes == null ) { return; } headerWrapper.getStyle().setPropertyPx( "height", sizes.headerTableHeight ); if ( footerWrapper != null ) { footerWrapper.getStyle().setPropertyPx( "height", sizes.footerTableHeight ); } dataWrapper.getStyle().setPropertyPx( "height", Math.max( sizes.dataTableHeight, 0 ) ); dataWrapper.getStyle().setProperty( "overflow", "hidden" ); dataWrapper.getStyle().setProperty( "overflow", "auto" ); } /** * Get the width available for the tables. * * @return the available width, or -1 if not defined */ private int getAvailableWidth() { int clientWidth = absoluteElem.getPropertyInt( "clientWidth" ); if ( scrollPolicy == ScrollPolicy.BOTH ) { int scrollbarWidth = mockScrollable.getOffsetWidth() - mockScrollable.getPropertyInt( "clientWidth" ); clientWidth = absoluteElem.getPropertyInt( "clientWidth" ) - scrollbarWidth - 1; } return Math.max( clientWidth, -1 ); } /** * Get the widths of all columns, either to their preferred sizes or just * ensure that they are within their min/max boundaries. * * @param boundsOnly true to only ensure the widths are within the bounds * * @return the column widths */ private List<ColumnWidthInfo> getBoundedColumnWidths( boolean boundsOnly ) { if ( !isAttached() ) { return null; } // Calculate the new column widths int numColumns = dataTable.getColumnCount(); int totalWidth = 0; List<ColumnWidthInfo> colWidthInfos = getColumnWidthInfo( 0, numColumns ); // If we are reseting to original widths, set all widths to 0 if ( !boundsOnly ) { for ( ColumnWidthInfo info : colWidthInfos ) { totalWidth += info.getCurrentWidth(); info.setCurrentWidth( 0 ); } } // Run the resize algorithm columnResizer.distributeWidth( colWidthInfos, totalWidth ); // Set the new column widths return colWidthInfos; } /** * Get info about the width of a column. * * @param column the column index * * @return the info about the column width */ private ColumnWidthInfo getColumnWidthInfo( int column ) { int minWidth = getMinimumColumnWidth( column ); int maxWidth = getMaximumColumnWidth( column ); int preferredWidth = getPreferredColumnWidth( column ); int curWidth = getColumnWidth( column ); // Adjust the widths if the columns are not truncatable, up to maxWidth if ( !isColumnTruncatable( column ) ) { maybeRecalculateIdealColumnWidths( null ); int idealWidth = getDataTable().getIdealColumnWidth( column ); if ( maxWidth != MaximumWidthProperty.NO_MAXIMUM_WIDTH ) { idealWidth = Math.min( idealWidth, maxWidth ); } minWidth = Math.max( minWidth, idealWidth ); } if ( !isHeaderColumnTruncatable( column ) ) { maybeRecalculateIdealColumnWidths( null ); int idealWidth = getHeaderTable().getIdealColumnWidth( column + getHeaderOffset() ); if ( maxWidth != MaximumWidthProperty.NO_MAXIMUM_WIDTH ) { idealWidth = Math.min( idealWidth, maxWidth ); } minWidth = Math.max( minWidth, idealWidth ); } if ( footerTable != null && !isFooterColumnTruncatable( column ) ) { maybeRecalculateIdealColumnWidths( null ); int idealWidth = getFooterTable().getIdealColumnWidth( column + getHeaderOffset() ); if ( maxWidth != MaximumWidthProperty.NO_MAXIMUM_WIDTH ) { idealWidth = Math.min( idealWidth, maxWidth ); } minWidth = Math.max( minWidth, idealWidth ); } return new ColumnWidthInfo( minWidth, maxWidth, preferredWidth, curWidth ); } /** * Get info about the width of multiple columns. * * @param column the start column index * @param numColumns the number of columns * * @return the info about the column widths of the columns */ private List<ColumnWidthInfo> getColumnWidthInfo( int column, int numColumns ) { List<ColumnWidthInfo> infos = new ArrayList<ColumnWidthInfo>(); for ( int i = 0; i < numColumns; i++ ) { infos.add( getColumnWidthInfo( column + i ) ); } return infos; } /** * Get the column widths needed to fill with available ScrollTable width. * * @param info the optional precomputed sizes * * @return the column widths */ private List<ColumnWidthInfo> getFillColumnWidths( TableWidthInfo info ) { if ( !isAttached() ) { return null; } // Precompute some sizes if ( info == null ) { info = new TableWidthInfo( false ); } // Calculate how much room we have to work with int clientWidth = info.availableWidth; if ( clientWidth <= 0 ) { return null; } // Calculate the difference and number of column to resize int diff = 0; int numColumns = 0; { // Calculate the number of columns in each table int numHeaderCols = 0; int numDataCols = 0; int numFooterCols = 0; if ( info.headerTableWidth > 0 ) { numHeaderCols = headerTable.getColumnCount() - getHeaderOffset(); } if ( info.dataTableWidth > 0 ) { numDataCols = dataTable.getColumnCount(); } if ( footerTable != null && info.footerTableWidth > 0 ) { numFooterCols = footerTable.getColumnCount() - getHeaderOffset(); } // Determine the largest table if ( numHeaderCols >= numDataCols && numHeaderCols >= numFooterCols ) { numColumns = numHeaderCols; diff = clientWidth - info.headerTableWidth; } else if ( numFooterCols >= numDataCols && numFooterCols >= numHeaderCols ) { numColumns = numFooterCols; diff = clientWidth - info.footerTableWidth; } else if ( numDataCols > 0 ) { numColumns = numDataCols; diff = clientWidth - info.dataTableWidth; } } if ( numColumns <= 0 ) { return null; } // Calculate the new column widths List<ColumnWidthInfo> colWidthInfos = getColumnWidthInfo( 0, numColumns ); columnResizer.distributeWidth( colWidthInfos, diff ); return colWidthInfos; } /** * Get the offset between the data and header and footer tables. An offset of * one means that the header and footer table indexes are one greater than the * data table indexes, probably because the data table contains a checkbox * column. * * @return the offset */ private int getHeaderOffset() { if ( dataTable.getSelectionPolicy().hasInputColumn() ) { return 1; } return 0; } /** * Returns the new heights of the header, data, and footer tables based on the * {@link ScrollPolicy}. * * @return the new table heights, or null */ private TableHeightInfo getTableWrapperSizes() { // If we aren't attached, return immediately if ( !isAttached() ) { return null; } // Heights only apply with vertical scrolling if ( scrollPolicy == ScrollPolicy.DISABLED || scrollPolicy == ScrollPolicy.HORIZONTAL ) { return null; } // Give the data wrapper all remaining height return new TableHeightInfo(); } /** * Recalculate the ideal columns widths of all inner tables. * * @param command an optional command to execute while recalculating */ private void maybeRecalculateIdealColumnWidths( Command command ) { // Calculations require that we are attached if ( !isAttached() ) { return; } // Check if a recalculation is needed. if ( headerTable.isIdealColumnWidthsCalculated() && dataTable.isIdealColumnWidthsCalculated() && (footerTable == null || footerTable.isIdealColumnWidthsCalculated()) ) { if ( command != null ) { command.execute(); } return; } impl.recalculateIdealColumnWidths( this, command ); } /** * Prepare a table to be added to the {@link AbstractScrollTable}. * * @param table the table to prepare * @param cssName the style name added to the base name */ private void prepareTable( Widget table, String cssName ) { Element tableElem = table.getElement(); DOM.setStyleAttribute( tableElem, "margin", "0px" ); DOM.setStyleAttribute( tableElem, "border", "0px" ); table.addStyleName( cssName ); } /** * Show or hide to fillWidthImage depending on current policies. */ private void updateFillWidthImage() { if ( columnResizePolicy == ColumnResizePolicy.DISABLED || resizePolicy.isFixedWidth() ) { fillWidthImage.setVisible( false ); } else { fillWidthImage.setVisible( true ); } } } |
Commits for litesoft/trunk/Java/GWT/Client/src/com/google/gwt/gen2/table/client/AbstractScrollTable.java
Revision | Author | Commited | Message |
---|---|---|---|
950 Diff | GeorgeS | Thu 19 Jun, 2014 17:57:04 +0000 | New Lines |
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 |
613 Diff | GeorgeS | Thu 15 Mar, 2012 13:38:15 +0000 | Table Fix |
475 Diff | GeorgeS | Sat 03 Sep, 2011 13:54:51 +0000 | |
282 | GeorgeS | Fri 17 Jun, 2011 13:54:39 +0000 |