001package org.gwtbootstrap3.client.ui.gwt; 002 003/* 004 * #%L 005 * GwtBootstrap3 006 * %% 007 * Copyright (C) 2013 - 2014 GwtBootstrap3 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import org.gwtbootstrap3.client.ui.base.HasResponsiveness; 024import org.gwtbootstrap3.client.ui.base.helper.StyleHelper; 025import org.gwtbootstrap3.client.ui.constants.DeviceSize; 026import org.gwtbootstrap3.client.ui.constants.TableType; 027 028import com.google.gwt.core.client.GWT; 029import com.google.gwt.resources.client.ImageResource; 030import com.google.gwt.user.client.ui.Widget; 031import com.google.gwt.view.client.ProvidesKey; 032 033/** 034 * @author Joshua Godi 035 */ 036public class CellTable<T> extends com.google.gwt.user.cellview.client.CellTable<T> implements HasResponsiveness { 037 private static final int DEFAULT_PAGESIZE = 15; 038 private static Resources DEFAULT_RESOURCES; 039 040 /** 041 * Constructs a table with a default page size of 15. 042 */ 043 public CellTable() { 044 this(DEFAULT_PAGESIZE); 045 } 046 047 /** 048 * Constructs a table with the given page size. 049 * 050 * @param pageSize the page size 051 */ 052 public CellTable(final int pageSize) { 053 this(pageSize, getDefaultResources()); 054 } 055 056 /** 057 * Constructs a table with a default page size of 15, and the given 058 * {@link ProvidesKey key provider}. 059 * 060 * @param keyProvider an instance of ProvidesKey, or null if the record 061 * object should act as its own key 062 */ 063 public CellTable(final ProvidesKey<T> keyProvider) { 064 this(DEFAULT_PAGESIZE, keyProvider); 065 } 066 067 /** 068 * Constructs a table with the given page size with the specified 069 * {@link Resources}. 070 * 071 * @param pageSize the page size 072 * @param resources the resources to use for this widget 073 */ 074 public CellTable(final int pageSize, final Resources resources) { 075 super(pageSize, resources, null); 076 } 077 078 /** 079 * Constructs a table with the given page size and the given 080 * {@link ProvidesKey key provider}. 081 * 082 * @param pageSize the page size 083 * @param keyProvider an instance of ProvidesKey, or null if the record 084 * object should act as its own key 085 */ 086 public CellTable(final int pageSize, final ProvidesKey<T> keyProvider) { 087 super(pageSize, getDefaultResources(), keyProvider); 088 } 089 090 /** 091 * Constructs a table with the specified page size, {@link Resources}, key 092 * provider, and loading indicator. 093 * 094 * @param pageSize the page size 095 * @param resources the resources to use for this widget 096 * @param keyProvider an instance of ProvidesKey, or null if the record 097 * object should act as its own key 098 * @param loadingIndicator the widget to use as a loading indicator, or null 099 * to disable 100 */ 101 public CellTable(final int pageSize, final Resources resources, final ProvidesKey<T> keyProvider, final Widget loadingIndicator) { 102 this(pageSize, resources, keyProvider, loadingIndicator, true, true); 103 } 104 105 /** 106 * Constructs a table with the specified page size, {@link Resources}, key 107 * provider, and loading indicator. 108 * 109 * @param pageSize the page size 110 * @param resources the resources to use for this widget 111 * @param keyProvider an instance of ProvidesKey, or null if the record 112 * object should act as its own key 113 * @param loadingIndicator the widget to use as a loading indicator, or null 114 * to disable 115 * @param enableColGroup enable colgroup element. This is used when the table is using fixed 116 * layout and when column style is added. Ignoring this element will boost rendering 117 * performance. Note that when colgroup is disabled, {@link #setColumnWidth} 118 * @param attachLoadingPanel attaching the table section that contains the empty table widget and 119 * the loading indicator. Attaching this to the table significantly improve the rendering 120 * performance in webkit based browsers but also introduces significantly larger latency 121 * in IE. If the panel is not attached to the table, it won't be displayed. But the user 122 * can call {@link #getTableLoadingSection} and attach it to other elements outside the 123 * table element 124 */ 125 public CellTable(final int pageSize, final Resources resources, final ProvidesKey<T> keyProvider, 126 final Widget loadingIndicator, final boolean enableColGroup, final boolean attachLoadingPanel) { 127 super(pageSize, resources, keyProvider, loadingIndicator, enableColGroup, attachLoadingPanel); 128 StyleHelper.addEnumStyleName(this, TableType.DEFAULT); 129 } 130 131 private static Resources getDefaultResources() { 132 if (DEFAULT_RESOURCES == null) { 133 final CellTable.Resources cellTableResources = GWT.create(CellTable.Resources.class); 134 DEFAULT_RESOURCES = new ResourcesAdapter(cellTableResources); 135 } 136 return DEFAULT_RESOURCES; 137 } 138 139 @Override 140 public void setVisibleOn(final DeviceSize deviceSize) { 141 StyleHelper.setVisibleOn(this, deviceSize); 142 } 143 144 @Override 145 public void setHiddenOn(final DeviceSize deviceSize) { 146 StyleHelper.setHiddenOn(this, deviceSize); 147 } 148 149 public void setStriped(final boolean striped) { 150 if (striped) { 151 StyleHelper.addEnumStyleName(this, TableType.STRIPED); 152 } else { 153 StyleHelper.removeEnumStyleName(this, TableType.STRIPED); 154 } 155 } 156 157 public void setBordered(final boolean bordered) { 158 if (bordered) { 159 StyleHelper.addEnumStyleName(this, TableType.BORDERED); 160 } else { 161 StyleHelper.removeEnumStyleName(this, TableType.BORDERED); 162 } 163 } 164 165 public void setCondensed(final boolean condensed) { 166 if (condensed) { 167 StyleHelper.addEnumStyleName(this, TableType.CONDENSED); 168 } else { 169 StyleHelper.removeEnumStyleName(this, TableType.CONDENSED); 170 } 171 } 172 173 public void setHover(final boolean hover) { 174 if (hover) { 175 StyleHelper.addEnumStyleName(this, TableType.HOVER); 176 } else { 177 StyleHelper.removeEnumStyleName(this, TableType.HOVER); 178 } 179 } 180 181 /** 182 * Resources/Styles to remove the GWT styling of the tables! 183 */ 184 private static class ResourcesAdapter implements CellTable.Resources { 185 private final CellTable.Resources resources; 186 private final StyleAdapter style; 187 188 public ResourcesAdapter(final CellTable.Resources resources) { 189 this.resources = resources; 190 this.style = new StyleAdapter(); 191 } 192 193 @Override 194 public ImageResource cellTableFooterBackground() { 195 return resources.cellTableFooterBackground(); 196 } 197 198 @Override 199 public ImageResource cellTableHeaderBackground() { 200 return resources.cellTableHeaderBackground(); 201 } 202 203 @Override 204 public ImageResource cellTableLoading() { 205 return resources.cellTableLoading(); 206 } 207 208 @Override 209 public ImageResource cellTableSelectedBackground() { 210 return resources.cellTableSelectedBackground(); 211 } 212 213 @Override 214 public ImageResource cellTableSortAscending() { 215 return resources.cellTableSortAscending(); 216 } 217 218 @Override 219 public ImageResource cellTableSortDescending() { 220 return resources.cellTableSortDescending(); 221 } 222 223 @Override 224 public Style cellTableStyle() { 225 return style; 226 } 227 } 228 229 private static class StyleAdapter implements CellTable.Style { 230 private static final String B = "gwtb3-"; 231 private static final String DUMMY = B + "d"; 232 233 @Override 234 public boolean ensureInjected() { 235 return true; 236 } 237 238 @Override 239 public String cellTableCell() { 240 return B + "cell"; 241 } 242 243 @Override 244 public String cellTableEvenRow() { 245 return "even"; 246 } 247 248 @Override 249 public String cellTableEvenRowCell() { 250 return DUMMY; 251 } 252 253 @Override 254 public String cellTableFirstColumn() { 255 return DUMMY; // Bootstrap3 uses "smart selectors" 256 } 257 258 @Override 259 public String cellTableFirstColumnFooter() { 260 return DUMMY; 261 } 262 263 @Override 264 public String cellTableFirstColumnHeader() { 265 return DUMMY; 266 } 267 268 @Override 269 public String cellTableFooter() { 270 return DUMMY; 271 } 272 273 @Override 274 public String cellTableHeader() { 275 return DUMMY; 276 } 277 278 @Override 279 public String cellTableHoveredRow() { 280 return "active"; 281 } 282 283 @Override 284 public String cellTableHoveredRowCell() { 285 return "active"; 286 } 287 288 @Override 289 public String cellTableKeyboardSelectedCell() { 290 return DUMMY; 291 } 292 293 @Override 294 public String cellTableKeyboardSelectedRow() { 295 return DUMMY; 296 } 297 298 @Override 299 public String cellTableKeyboardSelectedRowCell() { 300 return DUMMY; 301 } 302 303 @Override 304 public String cellTableLastColumn() { 305 return DUMMY; 306 } 307 308 @Override 309 public String cellTableLastColumnFooter() { 310 return DUMMY; 311 } 312 313 @Override 314 public String cellTableLastColumnHeader() { 315 return DUMMY; 316 } 317 318 @Override 319 public String cellTableLoading() { 320 return DUMMY; 321 } 322 323 @Override 324 public String cellTableOddRow() { 325 return "odd"; 326 } 327 328 @Override 329 public String cellTableOddRowCell() { 330 return DUMMY; 331 } 332 333 @Override 334 public String cellTableSelectedRow() { 335 return "info"; 336 } 337 338 @Override 339 public String cellTableSelectedRowCell() { 340 return DUMMY; 341 } 342 343 @Override 344 public String cellTableSortableHeader() { 345 return DUMMY; 346 } 347 348 @Override 349 public String cellTableSortedHeaderAscending() { 350 return DUMMY; 351 } 352 353 @Override 354 public String cellTableSortedHeaderDescending() { 355 return DUMMY; 356 } 357 358 @Override 359 public String cellTableWidget() { 360 return "table"; 361 } 362 363 @Override 364 public String getText() { 365 return B; 366 } 367 368 @Override 369 public String getName() { 370 return B; 371 } 372 } 373}