1 /* 2 * Copyright (c) 1998, 2005, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.swing.plaf.metal; 27 28 import sun.swing.SwingUtilities2; 29 import sun.awt.AppContext; 30 31 import java.awt.*; 32 import java.awt.event.*; 33 import javax.swing.*; 34 import javax.swing.plaf.basic.*; 35 import javax.swing.border.*; 36 import javax.swing.plaf.*; 37 import java.io.Serializable; 38 import javax.swing.text.View; 39 40 41 /** 42 * RadioButtonUI implementation for MetalRadioButtonUI 43 * <p> 44 * <strong>Warning:</strong> 45 * Serialized objects of this class will not be compatible with 46 * future Swing releases. The current serialization support is 47 * appropriate for short term storage or RMI between applications running 48 * the same version of Swing. As of 1.4, support for long term storage 49 * of all JavaBeans<sup><font size="-2">TM</font></sup> 50 * has been added to the <code>java.beans</code> package. 51 * Please see {@link java.beans.XMLEncoder}. 52 * 53 * @author Michael C. Albers (Metal modifications) 54 * @author Jeff Dinkins (original BasicRadioButtonCode) 55 */ 56 public class MetalRadioButtonUI extends BasicRadioButtonUI { 57 58 private static final Object METAL_RADIO_BUTTON_UI_KEY = new Object(); 59 60 protected Color focusColor; 61 protected Color selectColor; 62 protected Color disabledTextColor; 63 64 private boolean defaults_initialized = false; 65 66 // ******************************** 67 // Create PlAF 68 // ******************************** 69 public static ComponentUI createUI(JComponent c) { 70 AppContext appContext = AppContext.getAppContext(); 71 MetalRadioButtonUI metalRadioButtonUI = 72 (MetalRadioButtonUI) appContext.get(METAL_RADIO_BUTTON_UI_KEY); 73 if (metalRadioButtonUI == null) { 74 metalRadioButtonUI = new MetalRadioButtonUI(); 75 appContext.put(METAL_RADIO_BUTTON_UI_KEY, metalRadioButtonUI); 76 } 77 return metalRadioButtonUI; 78 } 79 80 // ******************************** 81 // Install Defaults 82 // ******************************** 83 public void installDefaults(AbstractButton b) { 84 super.installDefaults(b); 85 if(!defaults_initialized) { 86 focusColor = UIManager.getColor(getPropertyPrefix() + "focus"); 87 selectColor = UIManager.getColor(getPropertyPrefix() + "select"); 88 disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText"); 89 defaults_initialized = true; 90 } 91 LookAndFeel.installProperty(b, "opaque", Boolean.TRUE); 92 } 93 94 protected void uninstallDefaults(AbstractButton b) { 95 super.uninstallDefaults(b); 96 defaults_initialized = false; 97 } 98 99 // ******************************** 100 // Default Accessors 101 // ******************************** 102 protected Color getSelectColor() { 103 return selectColor; 104 } 105 106 protected Color getDisabledTextColor() { 107 return disabledTextColor; 108 } 109 110 protected Color getFocusColor() { 111 return focusColor; 112 } 113 114 115 // ******************************** 116 // Paint Methods 117 // ******************************** 118 public synchronized void paint(Graphics g, JComponent c) { 119 120 AbstractButton b = (AbstractButton) c; 121 ButtonModel model = b.getModel(); 122 123 Dimension size = c.getSize(); 124 125 int w = size.width; 126 int h = size.height; 127 128 Font f = c.getFont(); 129 g.setFont(f); 130 FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f); 131 132 Rectangle viewRect = new Rectangle(size); 133 Rectangle iconRect = new Rectangle(); 134 Rectangle textRect = new Rectangle(); 135 136 Insets i = c.getInsets(); 137 viewRect.x += i.left; 138 viewRect.y += i.top; 139 viewRect.width -= (i.right + viewRect.x); 140 viewRect.height -= (i.bottom + viewRect.y); 141 142 Icon altIcon = b.getIcon(); 143 Icon selectedIcon = null; 144 Icon disabledIcon = null; 145 146 String text = SwingUtilities.layoutCompoundLabel( 147 c, fm, b.getText(), altIcon != null ? altIcon : getDefaultIcon(), 148 b.getVerticalAlignment(), b.getHorizontalAlignment(), 149 b.getVerticalTextPosition(), b.getHorizontalTextPosition(), 150 viewRect, iconRect, textRect, b.getIconTextGap()); 151 152 // fill background 153 if(c.isOpaque()) { 154 g.setColor(b.getBackground()); 155 g.fillRect(0,0, size.width, size.height); 156 } 157 158 159 // Paint the radio button 160 if(altIcon != null) { 161 162 if(!model.isEnabled()) { 163 if(model.isSelected()) { 164 altIcon = b.getDisabledSelectedIcon(); 165 } else { 166 altIcon = b.getDisabledIcon(); 167 } 168 } else if(model.isPressed() && model.isArmed()) { 169 altIcon = b.getPressedIcon(); 170 if(altIcon == null) { 171 // Use selected icon 172 altIcon = b.getSelectedIcon(); 173 } 174 } else if(model.isSelected()) { 175 if(b.isRolloverEnabled() && model.isRollover()) { 176 altIcon = b.getRolloverSelectedIcon(); 177 if (altIcon == null) { 178 altIcon = b.getSelectedIcon(); 179 } 180 } else { 181 altIcon = b.getSelectedIcon(); 182 } 183 } else if(b.isRolloverEnabled() && model.isRollover()) { 184 altIcon = b.getRolloverIcon(); 185 } 186 187 if(altIcon == null) { 188 altIcon = b.getIcon(); 189 } 190 191 altIcon.paintIcon(c, g, iconRect.x, iconRect.y); 192 193 } else { 194 getDefaultIcon().paintIcon(c, g, iconRect.x, iconRect.y); 195 } 196 197 198 // Draw the Text 199 if(text != null) { 200 View v = (View) c.getClientProperty(BasicHTML.propertyKey); 201 if (v != null) { 202 v.paint(g, textRect); 203 } else { 204 int mnemIndex = b.getDisplayedMnemonicIndex(); 205 if(model.isEnabled()) { 206 // *** paint the text normally 207 g.setColor(b.getForeground()); 208 } else { 209 // *** paint the text disabled 210 g.setColor(getDisabledTextColor()); 211 } 212 SwingUtilities2.drawStringUnderlineCharAt(c,g,text, 213 mnemIndex, textRect.x, textRect.y + fm.getAscent()); 214 } 215 if(b.hasFocus() && b.isFocusPainted() && 216 textRect.width > 0 && textRect.height > 0 ) { 217 paintFocus(g,textRect,size); 218 } 219 } 220 } 221 222 protected void paintFocus(Graphics g, Rectangle t, Dimension d){ 223 g.setColor(getFocusColor()); 224 g.drawRect(t.x-1, t.y-1, t.width+1, t.height+1); 225 } 226 }