jdk/src/share/classes/javax/swing/plaf/basic/BasicRadioButtonUI.java
author darcy
Wed, 02 Jul 2014 23:03:27 -0700
changeset 25565 ce603b34c98d
parent 5506 202f599c92aa
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.text.View;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.swing.SwingUtilities2;
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    35
import sun.awt.AppContext;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * RadioButtonUI implementation for BasicRadioButtonUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Jeff Dinkins
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class BasicRadioButtonUI extends BasicToggleButtonUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
{
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    45
    private static final Object BASIC_RADIO_BUTTON_UI_KEY = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    protected Icon icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private boolean defaults_initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private final static String propertyPrefix = "RadioButton" + ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    //        Create PLAF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static ComponentUI createUI(JComponent b) {
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    57
        AppContext appContext = AppContext.getAppContext();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    58
        BasicRadioButtonUI radioButtonUI =
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    59
                (BasicRadioButtonUI) appContext.get(BASIC_RADIO_BUTTON_UI_KEY);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    60
        if (radioButtonUI == null) {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    61
            radioButtonUI = new BasicRadioButtonUI();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    62
            appContext.put(BASIC_RADIO_BUTTON_UI_KEY, radioButtonUI);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    63
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return radioButtonUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected String getPropertyPrefix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return propertyPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    //        Install PLAF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    protected void installDefaults(AbstractButton b){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        super.installDefaults(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if(!defaults_initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            icon = UIManager.getIcon(getPropertyPrefix() + "icon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            defaults_initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
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
    //        Uninstall PLAF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    protected void uninstallDefaults(AbstractButton b){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        super.uninstallDefaults(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        defaults_initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public Icon getDefaultIcon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return icon;
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
    /* These Dimensions/Rectangles are allocated once for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * RadioButtonUI.paint() calls.  Re-using rectangles
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * rather than allocating them in each paint call substantially
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * reduced the time it took paint to run.  Obviously, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * method can't be re-entered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static Dimension size = new Dimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static Rectangle viewRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private static Rectangle iconRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static Rectangle textRect = new Rectangle();
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 the radio button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public synchronized void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        AbstractButton b = (AbstractButton) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        Font f = c.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        g.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        Insets i = c.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        size = b.getSize(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        viewRect.x = i.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        viewRect.y = i.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        viewRect.width = size.width - (i.right + viewRect.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        viewRect.height = size.height - (i.bottom + viewRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        textRect.x = textRect.y = textRect.width = textRect.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        Icon altIcon = b.getIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        Icon selectedIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Icon disabledIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        String text = SwingUtilities.layoutCompoundLabel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            c, fm, b.getText(), altIcon != null ? altIcon : getDefaultIcon(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            b.getVerticalAlignment(), b.getHorizontalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            viewRect, iconRect, textRect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            b.getText() == null ? 0 : b.getIconTextGap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        // fill background
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if(c.isOpaque()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            g.setColor(b.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            g.fillRect(0,0, size.width, size.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // Paint the radio button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if(altIcon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if(!model.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                if(model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                   altIcon = b.getDisabledSelectedIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                   altIcon = b.getDisabledIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            } else if(model.isPressed() && model.isArmed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                altIcon = b.getPressedIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                if(altIcon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    // Use selected icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    altIcon = b.getSelectedIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            } else if(model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                if(b.isRolloverEnabled() && model.isRollover()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   161
                        altIcon = b.getRolloverSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        if (altIcon == null) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   163
                                altIcon = b.getSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                } else {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   166
                        altIcon = b.getSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            } else if(b.isRolloverEnabled() && model.isRollover()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   169
                altIcon = b.getRolloverIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if(altIcon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                altIcon = b.getIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            altIcon.paintIcon(c, g, iconRect.x, iconRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            getDefaultIcon().paintIcon(c, g, iconRect.x, iconRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // Draw the Text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if(text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                v.paint(g, textRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                paintText(g, b, textRect, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if(b.hasFocus() && b.isFocusPainted() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
               textRect.width > 0 && textRect.height > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                paintFocus(g, textRect, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    protected void paintFocus(Graphics g, Rectangle textRect, Dimension size){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /* These Insets/Rectangles are allocated once for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * RadioButtonUI.getPreferredSize() calls.  Re-using rectangles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * rather than allocating them in each call substantially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * reduced the time it took getPreferredSize() to run.  Obviously,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * this method can't be re-entered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private static Rectangle prefViewRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private static Rectangle prefIconRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private static Rectangle prefTextRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private static Insets prefInsets = new Insets(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * The preferred size of the radio button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public Dimension getPreferredSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if(c.getComponentCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        AbstractButton b = (AbstractButton) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        String text = b.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   225
        Icon buttonIcon = b.getIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if(buttonIcon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            buttonIcon = getDefaultIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        Font font = b.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        FontMetrics fm = b.getFontMetrics(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        prefViewRect.x = prefViewRect.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        prefViewRect.width = Short.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        prefViewRect.height = Short.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        prefIconRect.x = prefIconRect.y = prefIconRect.width = prefIconRect.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        prefTextRect.x = prefTextRect.y = prefTextRect.width = prefTextRect.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        SwingUtilities.layoutCompoundLabel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            c, fm, text, buttonIcon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            b.getVerticalAlignment(), b.getHorizontalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            prefViewRect, prefIconRect, prefTextRect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            text == null ? 0 : b.getIconTextGap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // find the union of the icon and text rects (from Rectangle.java)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        int x1 = Math.min(prefIconRect.x, prefTextRect.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        int x2 = Math.max(prefIconRect.x + prefIconRect.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                          prefTextRect.x + prefTextRect.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        int y1 = Math.min(prefIconRect.y, prefTextRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        int y2 = Math.max(prefIconRect.y + prefIconRect.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                          prefTextRect.y + prefTextRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        int width = x2 - x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        int height = y2 - y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        prefInsets = b.getInsets(prefInsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        width += prefInsets.left + prefInsets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        height += prefInsets.top + prefInsets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return new Dimension(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
}