KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > html > HtmlRadioButtonInput


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.html;
39
40 import java.io.IOException JavaDoc;
41 import java.util.Map JavaDoc;
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 /**
51  * Wrapper for the html element "input"
52  *
53  * @version $Revision: 100 $
54  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
55  * @author David K. Taylor
56  * @author <a HREF="mailto:cse@dynabean.de">Christian Sell</a>
57  * @author Marc Guillemot
58  * @author Mike Bresnahan
59  */

60 public class HtmlRadioButtonInput extends HtmlInput {
61
62     //For Checkbox, radio
63
private final boolean initialCheckedState_;
64
65     /**
66      * Create an instance
67      * If no value is specified, it is set to "on" as browsers do (eg IE6 and Mozilla 1.7)
68      * even if spec says that it is not allowed
69      * (<a HREF="http://www.w3.org/TR/REC-html40/interact/forms.html#adef-value-INPUT">W3C</a>).
70      * @param page The page that contains this element
71      * @param attributes the initial attributes
72      */

73     public HtmlRadioButtonInput( final HtmlPage page, final Map JavaDoc attributes ) {
74         super(page, attributes);
75
76         // default value for both IE6 and Mozilla 1.7 even if spec says it is unspecified
77
if (getAttributeValue("value") == ATTRIBUTE_NOT_DEFINED) {
78             setAttributeValue("value", "on");
79         }
80
81         //From the checkbox creator
82
initialCheckedState_ = isAttributeDefined("checked");
83     }
84
85
86     /**
87      * Reset this element to its original values.
88      */

89     public void reset() {
90         if( initialCheckedState_ ) {
91             setAttributeValue("checked", "checked");
92         }
93         else {
94             removeAttribute("checked");
95         }
96     }
97
98     /**
99      * Set the "checked" attribute
100      *
101      * @param isChecked true if this element is to be selected
102      */

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                 // Shouldn't be possible
113
throw new IllegalStateException JavaDoc("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 JavaDoc[] args = new Object JavaDoc[] {event};
126             page.executeJavaScriptFunctionIfPossible(
127                 onchangeHandler, (Scriptable) getScriptObject(), args, this);
128         }
129     }
130
131     /**
132      * A radio button does not have a textual representation,
133      * but we invent one for it because it is useful for testing.
134      * @return "checked" or "unchecked" according to the radio state
135      */

136     public String JavaDoc asText() {
137         if (isChecked()) {
138             return "checked";
139         }
140         else {
141             return "unchecked";
142         }
143     }
144
145     /**
146      * Override of default clickAction that makes this radio button the selected
147      * one when it is clicked
148      *
149      * @param defaultPage The default page to return if the action does not
150      * load a new page.
151      * @return The page that is currently loaded after execution of this method
152      * @throws IOException If an IO error occured
153      */

154     protected Page doClickAction(final Page defaultPage) throws IOException JavaDoc {
155         setChecked(true);
156         return defaultPage;
157     }
158
159 }
160
161
Popular Tags