1 21 24 package org.lobobrowser.html.domimpl; 25 26 import java.util.ArrayList ; 27 28 import org.lobobrowser.html.FormInput; 29 import org.lobobrowser.html.js.Executor; 30 import org.mozilla.javascript.Function; 31 import org.w3c.dom.Node ; 32 import org.w3c.dom.html2.HTMLFormElement; 33 34 public abstract class HTMLBaseInputElement extends HTMLAbstractUIElement { 35 public HTMLBaseInputElement(String name) { 36 super(name); 37 } 38 39 protected InputContext inputContext; 40 41 public void setInputContext(InputContext ic) { 42 this.inputContext = ic; 43 } 44 45 public String getDefaultValue() { 46 return this.getAttribute("defaultValue"); 47 } 48 49 public void setDefaultValue(String defaultValue) { 50 this.setAttribute("defaultValue", defaultValue); 51 } 52 53 public HTMLFormElement getForm() { 54 Node parent = this.getParentNode(); 55 while(parent != null && !(parent instanceof HTMLFormElement)) { 56 parent = parent.getParentNode(); 57 } 58 return (HTMLFormElement) parent; 59 } 60 61 public void submitForm(FormInput[] extraFormInputs) { 62 HTMLFormElementImpl form = (HTMLFormElementImpl) this.getForm(); 63 if(form != null) { 64 form.submit(extraFormInputs); 65 } 66 } 67 68 public void resetForm() { 69 HTMLFormElement form = this.getForm(); 70 if (form != null) { 71 form.reset(); 72 } 73 } 74 75 public String getAccept() { 76 return this.getAttribute("accept"); 77 } 78 79 public void setAccept(String accept) { 80 this.setAttribute("accept", accept); 81 } 82 83 public String getAccessKey() { 84 return this.getAttribute("accessKey"); 85 } 86 87 public void setAccessKey(String accessKey) { 88 this.setAttribute("accessKey", accessKey); 89 } 90 91 public String getAlign() { 92 return this.getAttribute("align"); 93 } 94 95 public void setAlign(String align) { 96 this.setAttribute("align", align); 97 } 98 99 public String getAlt() { 100 return this.getAttribute("alit"); 101 } 102 103 public void setAlt(String alt) { 104 this.setAttribute("alt", alt); 105 } 106 107 public boolean getDisabled() { 108 InputContext ic = this.inputContext; 109 return ic == null ? false : ic.getDisabled(); 110 } 111 112 public void setDisabled(boolean disabled) { 113 InputContext ic = this.inputContext; 114 if(ic != null) { 115 ic.setDisabled(disabled); 116 } 117 } 118 119 public String getName() { 120 return this.getAttribute("name"); 122 } 123 124 public void setName(String name) { 125 this.setAttribute("name", name); 126 } 127 128 public boolean getReadOnly() { 129 InputContext ic = this.inputContext; 130 return ic == null ? false : ic.getReadOnly(); 131 } 132 133 public void setReadOnly(boolean readOnly) { 134 InputContext ic = this.inputContext; 135 if(ic != null) { 136 ic.setReadOnly(readOnly); 137 } 138 } 139 140 public int getTabIndex() { 141 InputContext ic = this.inputContext; 142 return ic == null ? 0 : ic.getTabIndex(); 143 } 144 145 public void setTabIndex(int tabIndex) { 146 InputContext ic = this.inputContext; 147 if(ic != null) { 148 ic.setTabIndex(tabIndex); 149 } 150 } 151 152 public String getValue() { 153 InputContext ic = this.inputContext; 154 if(ic != null) { 155 return ic.getValue(); 157 } 158 else { 159 String val = this.getAttribute("value"); 160 return val == null ? "" : val; 161 } 162 } 163 164 protected java.io.File getFileValue() { 165 InputContext ic = this.inputContext; 166 if(ic != null) { 167 return ic.getFileValue(); 168 } 169 else { 170 return null; 171 } 172 } 173 174 public void setValue(String value) { 175 InputContext ic = this.inputContext; 176 if(ic != null) { 177 ic.setValue(value); 179 } 180 else { 181 this.setAttribute("value", value); 182 } 183 } 184 185 public void blur() { 186 InputContext ic = this.inputContext; 187 if(ic != null) { 188 ic.blur(); 189 } 190 } 191 192 public void focus() { 193 InputContext ic = this.inputContext; 194 if(ic != null) { 195 ic.focus(); 196 } 197 } 198 199 public void select() { 200 InputContext ic = this.inputContext; 201 if(ic != null) { 202 ic.select(); 203 } 204 } 205 206 209 protected void assignAttributeField(String normalName, String value) { 210 if("value".equals(normalName)) { 211 InputContext ic = this.inputContext; 212 if(ic != null) { 213 ic.setValue(value); 214 } 215 } 216 else if("src".equals(normalName)) { 217 this.loadImage(value); 218 } 219 else { 220 super.assignAttributeField(normalName, value); 221 } 222 } 223 224 private Function onload; 225 226 public Function getOnload() { 227 return this.getEventFunction(this.onload, "onload"); 228 } 229 230 public void setOnload(Function onload) { 231 this.onload = onload; 232 } 233 234 private java.awt.Image image = null; 235 private String imageSrc; 236 237 private void loadImage(String src) { 238 HTMLDocumentImpl document = (HTMLDocumentImpl) this.document; 239 if(document != null) { 240 synchronized(this.imageListeners) { 241 this.imageSrc = src; 242 this.image = null; 243 } 244 if(src != null) { 245 document.loadImage(src, new LocalImageListener(src)); 246 } 247 } 248 } 249 250 public final java.awt.Image getImage() { 251 synchronized(this.imageListeners) { 252 return this.image; 253 } 254 } 255 256 private final ArrayList imageListeners = new ArrayList (1); 257 258 264 public void addImageListener(ImageListener listener) { 265 ArrayList l = this.imageListeners; 266 java.awt.Image currentImage; 267 synchronized(l) { 268 currentImage = this.image; 269 l.add(listener); 270 } 271 if(currentImage != null) { 272 listener.imageLoaded(new ImageEvent(this, currentImage)); 275 } 278 } 279 280 public void removeImageListener(ImageListener listener) { 281 ArrayList l = this.imageListeners; 282 synchronized(l) { 283 l.remove(l); 284 } 285 } 286 287 void resetInput() { 288 InputContext ic = this.inputContext; 289 if(ic != null) { 290 ic.resetInput(); 291 } 292 } 293 294 private void dispatchEvent(String expectedImgSrc, ImageEvent event) { 295 ArrayList l = this.imageListeners; 296 ImageListener[] listenerArray; 297 synchronized(l) { 298 if(!expectedImgSrc.equals(this.imageSrc)) { 299 return; 300 } 301 this.image = event.image; 302 listenerArray = (ImageListener[]) l.toArray(ImageListener.EMPTY_ARRAY); 304 } 305 int llength = listenerArray.length; 306 for(int i = 0; i < llength; i++) { 307 listenerArray[i].imageLoaded(event); 309 } 310 Function onload = this.getOnload(); 311 if(onload != null) { 312 Executor.executeFunction(HTMLBaseInputElement.this, onload, null); 314 } 315 } 316 317 private class LocalImageListener implements ImageListener { 318 private final String expectedImgSrc; 319 320 public LocalImageListener(String imgSrc) { 321 this.expectedImgSrc = imgSrc; 322 } 323 324 public void imageLoaded(ImageEvent event) { 325 dispatchEvent(this.expectedImgSrc, event); 326 } 327 } 328 329 } 330 | Popular Tags |