Hi, here is my proposal for a slight rewrite of the MetalButtonUI.update method: It first finds out whether gradient painting is needed or not and calls a specific private method which works out all the details.
The comments should explain it all.
Note: Some change to the lightweightdispatcher is still needed. Currently you
can click and hold on a toolbar button, move to another and it will not set the
rollover state to true. However if you click on another mouse button (while
still holding down the left one) the button under the cursor will receive focus
and everything gets messed up. In the RI the focus traversal is prevented
somehow.
Ok to commit?
The ChangeLog:
2006-05-19 Robert Schuster <[EMAIL PROTECTED]>
* javax/swing/metal/MetalButtonUI.java:
(update): Removed some subexpression from if-clause and call
updateWithGradient.
(updateWithGradient): New method.
cya
Robert
Index: javax/swing/plaf/metal/MetalButtonUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalButtonUI.java,v
retrieving revision 1.17
diff -u -r1.17 MetalButtonUI.java
--- javax/swing/plaf/metal/MetalButtonUI.java 11 May 2006 17:05:55 -0000 1.17
+++ javax/swing/plaf/metal/MetalButtonUI.java 19 May 2006 09:08:04 -0000
@@ -39,6 +39,7 @@
package javax.swing.plaf.metal;
import java.awt.Color;
+import java.awt.Container;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
@@ -48,7 +49,9 @@
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JComponent;
+import javax.swing.JToolBar;
import javax.swing.SwingConstants;
+import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
@@ -237,19 +240,63 @@
public void update(Graphics g, JComponent c)
{
AbstractButton b = (AbstractButton) c;
- ButtonModel m = b.getModel();
if (b.isContentAreaFilled()
&& (UIManager.get(getPropertyPrefix() + "gradient") != null)
- && ! m.isPressed() && ! m.isArmed()
&& b.isEnabled()
&& (b.getBackground() instanceof UIResource))
+ updateWidthGradient(g, b, b.getParent());
+ else
+ super.update(g, c);
+ }
+
+ private void updateWidthGradient(Graphics g, AbstractButton b, Container parent)
+ {
+ ButtonModel m = b.getModel();
+ String gradientPropertyName = getPropertyPrefix() + "gradient";
+
+ // Gradient painting behavior depends on whether the button is part of a
+ // JToolBar.
+ if (parent instanceof JToolBar)
+ {
+ if (! m.isPressed() && ! m.isArmed())
+ {
+ if (m.isRollover())
+ {
+ // Paint the gradient when the mouse cursor hovers over the
+ // button but is not pressed down.
+ MetalUtils.paintGradient(g, 0, 0, b.getWidth(), b.getHeight(),
+ SwingConstants.VERTICAL,
+ gradientPropertyName);
+ }
+ else
+ {
+ // If mouse does not hover over the button let the JToolBar
+ // paint itself at the location where the button is (the button
+ // is transparent).
+
+ // There where cases where the button was not repainted and
+ // therefore showed its old state. With this statement it does
+ // not happen.
+ b.repaint();
+
+ Rectangle area = new Rectangle();
+ SwingUtilities.calculateInnerArea(b, area);
+ SwingUtilities.convertRectangle(b, area, b.getParent());
+ b.getParent().repaint(area.x, area.y, area.width, area.height);
+ }
+ }
+
+ }
+ else if (! m.isPressed() && ! m.isArmed())
{
- MetalUtils.paintGradient(g, 0, 0, c.getWidth(), c.getHeight(),
+ // When the button is not part of a JToolBar just paint itself with a
+ // gradient and everything is fine.
+ MetalUtils.paintGradient(g, 0, 0, b.getWidth(), b.getHeight(),
SwingConstants.VERTICAL,
- getPropertyPrefix() + "gradient");
- paint(g, c);
+ gradientPropertyName);
}
- else
- super.update(g, c);
+
+ paint(g, b);
}
+
}
signature.asc
Description: OpenPGP digital signature
