| 1 package org.lobobrowser.html.domimpl; 2 3 import org.lobobrowser.html.*; 4 import org.lobobrowser.html.js.Executor; 5 import org.lobobrowser.js.*; 6 import org.mozilla.javascript.*; 7 import org.w3c.dom.*; 8 import java.util.*; 9 10 13 public class HTMLAbstractUIElement extends HTMLElementImpl { 14 private Function onfocus, onblur, onclick, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove, onmouseout, onkeypress, onkeydown, onkeyup; 15 16 public HTMLAbstractUIElement(String name) { 17 super(name); 18 } 19 20 public Function getOnblur() { 21 return this.getEventFunction(onblur, "onblur"); 22 } 23 24 public void setOnblur(Function onblur) { 25 this.onblur = onblur; 26 } 27 28 public Function getOnclick() { 29 return this.getEventFunction(onclick, "onclick"); 30 } 31 32 public void setOnclick(Function onclick) { 33 this.onclick = onclick; 34 } 35 36 public Function getOndblclick() { 37 return this.getEventFunction(ondblclick, "ondblclick"); 38 } 39 40 public void setOndblclick(Function ondblclick) { 41 this.ondblclick = ondblclick; 42 } 43 44 public Function getOnfocus() { 45 return this.getEventFunction(onfocus, "onfocus"); 46 } 47 48 public void setOnfocus(Function onfocus) { 49 this.onfocus = onfocus; 50 } 51 52 public Function getOnkeydown() { 53 return this.getEventFunction(onkeydown, "onkeydown"); 54 } 55 56 public void setOnkeydown(Function onkeydown) { 57 this.onkeydown = onkeydown; 58 } 59 60 public Function getOnkeypress() { 61 return this.getEventFunction(onkeypress, "onkeypress"); 62 } 63 64 public void setOnkeypress(Function onkeypress) { 65 this.onkeypress = onkeypress; 66 } 67 68 public Function getOnkeyup() { 69 return this.getEventFunction(onkeyup, "onkeyup"); 70 } 71 72 public void setOnkeyup(Function onkeyup) { 73 this.onkeyup = onkeyup; 74 } 75 76 public Function getOnmousedown() { 77 return this.getEventFunction(onmousedown, "onmousedown"); 78 } 79 80 public void setOnmousedown(Function onmousedown) { 81 this.onmousedown = onmousedown; 82 } 83 84 public Function getOnmousemove() { 85 return this.getEventFunction(onmousemove, "onmousemove"); 86 } 87 88 public void setOnmousemove(Function onmousemove) { 89 this.onmousemove = onmousemove; 90 } 91 92 public Function getOnmouseout() { 93 return this.getEventFunction(onmouseout, "onmouseout"); 94 } 95 96 public void setOnmouseout(Function onmouseout) { 97 this.onmouseout = onmouseout; 98 } 99 100 public Function getOnmouseover() { 101 return this.getEventFunction(onmouseover, "onmouseover"); 102 } 103 104 public void setOnmouseover(Function onmouseover) { 105 this.onmouseover = onmouseover; 106 } 107 108 public Function getOnmouseup() { 109 return this.getEventFunction(onmouseup, "onmouseup"); 110 } 111 112 public void setOnmouseup(Function onmouseup) { 113 this.onmouseup = onmouseup; 114 } 115 116 public void focus() { 117 UINode node = this.getUINode(); 118 if(node != null) { 119 node.focus(); 120 } 121 } 122 123 public void blur() { 124 UINode node = this.getUINode(); 125 if(node != null) { 126 node.blur(); 127 } 128 } 129 130 private Map functionByAttribute = null; 131 132 protected Function getEventFunction(Function varValue, String attributeName) { 133 if(varValue != null) { 134 return varValue; 135 } 136 String normalAttributeName = this.normalizeAttributeName(attributeName); 137 synchronized(this) { 138 Map fba = this.functionByAttribute; 139 Function f = fba == null ? null : (Function) fba.get(normalAttributeName); 140 if(f != null) { 141 return f; 142 } 143 UserAgentContext uac = this.getUserAgentContext(); 144 if(uac == null) { 145 throw new IllegalStateException ("No user agent context."); 146 } 147 if(uac.isScriptingEnabled()) { 148 String attributeValue = this.getAttribute(attributeName); 149 if(attributeValue == null || attributeValue.length() == 0) { 150 f = null; 151 } 152 else { 153 String functionCode = "function " + normalAttributeName + "_" + System.identityHashCode(this) + "() { " + attributeValue + " }"; 154 Document doc = this.document; 155 if(doc == null) { 156 throw new IllegalStateException ("Element does not belong to a document."); 157 } 158 Context ctx = Executor.createContext(this.getDocumentURL(), uac); 159 try { 160 Scriptable scope = (Scriptable) doc.getUserData(Executor.SCOPE_KEY); 161 if(scope == null) { 162 throw new IllegalStateException ("Scriptable (scope) instance was expected to be keyed as UserData to document using " + Executor.SCOPE_KEY); 163 } 164 Scriptable thisScope = (Scriptable) JavaScript.getInstance().getJavascriptObject(this, scope); 165 try { 166 f = ctx.compileFunction(thisScope, functionCode, this.getTagName() + "[" + this.getId() + "]." + attributeName, 1, null); 168 } catch(EcmaError ecmaError) { 169 this.warn("Javascript error at " + ecmaError.getSourceName() + ":" + ecmaError.getLineNumber() + ": " + ecmaError.getMessage()); 170 f = null; 171 } catch(Throwable err) { 172 this.warn("Unable to evaluate Javascript code", err); 173 f = null; 174 } 175 } finally { 176 Context.exit(); 177 } 178 } 179 if(fba == null) { 180 fba = new HashMap(1); 181 this.functionByAttribute = fba; 182 } 183 fba.put(normalAttributeName, f); 184 } 185 return f; 186 } 187 } 188 189 protected void assignAttributeField(String normalName, String value) { 190 super.assignAttributeField(normalName, value); 191 if(normalName.startsWith("on")) { 192 synchronized(this) { 193 Map fba = this.functionByAttribute; 194 if(fba != null) { 195 fba.remove(normalName); 196 } 197 } 198 } 199 } 200 } 201 | Popular Tags |