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