classpath-patches
[Top][All Lists]
Advanced

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

[cp-patches] FYI: MetalButtonUI etc


From: David Gilbert
Subject: [cp-patches] FYI: MetalButtonUI etc
Date: Tue, 27 Sep 2005 22:54:50 +0000
User-agent: Mozilla Thunderbird 1.0.6 (X11/20050728)

I committed this patch which makes "rollover" buttons work, and improves the drawing of buttons as they are clicked:

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

        * javax/swing/plaf/metal/MetalBorders.java
        (rolloverBorder): new field,
        (getRolloverBorder): new method,
        * javax/swing/plaf/metal/MetalButtonListener.java: new class,
        * javax/swing/plaf/metal/MetalButtonUI.java
        (instance): removed field,
        (constructor): initialise fields from UI defaults,
        (getFocusColor): just return field value,
        (getSelectColor): just return field value,
        (getDisabledTextColor): just return field value,
        (createUI): return a new instance every time,
        (installDefaults): check for isRolloverEnabled and install rollover
        border if necessary,
        (uninstallDefaults): implemented,
        (createButtonListener): implemented,
        (paintButtonPressed): implemented,
        (paintFocus): implemented,
        (paintText): implemented.

Regards,

Dave
Index: javax/swing/plaf/metal/MetalBorders.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalBorders.java,v
retrieving revision 1.21
diff -u -r1.21 MetalBorders.java
--- javax/swing/plaf/metal/MetalBorders.java    27 Sep 2005 20:53:56 -0000      
1.21
+++ javax/swing/plaf/metal/MetalBorders.java    27 Sep 2005 21:32:47 -0000
@@ -92,6 +92,9 @@
    */
   private static Border textBorder;
 
+  /** The shared instance for getRolloverBorder(). */
+  private static Border rolloverBorder;
+
   /**
    * A MarginBorder that gets shared by multiple components.
    * Created on demand by the private helper function address@hidden
@@ -1453,4 +1456,22 @@
       marginBorder = new BasicBorders.MarginBorder();
     return marginBorder;
   }
+
+  /**
+   * Returns a shared instance of a compound border for rollover buttons.
+   * 
+   * @return A shared border instance.
+   */
+  static Border getRolloverBorder()
+  {
+    if (rolloverBorder == null)
+      {
+        Border outer = new MetalBorders.RolloverButtonBorder();
+        Border inner = MetalBorders.getMarginBorder();
+        rolloverBorder = new BorderUIResource.CompoundBorderUIResource(outer, 
+            inner);
+      }
+    return rolloverBorder;
+  }
+
 }
Index: javax/swing/plaf/metal/MetalButtonListener.java
===================================================================
RCS file: javax/swing/plaf/metal/MetalButtonListener.java
diff -N javax/swing/plaf/metal/MetalButtonListener.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ javax/swing/plaf/metal/MetalButtonListener.java     27 Sep 2005 21:32:48 
-0000
@@ -0,0 +1,81 @@
+/* MetalButtonListener.java
+   Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package javax.swing.plaf.metal;
+
+import java.beans.PropertyChangeEvent;
+
+import javax.swing.AbstractButton;
+import javax.swing.plaf.UIResource;
+import javax.swing.plaf.basic.BasicButtonListener;
+
+/**
+ * A listener for buttons under the address@hidden MetalLookAndFeel}.
+ * 
+ * @see MetalButtonUI#createButtonListener()
+ */
+class MetalButtonListener extends BasicButtonListener 
+{
+  
+  /**
+   * Creates a new instance.
+   * 
+   * @param button  the button.
+   */
+  public MetalButtonListener(AbstractButton button)
+  {
+    super(button);
+  }
+  
+  /**
+   * Handles property change events.
+   * 
+   * @param e  the event.
+   */
+  public void propertyChange(PropertyChangeEvent e)
+  {
+    super.propertyChange(e);
+    if (e.getPropertyName().equals(
+            AbstractButton.ROLLOVER_ENABLED_CHANGED_PROPERTY))
+    {
+      AbstractButton b = (AbstractButton) e.getSource();
+      if (b.getBorder() instanceof UIResource)
+        b.setBorder(MetalBorders.getRolloverBorder());
+    }
+  }
+}
Index: javax/swing/plaf/metal/MetalButtonUI.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalButtonUI.java,v
retrieving revision 1.6
diff -u -r1.6 MetalButtonUI.java
--- javax/swing/plaf/metal/MetalButtonUI.java   21 Jul 2005 15:23:35 -0000      
1.6
+++ javax/swing/plaf/metal/MetalButtonUI.java   27 Sep 2005 21:32:48 -0000
@@ -39,13 +39,18 @@
 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.JToolBar;
 import javax.swing.UIDefaults;
 import javax.swing.UIManager;
 import javax.swing.plaf.ComponentUI;
+import javax.swing.plaf.UIResource;
+import javax.swing.plaf.basic.BasicButtonListener;
 import javax.swing.plaf.basic.BasicButtonUI;
 
 /**
@@ -58,27 +63,25 @@
   extends BasicButtonUI
 {
 
-  /** The cached MetalButtonUI instance. */
-  private static MetalButtonUI instance = null;
-
-  /** The color for the focus border. */
+  /** The color used to draw the focus rectangle around the text and/or icon. 
*/
   protected Color focusColor;
