summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Kennke <roman@kennke.org>2007-04-03 20:34:27 +0000
committerRoman Kennke <roman@kennke.org>2007-04-03 20:34:27 +0000
commitdba7858f5282d13daf279c2cfa7adc2635a94f48 (patch)
treed60e840da4a09d2ba454d09ef844d52ff7f11eec
parent93c97c768f85f766eb1b1f7238b345070f8b2204 (diff)
downloadclasspath-dba7858f5282d13daf279c2cfa7adc2635a94f48.tar.gz
2007-04-03 Roman Kennke <roman@kennke.org>
* javax/swing/plaf/basic/BasicLabelUI.java (cachedInsets): New field. Used for reusing the insets instance. (getFontMetrics): New helper method for fetching a suitable FontMetrics object. (getPreferredSize): Use new helper method for font metrics. (paint): Only do something if we have an icon or text. Use cached Insets instance and new font metrics helper. (paintDisabledText): Don't restore the graphics' color. (paintEnabledText): Don't restore the graphics' color.
-rw-r--r--ChangeLog12
-rw-r--r--javax/swing/plaf/basic/BasicLabelUI.java104
2 files changed, 77 insertions, 39 deletions
diff --git a/ChangeLog b/ChangeLog
index dbbaa1916..c6e49cf1f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
2007-04-03 Roman Kennke <roman@kennke.org>
+ * javax/swing/plaf/basic/BasicLabelUI.java
+ (cachedInsets): New field. Used for reusing the insets instance.
+ (getFontMetrics): New helper method for fetching a suitable
+ FontMetrics object.
+ (getPreferredSize): Use new helper method for font metrics.
+ (paint): Only do something if we have an icon or text.
+ Use cached Insets instance and new font metrics helper.
+ (paintDisabledText): Don't restore the graphics' color.
+ (paintEnabledText): Don't restore the graphics' color.
+
+2007-04-03 Roman Kennke <roman@kennke.org>
+
* javax/swing/plaf/metal/MetalButtonUI.java
(paintButtonPressed): Fill the whole button not only visibleRect.
* javax/swing/plaf/metal/MetalLookAndFeel.java
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;
+ }
}