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     */
016    package com.github.gwtbootstrap.client.ui;
017    
018    import com.github.gwtbootstrap.client.ui.base.HasStyle;
019    import com.github.gwtbootstrap.client.ui.base.IsResponsive;
020    import com.github.gwtbootstrap.client.ui.base.ResponsiveHelper;
021    import com.github.gwtbootstrap.client.ui.base.StyleHelper;
022    import com.github.gwtbootstrap.client.ui.constants.Device;
023    import com.google.gwt.core.client.GWT;
024    import com.google.gwt.resources.client.CssResource.ImportedWithPrefix;
025    import com.google.gwt.resources.client.ImageResource;
026    import com.google.gwt.user.client.ui.Image;
027    import com.google.gwt.user.client.ui.Widget;
028    import 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     */
040    public 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(
047                    "table-condensed");
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 Style 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
099                com.google.gwt.user.cellview.client.CellTable.Style {
100    
101            String DEFAULT_CSS = "com/github/gwtbootstrap/client/ui/GwtBootstrapCellTableSelectable.css";
102        }
103    
104        public CellTable() {
105            this(DEFAULT_PAGESIZE);
106        }
107    
108        public CellTable(int pageSize, Resources resources,
109                ProvidesKey<T> keyProvider, Widget loadingIndicator) {
110            super(pageSize, resources, keyProvider, loadingIndicator);
111            setStyleName("table");
112        }
113    
114        public CellTable(int pageSize,
115                Resources resources,
116                ProvidesKey<T> keyProvider) {
117            this(pageSize, resources, keyProvider, createDefaultLoadingIndicator(getDefaultResources()));
118        }
119    
120        public CellTable(int pageSize, Resources resources) {
121            this(pageSize, resources , null);
122        }
123    
124        public CellTable(int pageSize, ProvidesKey<T> keyProvider) {
125            this(pageSize, keyProvider, createDefaultLoadingIndicator(getDefaultResources()));
126        }
127    
128        public CellTable(int pageSize, ProvidesKey<T> keyProvider, Widget loadingIndicator) {
129            this(pageSize, getDefaultResources(), keyProvider, loadingIndicator);
130        }
131    
132        public CellTable(int pageSize) {
133            this(pageSize, getDefaultResources());
134        }
135    
136        public CellTable(ProvidesKey<T> keyProvider) {
137            this(DEFAULT_PAGESIZE, keyProvider);
138        }
139    
140        public void setStriped(boolean striped) {
141            if (striped) {
142                StyleHelper.addStyle(this, TableType.STRIPED);
143            } else {
144                StyleHelper.removeStyle(this, TableType.STRIPED);
145            }
146        }
147    
148        public void setBordered(boolean bordered) {
149            if (bordered) {
150                StyleHelper.addStyle(this, TableType.BORDERED);
151            } else {
152                StyleHelper.removeStyle(this, TableType.BORDERED);
153            }
154        }
155    
156        public void setCondensed(boolean condensed) {
157            if (condensed) {
158                StyleHelper.addStyle(this, TableType.CONDENSED);
159            } else {
160                StyleHelper.removeStyle(this, TableType.CONDENSED);
161            }
162        }
163    
164        /**
165         * Create the default loading indicator using the loading image in the
166         * specified {@link Resources}.
167         * 
168         * @param resources
169         *            the resources
170         * @return a widget loading indicator
171         */
172        private static Widget createDefaultLoadingIndicator(Resources resources) {
173            ImageResource loadingImg = resources.cellTableLoading();
174            return (loadingImg == null) ? null : new Image(loadingImg);
175        }
176    
177        /**
178         * {@inheritDoc}
179         */
180        @Override
181        public void setShowOn(Device device) {
182            ResponsiveHelper.setShowOn(this, device);
183        }
184    
185        /**
186         * {@inheritDoc}
187         */
188        @Override
189        public void setHideOn(Device device) {
190            ResponsiveHelper.setHideOn(this, device);
191    
192        }
193    
194        /**
195         * {@inheritDoc}
196         */
197        @Override
198        public void setStyle(com.github.gwtbootstrap.client.ui.base.Style style) {
199            StyleHelper.setStyle(this, style);
200    
201        }
202    
203        /**
204         * {@inheritDoc}
205         */
206        @Override
207        public void addStyle(com.github.gwtbootstrap.client.ui.base.Style style) {
208            StyleHelper.addStyle(this, style);
209    
210        }
211    
212        /**
213         * {@inheritDoc}
214         */
215        @Override
216        public void removeStyle(com.github.gwtbootstrap.client.ui.base.Style style) {
217            StyleHelper.removeStyle(this, style);
218    
219        }
220    }