This fixes the focus painting of the JRadioButton (which actually was a
bug with the border default), and adds support for HTML labels on
JRadioButtons.

2006-10-12  Roman Kennke  <[EMAIL PROTECTED]>

        PR 28057
        * javax/swing/plaf/basic/BasicRadioButtonUI.java
        (paint): Determine correct icon. Added support for HTML label.
        Added small optimizations.
        (getPreferredSize): Only consider the buttons iconTextGap, and
        only when the text is not null.
        * javax/swing/plaf/basic/BasicLookAndFeel.java
        (initComponentDefaults): Fetch border for RadioButton from
        BasicButtons.getRadioButtonBorder().
        * javax/swing/plaf/metal/MetalRadioButtonUI.java
        (paintFocus): Paint focus rectangle one pixel smaller.


/Roman

Index: javax/swing/plaf/basic/BasicRadioButtonUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicRadioButtonUI.java,v
retrieving revision 1.21
diff -u -1 -5 -r1.21 BasicRadioButtonUI.java
--- javax/swing/plaf/basic/BasicRadioButtonUI.java	20 Sep 2006 15:21:00 -0000	1.21
+++ javax/swing/plaf/basic/BasicRadioButtonUI.java	12 Oct 2006 12:24:14 -0000
@@ -40,30 +40,31 @@
 
 import java.awt.Color;
 import java.awt.Dimension;
 import java.awt.Font;
 import java.awt.Graphics;
 import java.awt.Insets;
 import java.awt.Rectangle;
 
 import javax.swing.AbstractButton;
 import javax.swing.ButtonModel;
 import javax.swing.Icon;
 import javax.swing.JComponent;
 import javax.swing.SwingUtilities;
 import javax.swing.UIManager;
 import javax.swing.plaf.ComponentUI;
