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