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.github.gwtbootstrap.client.ui.config.Configurator;
019    import com.github.gwtbootstrap.client.ui.constants.Device;
020    import com.github.gwtbootstrap.client.ui.constants.ResponsiveStyle;
021    
022    /**
023     * The Helper class for {@link IsResponsive} interface.
024     * 
025     * @since 2.0.4.0
026     * 
027     * @see IsResponsive
028     * @see Device
029     * @author ohashi keisuke
030     */
031    public class ResponsiveHelper {
032    
033            /**
034             * Sets the kind of device, this widget is shown on.
035             * 
036             * <p>
037             * The widget is not shown on any other device.
038             * </p>
039             * 
040             * <p>
041             * <b>Only works if responsive design is turned on!</b> Testlink
042             * </p>
043             * 
044             * @param widget
045             *            The widget to be applyed responsive.
046             * 
047             * @param device
048             *            The device to show widget.
049             * @see Configurator#hasResponsiveDesign()
050             * @see #setHideOn(HasStyle, Device)
051             */
052            public static <T extends HasStyle> void setShowOn(T widget, Device device) {
053                    removeResponsiveStyles(widget);
054                    switch (device) {
055                            case PHONE:
056                                    widget.addStyle(ResponsiveStyle.VISIBLE_PHONE);
057                                    break;
058                            case TABLET:
059                                    widget.addStyle(ResponsiveStyle.VISIBLE_TABLET);
060                                    break;
061                            case DESKTOP:
062                                    widget.addStyle(ResponsiveStyle.VISIBLE_DESKTOP);
063                                    break;
064                    }
065            }
066    
067            /**
068             * Sets the kind of device, this widget is hidden on.
069             * 
070             * <p>
071             * The widget is not hidden on any other device.
072             * </p>
073             * 
074             * <p>
075             * <b>Only works if responsive design is turned on!</b>
076             * </p>
077             * 
078             * @param widget
079             *            The widget to be applyed responsive.
080             * 
081             * @param device
082             *            The device to hide widget.
083             * 
084             * @see Configurator#hasResponsiveDesign()
085             * @see #setShowOn(HasStyle, Device)
086             */
087            public static <T extends HasStyle> void setHideOn(T widget, Device device) {
088                    removeResponsiveStyles(widget);
089                    switch (device) {
090                            case PHONE:
091                                    widget.addStyle(ResponsiveStyle.HIDDEN_PHONE);
092                                    break;
093                            case TABLET:
094                                    widget.addStyle(ResponsiveStyle.HIDDEN_TABLET);
095                                    break;
096                            case DESKTOP:
097                                    widget.addStyle(ResponsiveStyle.HIDDEN_DESKTOP);
098                                    break;
099                    }
100            }
101    
102            private static <T extends HasStyle> void removeResponsiveStyles(T element) {
103                    
104                    element.removeStyle(ResponsiveStyle.VISIBLE_PHONE);
105                    element.removeStyle(ResponsiveStyle.VISIBLE_TABLET);
106                    element.removeStyle(ResponsiveStyle.VISIBLE_DESKTOP);
107                    element.removeStyle(ResponsiveStyle.HIDDEN_PHONE);
108                    element.removeStyle(ResponsiveStyle.HIDDEN_TABLET);
109                    element.removeStyle(ResponsiveStyle.HIDDEN_DESKTOP);
110            }
111    
112    }