jdk/src/share/classes/javax/swing/plaf/basic/BasicButtonUI.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 438 2ae294e4518c
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.plaf.ButtonUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.plaf.UIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.plaf.ComponentUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.text.View;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * BasicButton implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Jeff Dinkins
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class BasicButtonUI extends ButtonUI{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // Shared UI object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private final static BasicButtonUI buttonUI = new BasicButtonUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // Visual constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // NOTE: This is not used or set any where. Were we allowed to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // fields, this would be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    protected int defaultTextIconGap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // Amount to offset text, the value of this comes from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // defaultTextShiftOffset once setTextShiftOffset has been invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private int shiftOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // Value that is set in shiftOffset once setTextShiftOffset has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // invoked. The value of this comes from the defaults table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected int defaultTextShiftOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private final static String propertyPrefix = "Button" + ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    //          Create PLAF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return buttonUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected String getPropertyPrefix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return propertyPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
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
    //          Install PLAF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        installDefaults((AbstractButton) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        installListeners((AbstractButton) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        installKeyboardActions((AbstractButton) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        BasicHTML.updateRenderer(c, ((AbstractButton) c).getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected void installDefaults(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // load shared instance defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        String pp = getPropertyPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        defaultTextShiftOffset = UIManager.getInt(pp + "textShiftOffset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // set the following defaults on the button
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (b.isContentAreaFilled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            LookAndFeel.installProperty(b, "opaque", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            LookAndFeel.installProperty(b, "opaque", Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if(b.getMargin() == null || (b.getMargin() instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            b.setMargin(UIManager.getInsets(pp + "margin"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        LookAndFeel.installColorsAndFont(b, pp + "background",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                         pp + "foreground", pp + "font");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        LookAndFeel.installBorder(b, pp + "border");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        Object rollover = UIManager.get(pp + "rollover");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (rollover != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            LookAndFeel.installProperty(b, "rolloverEnabled", rollover);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        LookAndFeel.installProperty(b, "iconTextGap", new Integer(4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    protected void installListeners(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        BasicButtonListener listener = createButtonListener(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if(listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            b.addMouseListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            b.addMouseMotionListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            b.addFocusListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            b.addPropertyChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            b.addChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    protected void installKeyboardActions(AbstractButton b){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        BasicButtonListener listener = getButtonListener(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if(listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            listener.installKeyboardActions(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    //         Uninstall PLAF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        uninstallKeyboardActions((AbstractButton) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        uninstallListeners((AbstractButton) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        uninstallDefaults((AbstractButton) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        BasicHTML.updateRenderer(c, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    protected void uninstallKeyboardActions(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        BasicButtonListener listener = getButtonListener(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if(listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            listener.uninstallKeyboardActions(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    protected void uninstallListeners(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        BasicButtonListener listener = getButtonListener(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if(listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            b.removeMouseListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            b.removeMouseMotionListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            b.removeFocusListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            b.removeChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            b.removePropertyChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    protected void uninstallDefaults(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        LookAndFeel.uninstallBorder(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    //        Create Listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    protected BasicButtonListener createButtonListener(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return new BasicButtonListener(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public int getDefaultTextIconGap(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return defaultTextIconGap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /* These rectangles/insets are allocated once for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * ButtonUI.paint() calls.  Re-using rectangles rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * allocating them in each paint call substantially reduced the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * it took paint to run.  Obviously, this method can't be re-entered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private static Rectangle viewRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private static Rectangle textRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private static Rectangle iconRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    //          Paint Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public void paint(Graphics g, JComponent c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        AbstractButton b = (AbstractButton) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        String text = layout(b, SwingUtilities2.getFontMetrics(b, g),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
               b.getWidth(), b.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        clearTextShiftOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        // perform UI specific press action, e.g. Windows L&F shifts text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (model.isArmed() && model.isPressed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            paintButtonPressed(g,b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        // Paint the Icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if(b.getIcon() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            paintIcon(g,c,iconRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (text != null && !text.equals("")){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                v.paint(g, textRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                paintText(g, b, textRect, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (b.isFocusPainted() && b.hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            // paint UI specific focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            paintFocus(g,b,viewRect,textRect,iconRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            AbstractButton b = (AbstractButton) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            Icon icon = b.getIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            Icon tmpIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if(icon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
               return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            Icon selectedIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            /* the fallback icon should be based on the selected state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                selectedIcon = (Icon) b.getSelectedIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                if (selectedIcon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    icon = selectedIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if(!model.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                if(model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                   tmpIcon = (Icon) b.getDisabledSelectedIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                   if (tmpIcon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                       tmpIcon = selectedIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                if (tmpIcon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    tmpIcon = (Icon) b.getDisabledIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            } else if(model.isPressed() && model.isArmed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                tmpIcon = (Icon) b.getPressedIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                if(tmpIcon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    // revert back to 0 offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    clearTextShiftOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            } else if(b.isRolloverEnabled() && model.isRollover()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                if(model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                   tmpIcon = (Icon) b.getRolloverSelectedIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                   if (tmpIcon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                       tmpIcon = selectedIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                if (tmpIcon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    tmpIcon = (Icon) b.getRolloverIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if(tmpIcon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                icon = tmpIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if(model.isPressed() && model.isArmed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                icon.paintIcon(c, g, iconRect.x + getTextShiftOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        iconRect.y + getTextShiftOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                icon.paintIcon(c, g, iconRect.x, iconRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * As of Java 2 platform v 1.4 this method should not be used or overriden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Use the paintText method which takes the AbstractButton argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        AbstractButton b = (AbstractButton) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        int mnemonicIndex = b.getDisplayedMnemonicIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        /* Draw the Text */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if(model.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            /*** paint the text normally */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            g.setColor(b.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            SwingUtilities2.drawStringUnderlineCharAt(c, g,text, mnemonicIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                          textRect.x + getTextShiftOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                          textRect.y + fm.getAscent() + getTextShiftOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            /*** paint the text disabled ***/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            g.setColor(b.getBackground().brighter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            SwingUtilities2.drawStringUnderlineCharAt(c, g,text, mnemonicIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                          textRect.x, textRect.y + fm.getAscent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            g.setColor(b.getBackground().darker());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            SwingUtilities2.drawStringUnderlineCharAt(c, g,text, mnemonicIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                                          textRect.x - 1, textRect.y + fm.getAscent() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Method which renders the text of the current button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param g Graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @param b Current button to render
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @param textRect Bounding rectangle to render the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @param text String to render
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        paintText(g, (JComponent)b, textRect, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    // Method signature defined here overriden in subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    // Perhaps this class should be abstract?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    protected void paintFocus(Graphics g, AbstractButton b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                              Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    protected void paintButtonPressed(Graphics g, AbstractButton b){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    protected void clearTextShiftOffset(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        this.shiftOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    protected void setTextShiftOffset(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        this.shiftOffset = defaultTextShiftOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    protected int getTextShiftOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return shiftOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    //          Layout Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public Dimension getMinimumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        Dimension d = getPreferredSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public Dimension getPreferredSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        AbstractButton b = (AbstractButton)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return BasicGraphicsUtils.getPreferredButtonSize(b, b.getIconTextGap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        Dimension d = getPreferredSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Returns the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        super.getBaseline(c, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        AbstractButton b = (AbstractButton)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        String text = b.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (text == null || "".equals(text)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        FontMetrics fm = b.getFontMetrics(b.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        layout(b, fm, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return BasicHTML.getBaseline(b, textRect.y, fm.getAscent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                                     textRect.width, textRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Returns an enum indicating how the baseline of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * changes as the size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        super.getBaselineResizeBehavior(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (c.getClientProperty(BasicHTML.propertyKey) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            return Component.BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        switch(((AbstractButton)c).getVerticalAlignment()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        case AbstractButton.TOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        case AbstractButton.BOTTOM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            return Component.BaselineResizeBehavior.CONSTANT_DESCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        case AbstractButton.CENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            return Component.BaselineResizeBehavior.CENTER_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return Component.BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    private String layout(AbstractButton b, FontMetrics fm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                          int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        Insets i = b.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        viewRect.x = i.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        viewRect.y = i.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        viewRect.width = width - (i.right + viewRect.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        viewRect.height = height - (i.bottom + viewRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        textRect.x = textRect.y = textRect.width = textRect.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        // layout the text and icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        return SwingUtilities.layoutCompoundLabel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            b, fm, b.getText(), b.getIcon(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            b.getVerticalAlignment(), b.getHorizontalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            viewRect, iconRect, textRect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            b.getText() == null ? 0 : b.getIconTextGap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Returns the ButtonListener for the passed in Button, or null if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * could not be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    private BasicButtonListener getButtonListener(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        MouseMotionListener[] listeners = b.getMouseMotionListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (listeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            for (int counter = 0; counter < listeners.length; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                if (listeners[counter] instanceof BasicButtonListener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    return (BasicButtonListener)listeners[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
}