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