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.base; 017 018import 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 */ 031public 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 043 if(style == null || style.get().isEmpty()) { 044 return; 045 } 046 047 widget.addStyleName(style.get()); 048 } 049 050 /** 051 * Replaces the widget's style with the provided one. 052 * 053 * @param widget 054 * the widget to be applied style. 055 * @param style 056 * the style to be applied to the Widget. 057 */ 058 public static <T extends UIObject> void setStyle(T widget, Style style) { 059 widget.setStyleName(style.get()); 060 } 061 062 /** 063 * Removes the provided style from the widget. 064 * 065 * @param widget 066 * the widget to be removed style. 067 * @param style 068 * the style to be removed from the Widget. 069 */ 070 public static <T extends UIObject> void removeStyle(T widget, Style style) { 071 String styleString = style.get(); 072 073 if (!styleString.isEmpty()) { 074 widget.removeStyleName(styleString); 075 } 076 } 077 078 /** 079 * Change the style. 080 * @param widget the widget to be changed style. 081 * @param style the style to be applied to the widget. 082 * @param styleEnums other styles. 083 */ 084 public static <S extends Enum<S> & Style> void changeStyle(UIObject widget, S style,Class<S> styleEnums) { 085 assert styleEnums != null : "styleEnums should not be null."; 086 087 changeStyle(widget, style, styleEnums.getEnumConstants()); 088 } 089 090 /** 091 * Change the style. 092 * @param widget the widget to be changed style. 093 * @param style the style to be applied to the widget. 094 * @param otherStyles other styles. 095 */ 096 public static <S extends Style> void changeStyle(UIObject widget,S style,S[] otherStyles) { 097 098 assert widget != null && otherStyles != null : "any args should not be null."; 099 100 for(S s : otherStyles) { 101 removeStyle(widget, s); 102 } 103 104 addStyle(widget, style); 105 } 106 107}