aboutsummaryrefslogtreecommitdiff
path: root/libjava/classpath/javax/swing/plaf/metal/MetalToggleButtonUI.java
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/classpath/javax/swing/plaf/metal/MetalToggleButtonUI.java')
-rw-r--r--libjava/classpath/javax/swing/plaf/metal/MetalToggleButtonUI.java129
1 files changed, 105 insertions, 24 deletions
diff --git a/libjava/classpath/javax/swing/plaf/metal/MetalToggleButtonUI.java b/libjava/classpath/javax/swing/plaf/metal/MetalToggleButtonUI.java
index be6d0c39ec8..46a19bdbe9e 100644
--- a/libjava/classpath/javax/swing/plaf/metal/MetalToggleButtonUI.java
+++ b/libjava/classpath/javax/swing/plaf/metal/MetalToggleButtonUI.java
@@ -39,13 +39,24 @@ exception statement from your version. */
package javax.swing.plaf.metal;
import java.awt.Color;
+import java.awt.Font;
+import java.awt.FontMetrics;
+import java.awt.Graphics;
+import java.awt.Rectangle;
+import javax.swing.AbstractButton;
import javax.swing.JComponent;
+import javax.swing.JToggleButton;
+import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
+import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.plaf.basic.BasicToggleButtonUI;
+/**
+ * A UI delegate for the {@link JToggleButton} component.
+ */
public class MetalToggleButtonUI
extends BasicToggleButtonUI
{
@@ -59,21 +70,26 @@ public class MetalToggleButtonUI
/** The color for disabled button labels. */
protected Color disabledTextColor;
- /** The shared UI instance for MetalToggleButtonUIs */
- private static MetalToggleButtonUI instance = null;
+ /**
+ * Returns a new instance of <code>MetalToggleButtonUI</code>.
+ *
+ * @param component the component for which we return an UI instance
+ *
+ * @return A new instance of <code>MetalToggleButtonUI</code>.
+ */
+ public static ComponentUI createUI(JComponent component)
+ {
+ return new MetalToggleButtonUI();
+ }
/**
- * Constructs a new instance of MetalToggleButtonUI.
+ * Constructs a new instance of <code>MetalToggleButtonUI</code>.
*/
public MetalToggleButtonUI()
{
super();
- focusColor = getFocusColor();
- selectColor = getSelectColor();
- disabledTextColor = getDisabledTextColor();
}
-
/**
* Returns the color for the focus border.
*
@@ -81,8 +97,7 @@ public class MetalToggleButtonUI
*/
protected Color getFocusColor()
{
- UIDefaults def = UIManager.getLookAndFeelDefaults();
- return def.getColor(getPropertyPrefix() + ".focus");
+ return focusColor;
}
/**
@@ -92,32 +107,98 @@ public class MetalToggleButtonUI
*/
protected Color getSelectColor()
{
- UIDefaults def = UIManager.getLookAndFeelDefaults();
- return def.getColor(getPropertyPrefix() + ".select");
+ return selectColor;
}
/**
- * Returns the color for the text label of disabled buttons.
+ * Returns the color for the text label of disabled buttons. The value
+ * is initialised in the {@link #installDefaults(AbstractButton)} method
+ * by reading the <code>ToggleButton.disabledText</code> item from the UI
+ * defaults.
*
- * @return the color for the text label of disabled buttons
+ * @return The color for the text label of disabled buttons.
*/
protected Color getDisabledTextColor()
{
- UIDefaults def = UIManager.getLookAndFeelDefaults();
- return def.getColor(getPropertyPrefix() + ".disabledText");
+ return disabledTextColor;
}
/**
- * Returns an instance of MetalToggleButtonUI.
- *
- * @param component the component for which we return an UI instance
- *
- * @return an instance of MetalToggleButtonUI
+ * Updates the button with the defaults for this look and feel.
+ *
+ * @param b the button.
*/
- public static ComponentUI createUI(JComponent component)
+ public void installDefaults(AbstractButton b)
+ {
+ super.installDefaults(b);
+ UIDefaults defaults = UIManager.getLookAndFeelDefaults();
+ focusColor = defaults.getColor(getPropertyPrefix() + "focus");
+ selectColor = defaults.getColor(getPropertyPrefix() + "select");
+ disabledTextColor = defaults.getColor(getPropertyPrefix() + "disabledText");
+ }
+
+ /**
+ * Paints the button background when it is pressed/selected.
+ *
+ * @param g the graphics device.
+ * @param b the button.
+ */
+ protected void paintButtonPressed(Graphics g, AbstractButton b)
+ {
+ if (b.isContentAreaFilled() && b.isOpaque())
+ {
+ Color saved = g.getColor();
+ Rectangle bounds = SwingUtilities.getLocalBounds(b);
+ g.setColor(selectColor);
+ g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
+ g.setColor(saved);
+ }
+ }
+
+ /**
+ * Paints the text for the button.
+ *
+ * @param g the graphics device.
+ * @param c the component.
+ * @param textRect the bounds for the text.
+ * @param text the text.
+ *
+ * @deprecated 1.4 Use {@link BasicButtonUI#paintText(java.awt.Graphics,
+ * javax.swing.AbstractButton, java.awt.Rectangle, java.lang.String)}.
+ */
+ protected void paintText(Graphics g, JComponent c, Rectangle textRect,
+ String text)
+ {
+ Font savedFont = g.getFont();
+ Color savedColor = g.getColor();
+ g.setFont(c.getFont());
+ if (c.isEnabled())
+ g.setColor(c.getForeground());
+ else
+ g.setColor(disabledTextColor);
+ FontMetrics fm = g.getFontMetrics(c.getFont());
+ int ascent = fm.getAscent();
+ g.drawString(text, textRect.x, textRect.y + ascent);
+ g.setFont(savedFont);
+ g.setColor(savedColor);
+ }
+
+ /**
+ * Draws the focus highlight around the text and icon.
+ *
+ * @param g the graphics device.
+ * @param b the button.
+ */
+ protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect,
+ Rectangle textRect, Rectangle iconRect)
{
- if (instance == null)
- instance = new MetalToggleButtonUI();
- return instance;
+ if (!b.hasFocus())
+ return;
+ Color saved = g.getColor();
+ g.setColor(focusColor);
+ Rectangle fr = iconRect.union(textRect);
+ g.drawRect(fr.x - 1, fr.y - 1, fr.width + 1, fr.height + 1);
+ g.setColor(saved);
}
+
}