jdk/src/share/classes/javax/swing/plaf/metal/MetalToggleButtonUI.java
author alexp
Fri, 06 Nov 2009 19:48:09 +0300
changeset 4225 57ec8171fcd5
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6657138: Mutable statics in Windows PL&F (findbugs) Reviewed-by: peterz, hawtin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.metal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.swing.SwingUtilities2;
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    29
import sun.awt.AppContext;
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    30
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.ref.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.plaf.basic.BasicToggleButtonUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * MetalToggleButton implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Tom Santos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class MetalToggleButtonUI extends BasicToggleButtonUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    60
    private static final Object METAL_TOGGLE_BUTTON_UI_KEY = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected Color focusColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected Color selectColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected Color disabledTextColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private boolean defaults_initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    //        Create PLAF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public static ComponentUI createUI(JComponent b) {
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    72
        AppContext appContext = AppContext.getAppContext();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    73
        MetalToggleButtonUI metalToggleButtonUI =
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    74
                (MetalToggleButtonUI) appContext.get(METAL_TOGGLE_BUTTON_UI_KEY);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    75
        if (metalToggleButtonUI == null) {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    76
            metalToggleButtonUI = new MetalToggleButtonUI();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    77
            appContext.put(METAL_TOGGLE_BUTTON_UI_KEY, metalToggleButtonUI);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    78
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return metalToggleButtonUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    //        Install Defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public void installDefaults(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        super.installDefaults(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if(!defaults_initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            selectColor = UIManager.getColor(getPropertyPrefix() + "select");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            defaults_initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    protected void uninstallDefaults(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        super.uninstallDefaults(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        defaults_initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    //         Default Accessors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    protected Color getSelectColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return selectColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    protected Color getDisabledTextColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return disabledTextColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    protected Color getFocusColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return focusColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    //        Paint Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * If necessary paints the background of the component, then invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <code>paint</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param g Graphics to paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param c JComponent painting on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @throws NullPointerException if <code>g</code> or <code>c</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *         null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @see javax.swing.plaf.ComponentUI#update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @see javax.swing.plaf.ComponentUI#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public void update(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        AbstractButton button = (AbstractButton)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if ((c.getBackground() instanceof UIResource) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        button.isContentAreaFilled() && c.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            ButtonModel model = button.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (!MetalUtils.isToolBarButton(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                if (!model.isArmed() && !model.isPressed() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        MetalUtils.drawGradient(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        c, g, "ToggleButton.gradient", 0, 0, c.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        c.getHeight(), true)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    paint(g, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            else if ((model.isRollover() || model.isSelected()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        MetalUtils.drawGradient(c, g, "ToggleButton.gradient",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        0, 0, c.getWidth(), c.getHeight(), true)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                paint(g, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        super.update(g, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    protected void paintButtonPressed(Graphics g, AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if ( b.isContentAreaFilled() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            g.setColor(getSelectColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            g.fillRect(0, 0, b.getWidth(), b.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        AbstractButton b = (AbstractButton) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        FontMetrics fm = SwingUtilities2.getFontMetrics(b, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        int mnemIndex = b.getDisplayedMnemonicIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        /* Draw the Text */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if(model.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            /*** paint the text normally */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            g.setColor(b.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            /*** paint the text disabled ***/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if (model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                g.setColor(c.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                g.setColor(getDisabledTextColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                textRect.x, textRect.y + fm.getAscent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    protected void paintFocus(Graphics g, AbstractButton b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                              Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        Rectangle focusRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        String text = b.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        boolean isIcon = b.getIcon() != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // If there is text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if ( text != null && !text.equals( "" ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if ( !isIcon ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                focusRect.setBounds( textRect );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                focusRect.setBounds( iconRect.union( textRect ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        // If there is an icon and no text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        else if ( isIcon ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            focusRect.setBounds( iconRect );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        g.setColor(getFocusColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        g.drawRect((focusRect.x-1), (focusRect.y-1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                  focusRect.width+1, focusRect.height+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Paints the appropriate icon of the button <code>b</code> in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * space <code>iconRect</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param g Graphics to paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param b Button to render for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param iconRect space to render in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @throws NullPointerException if any of the arguments are null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        super.paintIcon(g, b, iconRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
}