1 38 package com.gargoylesoftware.htmlunit.html; 39 40 import java.io.IOException ; 41 import java.util.Map ; 42 43 import org.mozilla.javascript.Function; 44 import org.mozilla.javascript.Scriptable; 45 46 import com.gargoylesoftware.htmlunit.ElementNotFoundException; 47 import com.gargoylesoftware.htmlunit.Page; 48 import com.gargoylesoftware.htmlunit.javascript.host.Event; 49 50 60 public class HtmlRadioButtonInput extends HtmlInput { 61 62 private final boolean initialCheckedState_; 64 65 73 public HtmlRadioButtonInput( final HtmlPage page, final Map attributes ) { 74 super(page, attributes); 75 76 if (getAttributeValue("value") == ATTRIBUTE_NOT_DEFINED) { 78 setAttributeValue("value", "on"); 79 } 80 81 initialCheckedState_ = isAttributeDefined("checked"); 83 } 84 85 86 89 public void reset() { 90 if( initialCheckedState_ ) { 91 setAttributeValue("checked", "checked"); 92 } 93 else { 94 removeAttribute("checked"); 95 } 96 } 97 98 103 public void setChecked( final boolean isChecked ) { 104 final HtmlForm form = getEnclosingForm(); 105 final boolean changed = isChecked() != isChecked; 106 107 if( isChecked ) { 108 try { 109 form.setCheckedRadioButton( getNameAttribute(), getValueAttribute() ); 110 } 111 catch( final ElementNotFoundException e ) { 112 throw new IllegalStateException ("Can't find this element when going up to the form and back down."); 114 } 115 } 116 else { 117 removeAttribute( "checked" ); 118 } 119 120 final Function onchangeHandler = getEventHandler("onchange"); 121 final HtmlPage page = getPage(); 122 123 if (changed && onchangeHandler != null && page.getWebClient().isJavaScriptEnabled()) { 124 final Event event = new Event(this, getScriptObject()); 125 final Object [] args = new Object [] {event}; 126 page.executeJavaScriptFunctionIfPossible( 127 onchangeHandler, (Scriptable) getScriptObject(), args, this); 128 } 129 } 130 131 136 public String asText() { 137 if (isChecked()) { 138 return "checked"; 139 } 140 else { 141 return "unchecked"; 142 } 143 } 144 145 154 protected Page doClickAction(final Page defaultPage) throws IOException { 155 setChecked(true); 156 return defaultPage; 157 } 158 159 } 160 161 | Popular Tags |