litesoft
@ 948
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
/* * Copyright 2009 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 com.google.gwt.core.client.*; import com.google.gwt.dom.client.*; import com.google.gwt.user.client.*; import com.google.gwt.user.client.Element; /** * Implementation class that handles common code shared between the * {@link FixedWidthGrid} and {@link FixedWidthFlexTable}. */ class FixedWidthTableImpl { /** * The implementation singleton. */ private static Impl impl; /** * Get the implementation singleton. */ public static Impl get() { if ( impl == null ) { impl = GWT.create( Impl.class ); } return impl; } /** * Information used to calculate the ideal column widths of a table. */ public static class IdealColumnWidthInfo { private HTMLTable table; private TableRowElement tr; private int columnCount; private int offset; public IdealColumnWidthInfo( HTMLTable table, TableRowElement tr, int columnCount, int offset ) { this.table = table; this.tr = tr; this.columnCount = columnCount; this.offset = offset; } } /** * An implementation used accommodate differences in column width * implementations between browsers. */ public static class Impl { /** * Create a cell to use in the ghost row. * * @param td the optional td to modify * * @return the cell */ public Element createGhostCell( Element td ) { if ( td == null ) { td = OverrideDOM.createTD(); } td.getStyle().setPropertyPx( "height", 0 ); td.getStyle().setProperty( "overflow", "hidden" ); td.getStyle().setPropertyPx( "paddingTop", 0 ); td.getStyle().setPropertyPx( "paddingBottom", 0 ); td.getStyle().setPropertyPx( "borderTop", 0 ); td.getStyle().setPropertyPx( "borderBottom", 0 ); td.getStyle().setPropertyPx( "margin", 0 ); return td; } /** * Create the ghost row. * * @return the ghost row element */ public Element createGhostRow() { Element ghostRow = DOM.createTR(); ghostRow.getStyle().setPropertyPx( "margin", 0 ); ghostRow.getStyle().setPropertyPx( "padding", 0 ); ghostRow.getStyle().setPropertyPx( "height", 0 ); ghostRow.getStyle().setProperty( "overflow", "hidden" ); return ghostRow; } /** * Returns a cell in the ghost row. * * @param column the cell's column * * @return the ghost cell */ public Element getGhostCell( Element ghostRow, int column ) { return DOM.getChild( ghostRow, column ); } /** * Recalculate the ideal column widths of each column in the data table. * This method assumes that the tableLayout has already been changed. * * @param info the {@link IdealColumnWidthInfo} * * @return the ideal column widths */ public int[] recalculateIdealColumnWidths( IdealColumnWidthInfo info ) { // Workaround for IE re-entrant bug on window resize if ( info == null ) { return new int[0]; } // We need at least one cell to do any calculations int columnCount = info.columnCount; HTMLTable table = info.table; if ( !table.isAttached() || table.getRowCount() == 0 || columnCount < 1 ) { return new int[0]; } // Determine the width of each column int[] idealWidths = new int[columnCount]; com.google.gwt.dom.client.Element td = info.tr.getFirstChildElement(); for ( int i = 0; i < info.offset; i++ ) { td = td.getNextSiblingElement(); } for ( int i = 0; i < columnCount; i++ ) { idealWidths[i] = td.getClientWidth(); td = td.getNextSiblingElement(); } return idealWidths; } /** * Setup to recalculate column widths. * * @param table the table * @param columnCount the number of columns in the table * @param offset the offset of the first logical column * * @return info used to calculate ideal column widths */ public IdealColumnWidthInfo recalculateIdealColumnWidthsSetup( HTMLTable table, int columnCount, int offset ) { // Switch to normal layout table.getElement().getStyle().setProperty( "tableLayout", "" ); // Add a full row to get ideal widths TableRowElement tr = Document.get().createTRElement(); TableCellElement td = Document.get().createTDElement(); td.setInnerHTML( "<div style=\"height:1px;width:1px;\"></div>" ); for ( int i = 0; i < columnCount + offset; i++ ) { tr.appendChild( td.cloneNode( true ) ); } getTableBody( table ).appendChild( tr ); return new IdealColumnWidthInfo( table, tr, columnCount, offset ); } /** * Tear down after recalculating column widths. * * @param info the {@link IdealColumnWidthInfo} */ public void recalculateIdealColumnWidthsTeardown( IdealColumnWidthInfo info ) { // Workaround for IE re-entrant bug on window resize if ( info == null ) { return; } info.table.getElement().getStyle().setProperty( "tableLayout", "fixed" ); getTableBody( info.table ).removeChild( info.tr ); } /** * Set the width of a column using the ghost row. * * @param table the table containing the column * @param ghostRow the ghost row element * @param column the index of the column * @param width the width in pixels * * @throws IndexOutOfBoundsException */ public void setColumnWidth( HTMLTable table, Element ghostRow, int column, int width ) { getGhostCell( ghostRow, column ).getStyle().setPropertyPx( "width", width ); } /** * Get the body element of an {@link HTMLTable}. * * @param table the table * * @return the body elements */ private native Element getTableBody( HTMLTable table ) /*-{ return table.@com.google.gwt.gen2.table.client.HTMLTable::getBodyElement()(); }-*/; } /** * IE version of the implementation. IE sets the overall column width instead * of the innerWidth, so we need to add the padding and spacing. */ public static class ImplTrident extends Impl { @Override public void setColumnWidth( HTMLTable table, Element ghostRow, int column, int width ) { width += 2 * table.getCellPadding() + table.getCellSpacing(); super.setColumnWidth( table, ghostRow, column, width ); } } /** * IE6 version of the implementation hides the ghost row using the display * attribute. */ public static class ImplIE6 extends ImplTrident { @Override public Element createGhostRow() { Element ghostRow = super.createGhostRow(); ghostRow.getStyle().setProperty( "display", "none" ); return ghostRow; } } /** * IE8 version of the implementation. */ public static class ImplIE8 extends ImplTrident { } /** * Safari version of the implementation. Safari sets the overall column width * instead of the innerWidth, so we need to add the padding and spacing. */ public static class ImplSafari extends Impl { @Override public void setColumnWidth( HTMLTable table, Element ghostRow, int column, int width ) { width += 2 * table.getCellPadding() + table.getCellSpacing(); super.setColumnWidth( table, ghostRow, column, width ); } } /** * Opera version of the implementation refreshes the table display so the new * column size takes effect. Without the display refresh, the column width * doesn't update in the browser. */ public static class ImplOpera extends Impl { @Override public void setColumnWidth( HTMLTable table, Element ghostRow, int column, int width ) { super.setColumnWidth( table, ghostRow, column, width ); Element tableElem = table.getElement(); Element parentElem = DOM.getParent( tableElem ); int scrollLeft = 0; if ( parentElem != null ) { scrollLeft = parentElem.getScrollLeft(); } tableElem.getStyle().setProperty( "display", "none" ); tableElem.getStyle().setProperty( "display", "" ); if ( parentElem != null ) { parentElem.setScrollLeft( scrollLeft ); } } } } |