001: /*
002: * WbTextLabel.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.components;
013:
014: import java.awt.Color;
015: import java.awt.Font;
016: import java.awt.FontMetrics;
017: import java.awt.Graphics;
018: import javax.swing.JComponent;
019: import javax.swing.SwingConstants;
020: import javax.swing.UIManager;
021: import javax.swing.border.Border;
022:
023: /**
024: * Displays a Label left or right aligned with no further overhead in painting
025: * (Faster then JLabel) this is used to in DwStatusBar to speed
026: * up processes that do give a lot of feedback (e.g. import)
027: */
028: public class WbTextLabel extends JComponent {
029: private final static int DEFAULT_TEXT_Y = 15;
030: private String text;
031: private final Color textColor;
032: private int textX = 2;
033: private int textY = DEFAULT_TEXT_Y;
034: private int alignment = SwingConstants.LEFT;
035: private FontMetrics fm;
036: private boolean hasBorder = false;
037:
038: public WbTextLabel() {
039: super ();
040: this .setDoubleBuffered(true);
041: this .setBackground(UIManager.getColor("Label.background"));
042: this .textColor = UIManager.getColor("Label.foreground");
043: this .setForeground(textColor);
044: this .setOpaque(false);
045: Font f = UIManager.getFont("Label.font");
046: if (f != null) {
047: super .setFont(f);
048: this .fm = this .getFontMetrics(f);
049: textY = (fm != null ? fm.getAscent() + 2 : DEFAULT_TEXT_Y);
050: }
051: }
052:
053: public void setBorder(Border b) {
054: super .setBorder(b);
055: hasBorder = (b != null);
056: }
057:
058: public void setHorizontalAlignment(int align) {
059: this .alignment = align;
060: }
061:
062: public void setFont(Font f) {
063: super .setFont(f);
064: this .fm = this .getFontMetrics(f);
065: textY = (fm != null ? fm.getAscent() + 2 : DEFAULT_TEXT_Y);
066: }
067:
068: public String getText() {
069: return this .text;
070: }
071:
072: public void setText(String label) {
073: this .text = label;
074: if (alignment == SwingConstants.RIGHT) {
075: int w = (fm != null ? fm.stringWidth(this .text) : 15);
076: textX = this .getWidth() - w - 4;
077: }
078: invalidate();
079: repaint();
080: }
081:
082: public void forcePaint() {
083: Graphics g = getGraphics();
084: if (g == null)
085: return;
086: g.setColor(getBackground());
087: g.clearRect(0, 0, this .getWidth() - 4, getHeight() - 1);
088: paint(g);
089: }
090:
091: public void paint(Graphics g) {
092: if (g == null)
093: return;
094: if (hasBorder)
095: super.paint(g);
096: if (text != null) {
097: g.setColor(this.textColor);
098: g.drawString(this.text, textX, textY);
099: }
100: }
101:
102: }
|