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