1 package com.sshtools.ui.awt; 2 3 import java.awt.Canvas ; 4 import java.awt.Color ; 5 import java.awt.Container ; 6 import java.awt.Dimension ; 7 import java.awt.Font ; 8 import java.awt.FontMetrics ; 9 import java.awt.Graphics ; 10 import java.awt.Image ; 11 import java.awt.Insets ; 12 import java.awt.SystemColor ; 13 import java.awt.image.FilteredImageSource ; 14 15 21 public class ImageTextLabel extends Canvas { 22 23 26 public static final int NONE = 0; 27 28 31 public static final int LOWERED_BEVEL = 1; 32 33 36 public static final int RAISED_BEVEL = 2; 37 38 41 public static final int EMPTY = 3; 42 43 46 public static final int RAISED_ROUNDED = 4; 47 48 51 public static final int FLAT = 6; 52 53 56 public static final int LEFT_ALIGNMENT = 0; 57 58 61 public static final int CENTER_ALIGNMENT = 1; 62 63 66 public static final int RIGHT_ALIGNMENT = 2; 67 68 private static final Insets DEFAULT_MARGIN = new Insets (0, 0, 0, 0); 70 private static final int DEFAULT_TEXT_IMAGE_GAP = 3; 71 72 private Image image; 74 private Image grayImage; 75 private Image buffer; 76 private String text; 77 private Insets margin; 78 private int borderType = NONE; 79 private Color borderShadowColor = SystemColor.controlShadow; 80 private Color borderDarkShadowColor = SystemColor.controlDkShadow; 81 private int width; 82 private int height; 83 private Container parentContainer; 84 private FontMetrics 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 image, String 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 113 public void setTextVisible(boolean textVisible) { 114 this.textVisible = textVisible; 115 buffer = null; 116 repaint(); 117 } 118 119 124 public boolean isTextVisible() { 125 return textVisible; 126 } 127 128 public void setMargin(Insets margin) { 129 this.margin = margin; 130 doLayout(); 131 repaint(); 132 } 133 134 public Insets getMargin() { 135 return margin; 136 } 137 138 public void paint(Graphics g1) { 139 Dimension 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 t) { 149 return; 151 } 152 } 153 wasEnabled = enabled; 154 Graphics 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 g, Dimension 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 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 Insets 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 if (imageX != -1) { 204 g.drawImage(enabled ? image : grayImage, offx + imageX, (d.height - image.getHeight(this)) / 2, this); 205 } 206 207 if (textX != -1) { 209 Color 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 (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 } 230 paintBorder(g); 231 } 232 233 public void setFont(Font font) { 234 super.setFont(font); 235 metrics = null; 236 } 237 238 public void doLayout() { 239 buffer = null; 240 } 241 242 249 public void setTextImageGap(int textImageGap) { 250 this.textImageGap = textImageGap; 251 buffer = null; 252 repaint(); 253 } 254 255 261 public int getTextImageGap() { 262 return textImageGap; 263 } 264 265 270 public Image getImage() { 271 return image; 272 } 273 274 280 public void setImage(Image image) { 281 this.image = image; 282 if (image != null) { 283 UIUtil.waitFor(image, this); 284 grayImage = createImage(new FilteredImageSource (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 getBorderShadowColor() { 296 return borderShadowColor; 297 } 298 299 public void setBorderShadowColor(Color borderShadowColor) { 300 this.borderShadowColor = borderShadowColor; 301 } 302 303 public Color getBorderDarkShadowColor() { 304 return borderDarkShadowColor; 305 } 306 307 public void setBorderDarkShadowColor(Color borderDarkShadowColor) { 308 this.borderDarkShadowColor = borderDarkShadowColor; 309 } 310 311 317 public void setText(String text) { 318 this.text = text; 319 buffer = null; 320 repaint(); 321 } 322 323 329 public String getText() { 330 return text; 331 } 332 333 345 public void setBorderType(int borderType) { 346 this.borderType = borderType; 347 buffer = null; 348 repaint(); 349 } 350 351 354 public Insets getInsets() { 355 Insets i = borderType == NONE ? new Insets (0, 0, 0, 0) : new Insets (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 370 public void paintBorder(Graphics g) { 371 Dimension d = getSize(); 372 Color si = getBorderDarkShadowColor(); 373 Color so = si.darker(); 374 Color hi = getBorderShadowColor(); 375 Color 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 428 public void update(Graphics g) { 429 paint(g); 430 } 431 432 public Dimension getPreferredSize() { 433 Insets i = getInsets(); 434 return new Dimension (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 getMinimumSize() { 441 return getPreferredSize(); 442 } 443 444 public void setHorizontalAlignment(int horizontalAlignment) { 445 this.horizontalAlignment = horizontalAlignment; 446 repaint(); 447 } 448 } | Popular Tags |