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