diff options
Diffstat (limited to 'javax/swing')
-rw-r--r-- | javax/swing/plaf/basic/BasicLabelUI.java | 104 |
1 files changed, 65 insertions, 39 deletions
diff --git a/javax/swing/plaf/basic/BasicLabelUI.java b/javax/swing/plaf/basic/BasicLabelUI.java index 1ec020b1c..ae992594d 100644 --- a/javax/swing/plaf/basic/BasicLabelUI.java +++ b/javax/swing/plaf/basic/BasicLabelUI.java @@ -37,13 +37,14 @@ package javax.swing.plaf.basic; -import java.awt.Color; import java.awt.Component; import java.awt.Dimension; +import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Insets; import java.awt.Rectangle; +import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.beans.PropertyChangeEvent; @@ -58,6 +59,7 @@ import javax.swing.JLabel; import javax.swing.KeyStroke; import javax.swing.LookAndFeel; import javax.swing.SwingUtilities; +import javax.swing.UIManager; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.LabelUI; import javax.swing.text.View; @@ -80,6 +82,11 @@ public class BasicLabelUI extends LabelUI implements PropertyChangeListener private Rectangle tr; /** + * A cached Insets object for reuse in the label layout methods. + */ + private Insets cachedInsets; + + /** * Creates a new BasicLabelUI object. */ public BasicLabelUI() @@ -131,7 +138,7 @@ public class BasicLabelUI extends LabelUI implements PropertyChangeListener icon.getIconHeight() + insetsY); else { - FontMetrics fm = lab.getFontMetrics(lab.getFont()); + FontMetrics fm = getFontMetrics(lab); ir.x = 0; ir.y = 0; ir.width = 0; @@ -189,43 +196,46 @@ public class BasicLabelUI extends LabelUI implements PropertyChangeListener public void paint(Graphics g, JComponent c) { JLabel b = (JLabel) c; - FontMetrics fm = g.getFontMetrics(); - - Insets i = c.getInsets(); - vr.x = i.left; - vr.y = i.right; - vr.width = c.getWidth() - i.left + i.right; - vr.height = c.getHeight() - i.top + i.bottom; - ir.x = 0; - ir.y = 0; - ir.width = 0; - ir.height = 0; - tr.x = 0; - tr.y = 0; - tr.width = 0; - tr.height = 0; Icon icon = (b.isEnabled()) ? b.getIcon() : b.getDisabledIcon(); + String text = b.getText(); + if (icon != null || (text != null && ! text.equals(""))) + { + FontMetrics fm = getFontMetrics(b); + Insets i = c.getInsets(cachedInsets); + vr.x = i.left; + vr.y = i.right; + vr.width = c.getWidth() - i.left - i.right; + vr.height = c.getHeight() - i.top - i.bottom; + ir.x = 0; + ir.y = 0; + ir.width = 0; + ir.height = 0; + tr.x = 0; + tr.y = 0; + tr.width = 0; + tr.height = 0; - String text = layoutCL(b, fm, b.getText(), icon, vr, ir, tr); + text = layoutCL(b, fm, text, icon, vr, ir, tr); - if (icon != null) - icon.paintIcon(b, g, ir.x, ir.y); + if (icon != null) + icon.paintIcon(b, g, ir.x, ir.y); - Object htmlRenderer = b.getClientProperty(BasicHTML.propertyKey); - if (htmlRenderer == null) - { - if (text != null && !text.equals("")) + if (text != null && ! text.equals("")) { - if (b.isEnabled()) - paintEnabledText(b, g, text, tr.x, tr.y + fm.getAscent()); + Object htmlRenderer = b.getClientProperty(BasicHTML.propertyKey); + if (htmlRenderer == null) + { + if (b.isEnabled()) + paintEnabledText(b, g, text, tr.x, tr.y + fm.getAscent()); + else + paintDisabledText(b, g, text, tr.x, tr.y + fm.getAscent()); + } else - paintDisabledText(b, g, text, tr.x, tr.y + fm.getAscent()); + { + ((View) htmlRenderer).paint(g, tr); + } } } - else - { - ((View) htmlRenderer).paint(g, tr); - } } /** @@ -265,8 +275,6 @@ public class BasicLabelUI extends LabelUI implements PropertyChangeListener protected void paintDisabledText(JLabel l, Graphics g, String s, int textX, int textY) { - Color saved_color = g.getColor(); - g.setColor(l.getBackground().brighter()); int mnemIndex = l.getDisplayedMnemonicIndex(); @@ -283,8 +291,6 @@ public class BasicLabelUI extends LabelUI implements PropertyChangeListener textY + 1); else g.drawString(s, textX + 1, textY + 1); - - g.setColor(saved_color); } /** @@ -298,9 +304,8 @@ public class BasicLabelUI extends LabelUI implements PropertyChangeListener * @param textY The y coordinate of the start of the baseline. */ protected void paintEnabledText(JLabel l, Graphics g, String s, int textX, - int textY) + int textY) { - Color saved_color = g.getColor(); g.setColor(l.getForeground()); int mnemIndex = l.getDisplayedMnemonicIndex(); @@ -310,8 +315,6 @@ public class BasicLabelUI extends LabelUI implements PropertyChangeListener textY); else g.drawString(s, textX, textY); - - g.setColor(saved_color); } /** @@ -514,4 +517,27 @@ public class BasicLabelUI extends LabelUI implements PropertyChangeListener "press"); } } + + /** + * Fetches a font metrics object for the specified label. This first + * tries to get it from the label object itself by calling + * {@link Component#getFontMetrics(Font)}, and if that does not work + * (for instance, when we are in the initialization and have no parent yet), + * it asks the Toolkit for a font metrics object. + * + * @param l the label + * + * @return a suitable font metrics object + */ + private FontMetrics getFontMetrics(JLabel l) + { + Font font = l.getFont(); + FontMetrics fm = l.getFontMetrics(font); + if (fm == null) + { + Toolkit tk = Toolkit.getDefaultToolkit(); + fm = tk.getFontMetrics(font); + } + return fm; + } } |