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