Source for javax.swing.plaf.metal.MetalToggleButtonUI

   1: /* MetalToggleButtonUI.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.JComponent;
  49: import javax.swing.JToggleButton;
  50: import javax.swing.SwingConstants;
  51: import javax.swing.SwingUtilities;
  52: import javax.swing.UIManager;
  53: import javax.swing.plaf.ComponentUI;
  54: import javax.swing.plaf.basic.BasicButtonUI;
  55: import javax.swing.plaf.basic.BasicToggleButtonUI;
  56: 
  57: /**
  58:  * A UI delegate for the {@link JToggleButton} component.
  59:  */
  60: public class MetalToggleButtonUI
  61:   extends BasicToggleButtonUI
  62: {
  63: 
  64:   /** The color for the focus border. */
  65:   protected Color focusColor;
  66: 
  67:   /** The color that indicates a selected button. */
  68:   protected Color selectColor;
  69: 
  70:   /** The color for disabled button labels. */
  71:   protected Color disabledTextColor;
  72: 
  73:   /**
  74:    * Returns a new instance of <code>MetalToggleButtonUI</code>.
  75:    *
  76:    * @param component the component for which we return an UI instance
  77:    *
  78:    * @return A new instance of <code>MetalToggleButtonUI</code>.
  79:    */
  80:   public static ComponentUI createUI(JComponent component)
  81:   {
  82:     return new MetalToggleButtonUI();
  83:   }
  84: 
  85:   /**
  86:    * Constructs a new instance of <code>MetalToggleButtonUI</code>.
  87:    */
  88:   public MetalToggleButtonUI()
  89:   {
  90:     super();
  91:   }
  92: 
  93:   /**
  94:    * Returns the color for the focus border.
  95:    *
  96:    * @return the color for the focus border
  97:    */
  98:   protected Color getFocusColor()
  99:   {
 100:     return focusColor;
 101:   }
 102: 
 103:   /**
 104:    * Returns the color that indicates a selected button.
 105:    *
 106:    * @return the color that indicates a selected button
 107:    */
 108:   protected Color getSelectColor()
 109:   {
 110:     return selectColor;
 111:   }
 112: 
 113:   /**
 114:    * Returns the color for the text label of disabled buttons.  The value 
 115:    * is initialised in the {@link #installDefaults(AbstractButton)} method
 116:    * by reading the <code>ToggleButton.disabledText</code> item from the UI 
 117:    * defaults.
 118:    *
 119:    * @return The color for the text label of disabled buttons.
 120:    */
 121:   protected Color getDisabledTextColor()
 122:   {
 123:     return disabledTextColor;
 124:   }
 125: 
 126:   /**
 127:    * Updates the button with the defaults for this look and feel.
 128:    * 
 129:    * @param b  the button.
 130:    */
 131:   public void installDefaults(AbstractButton b)
 132:   {
 133:     super.installDefaults(b);
 134:     focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
 135:     selectColor = UIManager.getColor(getPropertyPrefix() + "select");
 136:     disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText");
 137:   }
 138:   
 139:   /**
 140:    * Paints the button background when it is pressed/selected. 
 141:    * 
 142:    * @param g  the graphics device.
 143:    * @param b  the button.
 144:    */
 145:   protected void paintButtonPressed(Graphics g, AbstractButton b)
 146:   {
 147:     if (b.isContentAreaFilled() && b.isOpaque())
 148:       {
 149:         Color saved = g.getColor();
 150:         Rectangle bounds = SwingUtilities.getLocalBounds(b);
 151:         g.setColor(selectColor);
 152:         g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
 153:         g.setColor(saved);
 154:       }
 155:   }
 156:   
 157:   /**
 158:    * Paints the text for the button.
 159:    * 
 160:    * @param g  the graphics device.
 161:    * @param c  the component.
 162:    * @param textRect  the bounds for the text.
 163:    * @param text  the text.
 164:    * 
 165:    * @deprecated 1.4 Use {@link BasicButtonUI#paintText(java.awt.Graphics, 
 166:    * javax.swing.AbstractButton, java.awt.Rectangle, java.lang.String)}.
 167:    */
 168:   protected void paintText(Graphics g, JComponent c, Rectangle textRect,
 169:                            String text)
 170:   {
 171:     Font savedFont = g.getFont();
 172:     Color savedColor = g.getColor();
 173:     g.setFont(c.getFont());
 174:     if (c.isEnabled())
 175:       g.setColor(c.getForeground());
 176:     else
 177:       g.setColor(disabledTextColor);
 178:     FontMetrics fm = g.getFontMetrics(c.getFont());
 179:     int ascent = fm.getAscent();
 180:     g.drawString(text, textRect.x, textRect.y + ascent);
 181:     g.setFont(savedFont);
 182:     g.setColor(savedColor);
 183:   }
 184:   
 185:   /**
 186:    * Draws the focus highlight around the text and icon.
 187:    * 
 188:    * @param g  the graphics device.
 189:    * @param b  the button.
 190:    */
 191:   protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect,
 192:       Rectangle textRect, Rectangle iconRect)
 193:   {
 194:     if (!b.hasFocus())
 195:       return;
 196:     Color saved = g.getColor();
 197:     g.setColor(focusColor);
 198:     Rectangle fr = iconRect.union(textRect);
 199:     g.drawRect(fr.x - 1, fr.y - 1, fr.width + 1, fr.height + 1);
 200:     g.setColor(saved);    
 201:   }
 202: 
 203:   /**
 204:    * If the property <code>ToggleButton.gradient</code> is set, then a gradient
 205:    * is painted as background, otherwise the normal superclass behaviour is
 206:    * called.
 207:    */
 208:   public void update(Graphics g, JComponent c)
 209:   {
 210:     if (c.isOpaque() && UIManager.get(getPropertyPrefix() + "gradient") != null)
 211:       {
 212:         MetalUtils.paintGradient(g, 0, 0, c.getWidth(), c.getHeight(),
 213:                                  SwingConstants.VERTICAL,
 214:                                  getPropertyPrefix() + "gradient");
 215:         paint(g, c);
 216:       }
 217:     else
 218:       super.update(g, c);
 219:   }
 220:   
 221: }