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.github.gwtbootstrap.client.ui.Icon;
019import com.github.gwtbootstrap.client.ui.InputAddOn;
020import com.github.gwtbootstrap.client.ui.constants.*;
021import com.google.gwt.uibinder.client.UiChild;
022import com.google.gwt.user.client.ui.HasText;
023import com.google.gwt.user.client.ui.IsWidget;
024
025/**
026 * Input add-on.
027 * <p>
028 * AddOn can set text or icon.
029 * <h3>UiBinder Usage:</h3>
030 * 
031 * <pre>
032 * {@code
033 * <!-- text addon -->
034 * <b:AddOn text="@"/>
035 * 
036 * <!-- text addon another usage -->
037 * <b:AddOn>@</b:AddOn>
038 * 
039 * <!-- icon addon -->
040 * <b:AddOn icon="STAR"/>
041 * 
042 * <!-- widget addon -->
043 * <b:Addon><b:widget><b:CheckBox/></b:widget></b:AddOn>
044 * }
045 * </pre>
046 * 
047 * </p>
048 * 
049 * @since 2.0.4.0
050 * 
051 * @author ohashi keisuke
052 * @see InputAddOn
053 * @see IconType
054 * @see <a
055 *      href="http://twitter.github.com/bootstrap/base-css.html#forms">Bootstrap
056 *      Docs</a>
057 */
058public class AddOn extends ComplexWidget implements HasText, HasIcon {
059
060    /** text */
061    private String text;
062
063    /** icon */
064    private Icon icon = new Icon();
065
066    /**
067     * Creates an empty widget.
068     */
069    public AddOn() {
070        super("span");
071        setStyleName(Constants.ADD_ON);
072    }
073
074    /**
075     * {@inheritDoc}
076     */
077    public String getText() {
078        return getElement().getInnerText();
079    }
080
081    /**
082     * {@inheritDoc}
083     */
084    public void setText(String text) {
085        this.text = text;
086
087        if (icon.isAttached()) {
088            icon.removeFromParent();
089        }
090        getElement().setInnerText(text);
091    }
092
093    /**
094     * {@inheritDoc}
095     */
096    @Override
097    public void setIcon(IconType type) {
098        setBaseIcon(type);
099    }
100
101    /**
102     * {@inheritDoc}
103     */
104    @Override
105    public void setBaseIcon(BaseIconType type) {
106        if (text != null) {
107            getElement().setInnerHTML("");
108        }
109
110        icon.setBaseType(type);
111
112        if (!icon.isAttached()) {
113            add(icon);
114        }
115    }
116
117    /**
118     * {@inheritDoc}
119     */
120    @Override
121    public void setIconSize(IconSize size) {
122        icon.setIconSize(size);
123    }
124
125    /**
126     * Add widget. Its method is for uibinder syntax.
127     * 
128     * @param w
129     *            added widget
130     */
131    @UiChild(tagname = "widget", limit = 1)
132    public void addWidget(IsWidget w) {
133        add(w);
134    }
135
136    /**
137     * {@inheritDoc}
138     */
139    @Override
140    public void setCustomIconStyle(String customIconStyle) {
141        icon.addStyleName(customIconStyle);
142    }
143
144    @Override
145    @Deprecated
146    public void setIconPosition(IconPosition position) {
147        throw new UnsupportedOperationException("Addon does not support this methods");
148    }
149
150}