KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.gargoylesoftware.htmlunit.ElementNotFoundException;
41 import com.gargoylesoftware.htmlunit.KeyValuePair;
42 import com.gargoylesoftware.htmlunit.Page;
43 import java.io.IOException JavaDoc;
44 import java.util.Map JavaDoc;
45
46 import org.apache.commons.lang.StringUtils;
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 Marc Guillemot
56  */

57 public class HtmlImageInput extends HtmlInput {
58
59     // For click with x, y position.
60
private boolean wasPositionSpecified_ = false;
61     private int xPosition_;
62     private int yPosition_;
63
64     /**
65      * Create an instance
66      *
67      * @param page The page that contains this element
68      * @param attributes the initial attributes
69      */

70     public HtmlImageInput( final HtmlPage page, final Map JavaDoc attributes ) {
71         super( page, attributes );
72     }
73
74
75     /**
76      * Return an array of KeyValuePairs that are the values that will be sent
77      * back to the server whenever the current form is submitted.<p>
78      *
79      * THIS METHOD IS INTENDED FOR THE USE OF THE FRAMEWORK ONLY AND SHOULD NOT
80      * BE USED BY CONSUMERS OF HTMLUNIT. USE AT YOUR OWN RISK.
81      *
82      * @return See above
83      */

84     public KeyValuePair[] getSubmitKeyValuePairs() {
85         final String JavaDoc name = getNameAttribute();
86         final String JavaDoc prefix;
87         // a clicked image without name sends parameter x and y
88
if (StringUtils.isEmpty(name)) {
89             prefix = "";
90         }
91         else {
92             prefix = name + ".";
93         }
94             
95         if( wasPositionSpecified_ ) {
96             return new KeyValuePair[]{
97                 new KeyValuePair(prefix + "x", String.valueOf(xPosition_)),
98                 new KeyValuePair(prefix + "y", String.valueOf(yPosition_))
99             };
100         }
101         return new KeyValuePair[]{new KeyValuePair( getNameAttribute(), getValueAttribute() )};
102     }
103
104
105     /**
106      * Submit the form that contains this input. Only a couple of the inputs
107      * support this method so it is made protected here. Those subclasses
108      * that wish to expose it will override and make it public.
109      *
110      * @return The Page that is the result of submitting this page to the
111      * server
112      * @exception IOException If an io error occurs
113      */

114     public Page click() throws IOException JavaDoc {
115         return click(0,0);
116     }
117
118     /**
119      * This method will be called if there either wasn't an onclick handler or there was
120      * but the result of that handler was true. This is the default behaviour of clicking
121      * the element. The default implementation returns the current page - subclasses
122      * requiring different behaviour (like {@link HtmlSubmitInput}) will override this
123      * method.
124      *
125      * @param defaultPage The default page to return if the action does not
126      * load a new page.
127      * @return The page that is currently loaded after execution of this method
128      * @throws IOException If an IO error occured
129      */

130     protected Page doClickAction(final Page defaultPage) throws IOException JavaDoc {
131         return getEnclosingFormOrDie().submit(this);
132     }
133
134
135     /**
136      * Simulate clicking this input with a pointing device. The x and y coordinates
137      * of the pointing device will be sent to the server.
138      *
139      * @param x The x coordinate of the pointing device at the time of clicking
140      * @param y The y coordinate of the pointing device at the time of clicking
141      * @return The page that is loaded after the click has taken place.
142      * @exception IOException If an io error occurs
143      * @exception ElementNotFoundException If a particular xml element could
144      * not be found in the dom model
145      */

146     public Page click( final int x, final int y )
147         throws
148             IOException JavaDoc,
149             ElementNotFoundException {
150
151         wasPositionSpecified_ = true;
152         xPosition_ = x;
153         yPosition_ = y;
154         return super.click();
155     }
156 }
157
158
Popular Tags