001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.html;
039:
040: import java.io.IOException;
041: import java.util.Map;
042:
043: import com.gargoylesoftware.htmlunit.Page;
044: import com.gargoylesoftware.htmlunit.ScriptResult;
045: import com.gargoylesoftware.htmlunit.javascript.host.Event;
046:
047: /**
048: * Wrapper for the html element "input"
049: *
050: * @version $Revision: 2132 $
051: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
052: * @author David K. Taylor
053: * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
054: * @author Marc Guillemot
055: * @author Mike Bresnahan
056: * @author Daniel Gredler
057: * @author Bruce Faulkner
058: * @author Ahmed Ashour
059: */
060: public class HtmlRadioButtonInput extends HtmlInput {
061:
062: private static final long serialVersionUID = 425993174633373218L;
063:
064: private boolean defaultCheckedState_;
065:
066: /**
067: * Create an instance
068: * If no value is specified, it is set to "on" as browsers do (eg IE6 and Mozilla 1.7)
069: * even if spec says that it is not allowed
070: * (<a href="http://www.w3.org/TR/REC-html40/interact/forms.html#adef-value-INPUT">W3C</a>).
071: * @param page The page that contains this element
072: * @param attributes the initial attributes
073: * @deprecated You should not directly construct HtmlRadioButtonInput.
074: */
075: //TODO: to be removed, deprecated after 1.11
076: public HtmlRadioButtonInput(final HtmlPage page,
077: final Map attributes) {
078: this (null, TAG_NAME, page, attributes);
079: }
080:
081: /**
082: * Create an instance
083: * If no value is specified, it is set to "on" as browsers do (eg IE6 and Mozilla 1.7)
084: * even if spec says that it is not allowed
085: * (<a href="http://www.w3.org/TR/REC-html40/interact/forms.html#adef-value-INPUT">W3C</a>).
086: * @param namespaceURI the URI that identifies an XML namespace.
087: * @param qualifiedName The qualified name of the element type to instantiate
088: * @param page The page that contains this element
089: * @param attributes the initial attributes
090: */
091: HtmlRadioButtonInput(final String namespaceURI,
092: final String qualifiedName, final HtmlPage page,
093: final Map attributes) {
094: super (namespaceURI, qualifiedName, page, attributes);
095:
096: // default value for both IE6 and Mozilla 1.7 even if spec says it is unspecified
097: if (getAttributeValue("value") == ATTRIBUTE_NOT_DEFINED) {
098: setAttributeValue("value", "on");
099: }
100:
101: defaultCheckedState_ = isAttributeDefined("checked");
102: }
103:
104: /**
105: * {@inheritDoc}
106: * @see SubmittableElement#reset()
107: */
108: public void reset() {
109: if (defaultCheckedState_) {
110: setAttributeValue("checked", "checked");
111: } else {
112: removeAttribute("checked");
113: }
114: }
115:
116: /**
117: * Set the "checked" attribute
118: *
119: * @param isChecked true if this element is to be selected
120: * @return The page that occupies this window after setting checked status.
121: * It may be the same window or it may be a freshly loaded one.
122: */
123: public Page setChecked(final boolean isChecked) {
124: final HtmlForm form = getEnclosingForm();
125: final boolean changed = isChecked() != isChecked;
126:
127: if (isChecked) {
128: if (form != null) {
129: form.setCheckedRadioButton(this );
130: } else {
131: getPage().setCheckedRadioButton(this );
132: }
133: } else {
134: removeAttribute("checked");
135: }
136:
137: Page page = getPage();
138:
139: if (changed) {
140: final ScriptResult scriptResult = fireEvent(Event.TYPE_CHANGE);
141: if (scriptResult != null) {
142: page = scriptResult.getNewPage();
143: }
144: }
145: return page;
146: }
147:
148: /**
149: * A radio button does not have a textual representation,
150: * but we invent one for it because it is useful for testing.
151: * @return "checked" or "unchecked" according to the radio state
152: */
153: public String asText() {
154: if (isChecked()) {
155: return "checked";
156: } else {
157: return "unchecked";
158: }
159: }
160:
161: /**
162: * Override of default clickAction that makes this radio button the selected
163: * one when it is clicked
164: *
165: * @param defaultPage The default page to return if the action does not
166: * load a new page.
167: * @return The page that is currently loaded after execution of this method
168: * @throws IOException If an IO error occurred
169: */
170: protected Page doClickAction(final Page defaultPage)
171: throws IOException {
172: setChecked(true);
173: return defaultPage;
174: }
175:
176: /**
177: * {@inheritDoc} Also sets the value to the new default value.
178: * @see SubmittableElement#setDefaultValue(String)
179: */
180: public void setDefaultValue(final String defaultValue) {
181: super .setDefaultValue(defaultValue);
182: setValueAttribute(defaultValue);
183: }
184:
185: /**
186: * {@inheritDoc}
187: * @see SubmittableElement#setDefaultChecked(boolean)
188: */
189: public void setDefaultChecked(final boolean defaultChecked) {
190: defaultCheckedState_ = defaultChecked;
191: if (getPage().getWebClient().getBrowserVersion().isNetscape()) {
192: setChecked(defaultChecked);
193: }
194: }
195:
196: /**
197: * {@inheritDoc}
198: * @see SubmittableElement#isDefaultChecked()
199: */
200: public boolean isDefaultChecked() {
201: return defaultCheckedState_;
202: }
203:
204: /**
205: * {@inheritDoc}
206: * @see com.gargoylesoftware.htmlunit.html.ClickableElement#isStateUpdateFirst()
207: */
208: protected boolean isStateUpdateFirst() {
209: return true;
210: }
211: }
|