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.StyleHelper;
019import com.github.gwtbootstrap.client.ui.constants.BaseIconType;
020import com.github.gwtbootstrap.client.ui.constants.IconSize;
021import com.github.gwtbootstrap.client.ui.constants.IconType;
022import com.google.gwt.uibinder.client.UiConstructor;
023import com.google.gwt.user.client.DOM;
024import com.google.gwt.user.client.ui.Widget;
025
026//@formatter:off
027
028/**
029 * Widget with a black or white icon.
030 * <p>
031 * The icons are provided by <a href="http://glyphicons.com/">Glyphicons</a>
032 * and <a href="http://fortawesome.github.com/Font-Awesome/">Font Awesome</a>.
033 *
034 * <p/>
035 * <p>
036 * <h3>UiBinder Usage:</h3>
037 * <p/>
038 * <pre>
039 * {@code
040 * <b:Icon type="PLANE" />
041 * }
042 * </pre>
043 * </p>
044 *
045 * @author Carlos Alexandro Becker
046 * @author Dominik Mayer
047 * @see <a href="http://twitter.github.com/bootstrap/base-css.html#icons">Bootstrap documentation</a>
048 * @see <a href="http://fortawesome.github.com/Font-Awesome/">Font Awesome page</a>
049 * @since 2.0.4.0
050 */
051//@formatter:on
052public class Icon extends Widget {
053
054    private BaseIconType type;
055
056    /**
057     * Creates a widget but doesn't set an icon yet.
058     * <p/>
059     * (This is probably not what you want to do most of the time.)
060     */
061    public Icon() {
062        setElement(DOM.createElement("i"));
063    }
064
065    /**
066     * Creates a black icon of given type.
067     *
068     * @param type the icon type
069     */
070    @UiConstructor
071    public Icon(IconType type) {
072        this();
073        setBaseType(type);
074    }
075
076    /**
077     * Sets the icon type.
078     *
079     * @param type the icon type
080     */
081    public void setType(IconType type) {
082        setBaseType(type);
083    }
084
085    /**
086     * Sets the base icon type.
087     *
088     * @param type the icon type
089     */
090    public void setBaseType(BaseIconType type) {
091        if(this.type != null) {
092            StyleHelper.removeStyle(this, this.type);
093        }
094        this.type = type;
095        StyleHelper.addStyle(this, type);
096    }
097
098    /**
099     * Sets the icon size.
100     *
101     * @param size the icon size
102     */
103    public void setIconSize(IconSize size) {
104        StyleHelper.changeStyle(this, size, IconSize.class);
105    }
106    
107    /**
108     * Get the icon type
109     * 
110     * @return icon type, or null, if the Icon is no instance of {@link IconType}
111     */
112    public IconType getIconType() {
113        if(type instanceof IconType) {
114            return (IconType) type;
115        }
116        return null;
117    }
118
119    /**
120     * Get the base icon type
121     *
122     * @return base icon type
123     */
124    public BaseIconType getBaseIconType() {
125        return type;
126    }
127
128    /**
129     * Sets the base icon type.
130     *
131     * @param type the base icon type
132     */
133    public void setIcon(IconType type) {
134        setBaseType(type);
135    }
136}