KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.lobobrowser.html.renderer;
22
23 import java.awt.Color JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.EventQueue JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.Image JavaDoc;
28 import java.awt.Insets JavaDoc;
29 import java.awt.image.ImageObserver JavaDoc;
30 import java.awt.event.*;
31
32 import org.lobobrowser.html.domimpl.*;
33 import org.lobobrowser.html.style.HtmlValues;
34 import org.lobobrowser.util.gui.*;
35
36 class InputImageControl extends BaseInputControl implements ImageListener {
37     //private JButton button;
38
private boolean mouseBeingPressed;
39     
40     public InputImageControl(final HTMLBaseInputElement modelNode) {
41         super(modelNode);
42         this.setLayout(WrapperLayout.getInstance());
43 // JButton button = new LocalButton();
44
// this.button = button;
45
// button.setMargin(RBlockViewport.ZERO_INSETS);
46
// button.setBorder(null);
47
// this.add(button);
48
modelNode.addImageListener(this);
49         this.addMouseListener(new MouseAdapter() {
50             public void mousePressed(MouseEvent e) {
51                 mouseBeingPressed = true;
52                 repaint();
53             }
54
55 // public void mouseExited(MouseEvent e) {
56
// mouseBeingPressed = false;
57
// repaint();
58
// }
59

60             public void mouseReleased(MouseEvent e) {
61                 mouseBeingPressed = false;
62                 repaint();
63                 HtmlController.getInstance().onPressed(modelNode, e, e.getX(), e.getY());
64             }
65         });
66     }
67
68     private int valign = RElement.VALIGN_BASELINE;
69     private Dimension JavaDoc preferredSize;
70     private int declaredWidth;
71     private int declaredHeight;
72     private Image JavaDoc image;
73
74     public void reset(int availWidth, int availHeight) {
75         super.reset(availWidth, availHeight);
76         HTMLElementImpl element = this.controlElement;
77         int dw = HtmlValues.getOldSyntaxPixelSize(element.getAttribute("width"), availWidth, -1);
78         int dh = HtmlValues.getOldSyntaxPixelSize(element.getAttribute("height"), availHeight, -1);
79         this.declaredWidth = dw;
80         this.declaredHeight = dh;
81         this.preferredSize = this.createPreferredSize(dw, dh);
82         int valign;
83         String JavaDoc alignText = element.getAttribute("align");
84         if(alignText == null) {
85             valign = RElement.VALIGN_BASELINE;
86         }
87         else {
88             alignText = alignText.toLowerCase().trim();
89             if("middle".equals(alignText)) {
90                 valign = RElement.VALIGN_MIDDLE;
91             }
92             else if("absmiddle".equals(alignText)) {
93                 valign = RElement.VALIGN_ABSMIDDLE;
94             }
95             else if("top".equals(alignText)) {
96                 valign = RElement.VALIGN_TOP;
97             }
98             else if("bottom".equals(alignText)) {
99                 valign = RElement.VALIGN_BOTTOM;
100             }
101             else if("baseline".equals(alignText)) {
102                 valign = RElement.VALIGN_BASELINE;
103             }
104             else if("absbottom".equals(alignText)) {
105                 valign = RElement.VALIGN_ABSBOTTOM;
106             }
107             else {
108                 valign = RElement.VALIGN_BASELINE;
109             }
110         }
111         this.valign = valign;
112     }
113
114     public int getVAlign() {
115         return this.valign;
116     }
117     
118     public void paintComponent(Graphics JavaDoc g) {
119         super.paintComponent(g);
120         Dimension JavaDoc size = this.getSize();
121         Insets JavaDoc insets = this.getInsets();
122         synchronized(this) {}
123         Image JavaDoc image = this.image;
124         if(image != null) {
125             g.drawImage(image,
126                     insets.left, insets.top,
127                     size.width - insets.left - insets.right,
128                     size.height - insets.top - insets.bottom, this);
129         }
130         else {
131             //TODO: alt
132
}
133         if(this.mouseBeingPressed) {
134             Color JavaDoc over = new Color JavaDoc(255, 100, 100, 64);
135             if(over != null) {
136                 Color JavaDoc oldColor = g.getColor();
137                 try {
138                     g.setColor(over);
139                     g.fillRect(0, 0, size.width, size.height);
140                 } finally {
141                     g.setColor(oldColor);
142                 }
143             }
144         }
145     }
146
147     public Dimension JavaDoc getPreferredSize() {
148         Dimension JavaDoc ps = this.preferredSize;
149         return ps == null ? new Dimension JavaDoc(0, 0) : ps;
150     }
151
152     public Dimension JavaDoc createPreferredSize(int dw, int dh) {
153         Image JavaDoc img = this.image;
154         if(dw == -1) {
155             dw = img == null ? -1 : img.getWidth(this);
156             if(dw == -1) {
157                 dw = 0;
158             }
159         }
160         if(dh == -1) {
161             dh = img == null ? -1 : img.getHeight(this);
162             if(dh == -1) {
163                 dh = 0;
164             }
165         }
166         return new Dimension JavaDoc(dw, dh);
167     }
168
169     private final boolean checkPreferredSizeChange() {
170         Dimension JavaDoc newPs = this.createPreferredSize(this.declaredWidth, this.declaredHeight);
171         Dimension JavaDoc ps = this.preferredSize;
172         if(ps == null) {
173             return true;
174         }
175         if(ps.width != newPs.width || ps.height != newPs.height) {
176             this.preferredSize = newPs;
177             return true;
178         }
179         else {
180             return false;
181         }
182     }
183
184     /* (non-Javadoc)
185      * @see java.awt.Component#imageUpdate(java.awt.Image, int, int, int, int, int)
186      */

187     public boolean imageUpdate(Image JavaDoc img, int infoflags, int x, int y, final int w, final int h) {
188         if((infoflags & ImageObserver.ALLBITS) != 0 || (infoflags & ImageObserver.FRAMEBITS) != 0) {
189             EventQueue.invokeLater(new Runnable JavaDoc() {
190                 public void run() {
191                     if(!checkPreferredSizeChange()) {
192                         repaint();
193                     }
194                     else {
195                         ruicontrol.preferredSizeInvalidated();
196                     }
197                 }
198             });
199         }
200         return true;
201     }
202
203     /* (non-Javadoc)
204      * @see java.awt.Component#imageUpdate(java.awt.Image, int, int, int, int, int)
205      */

206     public void imageUpdate(Image JavaDoc img, final int w, final int h) {
207         EventQueue.invokeLater(new Runnable JavaDoc() {
208             public void run() {
209                 if(!checkPreferredSizeChange()) {
210                     repaint();
211                 }
212                 else {
213                     ruicontrol.preferredSizeInvalidated();
214                 }
215             }
216         });
217     }
218
219     public boolean paintSelection(Graphics JavaDoc g, boolean inSelection, RenderableSpot startPoint, RenderableSpot endPoint) {
220         return inSelection;
221     }
222     
223     public void imageLoaded(ImageEvent event) {
224         // Implementation of ImageListener. Invoked in a request thread most likely.
225
Image JavaDoc image = event.image;
226 // ImageIcon imageIcon = new ImageIcon(image);
227
// this.button.setIcon(imageIcon);
228
this.image = image;
229         int width = image.getWidth(this);
230         int height = image.getHeight(this);
231         if(width != -1 && height != -1) {
232             this.imageUpdate(image, width, height);
233         }
234     }
235     
236     public void resetInput() {
237         // NOP
238
}
239     
240 // private static class LocalButton extends JButton {
241
// public void revalidate() {
242
// // ignore
243
// }
244
//
245
// public void repaint() {
246
// // ignore
247
// }
248
// }
249
}
250
Popular Tags