+import javax.swing.text.View;
 
 /**
  * The BasicLookAndFeel UI implementation for
  * [EMAIL PROTECTED] javax.swing.JRadioButton}s.
  */
 public class BasicRadioButtonUI extends BasicToggleButtonUI
 {
   /**
    * The default icon for JRadioButtons. The default icon displays the usual
    * RadioButton and is sensible to the selection state of the button,
    * and can be used both as normal icon as well as selectedIcon.
    */
   protected Icon icon;
 
   /**
@@ -117,76 +118,116 @@
    */
   public Icon getDefaultIcon()
   {
     return icon;
   }
 
   /**
    * Paints the RadioButton.
    *
    * @param g the Graphics context to paint with
    * @param c the button to paint
    */
   public void paint(Graphics g, JComponent c)
   {
     AbstractButton b = (AbstractButton) c;
-
+    Dimension size = c.getSize();
     Insets i = b.getInsets();
-    Rectangle tr = textR;
     textR.x = 0;
     textR.y = 0;
     textR.width = 0;
     textR.height = 0;
-    Rectangle ir = iconR;
     iconR.x = 0;
     iconR.y = 0;
     iconR.width = 0;
     iconR.height = 0;
-    Rectangle vr = viewR;
     viewR.x = i.left;
     viewR.y = i.right;
-    viewR.width = b.getWidth() - i.left - i.right;
-    viewR.height = b.getHeight() - i.top - i.bottom;
+    viewR.width = size.width - i.left - i.right;
+    viewR.height = size.height - i.top - i.bottom;
 
     Font f = c.getFont();
 
     g.setFont(f);
 
     ButtonModel m = b.getModel();
-    // FIXME: Do a filtering on any customized icon if the following property
-    // is set.
-    boolean enabled = b.isEnabled();
-    
-    Icon currentIcon = b.getIcon();
 
-    if (currentIcon == null)
-      {
-        currentIcon = getDefaultIcon();
-      }
-    
+    // This is the icon that we use for layout.
+    Icon icon = b.getIcon();
+    if (icon == null)
+      icon = getDefaultIcon();
+
+    // Do the layout.
     String text = SwingUtilities.layoutCompoundLabel(c, g.getFontMetrics(f), 
-       b.getText(), currentIcon,
+       b.getText(), icon,
        b.getVerticalAlignment(), b.getHorizontalAlignment(),
        b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
-       vr, ir, tr, b.getIconTextGap() + defaultTextShiftOffset);
+       viewR, iconR, textR, b.getIconTextGap());
 
-    currentIcon.paintIcon(c, g, ir.x, ir.y);
-    
+    // Figure out the correct icon.
+    icon = b.getIcon();
+    if (icon == null)
+      icon = getDefaultIcon();
+    else
+      {
+        if (! m.isEnabled())
+          {
+            if (m.isSelected())
+              icon = b.getDisabledSelectedIcon();
+            else
+              icon = b.getDisabledIcon();
+          }
+        else if (m.isArmed() && m.isPressed())
+          {
+            icon = b.getPressedIcon();
+            if (icon == null)
+              icon = b.getSelectedIcon();
+          }
+        else if (m.isSelected())
+          {
+            if (b.isRolloverEnabled() && m.isRollover())
+              {
+                icon = b.getRolloverSelectedIcon();
+                if (icon == null)
+                  icon = b.getSelectedIcon();
+              }
+            else
+              icon = b.getSelectedIcon();
+          }
+        else if (b.isRolloverEnabled() && m.isRollover())
+          icon = b.getRolloverIcon();
+        if (icon == null)
+          icon = b.getIcon();
+      }
+    // .. and paint it.
+    icon.paintIcon(c, g, iconR.x, iconR.y);
+
+    // Paint text and focus indicator.
     if (text != null)
-      paintText(g, b, tr, text);
-    if (b.hasFocus() && b.isFocusPainted() && m.isEnabled())
-      paintFocus(g, tr, c.getSize());
+      {
+        // Maybe render HTML in the radio button.
+        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
+        if (v != null)
+          v.paint(g, textR);
+        else
+          paintText(g, b, textR, text);
+
+        // Paint focus indicator if necessary.
+        if (b.hasFocus() && b.isFocusPainted()
+            && textR.width > 0 && textR.height > 0)
+          paintFocus(g, textR, size);
+      }
   }
   
   public Dimension getPreferredSize(JComponent c)
   {
     // This is basically the same code as in
     // BasicGraphicsUtils.getPreferredButtonSize() but takes the default icon
     // property into account. JRadioButton and subclasses always have an icon:
     // the check box. If the user explicitly changes it with setIcon() that
     // one will be used for layout calculations and painting instead.
     // The other icon properties are ignored.
     AbstractButton b = (AbstractButton) c;
     
     Insets insets = b.getInsets();
 
     String text = b.getText();
@@ -195,41 +236,38 @@
       i = getDefaultIcon(); 
     
     textR.x = 0;
     textR.y = 0;
     textR.width = 0;
     textR.height = 0;
     iconR.x = 0;
     iconR.y = 0;
     iconR.width = 0;
     iconR.height = 0;
     viewR.x = 0;
     viewR.y = 0;
     viewR.width = Short.MAX_VALUE;
     viewR.height = Short.MAX_VALUE;
 
-    SwingUtilities.layoutCompoundLabel(
-      b, // for the component orientation
-      b.getFontMetrics(b.getFont()),
-      b.getText(),
-      i,
-      b.getVerticalAlignment(), 
-      b.getHorizontalAlignment(),
-      b.getVerticalTextPosition(),
-      b.getHorizontalTextPosition(),
-      viewR, iconR, textR,
-      defaultTextIconGap + defaultTextShiftOffset);
+    SwingUtilities.layoutCompoundLabel(b, // for the component orientation
+                                       b.getFontMetrics(b.getFont()),
+                                       text, i, b.getVerticalAlignment(), 
+                                       b.getHorizontalAlignment(),
+                                       b.getVerticalTextPosition(),
+                                       b.getHorizontalTextPosition(),
+                                       viewR, iconR, textR,
+                                       text == null ? 0 : b.getIconTextGap());
 
     Rectangle r = SwingUtilities.computeUnion(textR.x, textR.y, textR.width,
                                               textR.height, iconR);
 
     return new Dimension(insets.left + r.width + insets.right,
                          insets.top + r.height + insets.bottom);
   }
 
   /**
    * Paints the focus indicator for JRadioButtons.
    *
    * @param g the graphics context
    * @param tr the rectangle for the text label
    * @param size the size of the <code>JRadioButton</code> component.
    */
