KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > html > ClickableElement


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.html;
39
40 import java.io.IOException JavaDoc;
41 import java.util.Map JavaDoc;
42
43 import org.mozilla.javascript.Function;
44 import org.mozilla.javascript.Scriptable;
45
46 import com.gargoylesoftware.htmlunit.Page;
47 import com.gargoylesoftware.htmlunit.ScriptResult;
48 import com.gargoylesoftware.htmlunit.javascript.host.Event;
49
50 /**
51  * Intermediate base class for "clickable" HTML elements. As defined
52  * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a> documentation,
53  * this class is a base class for all HTML elements except these:
54  * applet, base, basefront, bdo, br, font, frame, frameset, head, html,
55  * iframe, isindex, meta, param, script, style, and title.
56  *
57  * @version $Revision: 100 $
58  * @author David K. Taylor
59  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
60  * @author <a HREF="mailto:chen_jun@users.sourceforge.net">Jun Chen</a>
61  * @author <a HREF="mailto:cse@dynabean.de">Christian Sell</a>
62  * @author David D. Kilzer
63  */

64 public abstract class ClickableElement extends StyledElement {
65
66     /**
67      * Create an instance
68      *
69      * @param page The page that contains this element
70      * @param attributes the initial attributes
71      */

72     protected ClickableElement( final HtmlPage page, final Map JavaDoc attributes) {
73         super(page, attributes);
74     }
75
76
77     /**
78      * Simulate clicking this link
79      *
80      * @return The page that occupies this window after this element is
81      * clicked. It may be the same window or it may be a freshly loaded one.
82      * @exception IOException If an IO error occurs
83      */

84     public Page click()
85         throws IOException JavaDoc {
86
87         if( this instanceof DisabledElement ) {
88             if( ((DisabledElement) this).isDisabled() ) {
89                 return getPage();
90             }
91         }
92
93         final HtmlPage page = getPage();
94
95         final Function function = getEventHandler("onclick");
96                 
97         if (function != null && page.getWebClient().isJavaScriptEnabled()) {
98             boolean stateUpdated = false;
99             if (isStateUpdateFirst()) {
100                 doClickAction(page);
101                 stateUpdated = true;
102             }
103             final Event event = new Event(this, getScriptObject());
104             
105             final Object JavaDoc[] args = new Object JavaDoc[] {event};
106             
107             final ScriptResult scriptResult =
108                 page.executeJavaScriptFunctionIfPossible(
109                     function, (Scriptable) getScriptObject(), args, this);
110             
111             final Page scriptPage = scriptResult.getNewPage();
112             if( stateUpdated || scriptResult.getJavaScriptResult().equals( Boolean.FALSE ) ) {
113                 return scriptPage;
114             }
115             else {
116                 return doClickAction(scriptPage);
117             }
118         }
119         else {
120             return doClickAction(page);
121         }
122     }
123
124
125     /**
126      * This method will be called if there either wasn't an onclick handler or
127      * there was but the result of that handler was true. This is the default
128      * behaviour of clicking the element. The default implementation returns
129      * the current page - subclasses requiring different behaviour (like
130      * {@link HtmlSubmitInput}) will override this method.
131      *
132      * @param defaultPage The default page to return if the action does not
133      * load a new page.
134      * @return The page that is currently loaded after execution of this method
135      * @throws IOException If an IO error occured
136      */

137     protected Page doClickAction(final Page defaultPage) throws IOException JavaDoc {
138
139         return defaultPage;
140     }
141
142
143     /**
144      * Return the value of the attribute "lang". Refer to the
145      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
146      * documentation for details on the use of this attribute.
147      *
148      * @return The value of the attribute "lang"
149      * or an empty string if that attribute isn't defined.
150      */

151     public final String JavaDoc getLangAttribute() {
152         return getAttributeValue("lang");
153     }
154
155
156     /**
157      * Return the value of the attribute "xml:lang". Refer to the
158      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
159      * documentation for details on the use of this attribute.
160      *
161      * @return The value of the attribute "xml:lang"
162      * or an empty string if that attribute isn't defined.
163      */

164     public final String JavaDoc getXmlLangAttribute() {
165         return getAttributeValue("xml:lang");
166     }
167
168
169     /**
170      * Return the value of the attribute "dir". Refer to the
171      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
172      * documentation for details on the use of this attribute.
173      *
174      * @return The value of the attribute "dir"
175      * or an empty string if that attribute isn't defined.
176      */

177     public final String JavaDoc getTextDirectionAttribute() {
178         return getAttributeValue("dir");
179     }
180
181
182     /**
183      * Return the value of the attribute "onclick". Refer to the
184      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
185      * documentation for details on the use of this attribute.
186      *
187      * @return The value of the attribute "onclick"
188      * or an empty string if that attribute isn't defined.
189      */

190     public final String JavaDoc getOnClickAttribute() {
191         return getAttributeValue("onclick");
192     }
193
194
195     /**
196      * Return the value of the attribute "ondblclick". Refer to the
197      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
198      * documentation for details on the use of this attribute.
199      *
200      * @return The value of the attribute "ondblclick"
201      * or an empty string if that attribute isn't defined.
202      */

203     public final String JavaDoc getOnDblClickAttribute() {
204         return getAttributeValue("ondblclick");
205     }
206
207
208     /**
209      * Return the value of the attribute "onmousedown". Refer to the
210      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
211      * documentation for details on the use of this attribute.
212      *
213      * @return The value of the attribute "onmousedown"
214      * or an empty string if that attribute isn't defined.
215      */

216     public final String JavaDoc getOnMouseDownAttribute() {
217         return getAttributeValue("onmousedown");
218     }
219
220
221     /**
222      * Return the value of the attribute "onmouseup". Refer to the
223      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
224      * documentation for details on the use of this attribute.
225      *
226      * @return The value of the attribute "onmouseup"
227      * or an empty string if that attribute isn't defined.
228      */

229     public final String JavaDoc getOnMouseUpAttribute() {
230         return getAttributeValue("onmouseup");
231     }
232
233
234     /**
235      * Return the value of the attribute "onmouseover". Refer to the
236      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
237      * documentation for details on the use of this attribute.
238      *
239      * @return The value of the attribute "onmouseover"
240      * or an empty string if that attribute isn't defined.
241      */

242     public final String JavaDoc getOnMouseOverAttribute() {
243         return getAttributeValue("onmouseover");
244     }
245
246
247     /**
248      * Return the value of the attribute "onmousemove". Refer to the
249      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
250      * documentation for details on the use of this attribute.
251      *
252      * @return The value of the attribute "onmousemove"
253      * or an empty string if that attribute isn't defined.
254      */

255     public final String JavaDoc getOnMouseMoveAttribute() {
256         return getAttributeValue("onmousemove");
257     }
258
259
260     /**
261      * Return the value of the attribute "onmouseout". Refer to the
262      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
263      * documentation for details on the use of this attribute.
264      *
265      * @return The value of the attribute "onmouseout"
266      * or an empty string if that attribute isn't defined.
267      */

268     public final String JavaDoc getOnMouseOutAttribute() {
269         return getAttributeValue("onmouseout");
270     }
271
272
273     /**
274      * Return the value of the attribute "onkeypress". Refer to the
275      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
276      * documentation for details on the use of this attribute.
277      *
278      * @return The value of the attribute "onkeypress"
279      * or an empty string if that attribute isn't defined.
280      */

281     public final String JavaDoc getOnKeyPressAttribute() {
282         return getAttributeValue("onkeypress");
283     }
284
285
286     /**
287      * Return the value of the attribute "onkeydown". Refer to the
288      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
289      * documentation for details on the use of this attribute.
290      *
291      * @return The value of the attribute "onkeydown"
292      * or an empty string if that attribute isn't defined.
293      */

294     public final String JavaDoc getOnKeyDownAttribute() {
295         return getAttributeValue("onkeydown");
296     }
297
298
299     /**
300      * Return the value of the attribute "onkeyup". Refer to the
301      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
302      * documentation for details on the use of this attribute.
303      *
304      * @return The value of the attribute "onkeyup"
305      * or an empty string if that attribute isn't defined.
306      */

307     public final String JavaDoc getOnKeyUpAttribute() {
308         return getAttributeValue("onkeyup");
309     }
310
311     /**
312      * Return true if the state update should be done before onclick event
313      * handling.
314      * This is expected to be overriden to return "true" by derived classes
315      * like HtmlCheckBoxInput.
316      * @return Return true if state update should be done first.
317      */

318     protected boolean isStateUpdateFirst() {
319         return false;
320     }
321 }
322
Popular Tags