001/*
002 * #%L
003 * GwtMaterial
004 * %%
005 * Copyright (C) 2015 - 2017 GwtMaterialDesign
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 * 
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package gwt.material.design.client.ui.html;
021
022import com.google.gwt.dom.client.Document;
023import com.google.gwt.dom.client.OptionElement;
024import gwt.material.design.client.base.MaterialWidget;
025import gwt.material.design.client.ui.MaterialListBox;
026
027/**
028 * Option widget that encapsulated a <option> tag. This widget is to be
029 * used in conjunction with {@link MaterialListBox}:
030 * <p>
031 * <p>
032 * <blockquote>
033 * <p>
034 * <pre>
035 * {@code
036 * <m:MaterialListBox >
037 *     <m:html.Option text="One" value="1"/>
038 *     <m:html.Option text="Two" value="2"/>
039 *     <m:html.Option text="Three" value="3"/>
040 *  </m:MaterialListBox>
041 * }
042 * </pre>
043 * <p>
044 * </blockquote>
045 * </p>
046 *
047 * @author gilberto-torrezan
048 * @see OptionElement
049 */
050public class Option extends MaterialWidget {
051
052    public Option() {
053        super(Document.get().createElement(OptionElement.TAG));
054    }
055
056    public Option(String value) {
057        this();
058        setText(value);
059    }
060
061    /**
062     * The index of this OPTION in its parent SELECT, starting from 0.
063     */
064    public int getIndex() {
065        return OptionElement.as(this.getElement()).getIndex();
066    }
067
068    /**
069     * Option label for use in hierarchical menus.
070     *
071     * @see <a
072     * href="http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#adef-label-OPTION">W3C
073     * HTML Specification</a>
074     */
075    public String getLabel() {
076        return OptionElement.as(this.getElement()).getLabel();
077    }
078
079    /**
080     * The text contained within the option element.
081     */
082    public String getText() {
083        return OptionElement.as(this.getElement()).getText();
084    }
085
086    /**
087     * The current form control value.
088     *
089     * @see <a
090     * href="http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#adef-value-OPTION">W3C
091     * HTML Specification</a>
092     */
093    public String getValue() {
094        return OptionElement.as(this.getElement()).getValue();
095    }
096
097    /**
098     * Represents the value of the HTML selected attribute. The value of this
099     * attribute does not change if the state of the corresponding form control,
100     * in an interactive user agent, changes.
101     *
102     * @see <a
103     * href="http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#adef-selected">W3C
104     * HTML Specification</a>
105     */
106    public boolean isDefaultSelected() {
107        return OptionElement.as(this.getElement()).isDefaultSelected();
108    }
109
110    /**
111     * The control is unavailable in this context.
112     *
113     * @see <a
114     * href="http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#adef-disabled">W3C
115     * HTML Specification</a>
116     */
117    public boolean isDisabled() {
118        return OptionElement.as(this.getElement()).isDisabled();
119    }
120
121    /**
122     * Represents the current state of the corresponding form control, in an
123     * interactive user agent. Changing this attribute changes the state of the
124     * form control, but does not change the value of the HTML selected
125     * attribute of the element.
126     */
127    public boolean isSelected() {
128        return OptionElement.as(this.getElement()).isSelected();
129    }
130
131    /**
132     * Represents the value of the HTML selected attribute. The value of this
133     * attribute does not change if the state of the corresponding form control,
134     * in an interactive user agent, changes.
135     *
136     * @see <a
137     * href="http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#adef-selected">W3C
138     * HTML Specification</a>
139     */
140    public void setDefaultSelected(boolean selected) {
141        OptionElement.as(this.getElement()).setDefaultSelected(selected);
142    }
143
144    /**
145     * The control is unavailable in this context.
146     *
147     * @see <a
148     * href="http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#adef-disabled">W3C
149     * HTML Specification</a>
150     */
151    public void setDisabled(boolean disabled) {
152        OptionElement.as(this.getElement()).setDisabled(disabled);
153    }
154
155    /**
156     * Option label for use in hierarchical menus.
157     *
158     * @see <a
159     * href="http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#adef-label-OPTION">W3C
160     * HTML Specification</a>
161     */
162    public void setLabel(String label) {
163        OptionElement.as(this.getElement()).setLabel(label);
164    }
165
166    /**
167     * Represents the current state of the corresponding form control, in an
168     * interactive user agent. Changing this attribute changes the state of the
169     * form control, but does not change the value of the HTML selected
170     * attribute of the element.
171     */
172    public void setSelected(boolean selected) {
173        OptionElement.as(this.getElement()).setSelected(selected);
174    }
175
176    /**
177     * The text contained within the option element.
178     */
179    public void setText(String text) {
180        OptionElement.as(this.getElement()).setText(text);
181    }
182
183    /**
184     * The current form control value.
185     *
186     * @see <a
187     * href="http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#adef-value-OPTION">W3C
188     * HTML Specification</a>
189     */
190    public void setValue(String value) {
191        OptionElement.as(this.getElement()).setValue(value);
192    }
193
194    @Override
195    public void setEnabled(boolean enabled) {
196        super.setEnabled(enabled);
197        setDisabled(!enabled);
198    }
199}