Index: javax/swing/plaf/basic/BasicLookAndFeel.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicLookAndFeel.java,v
retrieving revision 1.99
diff -u -1 -5 -r1.99 BasicLookAndFeel.java
--- javax/swing/plaf/basic/BasicLookAndFeel.java	1 Sep 2006 18:00:50 -0000	1.99
+++ javax/swing/plaf/basic/BasicLookAndFeel.java	12 Oct 2006 12:24:15 -0000
@@ -1050,32 +1050,31 @@
                     "RIGHT", "selectParent",
                     "KP_RIGHT", "selectParent",
       },
       "ProgressBar.background", new ColorUIResource(Color.LIGHT_GRAY),
       "ProgressBar.border",
       new BorderUIResource.LineBorderUIResource(Color.GREEN, 2),
       "ProgressBar.cellLength", new Integer(1),
       "ProgressBar.cellSpacing", new Integer(0),
       "ProgressBar.font", new FontUIResource("Dialog", Font.PLAIN, 12),
       "ProgressBar.foreground", new ColorUIResource(0, 0, 128),
       "ProgressBar.selectionBackground", new ColorUIResource(0, 0, 128),
       "ProgressBar.selectionForeground", new ColorUIResource(Color.LIGHT_GRAY),
       "ProgressBar.repaintInterval", new Integer(50),
       "ProgressBar.cycleTime", new Integer(3000),
       "RadioButton.background", new ColorUIResource(light),
-      "RadioButton.border", new BorderUIResource.CompoundBorderUIResource(null,
-                                                                          null),
+      "RadioButton.border", BasicBorders.getRadioButtonBorder(),
       "RadioButton.darkShadow", new ColorUIResource(shadow),
       "RadioButton.focusInputMap", new UIDefaults.LazyInputMap(new Object[] {
         KeyStroke.getKeyStroke("SPACE"),  "pressed",
         KeyStroke.getKeyStroke("released SPACE"), "released"
       }),
       "RadioButton.font", new FontUIResource("Dialog", Font.PLAIN, 12),
       "RadioButton.foreground", new ColorUIResource(darkShadow),
       "RadioButton.highlight", new ColorUIResource(highLight),
       "RadioButton.icon",
       new UIDefaults.LazyValue()
       {
         public Object createValue(UIDefaults def)
         {
           return BasicIconFactory.getRadioButtonIcon();
         }
Index: javax/swing/plaf/metal/MetalRadioButtonUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalRadioButtonUI.java,v
retrieving revision 1.10
diff -u -1 -5 -r1.10 MetalRadioButtonUI.java
--- javax/swing/plaf/metal/MetalRadioButtonUI.java	10 May 2006 20:53:00 -0000	1.10
+++ javax/swing/plaf/metal/MetalRadioButtonUI.java	12 Oct 2006 12:24:15 -0000
@@ -165,19 +165,19 @@
     super.paint(g, c);
     // FIXME:  disabled text isn't being drawn correctly, it's possible that
     // it could be done here...
   }
   
   /**
    * Paints the focus rectangle for the [EMAIL PROTECTED] JRadioButton}.
    * 
    * @param g  the graphics device.
    * @param t  the bounding rectangle for the text.
    * @param d  ???
    */
   protected void paintFocus(Graphics g, Rectangle t, Dimension d)
   {
     g.setColor(focusColor);
-    g.drawRect(t.x - 1, t.y - 1, t.width + 2, t.height + 2);
+    g.drawRect(t.x - 1, t.y - 1, t.width + 1, t.height + 1);
   }
   
 }

Reply via email to