KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > awt > ImageTextLabel


1 package com.sshtools.ui.awt;
2
3 import java.awt.Canvas JavaDoc;
4 import java.awt.Color JavaDoc;
5 import java.awt.Container JavaDoc;
6 import java.awt.Dimension JavaDoc;
7 import java.awt.Font JavaDoc;
8 import java.awt.FontMetrics JavaDoc;
9 import java.awt.Graphics JavaDoc;
10 import java.awt.Image JavaDoc;
11 import java.awt.Insets JavaDoc;
12 import java.awt.SystemColor JavaDoc;
13 import java.awt.image.FilteredImageSource JavaDoc;
14
15 /**
16  * A label that can display both graphics and text, somewhat similar to swings
17  * JLabel.
18  *
19  * @author $Author: james $
20  */

21 public class ImageTextLabel extends Canvas JavaDoc {
22
23     /**
24      * No border. This will not take up any space.
25      */

26     public static final int NONE = 0;
27
28     /**
29      * Lowered bevel border (takes up 2 pixels)
30      */

31     public static final int LOWERED_BEVEL = 1;
32
33     /**
34      * Raised bevel border (takes up 2 pixels)
35      */

36     public static final int RAISED_BEVEL = 2;
37
38     /**
39      * Empty space (takes up 2 pixels)
40      */

41     public static final int EMPTY = 3;
42
43     /**
44      * Raised bevel border (takes up 2 pixels)
45      */

46     public static final int RAISED_ROUNDED = 4;
47
48     /**
49      * Flat (takes up 1 pixel)
50      */

51     public static final int FLAT = 6;
52
53     /**
54      * Left alignment
55      */

56     public static final int LEFT_ALIGNMENT = 0;
57
58     /**
59      * Center alignment
60      */

61     public static final int CENTER_ALIGNMENT = 1;
62
63     /**
64      * Center alignment
65      */

66     public static final int RIGHT_ALIGNMENT = 2;
67
68     // Private statics
69
private static final Insets JavaDoc DEFAULT_MARGIN = new Insets JavaDoc(0, 0, 0, 0);
70     private static final int DEFAULT_TEXT_IMAGE_GAP = 3;
71
72     // Private instance variables
73
private Image JavaDoc image;
74     private Image JavaDoc grayImage;
75     private Image JavaDoc buffer;
76     private String JavaDoc text;
77     private Insets JavaDoc margin;
78     private int borderType = NONE;
79     private Color JavaDoc borderShadowColor = SystemColor.controlShadow;
80     private Color JavaDoc borderDarkShadowColor = SystemColor.controlDkShadow;
81     private int width;
82     private int height;
83     private Container JavaDoc parentContainer;
84     private FontMetrics JavaDoc metrics;
85     private int textImageGap;
86     private boolean textVisible;
87     private boolean wasEnabled;
88     private int horizontalAlignment;
89
90     public ImageTextLabel() {
91         this(null, null);
92     }
93
94     public ImageTextLabel(Image JavaDoc image, String JavaDoc text) {
95         setImage(image);
96         this.text = text;
97         textVisible = true;
98         textImageGap = DEFAULT_TEXT_IMAGE_GAP;
99         margin = DEFAULT_MARGIN;
100     }
101
102     public void addNotify() {
103         super.addNotify();
104         metrics = getFontMetrics(getFont());
105     }
106
107     /**
108      * Set whether or not the text is visible
109      *
110      * @param textVisible
111      * text visible
112      */

113     public void setTextVisible(boolean textVisible) {
114         this.textVisible = textVisible;
115         buffer = null;
116         repaint();
117     }
118
119     /**
120      * Get whether or not the text is visible
121      *
122      * @return text visible
123      */

124     public boolean isTextVisible() {
125         return textVisible;
126     }
127
128     public void setMargin(Insets JavaDoc margin) {
129         this.margin = margin;
130         doLayout();
131         repaint();
132     }
133
134     public Insets JavaDoc getMargin() {
135         return margin;
136     }
137
138     public void paint(Graphics JavaDoc g1) {
139         Dimension JavaDoc d = getSize();
140         boolean enabled = isEnabled();
141         if (buffer == null || d.width != buffer.getWidth(this) || d.height != buffer.getHeight(this) || wasEnabled != enabled) {
142             if (buffer != null) {
143                 buffer.getGraphics().dispose();
144             }
145             try {
146                 buffer = createImage(d.width, d.height);
147             }
148             catch(Throwable JavaDoc t) {
149                 // Its possible the graphics cannot be created yet, just ignore and hope that they can be on the next repaint
150
return;
151             }
152         }
153         wasEnabled = enabled;
154         Graphics JavaDoc g = buffer == null ? g1 : buffer.getGraphics();
155         renderComponent(g, d, enabled);
156         if (buffer != null) {
157             g1.drawImage(buffer, 0, 0, this);
158         }
159     }
160
161     protected void renderComponent(Graphics JavaDoc g, Dimension JavaDoc d, boolean enabled) {
162         g.setFont(getFont());
163         if (metrics == null) {
164             metrics = g.getFontMetrics(g.getFont());
165         }
166         g.setColor(getBackground());
167         g.fillRect(0, 0, d.width, d.height);
168
169         int imageX = -1;
170         int textX = -1;
171         int totalWidth = 0;
172
173         // Get the relative positions for the image and the text
174
if (image != null) {
175             imageX = 0;
176             int imgWidth = image.getWidth(this);
177             totalWidth = imageX + imgWidth;
178             if (text != null && textVisible) {
179                 textX = imageX + imgWidth + textImageGap;
180                 totalWidth += textImageGap + getFontMetrics(getFont()).stringWidth(text);
181             }
182         } else {
183             if (text != null && textVisible) {
184                 textX = 0;
185                 totalWidth = getFontMetrics(getFont()).stringWidth(text);
186             }
187         }
188
189         // Get the offset based on the alignment and the width
190
Insets JavaDoc i = getInsets();
191         int offx = i.left;
192         int availableSpace = d.width - i.left - i.right;
193         switch (horizontalAlignment) {
194         case CENTER_ALIGNMENT:
195             offx += (availableSpace - totalWidth) / 2;
196             break;
197         case RIGHT_ALIGNMENT:
198             offx += availableSpace - totalWidth;
199             break;
200         }
201
202         // Draw the image
203
if (imageX != -1) {
204             g.drawImage(enabled ? image : grayImage, offx + imageX, (d.height - image.getHeight(this)) / 2, this);
205         }
206
207         // Draw the text
208
if (textX != -1) {
209             Color JavaDoc c = getForeground();
210             if (!enabled) {
211                 if (c == null) {
212                     c = Color.black;
213                 }
214                 float[] hsb = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);
215                 if (hsb[2] > 0.5) {
216                     hsb = new float[] { hsb[0], hsb[1], (hsb[2] - 0.4f) / 1.1f };
217                 } else {
218                     hsb = new float[] { hsb[0], hsb[1], (hsb[2] + 0.4f) * 1.1f };
219                 }
220                 c = new Color JavaDoc(Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]));
221             }
222             g.setColor(c);
223             
224
225             g.drawString(text, offx + textX, metrics.getHeight() - metrics.getDescent() + ((d.height - metrics.getHeight()) / 2));
226             
227             
228 // g.drawString(text, offx + textX, ((d.height - i.bottom) / 2 + metrics.getAscent() / 2));
229
}
230         paintBorder(g);
231     }
232     
233     public void setFont(Font JavaDoc font) {
234         super.setFont(font);
235         metrics = null;
236     }
237
238     public void doLayout() {
239         buffer = null;
240     }
241
242     /**
243      * Set the gap (in pixels) between the image and text. This will only be
244      * taken into account if both the image and text are set.
245      *
246      * @param textImageGap
247      * gap in pixels between text and image
248      */

