KickJava   Java API By Example, From Geeks To Geeks.

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


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 Nov 19, 2005
23  */

24 package org.lobobrowser.html.domimpl;
25
26 import java.util.*;
27
28 import org.lobobrowser.html.js.*;
29 import org.mozilla.javascript.Function;
30 import org.w3c.dom.html2.HTMLImageElement;
31
32 public class HTMLImageElementImpl extends HTMLAbstractUIElement implements
33         HTMLImageElement {
34     public HTMLImageElementImpl() {
35         super("IMG");
36     }
37
38     public HTMLImageElementImpl(String JavaDoc name) {
39         super(name);
40     }
41
42     public String JavaDoc getName() {
43         return this.getAttribute("name");
44     }
45
46     public void setName(String JavaDoc name) {
47         this.setAttribute("name", name);
48     }
49
50     public String JavaDoc getAlign() {
51         return this.getAttribute("align");
52     }
53
54     public void setAlign(String JavaDoc align) {
55         this.setAttribute("align", align);
56     }
57
58     public String JavaDoc getAlt() {
59         return this.getAttribute("alt");
60     }
61
62     public void setAlt(String JavaDoc alt) {
63         this.setAttribute("alt", alt);
64     }
65
66     public String JavaDoc getBorder() {
67         return this.getAttribute("border");
68     }
69
70     public void setBorder(String JavaDoc border) {
71         this.setAttribute("border", border);
72     }
73
74     public int getHeight() {
75         UINode r = this.uiNode;
76         return r == null ? 0 : r.getBounds().height;
77     }
78
79     public void setHeight(int height) {
80         this.setAttribute("height", String.valueOf(height));
81     }
82
83     public int getHspace() {
84         return this.getAttributeAsInt("hspace", 0);
85     }
86
87     public void setHspace(int hspace) {
88         this.setAttribute("hspace", String.valueOf("hspace"));
89     }
90
91     public boolean getIsMap() {
92         return this.getAttributeAsBoolean("isMap");
93     }
94
95     public void setIsMap(boolean isMap) {
96         this.setAttribute("isMap", isMap ? "isMap" : null);
97     }
98
99     public String JavaDoc getLongDesc() {
100         return this.getAttribute("longDesc");
101     }
102
103     public void setLongDesc(String JavaDoc longDesc) {
104         this.setAttribute("longDesc", longDesc);
105     }
106
107     public String JavaDoc getSrc() {
108         return this.getAttribute("src");
109     }
110
111     /**
112      * Sets the image URI and starts to load the image.
113      * Note that an HtmlRendererContext should be available
114      * to the HTML document for images to be loaded.
115      */

116     public void setSrc(String JavaDoc src) {
117         this.setAttribute("src", src);
118     }
119
120     public String JavaDoc getUseMap() {
121         return this.getAttribute("useMap");
122     }
123
124     public void setUseMap(String JavaDoc useMap) {
125         this.setAttribute("useMap", useMap);
126     }
127
128     public int getVspace() {
129         return this.getAttributeAsInt("vspace", 0);
130     }
131
132     public void setVspace(int vspace) {
133         this.setAttribute("vspace", String.valueOf(vspace));
134     }
135
136     public int getWidth() {
137         UINode r = this.uiNode;
138         return r == null ? 0 : r.getBounds().width;
139     }
140
141     public void setWidth(int width) {
142         this.setAttribute("width", String.valueOf(width));
143     }
144     
145 // /* (non-Javadoc)
146
// * @see org.xamjwg.html.renderer.RenderableContext#getHeightLength()
147
// */
148
// public HtmlLength getHeightLength() {
149
// return this.heightLength;
150
// }
151
//
152
// /* (non-Javadoc)
153
// * @see org.xamjwg.html.renderer.RenderableContext#getWidthLength()
154
// */
155
// public HtmlLength getWidthLength() {
156
// return this.widthLength;
157
// }
158

159 // /* (non-Javadoc)
160
// * @see org.xamjwg.html.renderer.RenderableContext#getAlignmentX()
161
// */
162
// public float getAlignmentX() {
163
// return 0.5f;
164
// }
165
//
166
// /* (non-Javadoc)
167
// * @see org.xamjwg.html.renderer.RenderableContext#getAlignmentY()
168
// */
169
// public float getAlignmentY() {
170
// return this.alignmentY;
171
// }
172

173 // private HtmlLength widthLength;
174
// private HtmlLength heightLength;
175
// private float alignmentY = 1.0f;
176

177 // /* (non-Javadoc)
178
// * @see org.xamjwg.html.domimpl.ElementImpl#assignAttributeField(java.lang.String, java.lang.String)
179
// */
180
// protected void assignAttributeField(String normalName, String value) {
181
// super.assignAttributeField(normalName, value);
182
// if("width".equals(normalName)) {
183
// try {
184
// this.widthLength = new HtmlLength(value);
185
// } catch(Exception err) {
186
// this.warn("Bad width spec: " + value, err);
187
// this.widthLength = null;
188
// }
189
// }
190
// else if("height".equals(normalName)) {
191
// try {
192
// this.heightLength = new HtmlLength(value);
193
// } catch(Exception err) {
194
// this.warn("Bad height spec: " + value, err);
195
// this.heightLength = null;
196
// }
197
// }
198
// }
199
//
200
// private final void assignAlignment(String value) {
201
// if(value.equalsIgnoreCase("middle")) {
202
// this.alignmentY = 0.5f;
203
// }
204
// else if(value.equalsIgnoreCase("top")) {
205
// this.alignmentY = 0.0f;
206
// }
207
// else if(value.equalsIgnoreCase("bottom")) {
208
// this.alignmentY = 1.0f;
209
// }
210
// else {
211
// this.alignmentY = 1.0f;
212
// }
213
// }
214

215
216     protected void assignAttributeField(String JavaDoc normalName, String JavaDoc value) {
217         super.assignAttributeField(normalName, value);
218         if("src".equals(normalName)) {
219             this.loadImage(value);
220         }
221     }
222     
223     private Function onload;
224     
225     public Function getOnload() {
226         return this.getEventFunction(this.onload, "onload");
227     }
228
229     public void setOnload(Function onload) {
230         this.onload = onload;
231     }
232
233     private java.awt.Image JavaDoc image = null;
234     private String JavaDoc imageSrc;
235     
236     private void loadImage(String JavaDoc src) {
237         HTMLDocumentImpl document = (HTMLDocumentImpl) this.document;
238         if(document != null) {
239             synchronized(this.listeners) {
240                 this.imageSrc = src;
241                 this.image = null;
242             }
243             if(src != null) {
244                 document.loadImage(src, new LocalImageListener(src));
245             }
246         }
247     }
248     
249     public final java.awt.Image JavaDoc getImage() {
250         synchronized(this.listeners) {
251             return this.image;
252         }
253     }
254     
255     private final ArrayList listeners = new ArrayList(1);
256     
257     /**
258      * Adds a listener of image loading events.
259      * The listener gets called right away if there's already
260      * an image.
261      * @param listener
262      */

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