com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput Maven / Gradle / Ivy
/*
* Copyright (c) 2002-2008 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gargoylesoftware.htmlunit.html;
import java.io.IOException;
import java.util.Map;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.ScriptResult;
import com.gargoylesoftware.htmlunit.SgmlPage;
import com.gargoylesoftware.htmlunit.javascript.host.Event;
/**
* Wrapper for the HTML element "input".
*
* @version $Revision: 3106 $
* @author Mike Bowler
* @author David K. Taylor
* @author Christian Sell
* @author Marc Guillemot
* @author Mike Bresnahan
* @author Daniel Gredler
* @author Bruce Faulkner
* @author Ahmed Ashour
*/
public class HtmlRadioButtonInput extends HtmlInput {
private static final long serialVersionUID = 425993174633373218L;
private boolean defaultCheckedState_;
/**
* Creates an instance.
* If no value is specified, it is set to "on" as browsers do (eg IE6 and Mozilla 1.7)
* even if spec says that it is not allowed
* (W3C).
* @param namespaceURI the URI that identifies an XML namespace
* @param qualifiedName the qualified name of the element type to instantiate
* @param page the page that contains this element
* @param attributes the initial attributes
*/
HtmlRadioButtonInput(final String namespaceURI, final String qualifiedName, final SgmlPage page,
final Map attributes) {
super(namespaceURI, qualifiedName, page, attributes);
// default value for both IE6 and Mozilla 1.7 even if spec says it is unspecified
if (getAttributeValue("value") == ATTRIBUTE_NOT_DEFINED) {
setAttributeValue("value", "on");
}
defaultCheckedState_ = isAttributeDefined("checked");
}
/**
* {@inheritDoc}
* @see SubmittableElement#reset()
*/
@Override
public void reset() {
if (defaultCheckedState_) {
setAttributeValue("checked", "checked");
}
else {
removeAttribute("checked");
}
}
/**
* Sets the "checked" attribute.
*
* @param isChecked true if this element is to be selected
* @return the page that occupies this window after setting checked status
* It may be the same window or it may be a freshly loaded one.
*/
@Override
public Page setChecked(final boolean isChecked) {
final HtmlForm form = getEnclosingForm();
final boolean changed = isChecked() != isChecked;
if (isChecked) {
if (form != null) {
form.setCheckedRadioButton(this);
}
else {
((HtmlPage) getPage()).setCheckedRadioButton(this);
}
}
else {
removeAttribute("checked");
}
Page page = getPage();
if (changed) {
final ScriptResult scriptResult = fireEvent(Event.TYPE_CHANGE);
if (scriptResult != null) {
page = scriptResult.getNewPage();
}
}
return page;
}
/**
* A radio button does not have a textual representation,
* but we invent one for it because it is useful for testing.
* @return "checked" or "unchecked" according to the radio state
*/
@Override
public String asText() {
if (isChecked()) {
return "checked";
}
return "unchecked";
}
/**
* Override of default clickAction that makes this radio button the selected
* one when it is clicked.
*
* @param defaultPage the default page to return if the action does not
* load a new page.
* @return the page that is currently loaded after execution of this method
* @throws IOException if an IO error occurred
*/
@Override
protected Page doClickAction(final Page defaultPage) throws IOException {
setChecked(true);
return defaultPage;
}
/**
* {@inheritDoc}
* Also sets the value to the new default value.
* @see SubmittableElement#setDefaultValue(String)
*/
@Override
public void setDefaultValue(final String defaultValue) {
super.setDefaultValue(defaultValue);
setValueAttribute(defaultValue);
}
/**
* {@inheritDoc}
* @see SubmittableElement#setDefaultChecked(boolean)
*/
@Override
public void setDefaultChecked(final boolean defaultChecked) {
defaultCheckedState_ = defaultChecked;
if (getPage().getWebClient().getBrowserVersion().isNetscape()) {
setChecked(defaultChecked);
}
}
/**
* {@inheritDoc}
* @see SubmittableElement#isDefaultChecked()
*/
@Override
public boolean isDefaultChecked() {
return defaultCheckedState_;
}
/**
* {@inheritDoc}
* @see com.gargoylesoftware.htmlunit.html.ClickableElement#isStateUpdateFirst()
*/
@Override
protected boolean isStateUpdateFirst() {
return true;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy