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