-
-  /** The color that indicates a selected button. */
+    
+  /** The background color for the button when it is pressed. */
   protected Color selectColor;
 
   /** The color for disabled button labels. */
   protected Color disabledTextColor;
 
   /**
-   * Creates a new instance of MetalButtonUI.
+   * Creates a new instance.
    */
   public MetalButtonUI()
   {
     super();
-    focusColor = getFocusColor();
-    selectColor = getSelectColor();
-    disabledTextColor = getDisabledTextColor();
+    UIDefaults def = UIManager.getLookAndFeelDefaults();
+    focusColor = def.getColor(getPropertyPrefix() + "focus");
+    selectColor = def.getColor(getPropertyPrefix() + "select");
+    disabledTextColor = def.getColor(getPropertyPrefix() + "disabledText");
   }
 
   /**
@@ -88,8 +91,7 @@
    */
   protected Color getFocusColor()
   {
-    UIDefaults def = UIManager.getLookAndFeelDefaults();
-    return def.getColor(getPropertyPrefix() + ".focus");
+    return focusColor;
   }
 
   /**
@@ -99,8 +101,7 @@
    */
   protected Color getSelectColor()
   {
-    UIDefaults def = UIManager.getLookAndFeelDefaults();
-    return def.getColor(getPropertyPrefix() + ".select");
+    return selectColor;
   }
 
   /**
@@ -110,36 +111,123 @@
    */
   protected Color getDisabledTextColor()
   {
-    UIDefaults def = UIManager.getLookAndFeelDefaults();
-    return def.getColor(getPropertyPrefix() + ".disabledText");
+    return disabledTextColor;
   }
 
   /**
-   * Returns an instance of MetalButtonUI.
-   *
-   * @param component a button for which a UI instance should be returned
+   * Returns a UI delegate for the specified component.
+   * 
+   * @param c  the component (should be a subclass of address@hidden 
AbstractButton}).
+   * 
+   * @return A new instance of <code>MetalButtonUI</code>.
    */
-  public static ComponentUI createUI(JComponent component)
-  {
-    if (instance == null)
-      instance = new MetalButtonUI();
-    return instance;
+  public static ComponentUI createUI(JComponent c) {
+    return new MetalButtonUI();
   }
 
   /**
-   * Install the Look &amp; Feel defaults for Buttons.
-   *
-   * @param button the button for which to install the Look &amp; Feel
+   * Installs the default settings for the specified button.
+   * 
+   * @param button  the button.
+   * 
+   * @see #uninstallDefaults(AbstractButton)
    */
   public void installDefaults(AbstractButton button)
   {
     super.installDefaults(button);
-
-    UIDefaults defaults = UIManager.getLookAndFeelDefaults();
-    button.setFont(defaults.getFont("Button.font"));
-
-    if (button.getParent() instanceof JToolBar)
-      button.setBorder(MetalBorders.getToolbarButtonBorder());
+    if (button.isRolloverEnabled())
+      {
+        if (button.getBorder() instanceof UIResource)
+          button.setBorder(MetalBorders.getRolloverBorder());
+      }
+  }
+    
+  /**
+   * Removes the defaults added by address@hidden 
#installDefaults(AbstractButton)}.
+   */
+  public void uninstallDefaults(AbstractButton button) 
+  {
+    super.uninstallDefaults(button);
+    if (button.getBorder() instanceof UIResource)
+      button.setBorder(null);
+  }
+
+  /**
+   * Returns a button listener for the specified button.
+   * 
+   * @param button  the button.
+   * 
+   * @return A button listener.
+   */
+  protected BasicButtonListener createButtonListener(AbstractButton button) 
+  {
+    return new MetalButtonListener(button);
+  }    
+
+  /**
+   * Paints the background of the button to indicate that it is in the 
"pressed"
+   * state.
+   * 
+   * @param g  the graphics context.
+   * @param b  the button.
+   */
+  protected void paintButtonPressed(Graphics g, AbstractButton b) 
+  { 
+    if (b.isContentAreaFilled())
+    {
+      Rectangle area = b.getVisibleRect();
+      g.setColor(selectColor);
+      g.fillRect(area.x, area.y, area.width, area.height);
+    }
+  }
+    
+  /** 
+   * Paints the focus rectangle around the button text and/or icon.
+   * 
+   * @param g  the graphics context.
+   * @param b  the button.
+   * @param viewRect  the button bounds.
+   * @param textRect  the text bounds.
+   * @param iconRect  the icon bounds.
+   */
+  protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect,
+          Rectangle textRect, Rectangle iconRect) {
+    if (b.hasFocus() && b.isFocusPainted())
+    {
+      Color savedColor = g.getColor();
+      g.setColor(getFocusColor());
+      Rectangle focusRect = iconRect.union(textRect);
+      g.drawRect(focusRect.x - 1, focusRect.y - 1,
+                 focusRect.width + 1, focusRect.height + 1);
+      g.setColor(savedColor);
+    }
+  }
+    
+  /**
+   * Paints the button text.
+   * 
+   * @param g  the graphics context.
+   * @param c  the button.
+   * @param textRect  the text bounds.
+   * @param text  the text to display.
+   */
+  protected void paintText(Graphics g, JComponent c, Rectangle textRect,
+          String text) 
+  {
+    AbstractButton b = (AbstractButton) c;
+    Font f = b.getFont();
+    g.setFont(f);
+    FontMetrics fm = g.getFontMetrics(f);
+
+    if (b.isEnabled())
+      {
+        g.setColor(b.getForeground());
+        g.drawString(text, textRect.x, textRect.y + fm.getAscent());
+      }
+    else
+      {
+        g.setColor(getDisabledTextColor());
+        g.drawString(text, textRect.x, textRect.y + fm.getAscent());
+      }  
   }
-
 }

reply via email to

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