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.base.HasStyle;
019import com.github.gwtbootstrap.client.ui.base.IsResponsive;
020import com.github.gwtbootstrap.client.ui.base.ResponsiveHelper;
021import com.github.gwtbootstrap.client.ui.base.StyleHelper;
022import com.github.gwtbootstrap.client.ui.constants.Device;
023import com.google.gwt.core.client.GWT;
024import com.google.gwt.resources.client.CssResource.ImportedWithPrefix;
025import com.google.gwt.resources.client.ImageResource;
026import com.google.gwt.user.client.ui.Image;
027import com.google.gwt.user.client.ui.Widget;
028import com.google.gwt.view.client.ProvidesKey;
029
030/**
031 * 
032 * CellTable for Bootstrap style.
033 * 
034 * @since 2.0.4.0
035 * @author ohashi keisuke
036 * 
037 * @param <T>
038 *            Data Set
039 */
040public class CellTable<T> extends
041        com.google.gwt.user.cellview.client.CellTable<T> implements HasStyle,
042        IsResponsive {
043
044    public static enum TableType implements
045            com.github.gwtbootstrap.client.ui.base.Style {
046        BORDERED("table-bordered"), STRIPED("table-striped"), CONDENSED("table-condensed"),
047        HOVER("table-hover");
048
049        private final String styleName;
050
051        private TableType(String styleName) {
052            this.styleName = styleName;
053        }
054
055        @Override
056        public String get() {
057            return styleName;
058        }
059    }
060
061    /**
062     * The default page size.
063     */
064    private static final int DEFAULT_PAGESIZE = 15;
065
066    private static Resources DEFAULT_RESOURCES = null;
067
068    private static Resources getDefaultResources() {
069        if (DEFAULT_RESOURCES == null) {
070            DEFAULT_RESOURCES = GWT.create(Resources.class);
071        }
072        return DEFAULT_RESOURCES;
073    }
074
075    public interface Resources extends
076            com.google.gwt.user.cellview.client.CellTable.Resources {
077
078        @Override
079        @Source(Style.DEFAULT_CSS)
080        public Style cellTableStyle();
081    }
082
083    public interface SelectableResources extends Resources {
084
085        @Override
086        @Source(SelectableStyle.DEFAULT_CSS)
087        public SelectableStyle cellTableStyle();
088    }
089
090    @ImportedWithPrefix("gwt-bootstrap-cellTable")
091    public interface Style extends
092            com.google.gwt.user.cellview.client.CellTable.Style {
093
094        String DEFAULT_CSS = "com/github/gwtbootstrap/client/ui/GwtBootstrapCellTable.css";
095    }
096
097    @ImportedWithPrefix("gwt-bootstrap-cellTable")
098    public interface SelectableStyle extends Style {
099
100        String DEFAULT_CSS = "com/github/gwtbootstrap/client/ui/GwtBootstrapCellTableSelectable.css";
101    }
102
103    public CellTable() {
104        this(DEFAULT_PAGESIZE);
105    }
106
107    public CellTable(int pageSize, Resources resources,
108            ProvidesKey<T> keyProvider, Widget loadingIndicator) {
109        super(pageSize, resources, keyProvider, loadingIndicator);
110        setStyleName("table");
111    }
112
113    public CellTable(int pageSize,
114            Resources resources,
115            ProvidesKey<T> keyProvider) {
116        this(pageSize, resources, keyProvider, createDefaultLoadingIndicator(getDefaultResources()));
117    }
118
119    public CellTable(int pageSize, Resources resources) {
120        this(pageSize, resources , null);
121    }
122
123    public CellTable(int pageSize, ProvidesKey<T> keyProvider) {
124        this(pageSize, keyProvider, createDefaultLoadingIndicator(getDefaultResources()));
125    }
126
127    public CellTable(int pageSize, ProvidesKey<T> keyProvider, Widget loadingIndicator) {
128        this(pageSize, getDefaultResources(), keyProvider, loadingIndicator);
129    }
130
131    public CellTable(int pageSize) {
132        this(pageSize, getDefaultResources());
133    }
134
135    public CellTable(ProvidesKey<T> keyProvider) {
136        this(DEFAULT_PAGESIZE, keyProvider);
137    }
138
139    public void setStriped(boolean striped) {
140        if (striped) {
141            StyleHelper.addStyle(this, TableType.STRIPED);
142        } else {
143            StyleHelper.removeStyle(this, TableType.STRIPED);
144        }
145    }
146
147    public void setBordered(boolean bordered) {
148        if (bordered) {
149            StyleHelper.addStyle(this, TableType.BORDERED);
150        } else {
151            StyleHelper.removeStyle(this, TableType.BORDERED);
152        }
153    }
154
155    public void setCondensed(boolean condensed) {
156        if (condensed) {
157            StyleHelper.addStyle(this, TableType.CONDENSED);
158        } else {
159            StyleHelper.removeStyle(this, TableType.CONDENSED);
160        }
161    }
162
163    public void setHover(boolean hover) {
164        if (hover) {
165            StyleHelper.addStyle(this, TableType.HOVER);
166        } else {
167            StyleHelper.removeStyle(this, TableType.HOVER);
168        }
169    }
170
171    /**
172     * Create the default loading indicator using the loading image in the
173     * specified {@link Resources}.
174     * 
175     * @param resources
176     *            the resources
177     * @return a widget loading indicator
178     */
179    private static Widget createDefaultLoadingIndicator(Resources resources) {
180        ImageResource loadingImg = resources.cellTableLoading();
181        return (loadingImg == null) ? null : new Image(loadingImg);
182    }
183
184    /**
185     * {@inheritDoc}
186     */
187    @Override
188    public void setShowOn(Device device) {
189        ResponsiveHelper.setShowOn(this, device);
190    }
191
192    /**
193     * {@inheritDoc}
194     */
195    @Override
196    public void setHideOn(Device device) {
197        ResponsiveHelper.setHideOn(this, device);
198
199    }
200
201    /**
202     * {@inheritDoc}
203     */
204    @Override
205    public void setStyle(com.github.gwtbootstrap.client.ui.base.Style style) {
206        StyleHelper.setStyle(this, style);
207
208    }
209
210    /**
211     * {@inheritDoc}
212     */
213    @Override
214    public void addStyle(com.github.gwtbootstrap.client.ui.base.Style style) {
215        StyleHelper.addStyle(this, style);
216
217    }
218
219    /**
220     * {@inheritDoc}
221     */
222    @Override
223    public void removeStyle(com.github.gwtbootstrap.client.ui.base.Style style) {
224        StyleHelper.removeStyle(this, style);
225
226    }
227}