1 38 package com.gargoylesoftware.htmlunit.html; 39 40 import java.io.IOException ; 41 import java.util.Map ; 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 64 public abstract class ClickableElement extends StyledElement { 65 66 72 protected ClickableElement( final HtmlPage page, final Map attributes) { 73 super(page, attributes); 74 } 75 76 77 84 public Page click() 85 throws IOException { 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 [] args = new Object [] {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 137 protected Page doClickAction(final Page defaultPage) throws IOException { 138 139 return defaultPage; 140 } 141 142 143 151 public final String getLangAttribute() { 152 return getAttributeValue("lang"); 153 } 154 155 156 164 public final String getXmlLangAttribute() { 165 return getAttributeValue("xml:lang"); 166 } 167 168 169 177 public final String getTextDirectionAttribute() { 178 return getAttributeValue("dir"); 179 } 180 181 182 190 public final String getOnClickAttribute() { 191 return getAttributeValue("onclick"); 192 } 193 194 195 203 public final String getOnDblClickAttribute() { 204 return getAttributeValue("ondblclick"); 205 } 206 207 208 216 public final String getOnMouseDownAttribute() { 217 return getAttributeValue("onmousedown"); 218 } 219 220 221 229 public final String getOnMouseUpAttribute() { 230 return getAttributeValue("onmouseup"); 231 } 232 233 234 242 public final String getOnMouseOverAttribute() { 243 return getAttributeValue("onmouseover"); 244 } 245 246 247 255 public final String getOnMouseMoveAttribute() { 256 return getAttributeValue("onmousemove"); 257 } 258 259 260 268 public final String getOnMouseOutAttribute() { 269 return getAttributeValue("onmouseout"); 270 } 271 272 273 281 public final String getOnKeyPressAttribute() { 282 return getAttributeValue("onkeypress"); 283 } 284 285 286 294 public final String getOnKeyDownAttribute() { 295 return getAttributeValue("onkeydown"); 296 } 297 298 299 307 public final String getOnKeyUpAttribute() { 308 return getAttributeValue("onkeyup"); 309 } 310 311 318 protected boolean isStateUpdateFirst() { 319 return false; 320 } 321 } 322 | Popular Tags |