001package org.gwtbootstrap3.client.ui;
002
003/*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 2013 - 2014 GwtBootstrap3
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import org.gwtbootstrap3.client.ui.constants.Styles;
024
025import com.google.gwt.dom.client.Document;
026import com.google.gwt.i18n.client.HasDirection.Direction;
027import com.google.gwt.i18n.shared.DirectionEstimator;
028import com.google.gwt.safehtml.shared.SafeHtml;
029import com.google.gwt.uibinder.client.UiConstructor;
030import com.google.gwt.user.client.DOM;
031
032/**
033 * An inline radio button widget.
034 *
035 * @author Sven Jacobs
036 * @see org.gwtbootstrap3.client.ui.Radio
037 */
038public class InlineRadio extends Radio {
039
040    /**
041     * Creates a new radio associated with a particular group, and initialized
042     * with the given HTML label. All radio buttons associated with the same
043     * group name belong to a mutually-exclusive set.
044     * 
045     * Radio buttons are grouped by their name attribute, so changing their name
046     * using the setName() method will also change their associated group.
047     * 
048     * @param name
049     *            the group name with which to associate the radio button
050     * @param label
051     *            this radio button's html label
052     */
053    public InlineRadio(String name, SafeHtml label) {
054        this(name, label.asString(), true);
055    }
056
057    /**
058     * @see #InlineRadio(String, SafeHtml)
059     * 
060     * @param name
061     *            the group name with which to associate the radio button
062     * @param label
063     *            this radio button's html label
064     * @param dir
065     *            the text's direction. Note that {@code DEFAULT} means
066     *            direction should be inherited from the widget's parent
067     *            element.
068     */
069    public InlineRadio(String name, SafeHtml label, Direction dir) {
070        this(name);
071        setHTML(label, dir);
072    }
073
074    /**
075     * @see #InlineRadio(String, SafeHtml)
076     * 
077     * @param name
078     *            the group name with which to associate the radio button
079     * @param label
080     *            this radio button's html label
081     * @param directionEstimator
082     *            A DirectionEstimator object used for automatic direction
083     *            adjustment. For convenience,
084     *            {@link #DEFAULT_DIRECTION_ESTIMATOR} can be used.
085     */
086    public InlineRadio(String name, SafeHtml label, DirectionEstimator directionEstimator) {
087        this(name);
088        setDirectionEstimator(directionEstimator);
089        setHTML(label.asString());
090    }
091
092    /**
093     * Creates a new radio associated with a particular group, and initialized
094     * with the given HTML label. All radio buttons associated with the same
095     * group name belong to a mutually-exclusive set.
096     * 
097     * Radio buttons are grouped by their name attribute, so changing their name
098     * using the setName() method will also change their associated group.
099     * 
100     * @param name
101     *            the group name with which to associate the radio button
102     * @param label
103     *            this radio button's label
104     */
105    public InlineRadio(String name, String label) {
106        this(name);
107        setText(label);
108    }
109
110    /**
111     * @see #InlineRadio(String, SafeHtml)
112     * 
113     * @param name
114     *            the group name with which to associate the radio button
115     * @param label
116     *            this radio button's label
117     * @param dir
118     *            the text's direction. Note that {@code DEFAULT} means
119     *            direction should be inherited from the widget's parent
120     *            element.
121     */
122    public InlineRadio(String name, String label, Direction dir) {
123        this(name);
124        setText(label, dir);
125    }
126
127    /**
128     * @see #InlineRadio(String, SafeHtml)
129     * 
130     * @param name
131     *            the group name with which to associate the radio button
132     * @param label
133     *            this radio button's label
134     * @param directionEstimator
135     *            A DirectionEstimator object used for automatic direction
136     *            adjustment. For convenience,
137     *            {@link #DEFAULT_DIRECTION_ESTIMATOR} can be used.
138     */
139    public InlineRadio(String name, String label, DirectionEstimator directionEstimator) {
140        this(name);
141        setDirectionEstimator(directionEstimator);
142        setText(label);
143    }
144
145    /**
146     * Creates a new radio button associated with a particular group, and
147     * initialized with the given label (optionally treated as HTML). All radio
148     * buttons associated with the same group name belong to a
149     * mutually-exclusive set.
150     * 
151     * Radio buttons are grouped by their name attribute, so changing their name
152     * using the setName() method will also change their associated group.
153     * 
154     * @param name
155     *            name the group with which to associate the radio button
156     * @param label
157     *            this radio button's label
158     * @param asHTML
159     *            <code>true</code> to treat the specified label as HTML
160     */
161    public InlineRadio(String name, String label, boolean asHTML) {
162        this(name);
163        if (asHTML) {
164            setHTML(label);
165        } else {
166            setText(label);
167        }
168    }
169
170    /**
171     * Creates a new radio associated with a particular group name. All radio
172     * buttons associated with the same group name belong to a
173     * mutually-exclusive set.
174     * 
175     * Radio buttons are grouped by their name attribute, so changing their name
176     * using the setName() method will also change their associated group.
177     * 
178     * @param name
179     *            the group name with which to associate the radio button
180     */
181    @UiConstructor
182    public InlineRadio(String name) {
183        super(DOM.createLabel(), Document.get().createRadioInputElement(name));
184        setStyleName(Styles.RADIO_INLINE);
185
186        getElement().appendChild(inputElem);
187        getElement().appendChild(labelElem);
188    }
189
190}