001/*
002 *  Copyright 2012 GWT-Bootstrap
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.github.gwtbootstrap.client.ui;
017
018import com.github.gwtbootstrap.client.ui.CellTable.TableType;
019import com.github.gwtbootstrap.client.ui.base.HasStyle;
020import com.github.gwtbootstrap.client.ui.base.IsResponsive;
021import com.github.gwtbootstrap.client.ui.base.ResponsiveHelper;
022import com.github.gwtbootstrap.client.ui.base.StyleHelper;
023import com.github.gwtbootstrap.client.ui.constants.Device;
024import com.google.gwt.core.client.GWT;
025import com.google.gwt.dom.client.Style.Unit;
026import com.google.gwt.resources.client.CssResource.ImportedWithPrefix;
027import com.google.gwt.resources.client.ImageResource;
028import com.google.gwt.user.client.ui.Image;
029import com.google.gwt.user.client.ui.Widget;
030import com.google.gwt.view.client.ProvidesKey;
031
032/**
033 * The Bootstrap style DataGrid
034 * @since 2.0.4.0
035 * @author ohashi keisuke
036 * @param <T>  the data type of each row
037 */
038public class DataGrid<T> extends
039        com.google.gwt.user.cellview.client.DataGrid<T> implements HasStyle, IsResponsive {
040    
041    
042    /**
043     * Basic GWT-Bootstrap style Table Resource.
044     * @author ohashi keisuke
045     */
046    public interface Resources extends com.google.gwt.user.cellview.client.DataGrid.Resources {
047
048        @Override
049        @Source(Style.DEFAULT_CSS)
050        Style dataGridStyle();
051    }
052    
053    /**
054     * Selectable GWT-Bootstrap style Table Resource.
055     * <p>
056     * If select row,apply style.
057     * It focus to Basic TwBootstrap style.
058     * If you use custom style,you should override {@ Resources} like this.
059     * </p>
060     * @author ohashi keisuke
061     *
062     */
063    public interface SelectableResources extends Resources {
064
065        @Override
066        @Source(SelectableStyle.DEFAULT_CSS)
067        Style dataGridStyle();
068    }
069    
070    @ImportedWithPrefix("gwt-bootstrap-dataGrid")
071    public interface Style extends com.google.gwt.user.cellview.client.DataGrid.Style {
072
073        String DEFAULT_CSS = "com/github/gwtbootstrap/client/ui/GwtBootstrapDataGrid.css";
074    }
075    
076    @ImportedWithPrefix("gwt-bootstrap-dataGrid")
077    public interface SelectableStyle extends com.google.gwt.user.cellview.client.DataGrid.Style {
078
079        String DEFAULT_CSS = "com/github/gwtbootstrap/client/ui/GwtBootstrapDataGridSelectable.css";
080    }
081    
082    private static final int DEFAULT_PAGESIZE = 50;
083    private static Resources DEFAULT_RESOURCES;
084
085    /**
086     * Create the default loading indicator using the loading image in the
087     * specified {@link Resources}.
088     * 
089     * @param resources the resources containing the loading image
090     * @return a widget loading indicator
091     */
092    private static Widget createDefaultLoadingIndicator(Resources resources) {
093      ImageResource loadingImg = resources.dataGridLoading();
094      if (loadingImg == null) {
095        return null;
096      }
097      Image image = new Image(loadingImg);
098      image.getElement().getStyle().setMarginTop(30.0, Unit.PX);
099      return image;
100    }
101    private static Resources getDefaultResources() {
102        if (DEFAULT_RESOURCES == null) {
103          DEFAULT_RESOURCES = GWT.create(Resources.class);
104        }
105        return DEFAULT_RESOURCES;
106      }
107    /**
108     * Constructs a table with a default page size of 50.
109     */
110    public DataGrid() {
111      this(DEFAULT_PAGESIZE);
112    }
113
114    /**
115     * Constructs a table with the given page size.
116     * 
117     * @param pageSize the page size
118     */
119    public DataGrid(final int pageSize) {
120      this(pageSize, getDefaultResources());
121    }
122
123    /**
124     * Constructs a table with the given page size and the given
125     * {@link ProvidesKey key provider}.
126     * 
127     * @param pageSize the page size
128     * @param keyProvider an instance of ProvidesKey<T>, or null if the record
129     *          object should act as its own key
130     */
131    public DataGrid(int pageSize, ProvidesKey<T> keyProvider) {
132      this(pageSize, getDefaultResources(), keyProvider);
133    }
134
135    /**
136     * Constructs a table with the given page size with the specified
137     * {@link Resources}.
138     * 
139     * @param pageSize the page size
140     * @param resources the resources to use for this widget
141     */
142    public DataGrid(int pageSize, Resources resources) {
143      this(pageSize, resources, null);
144    }
145
146    /**
147     * Constructs a table with the given page size, the specified
148     * {@link Resources}, and the given key provider.
149     * 
150     * @param pageSize the page size
151     * @param resources the resources to use for this widget
152     * @param keyProvider an instance of ProvidesKey<T>, or null if the record
153     *          object should act as its own key
154     */
155    public DataGrid(int pageSize, Resources resources, ProvidesKey<T> keyProvider) {
156      this(pageSize, resources, keyProvider, createDefaultLoadingIndicator(resources));
157    }
158
159    /**
160     * Constructs a table with the given page size, the specified
161     * {@link Resources}, and the given key provider.
162     * 
163     * @param pageSize the page size
164     * @param resources the resources to use for this widget
165     * @param keyProvider an instance of ProvidesKey<T>, or null if the record
166     *          object should act as its own key
167     * @param loadingIndicator the widget to use as a loading indicator, or null
168     *          to disable
169     */
170    public DataGrid(int pageSize, Resources resources, ProvidesKey<T> keyProvider,
171        Widget loadingIndicator) {
172        super(pageSize, resources, keyProvider, loadingIndicator);
173    }
174    
175    /**
176     * set Striped style
177     * @param striped true:set false:remove
178     */
179    public void setStriped(boolean striped) {
180        applyTableStyle(striped, TableType.STRIPED);
181    }
182    
183    /**
184     * set Bordered style
185     * @param bordered true:set false:remove
186     */
187    public void setBordered(boolean bordered) {
188        applyTableStyle(bordered, TableType.BORDERED);
189    }
190
191    /**
192     * set Condensed style
193     * @param condensed true:set false:remove
194     */
195    public void setCondensed(boolean condensed) {
196        applyTableStyle(condensed, TableType.CONDENSED);
197    }
198
199    /**
200     * set Hover style
201     * @param hover true:set false:remove
202     */
203    public void setHover(boolean hover) {
204        applyTableStyle(hover, TableType.HOVER);
205    }
206
207    private void applyTableStyle(boolean striped, TableType type) {
208        if(striped) {
209            getTableHeadElement().getParentElement().addClassName(type.get());
210            getTableBodyElement().getParentElement().addClassName(type.get());
211            getTableFootElement().getParentElement().addClassName(type.get());
212        } else {
213            getTableHeadElement().getParentElement().removeClassName(type.get());
214            getTableBodyElement().getParentElement().removeClassName(type.get());
215            getTableFootElement().getParentElement().removeClassName(type.get());
216        }
217    }
218    
219    /**
220     * {@inheritDoc}
221     */
222    @Override
223    public void setShowOn(Device device) {
224        ResponsiveHelper.setShowOn(this, device);
225    }
226
227    /**
228     * {@inheritDoc}
229     */
230    @Override
231    public void setHideOn(Device device) {
232        ResponsiveHelper.setHideOn(this, device);
233        
234    }
235
236    /**
237     * {@inheritDoc}
238     */
239    @Override
240    public void setStyle(com.github.gwtbootstrap.client.ui.base.Style style) {
241        StyleHelper.setStyle(this, style);
242        
243    }
244
245    /**
246     * {@inheritDoc}
247     */
248    @Override
249    public void addStyle(com.github.gwtbootstrap.client.ui.base.Style style) {
250        StyleHelper.addStyle(this, style);
251        
252    }
253
254    /**
255     * {@inheritDoc}
256     */
257    @Override
258    public void removeStyle(com.github.gwtbootstrap.client.ui.base.Style style) {
259        StyleHelper.removeStyle(this, style);
260        
261    }
262}