jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicToggleButtonUI.java
author prr
Sat, 19 Sep 2015 15:45:59 -0700
changeset 32865 f9cb6e427f9e
parent 25859 3317bb8137f4
permissions -rw-r--r--
8136783: Run blessed-modifier-order script on java.desktop Reviewed-by: martin, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
     2
 * Copyright (c) 1997, 2014, 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
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
    49
    private static final String propertyPrefix = "ToggleButton" + ".";
2
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
    // ********************************
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    54
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    55
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    56
     * Returns an instance of {@code BasicToggleButtonUI}.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    57
     *
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    58
     * @param b a component
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    59
     * @return an instance of {@code BasicToggleButtonUI}
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    60
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static ComponentUI createUI(JComponent b) {
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    62
        AppContext appContext = AppContext.getAppContext();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    63
        BasicToggleButtonUI toggleButtonUI =
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    64
                (BasicToggleButtonUI) appContext.get(BASIC_TOGGLE_BUTTON_UI_KEY);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    65
        if (toggleButtonUI == null) {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    66
            toggleButtonUI = new BasicToggleButtonUI();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    67
            appContext.put(BASIC_TOGGLE_BUTTON_UI_KEY, toggleButtonUI);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    68
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return toggleButtonUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    protected String getPropertyPrefix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return propertyPrefix;
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
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    //          Paint Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        AbstractButton b = (AbstractButton) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        Dimension size = b.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        FontMetrics fm = g.getFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Insets i = c.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        Rectangle viewRect = new Rectangle(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        viewRect.x += i.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        viewRect.y += i.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        viewRect.width -= (i.right + viewRect.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        viewRect.height -= (i.bottom + viewRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        Rectangle iconRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Rectangle textRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        Font f = c.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        g.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // layout the text and icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        String text = SwingUtilities.layoutCompoundLabel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            c, fm, b.getText(), b.getIcon(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            b.getVerticalAlignment(), b.getHorizontalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            viewRect, iconRect, textRect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            b.getText() == null ? 0 : b.getIconTextGap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        g.setColor(b.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (model.isArmed() && model.isPressed() || model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            paintButtonPressed(g,b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // Paint the Icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if(b.getIcon() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            paintIcon(g, b, iconRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // Draw the Text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if(text != null && !text.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
               v.paint(g, textRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
               paintText(g, b, textRect, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // draw the dashed focus line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (b.isFocusPainted() && b.hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            paintFocus(g, b, viewRect, textRect, iconRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   137
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   138
     * Paints an icon in the specified location.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   139
     *
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   140
     * @param g an instance of {@code Graphics}
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   141
     * @param b an instance of {@code Button}
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   142
     * @param iconRect bounds of an icon
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   143
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        Icon icon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if(!model.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if(model.isSelected()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   150
               icon = b.getDisabledSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            } else {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   152
               icon = b.getDisabledIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } else if(model.isPressed() && model.isArmed()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   155
            icon = b.getPressedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if(icon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                // Use selected icon
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   158
                icon = b.getSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        } else if(model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if(b.isRolloverEnabled() && model.isRollover()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   162
                icon = b.getRolloverSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                if (icon == null) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   164
                    icon = b.getSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            } else {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   167
                icon = b.getSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } else if(b.isRolloverEnabled() && model.isRollover()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   170
            icon = b.getRolloverIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if(icon == null) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   174
            icon = b.getIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        icon.paintIcon(b, g, iconRect.x, iconRect.y);
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
     * Overriden so that the text will not be rendered as shifted for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Toggle buttons and subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    protected int getTextShiftOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
}