classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] FYI: MetalToggleButtonUI


From: David Gilbert
Subject: [cp-patches] FYI: MetalToggleButtonUI
Date: Thu, 15 Sep 2005 14:20:12 +0000
User-agent: Mozilla Thunderbird 1.0.6 (X11/20050728)

I committed this patch to implement the missing methods in MetalToggleButtonUI.java and get this UI delegate to use the ToggleButtonBorder that I committed earlier:

2005-09-15  David Gilbert  <address@hidden>

        * javax/swing/plaf/basic/BasicButtonUI.java
        (installDefaults): set font,
        (uninstallDefaults): clear font,
        * javax/swing/plaf/metal/MetalLookAndFeel.java
        (initComponentDefaults): update ToggleButton defaults,
        * javax/swing/plaf/metal/MetalToggleButtonUI.java
        (instance): removed field,
        (createUI): just return new instance every time,
        (MetalToggleButtonUI): look up defaults directly,
        (getFocusColor): return value from field initialised in constructor,
        (getSelectColor): likewise,
        (getDisabledTextColor): likewise,
        (installDefaults): override to make public,
        (paintButtonPressed): implemented,
        (paintText): implemented,
        (paintFocus): implemented.

Regards,

Dave Gilbert
Index: javax/swing/plaf/basic/BasicButtonUI.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicButtonUI.java,v
retrieving revision 1.24
diff -u -r1.24 BasicButtonUI.java
--- javax/swing/plaf/basic/BasicButtonUI.java   31 Aug 2005 15:23:10 -0000      
1.24
+++ javax/swing/plaf/basic/BasicButtonUI.java   15 Sep 2005 13:08:42 -0000
@@ -55,6 +55,7 @@
 import javax.swing.UIManager;
 import javax.swing.plaf.ButtonUI;
 import javax.swing.plaf.ComponentUI;
+import javax.swing.plaf.UIResource;
 
 public class BasicButtonUI extends ButtonUI
 {
@@ -122,6 +123,7 @@
   {
     UIDefaults defaults = UIManager.getLookAndFeelDefaults();
     String prefix = getPropertyPrefix();
+    b.setFont(defaults.getFont(prefix + "font"));
     focusColor = defaults.getColor(prefix + "focus");
     b.setForeground(defaults.getColor(prefix + "foreground"));
     b.setBackground(defaults.getColor(prefix + "background"));
@@ -135,6 +137,8 @@
 
   protected void uninstallDefaults(AbstractButton b)
   {
+    if (b.getFont() instanceof UIResource)
+      b.setFont(null);
     b.setForeground(null);
     b.setBackground(null);
     b.setBorder(null);
Index: javax/swing/plaf/metal/MetalLookAndFeel.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalLookAndFeel.java,v
retrieving revision 1.55
diff -u -r1.55 MetalLookAndFeel.java
--- javax/swing/plaf/metal/MetalLookAndFeel.java        12 Sep 2005 23:04:55 
-0000      1.55
+++ javax/swing/plaf/metal/MetalLookAndFeel.java        15 Sep 2005 13:08:44 
-0000
@@ -1088,15 +1088,15 @@
       "TitledBorder.titleColor", getSystemTextColor(),
 
       "ToggleButton.background", getControl(),
-      "ToggleButton.border", MetalBorders.getButtonBorder(),
+      "ToggleButton.border", MetalBorders.getToggleButtonBorder(),
       "ToggleButton.darkShadow", getControlDarkShadow(),
       "ToggleButton.disabledText", getInactiveControlTextColor(),
-      "ToggleButton.focus", new ColorUIResource(getFocusColor()),
+      "ToggleButton.focus", getFocusColor(),
       "ToggleButton.font", getControlTextFont(),
       "ToggleButton.foreground", getControlTextColor(),
       "ToggleButton.highlight", getControlHighlight(),
       "ToggleButton.light", getControlHighlight(),
-      "ToggleButton.margin", new Insets(2, 14, 2, 14),
+      "ToggleButton.margin", new InsetsUIResource(2, 14, 2, 14),
       "ToggleButton.select", getControlShadow(),
       "ToggleButton.shadow", getControlShadow(),
 
Index: javax/swing/plaf/metal/MetalToggleButtonUI.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalToggleButtonUI.java,v
retrieving revision 1.3
diff -u -r1.3 MetalToggleButtonUI.java
--- javax/swing/plaf/metal/MetalToggleButtonUI.java     22 Jul 2005 08:04:42 
-0000      1.3
+++ javax/swing/plaf/metal/MetalToggleButtonUI.java     15 Sep 2005 13:08:44 
-0000
@@ -39,13 +39,24 @@
 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 address@hidden JToggleButton} components.
+ */
 public class MetalToggleButtonUI
   extends BasicToggleButtonUI
 {
@@ -59,8 +70,17 @@
   /** The color for disabled button labels. */
   protected Color disabledTextColor;
 
-  /** The shared UI instance for MetalToggleButtonUIs */
-  private static MetalToggleButtonUI instance = null;
+  /**
+   * Returns an instance of MetalToggleButtonUI.
+   *
+   * @param component the component for which we return an UI instance
+   *
+   * @return an instance of MetalToggleButtonUI
+   */
+  public static ComponentUI createUI(JComponent component)
+  {
+    return new MetalToggleButtonUI();
+  }
 
   /**
    * Constructs a new instance of MetalToggleButtonUI.
@@ -68,12 +88,12 @@
   public MetalToggleButtonUI()
   {
     super();
-    focusColor = getFocusColor();
-    selectColor = getSelectColor();
-    disabledTextColor = getDisabledTextColor();
+    UIDefaults defaults = UIManager.getLookAndFeelDefaults();
+    focusColor = defaults.getColor(getPropertyPrefix() + "focus");
+    selectColor = defaults.getColor(getPropertyPrefix() + "select");
+    disabledTextColor = defaults.getColor(getPropertyPrefix() + 
"disabledText");
   }
 
-
   /**
    * Returns the color for the focus border.
    *
@@ -81,8 +101,7 @@
    */
   protected Color getFocusColor()
   {
-    UIDefaults def = UIManager.getLookAndFeelDefaults();
-    return def.getColor(getPropertyPrefix() + ".focus");
+    return focusColor;
   }
 
   /**
@@ -92,8 +111,7 @@
    */
   protected Color getSelectColor()
   {
-    UIDefaults def = UIManager.getLookAndFeelDefaults();
-    return def.getColor(getPropertyPrefix() + ".select");
+    return selectColor;
   }
 
   /**
@@ -103,21 +121,80 @@
    */
   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)
+  {
+    // FIXME: for now, this override just changes the visibility of the method
+    // in the super-class, to satisfy japi...but there must be something else.
+    super.installDefaults(b);
+  }
+  
+  /**
+   * 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)
+  {
+    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 address@hidden 
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);    
   }
+  
 }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]