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.DivWidget;
019    import com.github.gwtbootstrap.client.ui.base.HasSize;
020    import com.github.gwtbootstrap.client.ui.base.SizeHelper;
021    import com.github.gwtbootstrap.client.ui.config.Configurator;
022    import com.github.gwtbootstrap.client.ui.constants.Constants;
023    import com.google.gwt.uibinder.client.UiConstructor;
024    import com.google.gwt.user.client.ui.Widget;
025    
026    //@formatter:off
027    /**
028     * Column of the Bootstrap grid system.
029     * <p>
030     * It has to be added to a {@link Row} or a {@link FluidRow}.
031     * </p>
032     * 
033     * <p>
034     * <h3>UiBinder Usage:</h3>
035     * 
036     * <pre>
037     * {@code 
038     * <b:Column size="3">
039     *     <b:Well />
040     * </b:Column>}
041     * </pre>
042     * Setting the <code>size</code> is mandatory.
043     * </p>
044     * 
045     * @since 2.0.4.0
046     * 
047     * @author Carlos Alexandro Becker
048     * @author Dominik Mayer
049     * 
050     * @see <a href="http://twitter.github.com/bootstrap/scaffolding.html#gridSystem">Bootstrap documentation</a>
051     * @see Configurator#hasResponsiveDesign()
052     */
053    //@formatter:on
054    public class Column extends DivWidget implements HasSize {
055    
056            private static final String OFFSET_ERROR_MESSAGE =
057                            "The offset of the Column has to be between "
058                                            + Constants.MINIMUM_OFFSET + " and "
059                                            + Constants.MAXIMUM_OFFSET + "!";
060    
061            /**
062             * Creates a new Column of given size.
063             * 
064             * @param size
065             *            the size of the Column in the Bootstrap grid system
066             */
067            @UiConstructor
068            public Column(int size) {
069                    super();
070                    setSize(size);
071            }
072    
073            /**
074             * Creates a new Column of given size and with given offset.
075             * 
076             * @param size
077             *            the size of the Column in the Bootstrap grid system
078             * @param offset
079             *            the offset from the left side
080             */
081            public Column(int size, int offset) {
082                    this(size);
083                    setOffset(offset);
084            }
085    
086            /**
087             * Creates a new Column of given size, with given offset and widgets
088             * 
089             * @param size
090             *            the size of the Column in the Bootstrap grid system
091             * @param offset
092             *            the offset from the left side
093             * @param widgets
094             *            the widgets to be added to the Column
095             */
096            public Column(int size, int offset, Widget... widgets) {
097                    this(size, offset);
098                    add(widgets);
099            }
100    
101            /**
102             * Creates a new Column of given size and with given widgets.
103             * 
104             * @param size
105             *            the size of the Column in the Bootstrap grid system
106             * @param widgets
107             *            the widgets to be added to the Column
108             */
109            public Column(int size, Widget... widgets) {
110                    this(size);
111                    add(widgets);
112            }
113    
114            /**
115             * Sets the size of the Column in the Bootstrap grid system.
116             * 
117             * @param size
118             *            the size of the Column
119             */
120            public void setSize(int size) {
121                    SizeHelper.setSize(this, size);
122            }
123    
124            /**
125             * Sets the offset of the Column from the left side.
126             * 
127             * @param offset
128             *            the offset in the Bootstrap grid system
129             */
130            public void setOffset(int offset) {
131                    if (offset < Constants.MINIMUM_OFFSET)
132                            throw new IllegalArgumentException(OFFSET_ERROR_MESSAGE);
133    
134                    if (offset > Constants.MAXIMUM_OFFSET)
135                            throw new IllegalArgumentException(OFFSET_ERROR_MESSAGE);
136    
137                    addStyleName(Constants.OFFSET + offset);
138            }
139    
140            /**
141             * Adds the provided widgets to the Column.
142             * 
143             * @param widgets
144             *            the widgets to be added
145             */
146            public void add(Widget... widgets) {
147                    for (Widget widget : widgets) {
148                            add(widget);
149                    }
150            }
151    }