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 org.apache.commons.lang.StringUtils;
044:
045: import com.gargoylesoftware.htmlunit.ElementNotFoundException;
046: import com.gargoylesoftware.htmlunit.KeyValuePair;
047: import com.gargoylesoftware.htmlunit.Page;
048:
049: /**
050: * Wrapper for the html element "input"
051: *
052: * @version $Revision: 2132 $
053: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
054: * @author David K. Taylor
055: * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
056: * @author Marc Guillemot
057: * @author Daniel Gredler
058: * @author Ahmed Ashour
059: */
060: public class HtmlImageInput extends HtmlInput {
061:
062: private static final long serialVersionUID = -2955826367201282767L;
063:
064: // For click with x, y position.
065: private boolean wasPositionSpecified_;
066: private int xPosition_;
067: private int yPosition_;
068:
069: /**
070: * Create an instance
071: *
072: * @param page The page that contains this element
073: * @param attributes the initial attributes
074: * @deprecated You should not directly construct HtmlImageInput.
075: */
076: //TODO: to be removed, deprecated after 1.11
077: public HtmlImageInput(final HtmlPage page, final Map attributes) {
078: this (null, TAG_NAME, page, attributes);
079: }
080:
081: /**
082: * Create an instance
083: *
084: * @param namespaceURI the URI that identifies an XML namespace.
085: * @param qualifiedName The qualified name of the element type to instantiate
086: * @param page The page that contains this element
087: * @param attributes the initial attributes
088: */
089: HtmlImageInput(final String namespaceURI,
090: final String qualifiedName, final HtmlPage page,
091: final Map attributes) {
092: super (namespaceURI, qualifiedName, page, attributes);
093: }
094:
095: /**
096: * Return an array of KeyValuePairs that are the values that will be sent
097: * back to the server whenever the current form is submitted.<p>
098: *
099: * THIS METHOD IS INTENDED FOR THE USE OF THE FRAMEWORK ONLY AND SHOULD NOT
100: * BE USED BY CONSUMERS OF HTMLUNIT. USE AT YOUR OWN RISK.
101: *
102: * @return See above
103: */
104: public KeyValuePair[] getSubmitKeyValuePairs() {
105: final String name = getNameAttribute();
106: final String prefix;
107: // a clicked image without name sends parameter x and y
108: if (StringUtils.isEmpty(name)) {
109: prefix = "";
110: } else {
111: prefix = name + ".";
112: }
113:
114: if (wasPositionSpecified_) {
115: return new KeyValuePair[] {
116: new KeyValuePair(prefix + 'x', String
117: .valueOf(xPosition_)),
118: new KeyValuePair(prefix + 'y', String
119: .valueOf(yPosition_)) };
120: }
121: return new KeyValuePair[] { new KeyValuePair(
122: getNameAttribute(), getValueAttribute()) };
123: }
124:
125: /**
126: * Submit the form that contains this input. Only a couple of the inputs
127: * support this method so it is made protected here. Those subclasses
128: * that wish to expose it will override and make it public.
129: *
130: * @return The Page that is the result of submitting this page to the server
131: * @exception IOException If an io error occurs
132: */
133: public Page click() throws IOException {
134: return click(0, 0);
135: }
136:
137: /**
138: * This method will be called if there either wasn't an onclick handler or there was
139: * but the result of that handler was true. This is the default behavior of clicking
140: * the element. The default implementation returns the current page - subclasses
141: * requiring different behavior (like {@link HtmlSubmitInput}) will override this
142: * method.
143: *
144: * @param defaultPage The default page to return if the action does not
145: * load a new page.
146: * @return The page that is currently loaded after execution of this method
147: * @throws IOException If an IO error occurred
148: */
149: protected Page doClickAction(final Page defaultPage)
150: throws IOException {
151: final HtmlForm form = getEnclosingForm();
152: if (form != null) {
153: return form.submit(this );
154: } else {
155: return super .doClickAction(defaultPage);
156: }
157: }
158:
159: /**
160: * Simulate clicking this input with a pointing device. The x and y coordinates
161: * of the pointing device will be sent to the server.
162: *
163: * @param x The x coordinate of the pointing device at the time of clicking
164: * @param y The y coordinate of the pointing device at the time of clicking
165: * @return The page that is loaded after the click has taken place.
166: * @exception IOException If an io error occurs
167: * @exception ElementNotFoundException If a particular xml element could not be found in the dom model
168: */
169: public Page click(final int x, final int y) throws IOException,
170: ElementNotFoundException {
171:
172: wasPositionSpecified_ = true;
173: xPosition_ = x;
174: yPosition_ = y;
175: return super .click();
176: }
177:
178: /**
179: * {@inheritDoc} Also sets the value to the new default value, just like IE.
180: * @see SubmittableElement#setDefaultValue(String)
181: */
182: public void setDefaultValue(final String defaultValue) {
183: super.setDefaultValue(defaultValue);
184: setValueAttribute(defaultValue);
185: }
186: }
|