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.DomText;
043: import com.gargoylesoftware.htmlunit.html.HTMLParser;
044: import com.gargoylesoftware.htmlunit.html.HtmlOption;
045: import com.gargoylesoftware.htmlunit.html.HtmlPage;
046:
047: /**
048: * The javascript object that represents an option.
049: *
050: * @version $Revision: 2132 $
051: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
052: * @author David K. Taylor
053: * @author Chris Erskine
054: * @author Marc Guillemot
055: * @author Ahmed Ashour
056: */
057: public class Option extends HTMLElement {
058: private static final long serialVersionUID = 947015932373556314L;
059:
060: /**
061: * Create an instance.
062: */
063: public Option() {
064: }
065:
066: /**
067: * Javascript constructor. This must be declared in every javascript file because
068: * the rhino engine won't walk up the hierarchy looking for constructors.
069: * @param newText The text
070: * @param newValue The value
071: * @param defaultSelected Whether the option is initially selected
072: * @param selected The current selection state of the option
073: */
074: public void jsConstructor(final String newText,
075: final String newValue, final boolean defaultSelected,
076: final boolean selected) {
077: final HtmlPage page = (HtmlPage) getWindow().getWebWindow()
078: .getEnclosedPage();
079: AttributesImpl attributes = null;
080: if (defaultSelected) {
081: attributes = new AttributesImpl();
082: attributes.addAttribute(null, "selected", "selected", null,
083: "selected");
084: }
085:
086: final HtmlOption htmlOption = (HtmlOption) HTMLParser
087: .getFactory(HtmlOption.TAG_NAME).createElement(page,
088: HtmlOption.TAG_NAME, attributes);
089: htmlOption.setSelected(selected);
090: setDomNode(htmlOption);
091:
092: if (newText != null && !newText.equals("undefined")) {
093: htmlOption.appendDomChild(new DomText(page, newText));
094: }
095: if (newValue != null && !newValue.equals("undefined")) {
096: htmlOption.setValueAttribute(newValue);
097: }
098: }
099:
100: /**
101: * Return the value of the "value" property
102: * @return The value property
103: */
104: public String jsxGet_value() {
105: return getHtmlOption().getValueAttribute();
106: }
107:
108: /**
109: * Set the value of the "value" property
110: * @param newValue The value property
111: */
112: public void jsxSet_value(final String newValue) {
113: getHtmlOption().setValueAttribute(newValue);
114: }
115:
116: /**
117: * Return the value of the "text" property
118: * @return The text property
119: */
120: public String jsxGet_text() {
121: final HtmlOption htmlOption = getHtmlOption();
122: if (htmlOption.isAttributeDefined("label")) {
123: return htmlOption.getLabelAttribute();
124: }
125: return htmlOption.asText();
126: }
127:
128: private HtmlOption getHtmlOption() {
129: return (HtmlOption) getHtmlElementOrNull();
130: }
131:
132: /**
133: * Set the value of the "text" property
134: * @param newText The text property
135: */
136: public void jsxSet_text(final String newText) {
137: getHtmlOption().setLabelAttribute(newText);
138: }
139:
140: /**
141: * Return the value of the "selected" property
142: * @return The text property
143: */
144: public boolean jsxGet_selected() {
145: return getHtmlOption().isSelected();
146: }
147:
148: /**
149: * Set the value of the "selected" property
150: * @param selected The new selected property
151: */
152: public void jsxSet_selected(final boolean selected) {
153: getHtmlOption().setSelected(selected);
154: }
155:
156: /**
157: * Return the value of the "defaultSelected" property
158: * @return The text property
159: */
160: public boolean jsxGet_defaultSelected() {
161: return getHtmlOption().isDefaultSelected();
162: }
163:
164: /**
165: * Return the value of the "label" property
166: * @return The label property
167: */
168: public String jsxGet_label() {
169: return getHtmlOption().getLabelAttribute();
170: }
171:
172: /**
173: * Set the value of the "label" property
174: * @param label The new label property
175: */
176: public void jsxSet_label(final String label) {
177: getHtmlOption().setLabelAttribute(label);
178: }
179: }
|