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 com.gargoylesoftware.htmlunit.BrowserVersion;
044: import com.gargoylesoftware.htmlunit.KeyValuePair;
045: import com.gargoylesoftware.htmlunit.Page;
046:
047: /**
048: * Wrapper for the html element "button"
049: *
050: * @version $Revision: 2132 $
051: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
052: * @author David K. Taylor
053: * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
054: * @author David D. Kilzer
055: * @author Daniel Gredler
056: * @author Ahmed Ashour
057: */
058: public class HtmlButton extends FocusableElement implements
059: DisabledElement, SubmittableElement {
060:
061: private static final long serialVersionUID = 4828725767615187345L;
062:
063: /** the HTML tag represented by this element */
064: public static final String TAG_NAME = "button";
065:
066: /**
067: * Create an instance
068: *
069: * @param page The page that contains this element
070: * @param attributes the initial attributes
071: * @deprecated You should not directly construct HtmlButton.
072: */
073: //TODO: to be removed, deprecated after 1.11
074: public HtmlButton(final HtmlPage page, final Map attributes) {
075: this (null, TAG_NAME, page, attributes);
076: }
077:
078: /**
079: * Create an instance
080: *
081: * @param namespaceURI the URI that identifies an XML namespace.
082: * @param qualifiedName The qualified name of the element type to instantiate
083: * @param page The page that contains this element
084: * @param attributes the initial attributes
085: */
086: HtmlButton(final String namespaceURI, final String qualifiedName,
087: final HtmlPage page, final Map attributes) {
088: super (namespaceURI, qualifiedName, page, attributes);
089: }
090:
091: /**
092: * Set the content of the "value" attribute
093: *
094: * @param newValue The new content
095: */
096: public void setValueAttribute(final String newValue) {
097: setAttributeValue("value", newValue);
098: }
099:
100: /**
101: * This method will be called if there either wasn't an onclick handler or there was
102: * but the result of that handler was true. This is the default behavior of clicking
103: * the element. The default implementation returns the current page - subclasses
104: * requiring different behavior (like {@link HtmlSubmitInput}) will override this
105: * method.
106: *
107: * @param defaultPage The default page to return if the action does not
108: * load a new page.
109: * @return The page that is currently loaded after execution of this method
110: * @throws IOException If an IO error occurred
111: */
112: protected Page doClickAction(final Page defaultPage)
113: throws IOException {
114: final String type = getTypeAttribute().toLowerCase();
115:
116: final HtmlForm form = getEnclosingForm();
117: if (form != null) {
118: if (type.equals("submit")) {
119: return form.submit(this );
120: } else if (type.equals("reset")) {
121: return form.reset();
122: }
123: }
124:
125: return defaultPage;
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: * Return an array of KeyValuePairs that are the values that will be sent
138: * back to the server whenever the current form is submitted.<p>
139: *
140: * THIS METHOD IS INTENDED FOR THE USE OF THE FRAMEWORK ONLY AND SHOULD NOT
141: * BE USED BY CONSUMERS OF HTMLUNIT. USE AT YOUR OWN RISK.
142: *
143: * @return See above
144: */
145: public KeyValuePair[] getSubmitKeyValuePairs() {
146: return new KeyValuePair[] { new KeyValuePair(
147: getNameAttribute(), getValueAttribute()) };
148: }
149:
150: /**
151: * {@inheritDoc}
152: * @see SubmittableElement#reset()
153: */
154: public void reset() {
155: getLog().debug("reset() not implemented for this element");
156: }
157:
158: /**
159: * {@inheritDoc}
160: * @see SubmittableElement#setDefaultValue(String)
161: */
162: public void setDefaultValue(final String defaultValue) {
163: getLog().debug(
164: "setDefaultValue() not implemented for this element");
165: }
166:
167: /**
168: * {@inheritDoc}
169: * @see SubmittableElement#getDefaultValue()
170: */
171: public String getDefaultValue() {
172: getLog().debug(
173: "getDefaultValue() not implemented for this element");
174: return "";
175: }
176:
177: /**
178: * {@inheritDoc} This implementation is empty; only checkboxes and radio buttons
179: * really care what the default checked value is.
180: * @see SubmittableElement#setDefaultChecked(boolean)
181: * @see HtmlRadioButtonInput#setDefaultChecked(boolean)
182: * @see HtmlCheckBoxInput#setDefaultChecked(boolean)
183: */
184: public void setDefaultChecked(final boolean defaultChecked) {
185: // Empty.
186: }
187:
188: /**
189: * {@inheritDoc} This implementation returns <tt>false</tt>; only checkboxes and
190: * radio buttons really care what the default checked value is.
191: * @see SubmittableElement#isDefaultChecked()
192: * @see HtmlRadioButtonInput#isDefaultChecked()
193: * @see HtmlCheckBoxInput#isDefaultChecked()
194: */
195: public boolean isDefaultChecked() {
196: return false;
197: }
198:
199: /**
200: * Return the value of the attribute "name". Refer to the
201: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
202: * documentation for details on the use of this attribute.
203: *
204: * @return The value of the attribute "name"
205: * or an empty string if that attribute isn't defined.
206: */
207: public final String getNameAttribute() {
208: return getAttributeValue("name");
209: }
210:
211: /**
212: * Return the value of the attribute "value". Refer to the
213: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
214: * documentation for details on the use of this attribute.
215: *
216: * @return The value of the attribute "value"
217: * or an empty string if that attribute isn't defined.
218: */
219: public final String getValueAttribute() {
220: return getAttributeValue("value");
221: }
222:
223: /**
224: * Return the value of the attribute "type". 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. Note that Internet
227: * Explorer doesn't follow the spec when the type isn't specified. It will return
228: * "button" rather than the "submit" specified in the spec.
229: *
230: * @return The value of the attribute "type"
231: * or the default value if that attribute isn't defined.
232: */
233: public final String getTypeAttribute() {
234: String type = getAttributeValue("type");
235: if (type == HtmlElement.ATTRIBUTE_NOT_DEFINED) {
236: final BrowserVersion browser = getPage().getWebClient()
237: .getBrowserVersion();
238: if (browser.isIE()) {
239: type = "button";
240: } else {
241: type = "submit";
242: }
243: }
244: return type;
245: }
246:
247: /**
248: * Return the value of the attribute "disabled". Refer to the
249: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
250: * documentation for details on the use of this attribute.
251: *
252: * @return The value of the attribute "disabled"
253: * or an empty string if that attribute isn't defined.
254: */
255: public final String getDisabledAttribute() {
256: return getAttributeValue("disabled");
257: }
258:
259: /**
260: * Return the value of the attribute "tabindex". Refer to the
261: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
262: * documentation for details on the use of this attribute.
263: *
264: * @return The value of the attribute "tabindex"
265: * or an empty string if that attribute isn't defined.
266: */
267: public final String getTabIndexAttribute() {
268: return getAttributeValue("tabindex");
269: }
270:
271: /**
272: * Return the value of the attribute "accesskey". 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 "accesskey"
277: * or an empty string if that attribute isn't defined.
278: */
279: public final String getAccessKeyAttribute() {
280: return getAttributeValue("accesskey");
281: }
282:
283: /**
284: * Return the value of the attribute "onfocus". Refer to the
285: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
286: * documentation for details on the use of this attribute.
287: *
288: * @return The value of the attribute "onfocus"
289: * or an empty string if that attribute isn't defined.
290: */
291: public final String getOnFocusAttribute() {
292: return getAttributeValue("onfocus");
293: }
294:
295: /**
296: * Return the value of the attribute "onblur". Refer to the
297: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
298: * documentation for details on the use of this attribute.
299: *
300: * @return The value of the attribute "onblur"
301: * or an empty string if that attribute isn't defined.
302: */
303: public final String getOnBlurAttribute() {
304: return getAttributeValue("onblur");
305: }
306: }
|