Source for javax.swing.plaf.basic.BasicRadioButtonUI

   1: /* BasicRadioButtonUI.java
   2:    Copyright (C) 2002, 2004 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.basic;
  40: 
  41: import java.awt.Color;
  42: import java.awt.Dimension;
  43: import java.awt.Font;
  44: import java.awt.Graphics;
  45: import java.awt.Rectangle;
  46: 
  47: import javax.swing.AbstractButton;
  48: import javax.swing.Icon;
  49: import javax.swing.JComponent;
  50: import javax.swing.SwingUtilities;
  51: import javax.swing.UIManager;
  52: import javax.swing.plaf.ComponentUI;
  53: 
  54: /**
  55:  * The BasicLookAndFeel UI implementation for
  56:  * {@link javax.swing.JRadioButton}s.
  57:  */
  58: public class BasicRadioButtonUI extends BasicToggleButtonUI
  59: {
  60:   /**
  61:    * The default icon for JRadioButtons. The default icon displays the usual
  62:    * RadioButton and is sensible to the selection state of the button,
  63:    * and can be used both as normal icon as well as selectedIcon.
  64:    */
  65:   protected Icon icon;
  66: 
  67:   /**
  68:    * Creates and returns a new instance of <code>BasicRadioButtonUI</code>.
  69:    *
  70:    * @return a new instance of <code>BasicRadioButtonUI</code>
  71:    */
  72:   public static ComponentUI createUI(final JComponent c)  {
  73:     return new BasicRadioButtonUI();
  74:   }
  75: 
  76:   /**
  77:    * Creates a new instance of <code>BasicButtonUI</code>.
  78:    */
  79:   public BasicRadioButtonUI()
  80:   {
  81:     icon = getDefaultIcon();
  82:   }
  83: 
  84:   /**
  85:    * Installs defaults from the Look &amp; Feel table on the specified
  86:    * button.
  87:    *
  88:    * @param b the button on which to install the defaults
  89:    */
  90:   protected void installDefaults(AbstractButton b)
  91:   {
  92:     super.installDefaults(b);
  93:     if (b.getIcon() == null)
  94:       b.setIcon(icon);
  95:     if (b.getSelectedIcon() == null)
  96:       b.setSelectedIcon(icon);
  97:     if (b.getDisabledIcon() == null)
  98:       b.setDisabledIcon(icon);
  99:     if (b.getDisabledSelectedIcon() == null)
 100:       b.setDisabledSelectedIcon(icon);
 101:   }
 102: 
 103:   /**
 104:    * Returns the prefix used for UIDefaults properties. This is
 105:    * <code>RadioButton</code> in this case.
 106:    *
 107:    * @return the prefix used for UIDefaults properties
 108:    */
 109:   protected String getPropertyPrefix()
 110:   {
 111:     return "RadioButton.";
 112:   }
 113: 
 114:   /**
 115:    * Returns the default icon for JRadioButtons.
 116:    * The default icon displays the usual
 117:    * RadioButton and is sensible to the selection state of the button,
 118:    * and can be used both as normal icon as well as selectedIcon.
 119:    *
 120:    * @return the default icon for JRadioButtons
 121:    */
 122:   public Icon getDefaultIcon()
 123:   {
 124:     return UIManager.getIcon(getPropertyPrefix() + "icon");
 125:   }
 126: 
 127:   /**
 128:    * Paints the RadioButton.
 129:    *
 130:    * @param g the Graphics context to paint with
 131:    * @param c the button to paint
 132:    */
 133:   public void paint(Graphics g, JComponent c)
 134:   {
 135:     AbstractButton b = (AbstractButton) c;
 136: 
 137:     Rectangle tr = new Rectangle();
 138:     Rectangle ir = new Rectangle();
 139:     Rectangle vr = new Rectangle();
 140: 
 141:     Font f = c.getFont();
 142: 
 143:     g.setFont(f);
 144: 
 145:     Icon currentIcon = null;
 146:     if (b.isSelected() && b.isEnabled())
 147:       currentIcon = b.getSelectedIcon();
 148:     else if (!b.isSelected() && b.isEnabled())
 149:       currentIcon = b.getIcon();
 150:     else if (b.isSelected() && !b.isEnabled())
 151:       currentIcon = b.getDisabledSelectedIcon();
 152:     else // (!b.isSelected() && !b.isEnabled())
 153:       currentIcon = b.getDisabledIcon();
 154: 
 155:     SwingUtilities.calculateInnerArea(b, vr);
 156:     String text = SwingUtilities.layoutCompoundLabel
 157:       (c, g.getFontMetrics(f), b.getText(), currentIcon,
 158:        b.getVerticalAlignment(), b.getHorizontalAlignment(),
 159:        b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
 160:        vr, ir, tr, b.getIconTextGap() + defaultTextShiftOffset);
 161:     
 162:     if (currentIcon != null)
 163:       {
 164:         currentIcon.paintIcon(c, g, ir.x, ir.y);
 165:       }
 166:     if (text != null)
 167:       paintText(g, b, tr, text);
 168:     // TODO: Figure out what is the size parameter?
 169:     if (b.hasFocus() && b.isFocusPainted() && b.isEnabled())
 170:       paintFocus(g, tr, null);
 171:   }
 172: 
 173:   /**
 174:    * Paints the focus indicator for JRadioButtons.
 175:    *
 176:    * @param g the graphics context
 177:    * @param tr the rectangle for the text label
 178:    * @param size the size (??)
 179:    */
 180:   // TODO: Figure out what for is the size parameter.
 181:   protected void paintFocus(Graphics g, Rectangle tr, Dimension size)
 182:   {
 183:     Color focusColor = UIManager.getColor(getPropertyPrefix() + ".focus");
 184:     Color saved = g.getColor();
 185:     g.setColor(focusColor);
 186:     g.drawRect(tr.x, tr.y, tr.width, tr.height);
 187:     g.setColor(saved);
 188:   }
 189: }