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.base;
017    
018    import java.util.ArrayList;
019    import java.util.Collections;
020    import java.util.List;
021    
022    import com.github.gwtbootstrap.client.ui.constants.Constants;
023    import com.google.gwt.user.client.ui.UIObject;
024    
025    /**
026     * THe helper for {@link HasSize} interface.
027     * 
028     * @since 2.0.4.0
029     * @author ohashi keisuke
030     * 
031     */
032    public class SizeHelper {
033    
034            private static List<SizeSpan> SIZE_LIST;
035    
036            // create SIZE_LIST
037            static {
038    
039                    List<SizeSpan> list = new ArrayList<SizeSpan>();
040    
041                    for (int i = Constants.MINIMUM_SPAN_SIZE; i <= Constants.MAXIMUM_SPAN_SIZE; i++) {
042                            list.add(new SizeSpan(i));
043                    }
044    
045                    SIZE_LIST = Collections.unmodifiableList(list);
046            }
047    
048            /**
049             * change size style
050             * 
051             * @param widget
052             *            target widget
053             * @param size
054             *            size
055             */
056            public static void setSize(UIObject widget, int size) {
057                    StyleHelper.changeStyle(widget,
058                                                                    new SizeSpan(size),
059                                                                    SIZE_LIST.toArray(new SizeSpan[SIZE_LIST.size()])
060                                                    );
061            }
062    
063            private static class SizeSpan implements Style {
064    
065                    private static final String SIZE_ERROR_MESSAGE = "The size of the Column has to be between " + Constants.MINIMUM_SPAN_SIZE + " and " + Constants.MAXIMUM_SPAN_SIZE + "!";
066    
067                    private final int size;
068    
069                    private SizeSpan(int size) {
070    
071                            if (size < Constants.MINIMUM_SPAN_SIZE)
072                                    throw new IllegalArgumentException(SIZE_ERROR_MESSAGE);
073    
074                            if (size > Constants.MAXIMUM_SPAN_SIZE)
075                                    throw new IllegalArgumentException(SIZE_ERROR_MESSAGE);
076    
077                            this.size = size;
078                    }
079    
080                    @Override
081                    public String get() {
082                            return Constants.SPAN + size;
083                    }
084    
085            }
086    
087    }