KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.gargoylesoftware.htmlunit.BrowserVersion;
44 import com.gargoylesoftware.htmlunit.ElementNotFoundException;
45 import com.gargoylesoftware.htmlunit.KeyValuePair;
46 import com.gargoylesoftware.htmlunit.Page;
47
48 /**
49  * Wrapper for the html element "button"
50  *
51  * @version $Revision: 100 $
52  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
53  * @author David K. Taylor
54  * @author <a HREF="mailto:cse@dynabean.de">Christian Sell</a>
55  * @author David D. Kilzer
56  */

57 public class HtmlButton extends FocusableElement implements DisabledElement, SubmittableElement {
58
59     /** the HTML tag represented by this element */
60     public static final String JavaDoc TAG_NAME = "button";
61
62     /**
63      * Create an instance
64      *
65      * @param page The page that contains this element
66      * @param attributes the initial attributes
67      */

68     public HtmlButton( final HtmlPage page, final Map JavaDoc attributes) {
69         super(page, attributes);
70     }
71
72     /**
73      * @return the HTML tag name
74      */

75     public String JavaDoc getTagName() {
76         return TAG_NAME;
77     }
78
79     /**
80      * Set the content of the "value" attribute
81      *
82      * @param newValue The new content
83      */

84     public void setValueAttribute( final String JavaDoc newValue ) {
85         setAttributeValue( "value", newValue );
86     }
87
88     /**
89      * Submit the form.
90      *
91      * @deprecated Use {@link #click()} instead
92      * @return The Page that wraps the response from the server
93      * @exception IOException If an IO problem occurs
94      * @exception ElementNotFoundException If a particular xml element could
95      * not be found in the dom model
96      */

97     public Page submit() throws IOException JavaDoc, ElementNotFoundException {
98
99         return click();
100     }
101
102     /**
103      * This method will be called if there either wasn't an onclick handler or there was
104      * but the result of that handler was true. This is the default behaviour of clicking
105      * the element. The default implementation returns the current page - subclasses
106      * requiring different behaviour (like {@link HtmlSubmitInput}) will override this
107      * method.
108      *
109      * @param defaultPage The default page to return if the action does not
110      * load a new page.
111      * @return The page that is currently loaded after execution of this method
112      * @throws IOException If an IO error occured
113      */

114     protected Page doClickAction(final Page defaultPage) throws IOException JavaDoc {
115         final String JavaDoc type = getTypeAttribute().toLowerCase();
116         if (type.equals("submit")) {
117             return getEnclosingFormOrDie().submit(this);
118         }
119         else if (type.equals("reset")){
120             return getEnclosingFormOrDie().reset();
121         }
122         else {
123             return defaultPage;
124         }
125     }
126
127
128     /**
129      * Return true if the disabled attribute is set for this element.
130      * @return Return true if this is disabled.
131      */

132     public final boolean isDisabled() {
133         return isAttributeDefined("disabled");
134     }
135
136
137     /**
138      * Return an array of KeyValuePairs that are the values that will be sent
139      * back to the server whenever the current form is submitted.<p>
140      *
141      * THIS METHOD IS INTENDED FOR THE USE OF THE FRAMEWORK ONLY AND SHOULD NOT
142      * BE USED BY CONSUMERS OF HTMLUNIT. USE AT YOUR OWN RISK.
143      *
144      * @return See above
145      */

146     public KeyValuePair[] getSubmitKeyValuePairs() {
147         return new KeyValuePair[]{new KeyValuePair( getNameAttribute(), getValueAttribute() )};
148     }
149
150
151     /**
152      * Return the value of this element to what it was at the time the page was loaded.
153      */

154     public void reset() {
155         getLog().debug("reset() not implemented for this element");
156     }
157
158
159     /**
160      * Return the value of the attribute "name". Refer to the
161      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
162      * documentation for details on the use of this attribute.
163      *
164      * @return The value of the attribute "name"
165      * or an empty string if that attribute isn't defined.
166      */

167     public final String JavaDoc getNameAttribute() {
168         return getAttributeValue("name");
169     }
170
171
172     /**
173      * Return the value of the attribute "value". Refer to the
174      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
175      * documentation for details on the use of this attribute.
176      *
177      * @return The value of the attribute "value"
178      * or an empty string if that attribute isn't defined.
179      */

180     public final String JavaDoc getValueAttribute() {
181         return getAttributeValue("value");
182     }
183
184
185     /**
186      * Return the value of the attribute "type". Refer to the
187      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
188      * documentation for details on the use of this attribute. Note that Internet
189      * Explorer doesn't follow the spec when the type isn't specified. It will return
190      * "button" rather than the "submit" specified in the spec.
191      *
192      * @return The value of the attribute "type"
193      * or the default value if that attribute isn't defined.
194      */

195     public final String JavaDoc getTypeAttribute() {
196         String JavaDoc type = getAttributeValue("type");
197         if( type == HtmlElement.ATTRIBUTE_NOT_DEFINED ) {
198             final BrowserVersion browser = getPage().getWebClient().getBrowserVersion();
199             if( browser.getApplicationName().equals(BrowserVersion.INTERNET_EXPLORER) ) {
200                 type = "button";
201             }
202             else {
203                 type = "submit";
204             }
205         }
206         return type;
207     }
208
209
210     /**
211      * Return the value of the attribute "disabled". Refer to the
212      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
213      * documentation for details on the use of this attribute.
214      *
215      * @return The value of the attribute "disabled"
216      * or an empty string if that attribute isn't defined.
217      */

218     public final String JavaDoc getDisabledAttribute() {
219         return getAttributeValue("disabled");
220     }
221
222
223     /**
224      * Return the value of the attribute "tabindex". Refer to the
225      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
226      * documentation for details on the use of this attribute.
227      *
228      * @return The value of the attribute "tabindex"
229      * or an empty string if that attribute isn't defined.
230      */

231     public final String JavaDoc getTabIndexAttribute() {
232         return getAttributeValue("tabindex");
233     }
234
235
236     /**
237      * Return the value of the attribute "accesskey". Refer to the
238      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
239      * documentation for details on the use of this attribute.
240      *
241      * @return The value of the attribute "accesskey"
242      * or an empty string if that attribute isn't defined.
243      */

244     public final String JavaDoc getAccessKeyAttribute() {
245         return getAttributeValue("accesskey");
246     }
247
248
249     /**
250      * Return the value of the attribute "onfocus". Refer to the
251      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
252      * documentation for details on the use of this attribute.
253      *
254      * @return The value of the attribute "onfocus"
255      * or an empty string if that attribute isn't defined.
256      */

257     public final String JavaDoc getOnFocusAttribute() {
258         return getAttributeValue("onfocus");
259     }
260
261
262     /**
263      * Return the value of the attribute "onblur". Refer to the
264      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
265      * documentation for details on the use of this attribute.
266      *
267      * @return The value of the attribute "onblur"
268      * or an empty string if that attribute isn't defined.
269      */

270     public final String JavaDoc getOnBlurAttribute() {
271         return getAttributeValue("onblur");
272     }
273 }
274
Popular Tags