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 com.google.gwt.user.client.ui.UIObject;
019    
020    /**
021     * The Helper class for {@link HasStyle} interface.
022     *
023     * @since 2.0.4.0
024     *
025     * @see HasStyle
026     * @see Style
027     *
028     * @author ohashi keisuke
029     * @author Carlos A Becker
030     */
031    public class StyleHelper {
032    
033            /**
034             * Adds the provided style to the widget.
035             *
036             * @param widget
037             *            the widget to be added style.
038             * @param style
039             *            the style to be added to the Widget.
040             */
041            public static <T extends UIObject> void addStyle(T widget, Style style) {
042            if (!style.get().isEmpty()) {
043                widget.addStyleName(style.get());
044            }
045            }
046    
047            /**
048             * Replaces the widget's style with the provided one.
049             *
050             * @param widget
051             *            the widget to be applied style.
052             * @param style
053             *            the style to be applied to the Widget.
054             */
055            public static <T extends UIObject> void setStyle(T widget, Style style) {
056                    widget.setStyleName(style.get());
057            }
058    
059            /**
060             * Removes the provided style from the widget.
061             *
062             * @param widget
063             *            the widget to be removed style.
064             * @param style
065             *            the style to be removed from the Widget.
066             */
067            public static <T extends UIObject> void removeStyle(T widget, Style style) {
068                    String styleString = style.get();
069    
070                    if (!styleString.isEmpty()) {
071                            widget.removeStyleName(styleString);
072                    }
073            }
074    
075            /**
076             * Change the style.
077             * @param widget the widget to be changed style.
078             * @param style the style to be applied to the widget.
079             * @param styleEnums other styles.
080             */
081            public static <S extends Enum<S> & Style> void changeStyle(UIObject widget, S style,Class<S> styleEnums) {
082                    assert styleEnums != null : "styleEnums should not be null.";
083    
084                    changeStyle(widget, style, styleEnums.getEnumConstants());
085            }
086    
087            /**
088             * Change the style.
089             * @param widget the widget to be changed style.
090             * @param style the style to be applied to the widget.
091             * @param otherStyles other styles.
092             */
093            public static <S extends Style> void changeStyle(UIObject widget,S style,S[] otherStyles) {
094    
095                    assert widget != null && style != null && otherStyles != null : "any args should not be null.";
096    
097                    for(S s : otherStyles) {
098                            removeStyle(widget, s);
099                    }
100    
101                    addStyle(widget, style);
102            }
103            
104    }