Hi,
I need help for this one.

I had the following problem:
BasicRadioButtonUI.installDefaults sets default values for the icon,
selectedIcon, disabledIcon etc properties. This is wrong, as the RI does not do
this. Minitestcase: assert(new JRadioButton().getIcon() == null)

I changed this and modified the paint() method within BasicRadioButtonUI in a
way that it draws the default icon (the checkbox thingie) if no explicit icon
was set (Btw: The RI seems to ignore the selectedIcon, disabledSelectedIcon and
disabledIcon property. It only takes the icon property into account.).

With this change I got a problem with layouting because getPreferredSize() needs
to know the icon which will be drawn beside the text. Currently this is handled
by BasicGraphicUtils.getPreferredButtonSize(). With the current implementation
it used getIcon() on the button which will be null for fresh unmodified radio
buttons and therefore the default icon is not taken into account for them.

I changed the code to make it aware of the default icon setting which radio
buttons can have but am not sure whether this is the right approach.

For the application I am debugging this patch fixes the issues.

No ChangeLog as I am waiting for further input.

cya
Robert
Index: javax/swing/plaf/basic/BasicGraphicsUtils.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicGraphicsUtils.java,v
retrieving revision 1.17
diff -u -r1.17 BasicGraphicsUtils.java
--- javax/swing/plaf/basic/BasicGraphicsUtils.java	18 Oct 2005 22:10:32 -0000	1.17
+++ javax/swing/plaf/basic/BasicGraphicsUtils.java	15 Jun 2006 17:28:51 -0000
@@ -54,6 +54,7 @@
 import javax.swing.Icon;
 import javax.swing.JComponent;
 import javax.swing.SwingUtilities;
+import javax.swing.plaf.ButtonUI;
 
 
 /**
@@ -603,18 +604,20 @@
     
     viewRect = new Rectangle();
 
-     /* java.awt.Toolkit.getFontMetrics is deprecated. However, it
-     * seems not obvious how to get to the correct FontMetrics object
-     * otherwise. The real problem probably is that the method
-     * javax.swing.SwingUtilities.layoutCompundLabel should take a
-     * LineMetrics, not a FontMetrics argument. But fixing this that
-     * would change the public API.
-     */
+    // Retrieve the icon fitting to the current button state.
+    Icon i = BasicButtonUI.currentIcon(b);
+    
+    // If no icon is given and we have a BasicRadioButtonUI
+    // we can ask it for the default icon.
+    ButtonUI ui = b.getUI();
+    if(i == null && ui instanceof BasicRadioButtonUI)
+      i = ((BasicRadioButtonUI) ui).getDefaultIcon();
+
    SwingUtilities.layoutCompoundLabel(
       b, // for the component orientation
-      b.getToolkit().getFontMetrics(b.getFont()), // see comment above
+      b.getFontMetrics(b.getFont()),
       b.getText(),
-      b.getIcon(),
+      i,
       b.getVerticalAlignment(), 
       b.getHorizontalAlignment(),
       b.getVerticalTextPosition(),
Index: javax/swing/plaf/basic/BasicRadioButtonUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicRadioButtonUI.java,v
retrieving revision 1.18
diff -u -r1.18 BasicRadioButtonUI.java
--- javax/swing/plaf/basic/BasicRadioButtonUI.java	13 Jun 2006 09:28:57 -0000	1.18
+++ javax/swing/plaf/basic/BasicRadioButtonUI.java	15 Jun 2006 17:28:51 -0000
@@ -92,14 +92,6 @@
   protected void installDefaults(AbstractButton b)
   {
     super.installDefaults(b);
-    if (b.getIcon() == null)
-      b.setIcon(icon);
-    if (b.getSelectedIcon() == null)
-      b.setSelectedIcon(icon);
-    if (b.getDisabledIcon() == null)
-      b.setDisabledIcon(icon);
-    if (b.getDisabledSelectedIcon() == null)
-      b.setDisabledSelectedIcon(icon);
   }
 
   /**
@@ -145,16 +137,17 @@
     g.setFont(f);
 
     ButtonModel m = b.getModel();
-    Icon currentIcon = null;
-    if (m.isSelected() && m.isEnabled())
-      currentIcon = b.getSelectedIcon();
-    else if (! m.isSelected() && m.isEnabled())
-      currentIcon = b.getIcon();
-    else if (m.isSelected() && ! m.isEnabled())
-      currentIcon = b.getDisabledSelectedIcon();
-    else // (!m.isSelected() && ! m.isEnabled())
-      currentIcon = b.getDisabledIcon();
+    // 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 = icon;
+      }
+    
     SwingUtilities.calculateInnerArea(b, vr);
     String text = SwingUtilities.layoutCompoundLabel(c, g.getFontMetrics(f), 
        b.getText(), currentIcon,
@@ -162,10 +155,8 @@
        b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
        vr, ir, tr, b.getIconTextGap() + defaultTextShiftOffset);
     
-    if (currentIcon != null)
-      {
-        currentIcon.paintIcon(c, g, ir.x, ir.y);
-      }
+    currentIcon.paintIcon(c, g, ir.x, ir.y);
+    
     if (text != null)
       paintText(g, b, tr, text);
     if (b.hasFocus() && b.isFocusPainted() && m.isEnabled())

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to