jdk/src/share/classes/javax/swing/plaf/basic/BasicToggleButtonUI.java
author darcy
Wed, 02 Jul 2014 23:03:27 -0700
changeset 25565 ce603b34c98d
parent 23715 54ae9dd9df73
child 25761 c408b10ef757
permissions -rw-r--r--
8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.* 8042849: Fix raw and unchecked warnings in com.sun.java.swing Reviewed-by: pchelko, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4225
diff changeset
     2
 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4225
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4225
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4225
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4225
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4225
diff changeset
    23
 * questions.
2
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.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    28
import sun.awt.AppContext;
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    29
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.text.View;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * BasicToggleButton implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Jeff Dinkins
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class BasicToggleButtonUI extends BasicButtonUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    47
    private static final Object BASIC_TOGGLE_BUTTON_UI_KEY = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final static String propertyPrefix = "ToggleButton" + ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    //          Create PLAF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static ComponentUI createUI(JComponent b) {
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    55
        AppContext appContext = AppContext.getAppContext();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    56
        BasicToggleButtonUI toggleButtonUI =
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    57
                (BasicToggleButtonUI) appContext.get(BASIC_TOGGLE_BUTTON_UI_KEY);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    58
        if (toggleButtonUI == null) {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    59
            toggleButtonUI = new BasicToggleButtonUI();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    60
            appContext.put(BASIC_TOGGLE_BUTTON_UI_KEY, toggleButtonUI);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    61
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return toggleButtonUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    protected String getPropertyPrefix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return propertyPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    //          Paint Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        AbstractButton b = (AbstractButton) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        Dimension size = b.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        FontMetrics fm = g.getFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        Insets i = c.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Rectangle viewRect = new Rectangle(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        viewRect.x += i.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        viewRect.y += i.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        viewRect.width -= (i.right + viewRect.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        viewRect.height -= (i.bottom + viewRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        Rectangle iconRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        Rectangle textRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        Font f = c.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        g.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // layout the text and icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        String text = SwingUtilities.layoutCompoundLabel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            c, fm, b.getText(), b.getIcon(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            b.getVerticalAlignment(), b.getHorizontalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            viewRect, iconRect, textRect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            b.getText() == null ? 0 : b.getIconTextGap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        g.setColor(b.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (model.isArmed() && model.isPressed() || model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            paintButtonPressed(g,b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // Paint the Icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if(b.getIcon() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            paintIcon(g, b, iconRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // Draw the Text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if(text != null && !text.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
               v.paint(g, textRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
               paintText(g, b, textRect, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // draw the dashed focus line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (b.isFocusPainted() && b.hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            paintFocus(g, b, viewRect, textRect, iconRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Icon icon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if(!model.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if(model.isSelected()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   136
               icon = b.getDisabledSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            } else {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   138
               icon = b.getDisabledIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } else if(model.isPressed() && model.isArmed()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   141
            icon = b.getPressedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if(icon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                // Use selected icon
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   144
                icon = b.getSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } else if(model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if(b.isRolloverEnabled() && model.isRollover()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   148
                icon = b.getRolloverSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                if (icon == null) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   150
                    icon = b.getSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            } else {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   153
                icon = b.getSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } else if(b.isRolloverEnabled() && model.isRollover()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   156
            icon = b.getRolloverIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if(icon == null) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   160
            icon = b.getIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        icon.paintIcon(b, g, iconRect.x, iconRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Overriden so that the text will not be rendered as shifted for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Toggle buttons and subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    protected int getTextShiftOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}