1 38 package com.gargoylesoftware.htmlunit.html; 39 40 import java.util.Map ; 41 42 51 public class HtmlOption extends ClickableElement implements DisabledElement { 52 53 54 public static final String TAG_NAME = "option"; 55 56 private final boolean initialSelectedState_; 57 58 64 public HtmlOption( final HtmlPage page, final Map attributes ) { 65 super(page, attributes); 66 initialSelectedState_ = isAttributeDefined("selected"); 67 } 68 69 72 public String getTagName() { 73 return TAG_NAME; 74 } 75 76 81 public boolean isSelected() { 82 return isAttributeDefined("selected"); 83 } 84 85 86 92 public void setSelected( final boolean selected ) { 93 getEnclosingSelectOrDie().setSelectedAttribute(this, selected); 94 } 95 96 97 private HtmlSelect getEnclosingSelectOrDie() { 98 DomNode parent = getParentNode(); 99 while( parent != null ) { 100 if( parent instanceof HtmlSelect ) { 101 return (HtmlSelect)parent; 102 } 103 parent = parent.getParentNode(); 104 } 105 throw new IllegalStateException ("Can't find enclosing select element"); 106 } 107 108 109 112 public void reset() { 113 if( initialSelectedState_ ) { 114 setAttributeValue("selected", "selected"); 115 } 116 else { 117 removeAttribute("selected"); 118 } 119 } 120 121 122 130 public final String getSelectedAttribute() { 131 return getAttributeValue("selected"); 132 } 133 134 135 140 public final boolean isDisabled() { 141 return isAttributeDefined("disabled"); 142 } 143 144 145 153 public final String getDisabledAttribute() { 154 return getAttributeValue("disabled"); 155 } 156 157 158 166 public final String getLabelAttribute() { 167 return getAttributeValue("label"); 168 } 169 170 171 178 public final void setLabelAttribute( final String newLabel ) { 179 setAttributeValue("label", newLabel); 180 } 181 182 183 191 public final String getValueAttribute() { 192 return getAttributeValue("value"); 193 } 194 195 196 203 public final void setValueAttribute( final String newValue ) { 204 setAttributeValue("value", newValue); 205 } 206 207 212 public String getValue() { 213 if (isAttributeDefined("value")){ 214 return getValueAttribute(); 215 } 216 else { 217 return asText(); 218 } 219 } 220 } 221 | Popular Tags |