1 /*
   2  * Copyright (c) 1998, 2014, 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&trade;
  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 @SuppressWarnings("serial") // Same-version serialization only
  57 public class MetalRadioButtonUI extends BasicRadioButtonUI {
  58 
  59     private static final Object METAL_RADIO_BUTTON_UI_KEY = new Object();
  60 
  61     /**
  62      * The color of the focused radio button.
  63      */
  64     protected Color focusColor;
  65 
  66     /**
  67      * The color of the selected radio button.
  68      */
  69     protected Color selectColor;
  70 
  71     /**
  72      * The color of a disabled text.
  73      */
  74     protected Color disabledTextColor;
  75 
  76     private boolean defaults_initialized = false;
  77 
  78     private TextUIDrawing textUIDrawing;
  79 
  80     // ********************************
  81     //        Create PlAF
  82     // ********************************
  83 
  84     /**
  85      * Returns an instance of {@code MetalRadioButtonUI}.
  86      *
  87      * @param c a component
  88      * @return an instance of {@code MetalRadioButtonUI}
  89      */
  90     public static ComponentUI createUI(JComponent c) {
  91         AppContext appContext = AppContext.getAppContext();
  92         MetalRadioButtonUI metalRadioButtonUI =
  93                 (MetalRadioButtonUI) appContext.get(METAL_RADIO_BUTTON_UI_KEY);
  94         if (metalRadioButtonUI == null) {
  95             metalRadioButtonUI = new MetalRadioButtonUI();
  96             appContext.put(METAL_RADIO_BUTTON_UI_KEY, metalRadioButtonUI);
  97         }
  98         return metalRadioButtonUI;
  99     }
 100 
 101     // ********************************
 102     //        Install Defaults
 103     // ********************************
 104     public void installDefaults(AbstractButton b) {
 105         super.installDefaults(b);
 106         if(!defaults_initialized) {
 107             focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
 108             selectColor = UIManager.getColor(getPropertyPrefix() + "select");
 109             disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText");
 110             defaults_initialized = true;
 111         }
 112         LookAndFeel.installProperty(b, "opaque", Boolean.TRUE);
 113         textUIDrawing = SwingUtilities2.getTextUIDrawing(textUIDrawing);
 114     }
 115 
 116     protected void uninstallDefaults(AbstractButton b) {
 117         super.uninstallDefaults(b);
 118         defaults_initialized = false;
 119         if (textUIDrawing != SwingUtilities2.DEFAULT_UI_TEXT_DRAWING
 120                 && textUIDrawing instanceof UIResource) {
 121             textUIDrawing = SwingUtilities2.DEFAULT_UI_TEXT_DRAWING;
 122         }
 123     }
 124 
 125     // ********************************
 126     //         Default Accessors
 127     // ********************************
 128 
 129     /**
 130      * Returns the color of the selected {@code JRadioButton}.
 131      *
 132      * @return the color of the selected {@code JRadioButton}
 133      */
 134     protected Color getSelectColor() {
 135         return selectColor;
 136     }
 137 
 138     /**
 139      * Returns the color of the disabled text.
 140      *
 141      * @return the color of the disabled text
 142      */
 143     protected Color getDisabledTextColor() {
 144         return disabledTextColor;
 145     }
 146 
 147     /**
 148      * Returns the color of the focused {@code JRadioButton}.
 149      *
 150      * @return the color of the focused {@code JRadioButton}
 151      */
 152     protected Color getFocusColor() {
 153         return focusColor;
 154     }
 155 
 156 
 157     // ********************************
 158     //        Paint Methods
 159     // ********************************
 160     public synchronized void paint(Graphics g, JComponent c) {
 161 
 162         AbstractButton b = (AbstractButton) c;
 163         ButtonModel model = b.getModel();
 164 
 165         Dimension size = c.getSize();
 166 
 167         int w = size.width;
 168         int h = size.height;
 169 
 170         Font f = c.getFont();
 171         g.setFont(f);
 172         FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f);
 173 
 174         Rectangle viewRect = new Rectangle(size);
 175         Rectangle iconRect = new Rectangle();
 176         Rectangle textRect = new Rectangle();
 177 
 178         Insets i = c.getInsets();
 179         viewRect.x += i.left;
 180         viewRect.y += i.top;
 181         viewRect.width -= (i.right + viewRect.x);
 182         viewRect.height -= (i.bottom + viewRect.y);
 183 
 184         Icon altIcon = b.getIcon();
 185         Icon selectedIcon = null;
 186         Icon disabledIcon = null;
 187 
 188         String text = SwingUtilities.layoutCompoundLabel(
 189             c, fm, b.getText(), altIcon != null ? altIcon : getDefaultIcon(),
 190             b.getVerticalAlignment(), b.getHorizontalAlignment(),
 191             b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
 192             viewRect, iconRect, textRect, b.getIconTextGap());
 193 
 194         // fill background
 195         if(c.isOpaque()) {
 196             g.setColor(b.getBackground());
 197             g.fillRect(0,0, size.width, size.height);
 198         }
 199 
 200 
 201         // Paint the radio button
 202         if(altIcon != null) {
 203 
 204             if(!model.isEnabled()) {
 205                 if(model.isSelected()) {
 206                    altIcon = b.getDisabledSelectedIcon();
 207                 } else {
 208                    altIcon = b.getDisabledIcon();
 209                 }
 210             } else if(model.isPressed() && model.isArmed()) {
 211                 altIcon = b.getPressedIcon();
 212                 if(altIcon == null) {
 213                     // Use selected icon
 214                     altIcon = b.getSelectedIcon();
 215                 }
 216             } else if(model.isSelected()) {
 217                 if(b.isRolloverEnabled() && model.isRollover()) {
 218                         altIcon = b.getRolloverSelectedIcon();
 219                         if (altIcon == null) {
 220                                 altIcon = b.getSelectedIcon();
 221                         }
 222                 } else {
 223                         altIcon = b.getSelectedIcon();
 224                 }
 225             } else if(b.isRolloverEnabled() && model.isRollover()) {
 226                 altIcon = b.getRolloverIcon();
 227             }
 228 
 229             if(altIcon == null) {
 230                 altIcon = b.getIcon();
 231             }
 232 
 233             altIcon.paintIcon(c, g, iconRect.x, iconRect.y);
 234 
 235         } else {
 236             getDefaultIcon().paintIcon(c, g, iconRect.x, iconRect.y);
 237         }
 238 
 239 
 240         // Draw the Text
 241         if(text != null) {
 242             View v = (View) c.getClientProperty(BasicHTML.propertyKey);
 243             if (v != null) {
 244                 v.paint(g, textRect);
 245             } else {
 246                int mnemIndex = b.getDisplayedMnemonicIndex();
 247                if(model.isEnabled()) {
 248                    // *** paint the text normally
 249                    g.setColor(b.getForeground());
 250                } else {
 251                    // *** paint the text disabled
 252                    g.setColor(getDisabledTextColor());
 253                }
 254                textUIDrawing.drawStringUnderlineCharAt(c,g,text,
 255                        mnemIndex, textRect.x, textRect.y + fm.getAscent());
 256            }
 257            if(b.hasFocus() && b.isFocusPainted() &&
 258               textRect.width > 0 && textRect.height > 0 ) {
 259                paintFocus(g,textRect,size);
 260            }
 261         }
 262     }
 263 
 264     protected void paintFocus(Graphics g, Rectangle t, Dimension d){
 265         g.setColor(getFocusColor());
 266         g.drawRect(t.x-1, t.y-1, t.width+1, t.height+1);
 267     }
 268 }