com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vaadin-client-compiler-deps Show documentation
Show all versions of vaadin-client-compiler-deps Show documentation
Vaadin is a web application framework for Rich Internet Applications (RIA).
Vaadin enables easy development and maintenance of fast and
secure rich web
applications with a stunning look and feel and a wide browser support.
It features a server-side architecture with the majority of the logic
running
on the server. Ajax technology is used at the browser-side to ensure a
rich
and interactive user experience.
/*
* Copyright (c) 2002-2011 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.BrowserVersionFeatures;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.SgmlPage;
/**
* Wrapper for the HTML element "input".
*
* @version $Revision: 6359 $
* @author Mike Bowler
* @author David K. Taylor
* @author Jun Chen
* @author Christian Sell
* @author Marc Guillemot
* @author Mike Bresnahan
* @author Daniel Gredler
* @author Ahmed Ashour
*/
public class HtmlCheckBoxInput extends HtmlInput {
private boolean defaultCheckedState_;
private boolean valueAtFocus_;
/**
* Creates an instance.
* If no value is specified, it is set to "on" as browsers do (e.g. 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
*/
HtmlCheckBoxInput(final String namespaceURI, final String qualifiedName, final SgmlPage page,
final Map attributes) {
super(namespaceURI, qualifiedName, page, attributes);
//From the checkbox creator
defaultCheckedState_ = hasAttribute("checked");
// default value for both IE6 and Mozilla 1.7 even if spec says it is unspecified
if (getAttribute("value") == ATTRIBUTE_NOT_DEFINED) {
setAttribute("value", "on");
}
}
/**
* {@inheritDoc}
*
* @see SubmittableElement#reset()
*/
@Override
public void reset() {
setChecked(defaultCheckedState_);
}
/**
* {@inheritDoc}
*/
@Override
public Page setChecked(final boolean isChecked) {
if (isChecked) {
setAttribute("checked", "checked");
}
else {
removeAttribute("checked");
}
if (getPage().getWebClient().getBrowserVersion()
.hasFeature(BrowserVersionFeatures.EVENT_ONCHANGE_LOSING_FOCUS)) {
return getPage();
}
return executeOnChangeHandlerIfAppropriate(this);
}
/**
* A checkbox 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
*/
// we need to preserve this method as it is there since many versions with the above documentation.
@Override
public String asText() {
return super.asText();
}
/**
* Override so that checkbox can change its state correctly when its
* click() method is called.
*
* {@inheritDoc}
*/
@Override
protected void doClickAction() throws IOException {
setChecked(!isChecked());
super.doClickAction();
}
/**
* Both IE and Mozilla will first update the internal state of checkbox
* and then handle "onclick" event.
* {@inheritDoc}
*/
@Override
protected boolean isStateUpdateFirst() {
return true;
}
/**
* {@inheritDoc}
*/
@Override
protected void preventDefault() {
setChecked(!isChecked());
}
/**
* {@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()
.hasFeature(BrowserVersionFeatures.HTMLINPUT_DEFAULT_IS_CHECKED)) {
setChecked(defaultChecked);
}
}
/**
* {@inheritDoc}
* @see SubmittableElement#isDefaultChecked()
*/
@Override
public boolean isDefaultChecked() {
return defaultCheckedState_;
}
/**
* {@inheritDoc}
*/
@Override
public void focus() {
super.focus();
valueAtFocus_ = isChecked();
}
/**
* {@inheritDoc}
*/
@Override
void removeFocus() {
super.removeFocus();
final boolean fireOnChange = getPage().getWebClient().getBrowserVersion()
.hasFeature(BrowserVersionFeatures.EVENT_ONCHANGE_LOSING_FOCUS);
if (fireOnChange && valueAtFocus_ != isChecked()) {
executeOnChangeHandlerIfAppropriate(this);
}
valueAtFocus_ = isChecked();
}
}