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