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.SwingUtilities;
  51: import javax.swing.UIDefaults;
  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:     UIDefaults defaults = UIManager.getLookAndFeelDefaults();
 135:     focusColor = defaults.getColor(getPropertyPrefix() + "focus");
 136:     selectColor = defaults.getColor(getPropertyPrefix() + "select");
 137:     disabledTextColor = defaults.getColor(getPropertyPrefix() + "disabledText");
 138:   }
 139:   
 140:   /**
 141:    * Paints the button background when it is pressed/selected. 
 142:    * 
 143:    * @param g  the graphics device.
 144:    * @param b  the button.
 145:    */
 146:   protected void paintButtonPressed(Graphics g, AbstractButton b)
 147:   {
 148:     if (b.isContentAreaFilled() && b.isOpaque())
 149:       {
 150:         Color saved = g.getColor();
 151:         Rectangle bounds = SwingUtilities.getLocalBounds(b);
 152:         g.setColor(selectColor);
 153:         g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
 154:         g.setColor(saved);
 155:       }
 156:   }
 157:   
 158:   /**
 159:    * Paints the text for the button.
 160:    * 
 161:    * @param g  the graphics device.
 162:    * @param c  the component.
 163:    * @param textRect  the bounds for the text.
 164:    * @param text  the text.
 165:    * 
 166:    * @deprecated 1.4 Use {@link BasicButtonUI#paintText(java.awt.Graphics, 
 167:    * javax.swing.AbstractButton, java.awt.Rectangle, java.lang.String)}.
 168:    */
 169:   protected void paintText(Graphics g, JComponent c, Rectangle textRect,
 170:                            String text)
 171:   {
 172:     Font savedFont = g.getFont();
 173:     Color savedColor = g.getColor();
 174:     g.setFont(c.getFont());
 175:     if (c.isEnabled())
 176:       g.setColor(c.getForeground());
 177:     else
 178:       g.setColor(disabledTextColor);
 179:     FontMetrics fm = g.getFontMetrics(c.getFont());
 180:     int ascent = fm.getAscent();
 181:     g.drawString(text, textRect.x, textRect.y + ascent);
 182:     g.setFont(savedFont);
 183:     g.setColor(savedColor);
 184:   }
 185:   
 186:   /**
 187:    * Draws the focus highlight around the text and icon.
 188:    * 
 189:    * @param g  the graphics device.
 190:    * @param b  the button.
 191:    */
 192:   protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect,
 193:       Rectangle textRect, Rectangle iconRect)
 194:   {
 195:     if (!b.hasFocus())
 196:       return;
 197:     Color saved = g.getColor();
 198:     g.setColor(focusColor);
 199:     Rectangle fr = iconRect.union(textRect);
 200:     g.drawRect(fr.x - 1, fr.y - 1, fr.width + 1, fr.height + 1);
 201:     g.setColor(saved);    
 202:   }
 203:   
 204: }