001: package com.meterware.httpunit;
002:
003: /********************************************************************************************************************
004: * $Id: Button.java,v 1.15 2004/06/29 23:24:12 russgold Exp $
005: *
006: * Copyright (c) 2002-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 com.meterware.httpunit.scripting.ScriptableDelegate;
024:
025: import java.io.IOException;
026:
027: import org.w3c.dom.Node;
028: import org.xml.sax.SAXException;
029:
030: /**
031: * A button in a form.
032: *
033: * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
034: **/
035: public class Button extends FormControl {
036:
037: static final public HTMLElementPredicate WITH_ID;
038: static final public HTMLElementPredicate WITH_LABEL;
039:
040: private String _onClickEvent = "";
041: private WebResponse _baseResponse;
042:
043: public String getType() {
044: return BUTTON_TYPE;
045: }
046:
047: Button(WebForm form) {
048: super (form);
049: }
050:
051: Button(WebForm form, Node node) {
052: super (form, node);
053: _onClickEvent = NodeUtils.getNodeAttribute(node, "onclick");
054: }
055:
056: Button(WebResponse response, Node node) {
057: super (null, node);
058: _onClickEvent = NodeUtils.getNodeAttribute(node, "onclick");
059: _baseResponse = response;
060: }
061:
062: /**
063: * Returns the value associated with this button.
064: **/
065: public String getValue() {
066: return getValueAttribute();
067: }
068:
069: /**
070: * Performs the action associated with clicking this button after running any 'onClick' script.
071: * For a submit button this typically submits the form.
072: */
073: public void click() throws IOException, SAXException {
074: verifyButtonEnabled();
075: if (doOnClickEvent())
076: doButtonAction();
077: }
078:
079: protected void verifyButtonEnabled() {
080: if (isDisabled())
081: throw new IllegalStateException("Button"
082: + (getName().length() == 0 ? "" : " '" + getName()
083: + "'")
084: + " is disabled and may not be clicked.");
085: }
086:
087: /**
088: * Returns true if this button is disabled, meaning that it cannot be clicked.
089: **/
090: public boolean isDisabled() {
091: return super .isDisabled();
092: }
093:
094: /**
095: * Does the 'onClick' event defined for this button.
096: * @return true if subsequent actions should be performed.
097: */
098: final protected boolean doOnClickEvent() {
099: return _onClickEvent.length() == 0
100: || getScriptableDelegate().doEvent(_onClickEvent);
101: }
102:
103: /**
104: * Perform the normal action of this button.
105: */
106: protected void doButtonAction() throws IOException, SAXException {
107: }
108:
109: //-------------------------------------------------- FormControl methods -----------------------------------------------
110:
111: String[] getValues() {
112: return new String[0];
113: }
114:
115: void addValues(ParameterProcessor processor, String characterSet)
116: throws IOException {
117: }
118:
119: protected ScriptableDelegate newScriptable() {
120: return new Scriptable();
121: }
122:
123: protected ScriptableDelegate getParentDelegate() {
124: if (getForm() != null)
125: return super .getParentDelegate();
126: return _baseResponse.getScriptableObject().getDocument();
127: }
128:
129: class Scriptable extends FormControl.Scriptable {
130:
131: public void click() throws IOException, SAXException {
132: doButtonAction();
133: }
134: }
135:
136: static {
137: WITH_ID = new HTMLElementPredicate() {
138: public boolean matchesCriteria(Object button, Object id) {
139: return ((Button) button).getID().equals(id);
140: };
141: };
142:
143: WITH_LABEL = new HTMLElementPredicate() {
144: public boolean matchesCriteria(Object button, Object label) {
145: return ((Button) button).getValue().equals(label);
146: };
147: };
148:
149: }
150: }
|