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: import java.util.Iterator;
043:
044: import com.gargoylesoftware.htmlunit.ElementNotFoundException;
045: import com.gargoylesoftware.htmlunit.Page;
046:
047: /**
048: * Wrapper for the html element "label".
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 Marc Guillemot
055: * @author Ahmed Ashour
056: */
057: public class HtmlLabel extends ClickableElement {
058:
059: private static final long serialVersionUID = -3007176633287091652L;
060:
061: /** the HTML tag represented by this element */
062: public static final String TAG_NAME = "label";
063:
064: /**
065: * Create an instance of HtmlLabel
066: *
067: * @param page The HtmlPage that contains this element.
068: * @param attributes the initial attributes
069: * @deprecated You should not directly construct HtmlLabel.
070: */
071: //TODO: to be removed, deprecated after 1.11
072: public HtmlLabel(final HtmlPage page, final Map attributes) {
073: this (null, TAG_NAME, page, attributes);
074: }
075:
076: /**
077: * Create an instance of HtmlLabel
078: *
079: * @param namespaceURI the URI that identifies an XML namespace.
080: * @param qualifiedName The qualified name of the element type to instantiate
081: * @param page The HtmlPage that contains this element.
082: * @param attributes the initial attributes
083: */
084: HtmlLabel(final String namespaceURI, final String qualifiedName,
085: final HtmlPage page, final Map attributes) {
086: super (namespaceURI, qualifiedName, page, attributes);
087: }
088:
089: /**
090: * Return the value of the attribute "for". Refer to the
091: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
092: * documentation for details on the use of this attribute.
093: *
094: * @return The value of the attribute "for"
095: * or an empty string if that attribute isn't defined.
096: */
097: public final String getForAttribute() {
098: return getAttributeValue("for");
099: }
100:
101: /**
102: * Return the value of the attribute "accesskey". Refer to the
103: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
104: * documentation for details on the use of this attribute.
105: *
106: * @return The value of the attribute "accesskey"
107: * or an empty string if that attribute isn't defined.
108: */
109: public final String getAccessKeyAttribute() {
110: return getAttributeValue("accesskey");
111: }
112:
113: /**
114: * Return the value of the attribute "onfocus". Refer to the
115: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
116: * documentation for details on the use of this attribute.
117: *
118: * @return The value of the attribute "onfocus"
119: * or an empty string if that attribute isn't defined.
120: */
121: public final String getOnFocusAttribute() {
122: return getAttributeValue("onfocus");
123: }
124:
125: /**
126: * Return the value of the attribute "onblur". Refer to the
127: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
128: * documentation for details on the use of this attribute.
129: *
130: * @return The value of the attribute "onblur"
131: * or an empty string if that attribute isn't defined.
132: */
133: public final String getOnBlurAttribute() {
134: return getAttributeValue("onblur");
135: }
136:
137: /**
138: * Remove focus from this element.
139: */
140: public void blur() {
141: final HtmlElement element = getReferencedElement();
142: if (element != null) {
143: element.blur();
144: }
145: }
146:
147: /**
148: * Set the focus to this element.
149: */
150: public void focus() {
151: final HtmlElement element = getReferencedElement();
152: if (element != null) {
153: element.focus();
154: }
155: }
156:
157: /**
158: * Gets the element referenced by this label. That is the element in the page which id is
159: * equal to the value of the for attribute of this label.
160: * @return the element, <code>null</code> if not found
161: */
162: public ClickableElement getReferencedElement() {
163: final String elementId = getForAttribute();
164: if (!ATTRIBUTE_NOT_DEFINED.equals(elementId)) {
165: try {
166: final HtmlElement element = getHtmlElementById(elementId);
167: if (element instanceof ClickableElement) {
168: return (ClickableElement) element;
169: }
170: } catch (final ElementNotFoundException e) {
171: return null;
172: }
173: } else {
174: final Iterator childIterator = getChildIterator();
175: while (childIterator.hasNext()) {
176: final DomNode element = (DomNode) childIterator.next();
177: if (element instanceof HtmlInput) {
178: return (HtmlInput) element;
179: }
180: }
181: }
182: return null;
183: }
184:
185: /**
186: * Clicks the label and propagates to the referenced element.
187: * {@inheritDoc}
188: */
189: public Page click() throws IOException {
190: // first the click on the label
191: final Page page = super .click();
192:
193: // not sure which page we should return
194: final Page response;
195:
196: // then the click on the referenced element
197: final ClickableElement element = getReferencedElement();
198: if (element != null) {
199: response = element.click();
200: } else {
201: response = page;
202: }
203:
204: return response;
205: }
206: }
|