Source for javax.swing.plaf.basic.BasicRadioButtonUI

   1: /* BasicRadioButtonUI.java
   2:    Copyright (C) 2002, 2004, 2006, 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.Insets;
  46: import java.awt.Rectangle;
  47: 
  48: import javax.swing.AbstractButton;
  49: import javax.swing.ButtonModel;
  50: import javax.swing.Icon;
  51: import javax.swing.JComponent;
  52: import javax.swing.SwingUtilities;
  53: import javax.swing.UIManager;
  54: import javax.swing.plaf.ComponentUI;
  55: 
  56: /**
  57:  * The BasicLookAndFeel UI implementation for
  58:  * {@link javax.swing.JRadioButton}s.
  59:  */
  60: public class BasicRadioButtonUI extends BasicToggleButtonUI
  61: {
  62:   /**
  63:    * The default icon for JRadioButtons. The default icon displays the usual
  64:    * RadioButton and is sensible to the selection state of the button,
  65:    * and can be used both as normal icon as well as selectedIcon.
  66:    */
  67:   protected Icon icon;
  68: 
  69:   /**
  70:    * Creates and returns a new instance of <code>BasicRadioButtonUI</code>.
  71:    *
  72:    * @return a new instance of <code>BasicRadioButtonUI</code>
  73:    */
  74:   public static ComponentUI createUI(final JComponent c)  
  75:   {
  76:     return new BasicRadioButtonUI();
  77:   }
  78: 
  79:   /**
  80:    * Creates a new instance of <code>BasicButtonUI</code>.
  81:    */
  82:   public BasicRadioButtonUI()
  83:   {
  84:     icon = getDefaultIcon();
  85:   }
  86: 
  87:   /**
  88:    * Installs defaults from the Look &amp; Feel table on the specified
  89:    * button.
  90:    *
  91:    * @param b the button on which to install the defaults
  92:    */
  93:   protected void installDefaults(AbstractButton b)
  94:   {
  95:     super.installDefaults(b);
  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:     return UIManager.getIcon(getPropertyPrefix() + "icon");
 120:   }
 121: 
 122:   /**
 123:    * Paints the RadioButton.
 124:    *
 125:    * @param g the Graphics context to paint with
 126:    * @param c the button to paint
 127:    */
 128:   public void paint(Graphics g, JComponent c)
 129:   {
 130:     AbstractButton b = (AbstractButton) c;
 131: 
 132:     Rectangle tr = new Rectangle();
 133:     Rectangle ir = new Rectangle();
 134:     Rectangle vr = new Rectangle();
 135: 
 136:     Font f = c.getFont();
 137: 
 138:     g.setFont(f);
 139: 
 140:     ButtonModel m = b.getModel();
 141:     // FIXME: Do a filtering on any customized icon if the following property
 142:     // is set.
 143:     boolean enabled = b.isEnabled();
 144:     
 145:     Icon currentIcon = b.getIcon();
 146: 
 147:     if (currentIcon == null)
 148:       {
 149:         currentIcon = getDefaultIcon();
 150:       }
 151:     
 152:     SwingUtilities.calculateInnerArea(b, vr);
 153:     String text = SwingUtilities.layoutCompoundLabel(c, g.getFontMetrics(f), 
 154:        b.getText(), currentIcon,
 155:        b.getVerticalAlignment(), b.getHorizontalAlignment(),
 156:        b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
 157:        vr, ir, tr, b.getIconTextGap() + defaultTextShiftOffset);
 158:     
 159:     currentIcon.paintIcon(c, g, ir.x, ir.y);
 160:     
 161:     if (text != null)
 162:       paintText(g, b, tr, text);
 163:     if (b.hasFocus() && b.isFocusPainted() && m.isEnabled())
 164:       paintFocus(g, tr, c.getSize());
 165:   }
 166:   
 167:   public Dimension getPreferredSize(JComponent c)
 168:   {
 169:     // This is basically the same code as in
 170:     // BasicGraphicsUtils.getPreferredButtonSize() but takes the default icon
 171:     // property into account. JRadioButton and subclasses always have an icon:
 172:     // the check box. If the user explicitly changes it with setIcon() that
 173:     // one will be used for layout calculations and painting instead.
 174:     // The other icon properties are ignored.
 175:     AbstractButton b = (AbstractButton) c;
 176:     
 177:     Rectangle contentRect;
 178:     Rectangle viewRect;
 179:     Rectangle iconRect = new Rectangle();
 180:     Rectangle textRect = new Rectangle();
 181:     Insets insets = b.getInsets();
 182:     
 183:     Icon i = b.getIcon();
 184:     if (i == null)
 185:       i = getDefaultIcon(); 
 186:     
 187:     viewRect = new Rectangle();
 188: 
 189:     SwingUtilities.layoutCompoundLabel(
 190:       b, // for the component orientation
 191:       b.getFontMetrics(b.getFont()),
 192:       b.getText(),
 193:       i,
 194:       b.getVerticalAlignment(), 
 195:       b.getHorizontalAlignment(),
 196:       b.getVerticalTextPosition(),
 197:       b.getHorizontalTextPosition(),
 198:       viewRect, iconRect, textRect,
 199:       defaultTextIconGap + defaultTextShiftOffset);
 200: 
 201:     contentRect = textRect.union(iconRect);
 202:     
 203:     return new Dimension(insets.left
 204:                          + contentRect.width 
 205:                          + insets.right + b.getHorizontalAlignment(),
 206:                          insets.top
 207:                          + contentRect.height 
 208:                          + insets.bottom);
 209:   }
 210: 
 211:   /**
 212:    * Paints the focus indicator for JRadioButtons.
 213:    *
 214:    * @param g the graphics context
 215:    * @param tr the rectangle for the text label
 216:    * @param size the size of the <code>JRadioButton</code> component.
 217:    */
 218:   protected void paintFocus(Graphics g, Rectangle tr, Dimension size)
 219:   {
 220:     Color focusColor = UIManager.getColor(getPropertyPrefix() + ".focus");
 221:     Color saved = g.getColor();
 222:     g.setColor(focusColor);
 223:     g.drawRect(tr.x, tr.y, tr.width, tr.height);
 224:     g.setColor(saved);
 225:   }
 226: }