KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > renderer > ImgControl


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.renderer;
25
26 import java.awt.*;
27 import java.awt.image.ImageObserver JavaDoc;
28
29 import org.lobobrowser.html.domimpl.*;
30 import org.lobobrowser.html.style.HtmlValues;
31
32 class ImgControl extends BaseControl implements ImageListener {
33     private volatile Image image;
34     //private final UserAgentContext browserContext;
35
private String JavaDoc lastSrc;
36     
37     public ImgControl(HTMLImageElementImpl modelNode) {
38         super(modelNode);
39         //this.browserContext = pcontext;
40
modelNode.addImageListener(this);
41     }
42     
43     public void paintComponent(Graphics g) {
44         super.paintComponent(g);
45         Dimension size = this.getSize();
46         Insets insets = this.getInsets();
47         synchronized(this) {}
48         Image image = this.image;
49         if(image != null) {
50             g.drawImage(image,
51                     insets.left, insets.top,
52                     size.width - insets.left - insets.right,
53                     size.height - insets.top - insets.bottom, this);
54         }
55         else {
56             //TODO: alt
57
}
58     }
59
60     private int valign = RElement.VALIGN_BASELINE;
61     private Dimension preferredSize;
62     private int declaredWidth;
63     private int declaredHeight;
64     
65     public void reset(int availWidth, int availHeight) {
66         // Expected in the GUI thread.
67
HTMLElementImpl element = this.controlElement;
68         int dw = HtmlValues.getOldSyntaxPixelSize(element.getAttribute("width"), availWidth, -1);
69         int dh = HtmlValues.getOldSyntaxPixelSize(element.getAttribute("height"), availHeight, -1);
70         this.declaredWidth = dw;
71         this.declaredHeight = dh;
72         this.preferredSize = this.createPreferredSize(dw, dh);
73         int valign;
74         String JavaDoc alignText = element.getAttribute("align");
75         if(alignText == null) {
76             valign = RElement.VALIGN_BASELINE;
77         }
78         else {
79             alignText = alignText.toLowerCase().trim();
80             if("middle".equals(alignText)) {
81                 valign = RElement.VALIGN_MIDDLE;
82             }
83             else if("absmiddle".equals(alignText)) {
84                 valign = RElement.VALIGN_ABSMIDDLE;
85             }
86             else if("top".equals(alignText)) {
87                 valign = RElement.VALIGN_TOP;
88             }
89             else if("bottom".equals(alignText)) {
90                 valign = RElement.VALIGN_BOTTOM;
91             }
92             else if("baseline".equals(alignText)) {
93                 valign = RElement.VALIGN_BASELINE;
94             }
95             else if("absbottom".equals(alignText)) {
96                 valign = RElement.VALIGN_ABSBOTTOM;
97             }
98             else {
99                 valign = RElement.VALIGN_BASELINE;
100             }
101         }
102         this.valign = valign;
103     }
104
105     public int getVAlign() {
106         return this.valign;
107     }
108     
109     public Dimension getPreferredSize() {
110         Dimension ps = this.preferredSize;
111         return ps == null ? new Dimension(0, 0) : ps;
112     }
113     
114     public Dimension createPreferredSize(int dw, int dh) {
115         Image img = this.image;
116         if(dw == -1) {
117             dw = img == null ? -1 : img.getWidth(this);
118             if(dw == -1) {
119                 dw = 0;
120             }
121         }
122         if(dh == -1) {
123             dh = img == null ? -1 : img.getHeight(this);
124             if(dh == -1) {
125                 dh = 0;
126             }
127         }
128         return new Dimension(dw, dh);
129     }
130
131     private final boolean checkPreferredSizeChange() {
132         Dimension newPs = this.createPreferredSize(this.declaredWidth, this.declaredHeight);
133         Dimension ps = this.preferredSize;
134         if(ps == null) {
135             return true;
136         }
137         if(ps.width != newPs.width || ps.height != newPs.height) {
138             this.preferredSize = newPs;
139             return true;
140         }
141         else {
142             return false;
143         }
144     }
145     
146 // private void checkImgSrc() {
147
// ModelNode rc = this.controlElement;
148
// if(rc instanceof HTMLImageElement) {
149
// HTMLImageElement ie = (HTMLImageElement) rc;
150
// String newSrc = ie.getSrc();
151
// if(newSrc == null) {
152
// this.lastSrc = null;
153
// this.image = null;
154
// }
155
// else if(!newSrc.equals(this.lastSrc)) {
156
// this.lastSrc = newSrc;
157
// this.image = null;
158
// this.loadImage(newSrc);
159
// }
160
// }
161
// }
162
//
163
// public void invalidate() {
164
// super.invalidate();
165
// this.checkImgSrc();
166
// }
167

168     /* (non-Javadoc)
169      * @see java.awt.Component#imageUpdate(java.awt.Image, int, int, int, int, int)
170      */

171     public boolean imageUpdate(Image img, int infoflags, int x, int y, final int w, final int h) {
172         if((infoflags & ImageObserver.ALLBITS) != 0 || (infoflags & ImageObserver.FRAMEBITS) != 0) {
173             EventQueue.invokeLater(new Runnable JavaDoc() {
174                 public void run() {
175                     if(!checkPreferredSizeChange()) {
176                         repaint();
177                     }
178                     else {
179                         ruicontrol.preferredSizeInvalidated();
180                     }
181                 }
182             });
183         }
184         return true;
185     }
186
187     /* (non-Javadoc)
188      * @see java.awt.Component#imageUpdate(java.awt.Image, int, int, int, int, int)
189      */

190     public void imageUpdate(Image img, final int w, final int h) {
191         EventQueue.invokeLater(new Runnable JavaDoc() {
192             public void run() {
193                 if(!checkPreferredSizeChange()) {
194                     repaint();
195                 }
196                 else {
197                     ruicontrol.preferredSizeInvalidated();
198                 }
199             }
200         });
201     }
202
203     public boolean paintSelection(Graphics g, boolean inSelection, RenderableSpot startPoint, RenderableSpot endPoint) {
204         return inSelection;
205     }
206     
207     public void imageLoaded(ImageEvent event) {
208         // Implementation of ImageListener. Invoked in a request thread most likely.
209
Image image = event.image;
210         this.image = image;
211         int width = image.getWidth(this);
212         int height = image.getHeight(this);
213         if(width != -1 && height != -1) {
214             this.imageUpdate(image, width, height);
215         }
216     }
217
218     public String JavaDoc toString() {
219         return "ImgControl[src=" + this.lastSrc + "]";
220     }
221 }
222
Popular Tags