249     public void setTextImageGap(int textImageGap) {
250         this.textImageGap = textImageGap;
251         buffer = null;
252         repaint();
253     }
254
255     /**
256      * Get the gap (in pixels) between the image and text. This will only be
257      * taken into account if both the image and text are set.
258      *
259      * @return image gap
260      */

261     public int getTextImageGap() {
262         return textImageGap;
263     }
264
265     /**
266      * Get the image
267      *
268      * @return image
269      */

270     public Image JavaDoc getImage() {
271         return image;
272     }
273
274     /**
275      * Set the image
276      *
277      * @param image
278      * image
279      */

280     public void setImage(Image JavaDoc image) {
281         this.image = image;
282         if (image != null) {
283             UIUtil.waitFor(image, this);
284             grayImage = createImage(new FilteredImageSource JavaDoc(image.getSource(), new GrayFilter()));
285             UIUtil.waitFor(grayImage, this);
286         } else {
287             grayImage = null;
288         }
289         buffer = null;
290         if (getGraphics() != null) {
291             repaint();
292         }
293     }
294
295     public Color JavaDoc getBorderShadowColor() {
296         return borderShadowColor;
297     }
298
299     public void setBorderShadowColor(Color JavaDoc borderShadowColor) {
300         this.borderShadowColor = borderShadowColor;
301     }
302
303     public Color JavaDoc getBorderDarkShadowColor() {
304         return borderDarkShadowColor;
305     }
306
307     public void setBorderDarkShadowColor(Color JavaDoc borderDarkShadowColor) {
308         this.borderDarkShadowColor = borderDarkShadowColor;
309     }
310
311     /**
312      * Set the text to display
313      *
314      * @param text
315      * text
316      */

317     public void setText(String JavaDoc text) {
318         this.text = text;
319         buffer = null;
320         repaint();
321     }
322
323     /**
324      * Get the text to display
325      *
326      * @param text
327      * text
328      */

