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.javascript.host;
039:
040: import org.xml.sax.helpers.AttributesImpl;
041:
042: import com.gargoylesoftware.htmlunit.html.HtmlInput;
043: import com.gargoylesoftware.htmlunit.html.InputElementFactory;
044:
045: /**
046: * The javascript object for form input elements (html tag <input ...>).
047: *
048: * @version $Revision: 2132 $
049: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
050: * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
051: * @author Marc Guillemot
052: * @author Chris Erskine
053: * @author Ahmed Ashour
054: */
055: public class HTMLInputElement extends FormField {
056:
057: private static final long serialVersionUID = 3712016051364495710L;
058:
059: /**
060: * Create an instance.
061: */
062: public HTMLInputElement() {
063: }
064:
065: /**
066: * Javascript constructor. This must be declared in every javascript file
067: * because the rhino engine won't walk up the hierarchy looking for constructors.
068: */
069: public void jsConstructor() {
070: }
071:
072: /**
073: * Sets the value of the attribute "type".
074: * Note: this replace the DOM node with a new one.
075: * @param newType the new type to set
076: */
077: public void jsxSet_type(final String newType) {
078: HtmlInput input = getHtmlInputOrDie();
079:
080: if (!input.getTypeAttribute().equalsIgnoreCase(newType)) {
081: final AttributesImpl attributes = readAttributes(input);
082: final int index = attributes.getIndex("type");
083: attributes.setValue(index, newType);
084:
085: final HtmlInput newInput = (HtmlInput) InputElementFactory.instance
086: .createElement(input.getPage(), "input", attributes);
087:
088: // Added check to make sure there is a previous sibling before trying to replace
089: // newly created input variable which has yet to be inserted into DOM tree
090: if (input.getPreviousDomSibling() != null) {
091: // if the input has a previous sibling, then it was already in the
092: // DOM tree and can be replaced
093: input.replace(newInput);
094: } else {
095: // the input hasn't yet been inserted into the DOM tree (likely has been
096: // created via document.createElement()), so simply replace it with the
097: // new Input instance created in the code above
098: input = newInput;
099: }
100:
101: input.setScriptObject(null);
102: setDomNode(newInput, true);
103: }
104: }
105:
106: /**
107: * Set the checked property. Although this property is defined in Input it
108: * doesn't make any sense for input's other than checkbox and radio. This
109: * implementation does nothing. The implementations in Checkbox and Radio
110: * actually do the work.
111: *
112: * @param checked True if this input should have the "checked" attribute set
113: */
114: public void jsxSet_checked(final boolean checked) {
115: ((HtmlInput) getDomNodeOrDie()).setChecked(checked);
116: }
117:
118: /**
119: * Commodity for <code>(HtmlInput) getHtmlElementOrDie()</code>
120: * @return the bound html input
121: */
122: protected HtmlInput getHtmlInputOrDie() {
123: return (HtmlInput) getHtmlElementOrDie();
124: }
125:
126: /**
127: * Return the value of the checked property. Although this property is
128: * defined in Input it doesn't make any sense for input's other than
129: * checkbox and radio. This implementation does nothing. The
130: * implementations in Checkbox and Radio actually do the work.
131: *
132: *@return The checked property.
133: */
134: public boolean jsxGet_checked() {
135: return ((HtmlInput) getDomNodeOrDie()).isChecked();
136: }
137:
138: /**
139: * Uses {@link #jsxSet_type(String)} if attribute's name is type to
140: * replace DOM node as well as long as we have subclasses of {@link HtmlInput}.
141: * {@inheritDoc}
142: */
143: public void jsxFunction_setAttribute(final String name,
144: final String value) {
145: if ("type".equals(name)) {
146: jsxSet_type(value);
147: } else {
148: super .jsxFunction_setAttribute(name, value);
149: }
150: }
151:
152: /**
153: * Returns the input's default value, used if the containing form gets reset.
154: * @return The input's default value, used if the containing form gets reset.
155: * @see <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/defaultvalue.asp">
156: * MSDN Documentation</a>
157: */
158: public String jsxGet_defaultValue() {
159: return ((HtmlInput) getDomNodeOrDie()).getDefaultValue();
160: }
161:
162: /**
163: * Sets the input's default value, used if the containing form gets reset.
164: * @param defaultValue The input's default value, used if the containing form gets reset.
165: * @see <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/defaultvalue.asp">
166: * MSDN Documentation</a>
167: */
168: public void jsxSet_defaultValue(final String defaultValue) {
169: ((HtmlInput) getDomNodeOrDie()).setDefaultValue(defaultValue);
170: }
171:
172: /**
173: * Returns the input's default checked value, used if the containing form gets reset.
174: * @return The input's default checked value, used if the containing form gets reset.
175: * @see <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/defaultchecked.asp">
176: * MSDN Documentation</a>
177: */
178: public boolean jsxGet_defaultChecked() {
179: return ((HtmlInput) getDomNodeOrDie()).isDefaultChecked();
180: }
181:
182: /**
183: * Sets the input's default checked value, used if the containing form gets reset.
184: * @param defaultChecked The input's default checked value, used if the containing form gets reset.
185: * @see <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/defaultchecked.asp">
186: * MSDN Documentation</a>
187: */
188: public void jsxSet_defaultChecked(final boolean defaultChecked) {
189: ((HtmlInput) getDomNodeOrDie())
190: .setDefaultChecked(defaultChecked);
191: }
192:
193: }
|