001 package 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
023 import org.gwtbootstrap3.client.ui.base.HasResponsiveness;
024 import org.gwtbootstrap3.client.ui.base.helper.StyleHelper;
025 import org.gwtbootstrap3.client.ui.constants.DeviceSize;
026 import org.gwtbootstrap3.client.ui.constants.TableType;
027
028 import com.google.gwt.core.client.GWT;
029 import com.google.gwt.resources.client.ImageResource;
030 import com.google.gwt.user.client.ui.Widget;
031 import com.google.gwt.view.client.ProvidesKey;
032
033 /**
034 * @author Joshua Godi
035 */
036 public class DataGrid<T> extends com.google.gwt.user.cellview.client.DataGrid<T> implements HasResponsiveness {
037 private static final int DEFAULT_PAGESIZE = 50;
038 private static Resources DEFAULT_RESOURCES;
039
040 /**
041 * Constructs a table with a default page size of 50.
042 */
043 public DataGrid() {
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 DataGrid(final int pageSize) {
053 this(pageSize, getDefaultResources());
054 }
055
056 /**
057 * Constructs a table with the given page size and the given
058 * {@link ProvidesKey key provider}.
059 *
060 * @param pageSize the page size
061 * @param keyProvider an instance of ProvidesKey, or null if the record
062 * object should act as its own key
063 */
064 public DataGrid(final int pageSize, final ProvidesKey<T> keyProvider) {
065 super(pageSize, getDefaultResources(), keyProvider);
066 getTableBodyElement().getParentElement().addClassName(TableType.DEFAULT.getCssName());
067 }
068
069 /**
070 * Constructs a table with the given page size with the specified
071 * {@link Resources}.
072 *
073 * @param pageSize the page size
074 * @param resources the resources to use for this widget
075 */
076 public DataGrid(final int pageSize, final DataGrid.Resources resources) {
077 super(pageSize, resources, null);
078 getTableBodyElement().getParentElement().addClassName(TableType.DEFAULT.getCssName());
079 }
080
081 /**
082 * Constructs a table with the given page size, the specified
083 * {@link Resources}, and the given key provider.
084 *
085 * @param pageSize the page size
086 * @param resources the resources to use for this widget
087 * @param keyProvider an instance of ProvidesKey, or null if the record
088 * object should act as its own key
089 * @param loadingIndicator the widget to use as a loading indicator, or null
090 * to disable
091 */
092 public DataGrid(final int pageSize, final Resources resources, final ProvidesKey<T> keyProvider, final Widget loadingIndicator) {
093 super(pageSize, resources, keyProvider, loadingIndicator);
094 getTableBodyElement().getParentElement().addClassName(TableType.DEFAULT.getCssName());
095 }
096
097 /**
098 * Constructs a table with a default page size of 50, and the given
099 * {@link ProvidesKey key provider}.
100 *
101 * @param keyProvider an instance of ProvidesKey, or null if the record
102 * object should act as its own key
103 */
104 public DataGrid(final ProvidesKey<T> keyProvider) {
105 this(DEFAULT_PAGESIZE, keyProvider);
106 }
107
108 private static DataGrid.Resources getDefaultResources() {
109 if (DEFAULT_RESOURCES == null) {
110 final DataGrid.Resources dataGridResources = GWT.create(Resources.class);
111 DEFAULT_RESOURCES = new ResourcesAdapter(dataGridResources);
112 }
113 return DEFAULT_RESOURCES;
114 }
115
116 @Override
117 public void setVisibleOn(final DeviceSize deviceSize) {
118 StyleHelper.setVisibleOn(this, deviceSize);
119 }
120
121 @Override
122 public void setHiddenOn(final DeviceSize deviceSize) {
123 StyleHelper.setHiddenOn(this, deviceSize);
124 }
125
126 public void setStriped(final boolean striped) {
127 if (striped) {
128 addToDataGrid(TableType.STRIPED);
129 } else {
130 removeFromDataGrid(TableType.STRIPED);
131 }
132 }
133
134 public void setBordered(final boolean bordered) {
135 if (bordered) {
136 addToDataGrid(TableType.BORDERED);
137 } else {
138 removeFromDataGrid(TableType.BORDERED);
139 }
140 }
141
142 public void setCondensed(final boolean condensed) {
143 if (condensed) {
144 addToDataGrid(TableType.CONDENSED);
145 } else {
146 removeFromDataGrid(TableType.CONDENSED);
147 }
148 }
149
150 public void setHover(final boolean hover) {
151 if (hover) {
152 addToDataGrid(TableType.HOVER);
153 } else {
154 removeFromDataGrid(TableType.HOVER);
155 }
156 }
157
158 private void addToDataGrid(final TableType tableType) {
159 getTableHeadElement().getParentElement().addClassName(tableType.getCssName());
160 getTableBodyElement().getParentElement().addClassName(tableType.getCssName());
161 getTableFootElement().getParentElement().addClassName(tableType.getCssName());
162 }
163
164 private void removeFromDataGrid(final TableType tableType) {
165 getTableHeadElement().getParentElement().removeClassName(tableType.getCssName());
166 getTableBodyElement().getParentElement().removeClassName(tableType.getCssName());
167 getTableFootElement().getParentElement().removeClassName(tableType.getCssName());
168 }
169
170 /**
171 * Resources/Styles to remove the GWT styling of the tables!
172 */
173 private static class ResourcesAdapter implements DataGrid.Resources {
174 private final DataGrid.Resources resources;
175 private final StyleAdapter style;
176
177 public ResourcesAdapter(final DataGrid.Resources resources) {
178 this.resources = resources;
179 this.style = new StyleAdapter();
180 }
181
182 @Override
183 public ImageResource dataGridLoading() {
184 return resources.dataGridLoading();
185 }
186
187 @Override
188 public ImageResource dataGridSortAscending() {
189 return resources.dataGridSortAscending();
190 }
191
192 @Override
193 public ImageResource dataGridSortDescending() {
194 return resources.dataGridSortDescending();
195 }
196
197 @Override
198 public DataGrid.Style dataGridStyle() {
199 return style;
200 }
201 }
202
203 private static class StyleAdapter implements DataGrid.Style {
204 private static final String B = "gwtb3-";
205 private static final String DUMMY = B + "d";
206
207 @Override
208 public boolean ensureInjected() {
209 return true;
210 }
211
212 @Override
213 public String getText() {
214 return B;
215 }
216
217 @Override
218 public String getName() {
219 return B;
220 }
221
222 @Override
223 public String dataGridCell() {
224 return B + "cell";
225 }
226
227 @Override
228 public String dataGridEvenRow() {
229 return "even";
230 }
231
232 @Override
233 public String dataGridEvenRowCell() {
234 return DUMMY;
235 }
236
237 @Override
238 public String dataGridFirstColumn() {
239 return DUMMY; // Bootstrap3 uses "smart selectors"
240 }
241
242 @Override
243 public String dataGridFirstColumnFooter() {
244 return DUMMY;
245 }
246
247 @Override
248 public String dataGridFirstColumnHeader() {
249 return DUMMY;
250 }
251
252 @Override
253 public String dataGridFooter() {
254 return DUMMY;
255 }
256
257 @Override
258 public String dataGridHeader() {
259 return DUMMY;
260 }
261
262 @Override
263 public String dataGridHoveredRow() {
264 return "active";
265 }
266
267 @Override
268 public String dataGridHoveredRowCell() {
269 return "active";
270 }
271
272 @Override
273 public String dataGridKeyboardSelectedCell() {
274 return DUMMY;
275 }
276
277 @Override
278 public String dataGridKeyboardSelectedRow() {
279 return DUMMY;
280 }
281
282 @Override
283 public String dataGridKeyboardSelectedRowCell() {
284 return DUMMY;
285 }
286
287 @Override
288 public String dataGridLastColumn() {
289 return DUMMY;
290 }
291
292 @Override
293 public String dataGridLastColumnFooter() {
294 return DUMMY;
295 }
296
297 @Override
298 public String dataGridLastColumnHeader() {
299 return DUMMY;
300 }
301
302 @Override
303 public String dataGridOddRow() {
304 return "odd";
305 }
306
307 @Override
308 public String dataGridOddRowCell() {
309 return DUMMY;
310 }
311
312 @Override
313 public String dataGridSelectedRow() {
314 return "info";
315 }
316
317 @Override
318 public String dataGridSelectedRowCell() {
319 return DUMMY;
320 }
321
322 @Override
323 public String dataGridSortableHeader() {
324 return DUMMY;
325 }
326
327 @Override
328 public String dataGridSortedHeaderAscending() {
329 return DUMMY;
330 }
331
332 @Override
333 public String dataGridSortedHeaderDescending() {
334 return DUMMY;
335 }
336
337 @Override
338 public String dataGridWidget() {
339 return "table";
340 }
341 }
342 }