329     public String JavaDoc getText() {
330         return text;
331     }
332
333     /**
334      * Set the border type. Can be one of :-
335      * </p>
336      *
337      * <ul>
338      * <li><code>ImageTextLabel.NONE</code></li>
339      * <li><code>ImageTextLabel.LOWERED</code></li>
340      * <li><code>ImageTextLabel.RAISED</code></li>
341      *
342      * @param borderType
343      * border type
344      */

345     public void setBorderType(int borderType) {
346         this.borderType = borderType;
347         buffer = null;
348         repaint();
349     }
350
351     /**
352      * Return insets sufficient for bevel and label drawing space.
353      */

354     public Insets JavaDoc getInsets() {
355         Insets JavaDoc i = borderType == NONE ? new Insets JavaDoc(0, 0, 0, 0) : new Insets JavaDoc(2, 2, 2, 2);
356         if (margin != null) {
357             i.top += margin.top;
358             i.bottom += margin.bottom;
359             i.left += margin.left;
360             i.right += margin.right;
361         }
362         return i;
363     }
364
365     /*
366      * (non-Javadoc)
367      *
368      * @see java.awt.Component#paint(java.awt.Graphics)
369      */

370     public void paintBorder(Graphics JavaDoc g) {
371         Dimension JavaDoc d = getSize();
372         Color JavaDoc si = getBorderDarkShadowColor();
373         Color JavaDoc so = si.darker();
374         Color JavaDoc hi = getBorderShadowColor();
375         Color JavaDoc ho = hi.brighter();
376         switch (borderType) {
377         case LOWERED_BEVEL:
378             g.setColor(si);
379             g.drawLine(0, 0, 0, d.height - 1);
380             g.drawLine(1, 0, d.width - 1, 0);
381             g.setColor(so);
382             g.drawLine(1, 1, 1, d.height - 2);
383             g.drawLine(2, 1, d.width - 2, 1);
384             g.setColor(ho);
385             g.drawLine(1, d.height - 1, d.width - 1, d.height - 1);
386             g.drawLine(d.width - 1, 1, d.width - 1, d.height - 2);
387             g.setColor(hi);
388             g.drawLine(2, d.height - 2, d.width - 2, d.height - 2);
389             g.drawLine(d.width - 2, 2, d.width - 2, d.height - 3);
390             break;
391         case RAISED_BEVEL:
392             g.setColor(ho);
393             g.drawLine(0, 0, 0, d.height - 2);
394             g.drawLine(1, 0, d.width - 2, 0);
395             g.setColor(hi);
396             g.drawLine(1, 1, 1, d.height - 3);
397             g.drawLine(2, 1, d.width - 3, 1);
398             g.setColor(so);
399             g.drawLine(0, d.height - 1, d.width - 1, d.height - 1);
400             g.drawLine(d.width - 1, 0, d.width - 1, d.height - 2);
401             g.setColor(si);
402             g.drawLine(1, d.height - 2, d.width - 2, d.height - 2);
403             g.drawLine(d.width - 2, 1, d.width - 2, d.height - 3);
404             break;
405         case RAISED_ROUNDED:
406             g.setColor(ho);
407             g.drawLine(1, 0, d.width - 2, 0);
408             g.drawLine(0, 1, 0, d.height - 2);
409             g.setColor(so);
410             g.drawLine(d.width - 1, 1, d.width - 1, d.height - 2);
411             g.drawLine(1, d.height - 1, d.width - 2, d.height - 1);
412             g.setColor(si);
413             g.drawLine(d.width - 2, 2, d.width - 2, d.height - 2);
414             g.drawLine(2, d.height - 2, d.width - 3, d.height - 2);
415             break;
416         case FLAT:
417             g.setColor(ho);
418             g.drawRect(0, 0, d.width - 1, d.height - 1);
419             break;
420         }
421     }
422
423     /*
424      * Prevent flicker
425      *
426      * @see java.awt.Component#update(java.awt.Graphics)
427      */

428     public void update(Graphics JavaDoc g) {
429         paint(g);
430     }
431
432     public Dimension JavaDoc getPreferredSize() {
433         Insets JavaDoc i = getInsets();
434         return new Dimension JavaDoc(i.left + i.right + 2 + (image != null && text != null && textVisible ? textImageGap : 0)
435                         + (image != null ? image.getWidth(this) : 0)
436                         + (metrics != null && textVisible && text != null ? metrics.stringWidth(text) : 0), i.top + i.bottom
437                         + Math.max(image != null ? image.getHeight(this) : 0, (metrics != null ? metrics.getHeight() : 0)));
438     }
439
440     public Dimension JavaDoc getMinimumSize() {
441         return getPreferredSize();
442     }
443
444     public void setHorizontalAlignment(int horizontalAlignment) {
445         this.horizontalAlignment = horizontalAlignment;
446         repaint();
447     }
448 }
Popular Tags