KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > host > Input


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.javascript.host;
39
40 import org.xml.sax.helpers.AttributesImpl JavaDoc;
41
42 import com.gargoylesoftware.htmlunit.html.HtmlInput;
43 import com.gargoylesoftware.htmlunit.html.InputElementFactory;
44
45 /**
46  * The javascript object for form input elements (html tag <input ...>).
47  *
48  * @version $Revision: 100 $
49  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
50  * @author <a HREF="mailto:cse@dynabean.de">Christian Sell</a>
51  * @author Marc Guillemot
52  * @author Chris Erskine
53  */

54 public class Input extends FormField {
55
56     private static final long serialVersionUID = 3712016051364495710L;
57
58
59     /**
60      * Create an instance.
61      */

62     public Input() {
63     }
64
65
66     /**
67      * Javascript constructor. This must be declared in every javascript file
68      * because the rhino engine won't walk up the hierarchy looking for
69      * constructors.
70      */

71     public void jsConstructor() {
72     }
73
74     /**
75      * Sets the value of the attribute "type".
76      * Note: this replace the DOM node with a new one.
77      * @param newType the new type to set
78      */

79     public void jsxSet_type(final String JavaDoc newType) {
80         HtmlInput input = getHtmlInputOrDie();
81
82         if (!input.getTypeAttribute().equalsIgnoreCase(newType)) {
83             final AttributesImpl JavaDoc attributes = readAttributes(input);
84             final int index = attributes.getIndex("type");
85             attributes.setValue(index, newType);
86
87             final HtmlInput newInput = (HtmlInput) InputElementFactory.instance
88                 .createElement(input.getPage(), "input", attributes);
89
90             // Added check to make sure there is a previous sibling before trying to replace
91
// newly created input variable which has yet to be inserted into DOM tree
92
if(input.getPreviousSibling() != null) {
93                 // if the input has a previous sibling, then it was already in the
94
// DOM tree and can be replaced
95
input.replace(newInput);
96             }
97             else {
98                 // the input hasn't yet been inserted into the DOM tree (likely has been
99
// created via document.createElement()), so simply replace it with the
100
// new Input instance created in the code above
101
input = newInput;
102             }
103             
104             input.setScriptObject(null);
105             setDomNode(newInput, true);
106         }
107     }
108
109     /**
110      * Set the checked property. Although this property is defined in Input it
111      * doesn't make any sense for input's other than checkbox and radio. This
112      * implementation does nothing. The implementations in Checkbox and Radio
113      * actually do the work.
114      *
115      *@param checked True if this input should have the "checked" attribute
116      * set
117      */

118     public void jsxSet_checked( final boolean checked ) {
119         final HtmlInput input = getHtmlInputOrDie();
120         final String JavaDoc type = input.getTypeAttribute().toLowerCase();
121         if (type.equals("checkbox") || type.equals("radio")){
122             input.setChecked(checked);
123         }
124         else {
125             getLog().debug( "Input.jsxSet_checked(" + checked
126                 + ") was called for class " + getClass().getName() );
127         }
128     }
129     
130     /**
131      * Commodity for <code>(HtmlInput) getHtmlElementOrDie()</code>
132      * @return the bound html input
133      */

134     protected HtmlInput getHtmlInputOrDie() {
135         return (HtmlInput) getHtmlElementOrDie();
136     }
137
138
139     /**
140      * Return the value of the checked property. Although this property is
141      * defined in Input it doesn't make any sense for input's other than
142      * checkbox and radio. This implementation does nothing. The
143      * implementations in Checkbox and Radio actually do the work.
144      *
145      *@return The checked property.
146      */

147     public boolean jsxGet_checked() {
148         final HtmlInput input = getHtmlInputOrDie();
149         final String JavaDoc type = input.getTypeAttribute().toLowerCase();
150         if (type.equals("checkbox") || type.equals("radio")){
151             return input.isChecked();
152         }
153         else {
154             getLog().warn( "Input.jsxGet_checked() was called for class " + getClass().getName() );
155             return false;
156         }
157     }
158
159 }
160
161
Popular Tags