001: package com.meterware.httpunit;
002:
003: /********************************************************************************************************************
004: * $Id: SubmitButton.java,v 1.20 2004/09/29 17:15:24 russgold Exp $
005: *
006: * Copyright (c) 2000-2004, Russell Gold
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
009: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
011: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
014: * of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
017: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
019: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: *******************************************************************************************************************/
023: import java.io.IOException;
024:
025: import org.w3c.dom.Node;
026: import org.xml.sax.SAXException;
027:
028: /**
029: * This class represents a submit button in an HTML form.
030: **/
031: public class SubmitButton extends Button {
032:
033: private boolean _fake;
034:
035: public String getType() {
036: return (isImageButton() ? IMAGE_BUTTON_TYPE
037: : SUBMIT_BUTTON_TYPE);
038: }
039:
040: /**
041: * Returns true if this submit button is an image map.
042: **/
043: public boolean isImageButton() {
044: return _isImageButton;
045: }
046:
047: /**
048: * Performs the action associated with clicking this button after running any 'onClick' script.
049: * For a submit button this typically submits the form.
050: *
051: * @since 1.6
052: */
053: public void click(int x, int y) throws IOException, SAXException {
054: if (!isImageButton())
055: throw new IllegalStateException(
056: "May only specify positions for an image button");
057: verifyButtonEnabled();
058: if (doOnClickEvent())
059: getForm().doFormSubmit(this , x, y);
060: }
061:
062: //--------------------------------- Button methods ----------------------------------------------
063:
064: /**
065: * Perform the normal action of this button.
066: */
067: protected void doButtonAction() throws IOException, SAXException {
068: getForm().doFormSubmit(this );
069: }
070:
071: //------------------------------------ Object methods ----------------------------------------
072:
073: public String toString() {
074: return "Submit with " + getName() + "=" + getValue();
075: }
076:
077: public int hashCode() {
078: return getName().hashCode() + getValue().hashCode();
079: }
080:
081: public boolean equals(Object o) {
082: return getClass().equals(o.getClass())
083: && equals((SubmitButton) o);
084: }
085:
086: //------------------------------------------ package members ----------------------------------
087:
088: SubmitButton(WebForm form, Node node) {
089: super (form, node);
090: _isImageButton = NodeUtils.getNodeAttribute(node, "type")
091: .equalsIgnoreCase(IMAGE_BUTTON_TYPE);
092: }
093:
094: SubmitButton(WebForm form) {
095: super (form);
096: _isImageButton = false;
097: }
098:
099: static SubmitButton createFakeSubmitButton(WebForm form) {
100: return new SubmitButton(form, /* fake */true);
101: }
102:
103: private SubmitButton(WebForm form, boolean fake) {
104: this (form);
105: _fake = fake;
106: }
107:
108: boolean isFake() {
109: return _fake;
110: }
111:
112: void setPressed(boolean pressed) {
113: _pressed = pressed;
114: }
115:
116: void setLocation(int x, int y) {
117: _x = x;
118: _y = y;
119: }
120:
121: //--------------------------------- FormControl methods ----------------------------------------------------------------
122:
123: /**
124: * Returns the current value(s) associated with this control. These values will be transmitted to the server
125: * if the control is 'successful'.
126: **/
127: String[] getValues() {
128: return (isDisabled() || !_pressed) ? NO_VALUE
129: : toArray(getValueAttribute());
130: }
131:
132: void addValues(ParameterProcessor processor, String characterSet)
133: throws IOException {
134: if (_pressed && !isDisabled() && getName().length() > 0) {
135: if (getValueAttribute().length() > 0) {
136: processor.addParameter(getName(), getValueAttribute(),
137: characterSet);
138: }
139: if (_isImageButton) {
140: processor.addParameter(getName() + ".x", Integer
141: .toString(_x), characterSet);
142: processor.addParameter(getName() + ".y", Integer
143: .toString(_y), characterSet);
144: }
145: }
146: }
147:
148: //------------------------------------------ private members ----------------------------------
149:
150: private String[] _value = new String[1];
151: private final boolean _isImageButton;
152: private boolean _pressed;
153: private int _x;
154: private int _y;
155:
156: private String[] toArray(String value) {
157: _value[0] = value;
158: return _value;
159: }
160:
161: private boolean equals(SubmitButton button) {
162: return getName().equals(button.getName())
163: && (getName().length() == 0 || getValue().equals(
164: button.getValue()));
165: }
166: }
|