jdk/src/share/classes/javax/swing/plaf/metal/MetalButtonUI.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 javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.plaf.basic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * MetalButtonUI implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Tom Santos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class MetalButtonUI extends BasicButtonUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // NOTE: These are not really needed, but at this point we can't pull
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // them. Their values are updated purely for historical reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    protected Color focusColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected Color selectColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    protected Color disabledTextColor;
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_BUTTON_UI_KEY = new Object();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    61
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    //          Create PLAF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static ComponentUI createUI(JComponent c) {
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    66
        AppContext appContext = AppContext.getAppContext();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    67
        MetalButtonUI metalButtonUI =
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    68
                (MetalButtonUI) appContext.get(METAL_BUTTON_UI_KEY);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    69
        if (metalButtonUI == null) {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    70
            metalButtonUI = new MetalButtonUI();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    71
            appContext.put(METAL_BUTTON_UI_KEY, metalButtonUI);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 2
diff changeset
    72
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return metalButtonUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    //          Install
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public void installDefaults(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        super.installDefaults(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public void uninstallDefaults(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        super.uninstallDefaults(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    //         Create Listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    protected BasicButtonListener createButtonListener(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return super.createButtonListener(b);
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
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    //         Default Accessors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    protected Color getSelectColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        selectColor = UIManager.getColor(getPropertyPrefix() + "select");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return selectColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    protected Color getDisabledTextColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        disabledTextColor = UIManager.getColor(getPropertyPrefix() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                               "disabledText");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return disabledTextColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected Color getFocusColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return focusColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    //          Paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * If necessary paints the background of the component, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * invokes <code>paint</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param g Graphics to paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param c JComponent painting on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws NullPointerException if <code>g</code> or <code>c</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *         null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @see javax.swing.plaf.ComponentUI#update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @see javax.swing.plaf.ComponentUI#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public void update(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        AbstractButton button = (AbstractButton)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if ((c.getBackground() instanceof UIResource) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                  button.isContentAreaFilled() && c.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            ButtonModel model = button.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (!MetalUtils.isToolBarButton(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                if (!model.isArmed() && !model.isPressed() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                        MetalUtils.drawGradient(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        c, g, "Button.gradient", 0, 0, c.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        c.getHeight(), true)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    paint(g, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            else if (model.isRollover() && MetalUtils.drawGradient(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        c, g, "Button.gradient", 0, 0, c.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        c.getHeight(), true)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                paint(g, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        super.update(g, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    protected void paintButtonPressed(Graphics g, AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if ( b.isContentAreaFilled() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            Dimension size = b.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            g.setColor(getSelectColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            g.fillRect(0, 0, size.width, size.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    protected void paintFocus(Graphics g, AbstractButton b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                              Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        Rectangle focusRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        String text = b.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        boolean isIcon = b.getIcon() != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // If there is text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if ( text != null && !text.equals( "" ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if ( !isIcon ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                focusRect.setBounds( textRect );
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
                focusRect.setBounds( iconRect.union( textRect ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // If there is an icon and no text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        else if ( isIcon ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            focusRect.setBounds( iconRect );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        g.setColor(getFocusColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        g.drawRect((focusRect.x-1), (focusRect.y-1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                  focusRect.width+1, focusRect.height+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        AbstractButton b = (AbstractButton) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        int mnemIndex = b.getDisplayedMnemonicIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        /* Draw the Text */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if(model.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            /*** paint the text normally */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            g.setColor(b.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            /*** paint the text disabled ***/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            g.setColor(getDisabledTextColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        SwingUtilities2.drawStringUnderlineCharAt(c, g,text,mnemIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                  textRect.x, textRect.y + fm.getAscent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
}