Source for javax.swing.plaf.metal.MetalButtonUI

   1: /* MetalButtonUI.java
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.swing.plaf.metal;
  40: 
  41: import java.awt.Color;
  42: import java.awt.Component;
  43: import java.awt.Font;
  44: import java.awt.FontMetrics;
  45: import java.awt.Graphics;
  46: import java.awt.Rectangle;
  47: 
  48: import javax.swing.AbstractButton;
  49: import javax.swing.ButtonModel;
  50: import javax.swing.JButton;
  51: import javax.swing.JComponent;
  52: import javax.swing.JToolBar;
  53: import javax.swing.SwingConstants;
  54: import javax.swing.UIManager;
  55: import javax.swing.plaf.ComponentUI;
  56: import javax.swing.plaf.UIResource;
  57: import javax.swing.plaf.basic.BasicButtonUI;
  58: 
  59: /**
  60:  * A UI delegate for the {@link JButton} component.
  61:  *
  62:  * @author Roman Kennke (roman@kennke.org)
  63:  */
  64: public class MetalButtonUI
  65:   extends BasicButtonUI
  66: {
  67: 
  68:   /**
  69:    * The shared button UI.
  70:    */
  71:   private static MetalButtonUI sharedUI;
  72: 
  73:   /**
  74:    * The color used to draw the focus rectangle around the text and/or icon.
  75:    */
  76:   protected Color focusColor;
  77: 
  78:   /**
  79:    * The background color for the button when it is pressed.
  80:    */
  81:   protected Color selectColor;
  82: 
  83:   /**
  84:    * The color for disabled button labels.
  85:    */
  86:   protected Color disabledTextColor;
  87: 
  88:   /**
  89:    * Returns a UI delegate for the specified component.
  90:    *
  91:    * @param c  the component (should be a subclass of {@link AbstractButton}).
  92:    *
  93:    * @return A new instance of <code>MetalButtonUI</code>.
  94:    */
  95:   public static ComponentUI createUI(JComponent c)
  96:   {
  97:     if (sharedUI == null)
  98:       sharedUI = new MetalButtonUI();
  99:     return sharedUI;
 100:   }
 101: 
 102:   /**
 103:    * Creates a new instance.
 104:    */
 105:   public MetalButtonUI()
 106:   {
 107:     super();
 108:   }
 109: 
 110:   /**
 111:    * Returns the color for the focus border.
 112:    *
 113:    * @return the color for the focus border
 114:    */
 115:   protected Color getFocusColor()
 116:   {
 117:     focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
 118:     return focusColor;
 119:   }
 120: 
 121:   /**
 122:    * Returns the color that indicates a selected button.
 123:    *
 124:    * @return the color that indicates a selected button
 125:    */
 126:   protected Color getSelectColor()
 127:   {
 128:     selectColor = UIManager.getColor(getPropertyPrefix() + "select");
 129:     return selectColor;
 130:   }
 131: 
 132:   /**
 133:    * Returns the color for the text label of disabled buttons.
 134:    *
 135:    * @return the color for the text label of disabled buttons
 136:    */
 137:   protected Color getDisabledTextColor()
 138:   {
 139:     disabledTextColor = UIManager.getColor(getPropertyPrefix()
 140:                                            + "disabledText");
 141:     return disabledTextColor;
 142:   }
 143: 
 144:   /**
 145:    * Installs the default settings for the specified button.
 146:    *
 147:    * @param button  the button.
 148:    *
 149:    * @see #uninstallDefaults(AbstractButton)
 150:    */
 151:   public void installDefaults(AbstractButton button)
 152:   {
 153:     // This is overridden to be public, for whatever reason.
 154:     super.installDefaults(button);
 155:   }
 156: 
 157:   /**
 158:    * Removes the defaults added by {@link #installDefaults(AbstractButton)}.
 159:    */
 160:   public void uninstallDefaults(AbstractButton button)
 161:   {
 162:     // This is overridden to be public, for whatever reason.
 163:     super.uninstallDefaults(button);
 164:   }
 165: 
 166:   /**
 167:    * Paints the background of the button to indicate that it is in the
 168:    * "pressed" state.
 169:    *
 170:    * @param g  the graphics context.
 171:    * @param b  the button.
 172:    */
 173:   protected void paintButtonPressed(Graphics g, AbstractButton b)
 174:   {
 175:     if (b.isContentAreaFilled())
 176:     {
 177:       g.setColor(getSelectColor());
 178:       g.fillRect(0, 0, b.getWidth(), b.getHeight());
 179:     }
 180:   }
 181: 
 182:   /**
 183:    * Paints the focus rectangle around the button text and/or icon.
 184:    *
 185:    * @param g  the graphics context.
 186:    * @param b  the button.
 187:    * @param viewRect  the button bounds.
 188:    * @param textRect  the text bounds.
 189:    * @param iconRect  the icon bounds.
 190:    */
 191:   protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect,
 192:           Rectangle textRect, Rectangle iconRect)
 193:   {
 194:     if (b.isEnabled() && b.hasFocus() && b.isFocusPainted())
 195:     {
 196:       Color savedColor = g.getColor();
 197:       g.setColor(getFocusColor());
 198:       Rectangle focusRect = iconRect.union(textRect);
 199:       g.drawRect(focusRect.x - 1, focusRect.y,
 200:                  focusRect.width + 1, focusRect.height);
 201:       g.setColor(savedColor);
 202:     }
 203:   }
 204: 
 205:   /**
 206:    * Paints the button text.
 207:    *
 208:    * @param g  the graphics context.
 209:    * @param c  the button.
 210:    * @param textRect  the text bounds.
 211:    * @param text  the text to display.
 212:    */
 213:   protected void paintText(Graphics g, JComponent c, Rectangle textRect,
 214:           String text)
 215:   {
 216:     AbstractButton b = (AbstractButton) c;
 217:     Font f = b.getFont();
 218:     g.setFont(f);
 219:     FontMetrics fm = g.getFontMetrics(f);
 220: 
 221:     if (b.isEnabled())
 222:       {
 223:         g.setColor(b.getForeground());
 224:         g.drawString(text, textRect.x, textRect.y + fm.getAscent());
 225:       }
 226:     else
 227:       {
 228:         g.setColor(getDisabledTextColor());
 229:         g.drawString(text, textRect.x, textRect.y + fm.getAscent());
 230:       }
 231:   }
 232: 
 233:   /**
 234:    * If the property <code>Button.gradient</code> is set, then a gradient is
 235:    * painted as background, otherwise the normal superclass behaviour is
 236:    * called.
 237:    */
 238:   public void update(Graphics g, JComponent c)
 239:   {
 240:     AbstractButton b = (AbstractButton) c;
 241:     if ((b.getBackground() instanceof UIResource)
 242:         && b.isContentAreaFilled() && b.isEnabled())
 243:       {
 244:         ButtonModel m = b.getModel();
 245:         String uiKey = "Button.gradient";
 246:         if (! isToolbarButton(b))
 247:           {
 248:             if (! m.isArmed() && ! m.isPressed() && isDrawingGradient(uiKey))
 249:               {
 250:                 MetalUtils.paintGradient(g, 0, 0, b.getWidth(), b.getHeight(),
 251:                                          SwingConstants.VERTICAL,
 252:                                          uiKey);
 253:                 paint(g, c);
 254:                 return;
 255:               }
 256:           }
 257:         else if (m.isRollover() && isDrawingGradient(uiKey))
 258:           {
 259:             MetalUtils.paintGradient(g, 0, 0, b.getWidth(), b.getHeight(),
 260:                                      SwingConstants.VERTICAL,
 261:                                      uiKey);
 262:             paint(g, c);
 263:             return;
 264:           }
 265:       }
 266:     // Fallback if we didn't have any of the two above cases.
 267:     super.update(g, c);
 268:   }
 269: 
 270:   /**
 271:    * Returns <code>true</code> when the button is a toolbar button,
 272:    * <code>false</code> otherwise.
 273:    *
 274:    * @param b the button component to test
 275:    *
 276:    * @return <code>true</code> when the button is a toolbar button,
 277:    *         <code>false</code> otherwise
 278:    */
 279:   private boolean isToolbarButton(Component b)
 280:   {
 281:     Component parent = b.getParent();
 282:     return parent instanceof JToolBar;
 283:   }
 284: 
 285:   /**
 286:    * Returns <code>true</code> if we should draw the button gradient,
 287:    * <code>false</code> otherwise.
 288:    *
 289:    * @param uiKey the UIManager key for the gradient
 290:    *
 291:    * @return <code>true</code> if we should draw the button gradient,
 292:    *         <code>false</code> otherwise
 293:    */
 294:   private boolean isDrawingGradient(String uiKey)
 295:   {
 296:     return (UIManager.get(uiKey) != null);
 297:   }
 298: }