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.Font;
  43: import java.awt.FontMetrics;
  44: import java.awt.Graphics;
  45: import java.awt.Rectangle;
  46: 
  47: import javax.swing.AbstractButton;
  48: import javax.swing.ButtonModel;
  49: import javax.swing.JButton;
  50: import javax.swing.JComponent;
  51: import javax.swing.SwingConstants;
  52: import javax.swing.UIManager;
  53: import javax.swing.plaf.ComponentUI;
  54: import javax.swing.plaf.UIResource;
  55: import javax.swing.plaf.basic.BasicButtonListener;
  56: import javax.swing.plaf.basic.BasicButtonUI;
  57: 
  58: /**
  59:  * A UI delegate for the {@link JButton} component.
  60:  *
  61:  * @author Roman Kennke (roman@kennke.org)
  62:  */
  63: public class MetalButtonUI
  64:   extends BasicButtonUI
  65: {
  66: 
  67:   /** The color used to draw the focus rectangle around the text and/or icon. */
  68:   protected Color focusColor;
  69:     
  70:   /** The background color for the button when it is pressed. */
  71:   protected Color selectColor;
  72: 
  73:   /** The color for disabled button labels. */
  74:   protected Color disabledTextColor;
  75: 
  76:   /**
  77:    * Creates a new instance.
  78:    */
  79:   public MetalButtonUI()
  80:   {
  81:     super();
  82:     focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
  83:     selectColor = UIManager.getColor(getPropertyPrefix() + "select");
  84:     disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText");
  85:   }
  86: 
  87:   /**
  88:    * Returns the color for the focus border.
  89:    *
  90:    * @return the color for the focus border
  91:    */
  92:   protected Color getFocusColor()
  93:   {
  94:     return focusColor;
  95:   }
  96: 
  97:   /**
  98:    * Returns the color that indicates a selected button.
  99:    *
 100:    * @return the color that indicates a selected button
 101:    */
 102:   protected Color getSelectColor()
 103:   {
 104:     return selectColor;
 105:   }
 106: 
 107:   /**
 108:    * Returns the color for the text label of disabled buttons.
 109:    *
 110:    * @return the color for the text label of disabled buttons
 111:    */
 112:   protected Color getDisabledTextColor()
 113:   {
 114:     return disabledTextColor;
 115:   }
 116: 
 117:   /**
 118:    * Returns a UI delegate for the specified component.
 119:    * 
 120:    * @param c  the component (should be a subclass of {@link AbstractButton}).
 121:    * 
 122:    * @return A new instance of <code>MetalButtonUI</code>.
 123:    */
 124:   public static ComponentUI createUI(JComponent c) {
 125:     return new MetalButtonUI();
 126:   }
 127: 
 128:   /**
 129:    * Installs the default settings for the specified button.
 130:    * 
 131:    * @param button  the button.
 132:    * 
 133:    * @see #uninstallDefaults(AbstractButton)
 134:    */
 135:   public void installDefaults(AbstractButton button)
 136:   {
 137:     super.installDefaults(button);
 138:     button.setRolloverEnabled(UIManager.getBoolean(
 139:                                             getPropertyPrefix() + "rollover"));
 140:   }
 141:     
 142:   /**
 143:    * Removes the defaults added by {@link #installDefaults(AbstractButton)}.
 144:    */
 145:   public void uninstallDefaults(AbstractButton button) 
 146:   {
 147:     super.uninstallDefaults(button);
 148:     button.setRolloverEnabled(false);
 149:   }
 150: 
 151:   /**
 152:    * Returns a button listener for the specified button.
 153:    * 
 154:    * @param button  the button.
 155:    * 
 156:    * @return A button listener.
 157:    */
 158:   protected BasicButtonListener createButtonListener(AbstractButton button) 
 159:   {
 160:     return new MetalButtonListener(button);
 161:   }    
 162: 
 163:   /**
 164:    * Paints the background of the button to indicate that it is in the
 165:    * "pressed" state.
 166:    * 
 167:    * @param g  the graphics context.
 168:    * @param b  the button.
 169:    */
 170:   protected void paintButtonPressed(Graphics g, AbstractButton b) 
 171:   { 
 172:     if (b.isContentAreaFilled())
 173:     {
 174:       Rectangle area = b.getVisibleRect();
 175:       g.setColor(selectColor);
 176:       g.fillRect(area.x, area.y, area.width, area.height);
 177:     }
 178:   }
 179:     
 180:   /** 
 181:    * Paints the focus rectangle around the button text and/or icon.
 182:    * 
 183:    * @param g  the graphics context.
 184:    * @param b  the button.
 185:    * @param viewRect  the button bounds.
 186:    * @param textRect  the text bounds.
 187:    * @param iconRect  the icon bounds.
 188:    */
 189:   protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect,
 190:           Rectangle textRect, Rectangle iconRect) {
 191:     if (b.isEnabled() && b.hasFocus() && b.isFocusPainted())
 192:     {
 193:       Color savedColor = g.getColor();
 194:       g.setColor(getFocusColor());
 195:       Rectangle focusRect = iconRect.union(textRect);
 196:       g.drawRect(focusRect.x - 1, focusRect.y,
 197:                  focusRect.width + 1, focusRect.height);
 198:       g.setColor(savedColor);
 199:     }
 200:   }
 201:     
 202:   /**
 203:    * Paints the button text.
 204:    * 
 205:    * @param g  the graphics context.
 206:    * @param c  the button.
 207:    * @param textRect  the text bounds.
 208:    * @param text  the text to display.
 209:    */
 210:   protected void paintText(Graphics g, JComponent c, Rectangle textRect,
 211:           String text) 
 212:   {
 213:     AbstractButton b = (AbstractButton) c;
 214:     Font f = b.getFont();
 215:     g.setFont(f);
 216:     FontMetrics fm = g.getFontMetrics(f);
 217: 
 218:     if (b.isEnabled())
 219:       {
 220:         g.setColor(b.getForeground());
 221:         g.drawString(text, textRect.x, textRect.y + fm.getAscent());
 222:       }
 223:     else
 224:       {
 225:         g.setColor(getDisabledTextColor());
 226:         g.drawString(text, textRect.x, textRect.y + fm.getAscent());
 227:       }  
 228:   }
 229: 
 230:   /**
 231:    * If the property <code>Button.gradient</code> is set, then a gradient is
 232:    * painted as background, otherwise the normal superclass behaviour is
 233:    * called.
 234:    */
 235:   public void update(Graphics g, JComponent c)
 236:   {
 237:     AbstractButton b = (AbstractButton) c;
 238:     ButtonModel m = b.getModel();
 239:     if (b.isContentAreaFilled()
 240:         && (UIManager.get(getPropertyPrefix() + "gradient") != null)
 241:         && ! m.isPressed() && ! m.isArmed()
 242:         && b.isEnabled()
 243:         && (b.getBackground() instanceof UIResource))
 244:       {
 245:         MetalUtils.paintGradient(g, 0, 0, c.getWidth(), c.getHeight(),
 246:                                  SwingConstants.VERTICAL,
 247:                                  getPropertyPrefix() + "gradient");
 248:         paint(g, c);
 249:       }
 250:     else
 251:       super.update(g, c);
 252:   }
 253: }