KickJava   Java API By Example, From Geeks To Geeks.

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


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.Assert;
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 "input"
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  * @author Marc Guillemot
57  */

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

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

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

85     public void setValueAttribute( final String JavaDoc newValue ) {
86         Assert.notNull( "newValue", newValue );
87         setAttributeValue( "value", newValue );
88
89         getPage().executeOnChangeHandlerIfAppropriate(this);
90     }
91
92
93     /**
94      * Return an array of KeyValuePairs that are the values that will be sent
95      * back to the server whenever the current form is submitted.<p>
96      *
97      * THIS METHOD IS INTENDED FOR THE USE OF THE FRAMEWORK ONLY AND SHOULD NOT
98      * BE USED BY CONSUMERS OF HTMLUNIT. USE AT YOUR OWN RISK.
99      *
100      * @return See above
101      */

102     public KeyValuePair[] getSubmitKeyValuePairs() {
103         return new KeyValuePair[]{new KeyValuePair( getNameAttribute(), getValueAttribute() )};
104     }
105
106     /**
107      * Return a text representation of this element that represents what would
108      * be visible to the user if this page was shown in a web browser. For
109      * example, a select element would return the currently selected value as
110      * text
111      *
112      * @return The element as text
113      */

114     public String JavaDoc asText() {
115         return getValueAttribute();
116     }
117
118
119     /**
120      * Return the value of the attribute "type". Refer to the
121      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
122      * documentation for details on the use of this attribute.
123      *
124      * @return The value of the attribute "type"
125      * or an empty string if that attribute isn't defined.
126      */

127     public final String JavaDoc getTypeAttribute() {
128         return getAttributeValue("type");
129     }
130
131
132     /**
133      * Return the value of the attribute "name". Refer to the
134      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
135      * documentation for details on the use of this attribute.
136      *
137      * @return The value of the attribute "name"
138      * or an empty string if that attribute isn't defined.
139      */

140     public final String JavaDoc getNameAttribute() {
141         return getAttributeValue("name");
142     }
143
144
145     /**
146      * <p>Return the value of the attribute "value". Refer to the
147      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
148      * documentation for details on the use of this attribute.</p>
149      *
150      * @return The value of the attribute "value" or an empty string if that
151      * attribute isn't defined
152      */

153     public final String JavaDoc getValueAttribute() {
154         return getAttributeValue("value");
155     }
156
157
158     /**
159      * Return the value of the attribute "checked". Refer to the
160      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
161      * documentation for details on the use of this attribute.
162      *
163      * @return The value of the attribute "checked"
164      * or an empty string if that attribute isn't defined.
165      */

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

179     public final String JavaDoc getDisabledAttribute() {
180         return getAttributeValue("disabled");
181     }
182
183
184     /**
185      * Return true if the disabled attribute is set for this element.
186      * @return Return true if this is disabled.
187      */

188     public final boolean isDisabled() {
189         return isAttributeDefined("disabled");
190     }
191
192
193     /**
194      * Return the value of the attribute "readonly". Refer to the
195      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
196      * documentation for details on the use of this attribute.
197      *
198      * @return The value of the attribute "readonly"
199      * or an empty string if that attribute isn't defined.
200      */

201     public final String JavaDoc getReadOnlyAttribute() {
202         return getAttributeValue("readonly");
203     }
204
205
206     /**
207      * Return the value of the attribute "size". Refer to the
208      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
209      * documentation for details on the use of this attribute.
210      *
211      * @return The value of the attribute "size"
212      * or an empty string if that attribute isn't defined.
213      */

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

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

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

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

266     public final String JavaDoc getUseMapAttribute() {
267         return getAttributeValue("usemap");
268     }
269
270
271     /**
272      * Return the value of the attribute "tabindex". Refer to the
273      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
274      * documentation for details on the use of this attribute.
275      *
276      * @return The value of the attribute "tabindex"
277      * or an empty string if that attribute isn't defined.
278      */

279     public final String JavaDoc getTabIndexAttribute() {
280         return getAttributeValue("tabindex");
281     }
282
283
284     /**
285      * Return the value of the attribute "accesskey". Refer to the
286      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
287      * documentation for details on the use of this attribute.
288      *
289      * @return The value of the attribute "accesskey"
290      * or an empty string if that attribute isn't defined.
291      */

