jdk/src/share/classes/javax/swing/plaf/basic/BasicButtonUI.java
author aeremeev
Thu, 10 Jul 2014 12:21:29 +0400
changeset 25763 51d1f910f68d
parent 23715 54ae9dd9df73
permissions -rw-r--r--
8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7 Reviewed-by: pchelko
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.swing.SwingUtilities2;
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1299
diff changeset
    29
import sun.awt.AppContext;
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1299
diff changeset
    30
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.plaf.ButtonUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.plaf.UIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.plaf.ComponentUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.swing.text.View;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * BasicButton implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Jeff Dinkins
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class BasicButtonUI extends ButtonUI{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // Visual constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // NOTE: This is not used or set any where. Were we allowed to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // fields, this would be removed.
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
    52
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
    53
     * The default gap between a text and an icon.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
    54
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    protected int defaultTextIconGap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // Amount to offset text, the value of this comes from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // defaultTextShiftOffset once setTextShiftOffset has been invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private int shiftOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // Value that is set in shiftOffset once setTextShiftOffset has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // invoked. The value of this comes from the defaults table.
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
    62
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
    63
     * The default offset of a text.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
    64
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    protected int defaultTextShiftOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final static String propertyPrefix = "Button" + ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1299
diff changeset
    69
    private static final Object BASIC_BUTTON_UI_KEY = new Object();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1299
diff changeset
    70
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    //          Create PLAF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // ********************************
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
    74
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
    75
     * Returns an instance of {@code BasicButtonUI}.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
    76
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
    77
     * @param c a component
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
    78
     * @return an instance of {@code BasicButtonUI}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
    79
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public static ComponentUI createUI(JComponent c) {
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1299
diff changeset
    81
        AppContext appContext = AppContext.getAppContext();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1299
diff changeset
    82
        BasicButtonUI buttonUI =
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1299
diff changeset
    83
                (BasicButtonUI) appContext.get(BASIC_BUTTON_UI_KEY);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1299
diff changeset
    84
        if (buttonUI == null) {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1299
diff changeset
    85
            buttonUI = new BasicButtonUI();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1299
diff changeset
    86
            appContext.put(BASIC_BUTTON_UI_KEY, buttonUI);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1299
diff changeset
    87
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return buttonUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
    91
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
    92
     * Returns the property prefix.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
    93
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
    94
     * @return the property prefix
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
    95
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    protected String getPropertyPrefix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return propertyPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    //          Install PLAF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        installDefaults((AbstractButton) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        installListeners((AbstractButton) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        installKeyboardActions((AbstractButton) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        BasicHTML.updateRenderer(c, ((AbstractButton) c).getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   111
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   112
     * Installs default properties.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   113
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   114
     * @param b an abstract button
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   115
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    protected void installDefaults(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // load shared instance defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        String pp = getPropertyPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        defaultTextShiftOffset = UIManager.getInt(pp + "textShiftOffset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // set the following defaults on the button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (b.isContentAreaFilled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            LookAndFeel.installProperty(b, "opaque", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            LookAndFeel.installProperty(b, "opaque", Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if(b.getMargin() == null || (b.getMargin() instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            b.setMargin(UIManager.getInsets(pp + "margin"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        LookAndFeel.installColorsAndFont(b, pp + "background",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                         pp + "foreground", pp + "font");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        LookAndFeel.installBorder(b, pp + "border");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        Object rollover = UIManager.get(pp + "rollover");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (rollover != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            LookAndFeel.installProperty(b, "rolloverEnabled", rollover);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   142
        LookAndFeel.installProperty(b, "iconTextGap", Integer.valueOf(4));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   145
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   146
     * Registers listeners.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   147
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   148
     * @param b an abstract button
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   149
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    protected void installListeners(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        BasicButtonListener listener = createButtonListener(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if(listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            b.addMouseListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            b.addMouseMotionListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            b.addFocusListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            b.addPropertyChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            b.addChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   161
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   162
     * Registers keyboard actions.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   163
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   164
     * @param b an abstract button
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   165
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    protected void installKeyboardActions(AbstractButton b){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        BasicButtonListener listener = getButtonListener(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if(listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            listener.installKeyboardActions(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    //         Uninstall PLAF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        uninstallKeyboardActions((AbstractButton) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        uninstallListeners((AbstractButton) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        uninstallDefaults((AbstractButton) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        BasicHTML.updateRenderer(c, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   185
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   186
     * Unregisters keyboard actions.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   187
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   188
     * @param b an abstract button
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   189
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    protected void uninstallKeyboardActions(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        BasicButtonListener listener = getButtonListener(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if(listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            listener.uninstallKeyboardActions(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   197
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   198
     * Unregisters listeners.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   199
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   200
     * @param b an abstract button
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   201
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    protected void uninstallListeners(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        BasicButtonListener listener = getButtonListener(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if(listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            b.removeMouseListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            b.removeMouseMotionListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            b.removeFocusListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            b.removeChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            b.removePropertyChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   213
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   214
     * Uninstalls default properties.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   215
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   216
     * @param b an abstract button
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   217
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    protected void uninstallDefaults(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        LookAndFeel.uninstallBorder(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    //        Create Listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    // ********************************
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   225
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   226
     * Returns a new instance of {@code BasicButtonListener}.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   227
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   228
     * @param b an abstract button
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   229
     * @return a new instance of {@code BasicButtonListener}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   230
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    protected BasicButtonListener createButtonListener(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return new BasicButtonListener(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   235
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   236
     * Returns the default gap between a text and an icon.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   237
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   238
     * @param b an abstract button
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   239
     * @return the default gap between text and an icon
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   240
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public int getDefaultTextIconGap(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return defaultTextIconGap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /* These rectangles/insets are allocated once for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * ButtonUI.paint() calls.  Re-using rectangles rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * allocating them in each paint call substantially reduced the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * it took paint to run.  Obviously, this method can't be re-entered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private static Rectangle viewRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private static Rectangle textRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private static Rectangle iconRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    //          Paint Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public void paint(Graphics g, JComponent c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        AbstractButton b = (AbstractButton) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        String text = layout(b, SwingUtilities2.getFontMetrics(b, g),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
               b.getWidth(), b.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        clearTextShiftOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // perform UI specific press action, e.g. Windows L&F shifts text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (model.isArmed() && model.isPressed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            paintButtonPressed(g,b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // Paint the Icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if(b.getIcon() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            paintIcon(g,c,iconRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (text != null && !text.equals("")){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                v.paint(g, textRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                paintText(g, b, textRect, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (b.isFocusPainted() && b.hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            // paint UI specific focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            paintFocus(g,b,viewRect,textRect,iconRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   293
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   294
     * Paints an icon of the current button.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   295
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   296
     * @param g an instance of {@code Graphics}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   297
     * @param c a component
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   298
     * @param iconRect a bounding rectangle to render the icon
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   299
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            AbstractButton b = (AbstractButton) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            Icon icon = b.getIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            Icon tmpIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if(icon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
               return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            Icon selectedIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            /* the fallback icon should be based on the selected state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (model.isSelected()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 438
diff changeset
   314
                selectedIcon = b.getSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                if (selectedIcon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    icon = selectedIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if(!model.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                if(model.isSelected()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 438
diff changeset
   322
                   tmpIcon = b.getDisabledSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                   if (tmpIcon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                       tmpIcon = selectedIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                if (tmpIcon == null) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 438
diff changeset
   329
                    tmpIcon = b.getDisabledIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            } else if(model.isPressed() && model.isArmed()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 438
diff changeset
   332
                tmpIcon = b.getPressedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                if(tmpIcon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    // revert back to 0 offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    clearTextShiftOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            } else if(b.isRolloverEnabled() && model.isRollover()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                if(model.isSelected()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 438
diff changeset
   339
                   tmpIcon = b.getRolloverSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                   if (tmpIcon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                       tmpIcon = selectedIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                if (tmpIcon == null) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 438
diff changeset
   346
                    tmpIcon = b.getRolloverIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            if(tmpIcon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                icon = tmpIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if(model.isPressed() && model.isArmed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                icon.paintIcon(c, g, iconRect.x + getTextShiftOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        iconRect.y + getTextShiftOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                icon.paintIcon(c, g, iconRect.x, iconRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   364
     * Method which renders the text of the current button.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   365
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * As of Java 2 platform v 1.4 this method should not be used or overriden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * Use the paintText method which takes the AbstractButton argument.
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   368
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   369
     * @param g an instance of {@code Graphics}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   370
     * @param c a component
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   371
     * @param textRect a bounding rectangle to render the text
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   372
     * @param text a string to render
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        AbstractButton b = (AbstractButton) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        int mnemonicIndex = b.getDisplayedMnemonicIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        /* Draw the Text */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if(model.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            /*** paint the text normally */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            g.setColor(b.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            SwingUtilities2.drawStringUnderlineCharAt(c, g,text, mnemonicIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                          textRect.x + getTextShiftOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                          textRect.y + fm.getAscent() + getTextShiftOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            /*** paint the text disabled ***/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            g.setColor(b.getBackground().brighter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            SwingUtilities2.drawStringUnderlineCharAt(c, g,text, mnemonicIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                                          textRect.x, textRect.y + fm.getAscent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            g.setColor(b.getBackground().darker());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            SwingUtilities2.drawStringUnderlineCharAt(c, g,text, mnemonicIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                          textRect.x - 1, textRect.y + fm.getAscent() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Method which renders the text of the current button.
23715
54ae9dd9df73 8039074: Tidy warnings cleanup for javax.swing
yan
parents: 5506
diff changeset
   401
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @param g Graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @param b Current button to render
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   404
     * @param textRect Bounding rectangle to render the text
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @param text String to render
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        paintText(g, (JComponent)b, textRect, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    // Method signature defined here overriden in subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    // Perhaps this class should be abstract?
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   414
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   415
     * Paints a focused button.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   416
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   417
     * @param g an instance of {@code Graphics}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   418
     * @param b an abstract button
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   419
     * @param viewRect a bounding rectangle to render the button
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   420
     * @param textRect a bounding rectangle to render the text
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   421
     * @param iconRect a bounding rectangle to render the icon
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   422
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    protected void paintFocus(Graphics g, AbstractButton b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                              Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   428
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   429
     * Paints a pressed button.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   430
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   431
     * @param g an instance of {@code Graphics}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   432
     * @param b an abstract button
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   433
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    protected void paintButtonPressed(Graphics g, AbstractButton b){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   437
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   438
     * Clears the offset of the text.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   439
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    protected void clearTextShiftOffset(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        this.shiftOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   444
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   445
     * Sets the offset of the text.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   446
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    protected void setTextShiftOffset(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        this.shiftOffset = defaultTextShiftOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   451
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   452
     * Returns the offset of the text.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   453
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   454
     * @return the offset of the text
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23715
diff changeset
   455
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    protected int getTextShiftOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        return shiftOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    //          Layout Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public Dimension getMinimumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        Dimension d = getPreferredSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    public Dimension getPreferredSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        AbstractButton b = (AbstractButton)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        return BasicGraphicsUtils.getPreferredButtonSize(b, b.getIconTextGap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        Dimension d = getPreferredSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * Returns the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        super.getBaseline(c, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        AbstractButton b = (AbstractButton)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        String text = b.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        if (text == null || "".equals(text)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        FontMetrics fm = b.getFontMetrics(b.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        layout(b, fm, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        return BasicHTML.getBaseline(b, textRect.y, fm.getAscent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                                     textRect.width, textRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * Returns an enum indicating how the baseline of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * changes as the size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        super.getBaselineResizeBehavior(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        if (c.getClientProperty(BasicHTML.propertyKey) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            return Component.BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        switch(((AbstractButton)c).getVerticalAlignment()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        case AbstractButton.TOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        case AbstractButton.BOTTOM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            return Component.BaselineResizeBehavior.CONSTANT_DESCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        case AbstractButton.CENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            return Component.BaselineResizeBehavior.CENTER_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        return Component.BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    private String layout(AbstractButton b, FontMetrics fm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                          int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        Insets i = b.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        viewRect.x = i.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        viewRect.y = i.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        viewRect.width = width - (i.right + viewRect.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        viewRect.height = height - (i.bottom + viewRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        textRect.x = textRect.y = textRect.width = textRect.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        // layout the text and icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        return SwingUtilities.layoutCompoundLabel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            b, fm, b.getText(), b.getIcon(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            b.getVerticalAlignment(), b.getHorizontalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            viewRect, iconRect, textRect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            b.getText() == null ? 0 : b.getIconTextGap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * Returns the ButtonListener for the passed in Button, or null if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * could not be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    private BasicButtonListener getButtonListener(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        MouseMotionListener[] listeners = b.getMouseMotionListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        if (listeners != null) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 438
diff changeset
   560
            for (MouseMotionListener listener : listeners) {
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 438
diff changeset
   561
                if (listener instanceof BasicButtonListener) {
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 438
diff changeset
   562
                    return (BasicButtonListener) listener;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
}