KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > domimpl > HTMLBaseInputElement


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Jan 15, 2006
23  */

24 package org.lobobrowser.html.domimpl;
25
26 import java.util.ArrayList JavaDoc;
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 JavaDoc;
32 import org.w3c.dom.html2.HTMLFormElement;
33
34 public abstract class HTMLBaseInputElement extends HTMLAbstractUIElement {
35     public HTMLBaseInputElement(String JavaDoc 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 JavaDoc getDefaultValue() {
46         return this.getAttribute("defaultValue");
47     }
48
49     public void setDefaultValue(String JavaDoc defaultValue) {
50         this.setAttribute("defaultValue", defaultValue);
51     }
52
53     public HTMLFormElement getForm() {
54         Node JavaDoc 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 JavaDoc getAccept() {
76         return this.getAttribute("accept");
77     }
78
79     public void setAccept(String JavaDoc accept) {
80         this.setAttribute("accept", accept);
81     }
82
83     public String JavaDoc getAccessKey() {
84         return this.getAttribute("accessKey");
85     }
86
87     public void setAccessKey(String JavaDoc accessKey) {
88         this.setAttribute("accessKey", accessKey);
89     }
90
91     public String JavaDoc getAlign() {
92         return this.getAttribute("align");
93     }
94
95     public void setAlign(String JavaDoc align) {
96         this.setAttribute("align", align);
97     }
98
99     public String JavaDoc getAlt() {
100         return this.getAttribute("alit");
101     }
102
103     public void setAlt(String JavaDoc 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 JavaDoc getName() {
120         //TODO: Should this return valid of "id"?
121
return this.getAttribute("name");
122     }
123
124     public void setName(String JavaDoc 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 JavaDoc getValue() {
153         InputContext ic = this.inputContext;
154         if(ic != null) {
155             //Note: Per HTML Spec, setValue does not set attribute.
156
return ic.getValue();
157         }
158         else {
159             String JavaDoc val = this.getAttribute("value");
160             return val == null ? "" : val;
161         }
162     }
163
164     protected java.io.File JavaDoc 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 JavaDoc value) {
175         InputContext ic = this.inputContext;
176         if(ic != null) {
177             //Note: Per HTML Spec, it does not set attribute.
178
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     /* (non-Javadoc)
207      * @see org.xamjwg.html.domimpl.HTMLElementImpl#assignAttributeField(java.lang.String, java.lang.String)
208      */

209     protected void assignAttributeField(String JavaDoc normalName, String JavaDoc 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 JavaDoc image = null;
235     private String JavaDoc imageSrc;
236     
237     private void loadImage(String JavaDoc 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 JavaDoc getImage() {
251         synchronized(this.imageListeners) {
252             return this.image;
253         }
254     }
255     
256     private final ArrayList JavaDoc imageListeners = new ArrayList JavaDoc(1);
257     
258     /**
259      * Adds a listener of image loading events.
260      * The listener gets called right away if there's already
261      * an image.
262      * @param listener
263      */

264     public void addImageListener(ImageListener listener) {
265         ArrayList JavaDoc l = this.imageListeners;
266         java.awt.Image JavaDoc currentImage;
267         synchronized(l) {
268             currentImage = this.image;
269             l.add(listener);
270         }
271         if(currentImage != null) {
272             // Call listener right away if there's already an
273
// image; holding no locks.
274
listener.imageLoaded(new ImageEvent(this, currentImage));
275             // Should not call onload handler here. That's taken
276
// care of otherwise.
277
}
278     }
279
280     public void removeImageListener(ImageListener listener) {
281         ArrayList JavaDoc 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 JavaDoc expectedImgSrc, ImageEvent event) {
295         ArrayList JavaDoc l = this.imageListeners;
296         ImageListener[] listenerArray;
297         synchronized(l) {
298             if(!expectedImgSrc.equals(this.imageSrc)) {
299                 return;
300             }
301             this.image = event.image;
302             // Get array of listeners while holding lock.
303
listenerArray = (ImageListener[]) l.toArray(ImageListener.EMPTY_ARRAY);
304         }
305         int llength = listenerArray.length;
306         for(int i = 0; i < llength; i++) {
307             // Inform listener, holding no lock.
308
listenerArray[i].imageLoaded(event);
309         }
310         Function onload = this.getOnload();
311         if(onload != null) {
312             //TODO: onload event object?
313
Executor.executeFunction(HTMLBaseInputElement.this, onload, null);
314         }
315     }
316     
317     private class LocalImageListener implements ImageListener {
318         private final String JavaDoc expectedImgSrc;
319
320         public LocalImageListener(String JavaDoc 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