292     public final String JavaDoc getAccessKeyAttribute() {
293         return getAttributeValue("accesskey");
294     }
295
296
297     /**
298      * Return the value of the attribute "onfocus". Refer to the
299      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
300      * documentation for details on the use of this attribute.
301      *
302      * @return The value of the attribute "onfocus"
303      * or an empty string if that attribute isn't defined.
304      */

305     public final String JavaDoc getOnFocusAttribute() {
306         return getAttributeValue("onfocus");
307     }
308
309
310     /**
311      * Return the value of the attribute "onblur". Refer to the
312      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
313      * documentation for details on the use of this attribute.
314      *
315      * @return The value of the attribute "onblur"
316      * or an empty string if that attribute isn't defined.
317      */

318     public final String JavaDoc getOnBlurAttribute() {
319         return getAttributeValue("onblur");
320     }
321
322
323     /**
324      * Return the value of the attribute "onselect". Refer to the
325      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
326      * documentation for details on the use of this attribute.
327      *
328      * @return The value of the attribute "onselect"
329      * or an empty string if that attribute isn't defined.
330      */

331     public final String JavaDoc getOnSelectAttribute() {
332         return getAttributeValue("onselect");
333     }
334
335
336     /**
337      * Return the value of the attribute "onchange". Refer to the
338      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
339      * documentation for details on the use of this attribute.
340      *
341      * @return The value of the attribute "onchange"
342      * or an empty string if that attribute isn't defined.
343      */

344     public final String JavaDoc getOnChangeAttribute() {
345         return getAttributeValue("onchange");
346     }
347
348
349     /**
350      * Return the value of the attribute "accept". Refer to the
351      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
352      * documentation for details on the use of this attribute.
353      *
354      * @return The value of the attribute "accept"
355      * or an empty string if that attribute isn't defined.
356      */

357     public final String JavaDoc getAcceptAttribute() {
358         return getAttributeValue("accept");
359     }
360
361
362     /**
363      * Return the value of the attribute "align". Refer to the
364      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
365      * documentation for details on the use of this attribute.
366      *
367      * @return The value of the attribute "align"
368      * or an empty string if that attribute isn't defined.
369      */

370     public final String JavaDoc getAlignAttribute() {
371         return getAttributeValue("align");
372     }
373
374
375     /**
376      * Reset this element to its original values.
377      */

378     public void reset() {
379         // By default this does nothing. Derived classes will override.
380
}
381
382     /**
383      * Set the "checked" attribute
384      *
385      * @param isChecked true if this element is to be selected
386      */

387     public void setChecked( final boolean isChecked ) {
388         // By default this does nothing. Derived classes will override.
389
}
390
391
392     /**
393      * Return true if this element is currently selected
394      *
395      * @return See above
396      */

397     public boolean isChecked() {
398          return isAttributeDefined("checked");
399     }
400
401
402     /**
403      * Simulate clicking this input with a pointing device. The x and y coordinates
404      * of the pointing device will be sent to the server.
405      *
406      * @param x The x coordinate of the pointing device at the time of clicking
407      * @param y The y coordinate of the pointing device at the time of clicking
408      * @return The page that is loaded after the click has taken place.
409      * @exception IOException If an io error occurs
410      * @exception ElementNotFoundException If a particular xml element could
411      * not be found in the dom model
412      */

413     public Page click( final int x, final int y )
414         throws
415             IOException JavaDoc,
416             ElementNotFoundException {
417
418         // By default this is no different than a click without coordinates.
419
return click();
420     }
421
422     /**
423      * Submit the form that contains this input
424      *
425      * @deprecated Use {@link #click()} instead
426      * @return The Page that is the result of submitting this page to the
427      * server
428      * @exception IOException If an io error occurs
429      * @exception ElementNotFoundException If a particular xml element could
430      * not be found in the dom model
431      */

432     public Page submit()
433         throws
434             IOException JavaDoc,
435             ElementNotFoundException {
436
437         return click();
438     }
439
440
441 }
442
Popular Tags