jdk/src/share/classes/javax/swing/plaf/basic/BasicLabelUI.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1289 43d45f4159b6
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 sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.swing.UIAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.text.View;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.event.ActionEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.event.ActionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.event.KeyEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.Insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.awt.FontMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * A Windows L&F implementation of LabelUI.  This implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * is completely static, i.e. there's only one UIView implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * that's shared by all JLabel objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class BasicLabelUI extends LabelUI implements  PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    * The default <code>BasicLabelUI</code> instance. This field might
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    * not be used. To change the default instance use a subclass which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    * overrides the <code>createUI</code> method, and place that class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    * name in defaults table under the key "LabelUI".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    protected static BasicLabelUI labelUI = new BasicLabelUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final static BasicLabelUI SAFE_BASIC_LABEL_UI = new BasicLabelUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static void loadActionMap(LazyActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        map.put(new Actions(Actions.PRESS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        map.put(new Actions(Actions.RELEASE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Forwards the call to SwingUtilities.layoutCompoundLabel().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * This method is here so that a subclass could do Label specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * layout and to shorten the method name a little.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @see SwingUtilities#layoutCompoundLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    protected String layoutCL(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        JLabel label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        FontMetrics fontMetrics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        Icon icon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        Rectangle viewR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        Rectangle iconR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Rectangle textR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return SwingUtilities.layoutCompoundLabel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            (JComponent) label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            fontMetrics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            icon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            label.getVerticalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            label.getHorizontalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            label.getVerticalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            label.getHorizontalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            viewR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            iconR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            textR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            label.getIconTextGap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Paint clippedText at textX, textY with the labels foreground color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @see #paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @see #paintDisabledText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    protected void paintEnabledText(JLabel l, Graphics g, String s, int textX, int textY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        int mnemIndex = l.getDisplayedMnemonicIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        g.setColor(l.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        SwingUtilities2.drawStringUnderlineCharAt(l, g, s, mnemIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                                     textX, textY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Paint clippedText at textX, textY with background.lighter() and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * shifted down and to the right by one pixel with background.darker().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @see #paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @see #paintEnabledText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    protected void paintDisabledText(JLabel l, Graphics g, String s, int textX, int textY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int accChar = l.getDisplayedMnemonicIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        Color background = l.getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        g.setColor(background.brighter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        SwingUtilities2.drawStringUnderlineCharAt(l, g, s, accChar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                                   textX + 1, textY + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        g.setColor(background.darker());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        SwingUtilities2.drawStringUnderlineCharAt(l, g, s, accChar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                                   textX, textY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /* These rectangles/insets are allocated once for this shared LabelUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * implementation.  Re-using rectangles rather than allocating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * them in each paint call halved the time it took paint to run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static Rectangle paintIconR = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private static Rectangle paintTextR = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private static Rectangle paintViewR = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private static Insets paintViewInsets = new Insets(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Paint the label text in the foreground color, if the label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * is opaque then paint the entire background with the background
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * color.  The Label text is drawn by paintEnabledText() or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * paintDisabledText().  The locations of the label parts are computed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * by layoutCL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @see #paintEnabledText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @see #paintDisabledText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @see #layoutCL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public void paint(Graphics g, JComponent c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        JLabel label = (JLabel)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        String text = label.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if ((icon == null) && (text == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        FontMetrics fm = SwingUtilities2.getFontMetrics(label, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        String clippedText = layout(label, fm, c.getWidth(), c.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (icon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                v.paint(g, paintTextR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                int textX = paintTextR.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                int textY = paintTextR.y + fm.getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                if (label.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    paintEnabledText(label, g, clippedText, textX, textY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    paintDisabledText(label, g, clippedText, textX, textY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private String layout(JLabel label, FontMetrics fm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                          int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        Insets insets = label.getInsets(paintViewInsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        String text = label.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        Icon icon = (label.isEnabled()) ? label.getIcon() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                          label.getDisabledIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        paintViewR.x = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        paintViewR.y = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        paintViewR.width = width - (insets.left + insets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        paintViewR.height = height - (insets.top + insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return layoutCL(label, fm, text, icon, paintViewR, paintIconR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        paintTextR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /* These rectangles/insets are allocated once for this shared LabelUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * implementation.  Re-using rectangles rather than allocating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * them in each getPreferredSize call sped up the method substantially.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private static Rectangle iconR = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private static Rectangle textR = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    private static Rectangle viewR = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private static Insets viewInsets = new Insets(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public Dimension getPreferredSize(JComponent c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        JLabel label = (JLabel)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        String text = label.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        Icon icon = (label.isEnabled()) ? label.getIcon() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                          label.getDisabledIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        Insets insets = label.getInsets(viewInsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        Font font = label.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        int dx = insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        int dy = insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if ((icon == null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            ((text == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
             ((text != null) && (font == null)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return new Dimension(dx, dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        else if ((text == null) || ((icon != null) && (font == null))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            return new Dimension(icon.getIconWidth() + dx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                 icon.getIconHeight() + dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            FontMetrics fm = label.getFontMetrics(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            iconR.x = iconR.y = iconR.width = iconR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            textR.x = textR.y = textR.width = textR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            viewR.x = dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            viewR.y = dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            viewR.width = viewR.height = Short.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            layoutCL(label, fm, text, icon, viewR, iconR, textR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            int x1 = Math.min(iconR.x, textR.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            int x2 = Math.max(iconR.x + iconR.width, textR.x + textR.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            int y1 = Math.min(iconR.y, textR.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            int y2 = Math.max(iconR.y + iconR.height, textR.y + textR.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            Dimension rv = new Dimension(x2 - x1, y2 - y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            rv.width += dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            rv.height += dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @return getPreferredSize(c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public Dimension getMinimumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        Dimension d = getPreferredSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @return getPreferredSize(c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        Dimension d = getPreferredSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Returns the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        super.getBaseline(c, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        JLabel label = (JLabel)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        String text = label.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (text == null || "".equals(text) || label.getFont() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        FontMetrics fm = label.getFontMetrics(label.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        layout(label, fm, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return BasicHTML.getBaseline(label, paintTextR.y, fm.getAscent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                                     paintTextR.width, paintTextR.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Returns an enum indicating how the baseline of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * changes as the size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        super.getBaselineResizeBehavior(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (c.getClientProperty(BasicHTML.propertyKey) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            return Component.BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        switch(((JLabel)c).getVerticalAlignment()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        case JLabel.TOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        case JLabel.BOTTOM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            return Component.BaselineResizeBehavior.CONSTANT_DESCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        case JLabel.CENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            return Component.BaselineResizeBehavior.CENTER_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return Component.BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        installDefaults((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        installComponents((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        installListeners((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        installKeyboardActions((JLabel)c);
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
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        uninstallDefaults((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        uninstallComponents((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        uninstallListeners((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        uninstallKeyboardActions((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     protected void installDefaults(JLabel c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
         LookAndFeel.installColorsAndFont(c, "Label.background", "Label.foreground", "Label.font");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
         LookAndFeel.installProperty(c, "opaque", Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    protected void installListeners(JLabel c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        c.addPropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    protected void installComponents(JLabel c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        BasicHTML.updateRenderer(c, c.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        c.setInheritsPopupMenu(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    protected void installKeyboardActions(JLabel l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        int dka = l.getDisplayedMnemonic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        Component lf = l.getLabelFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if ((dka != 0) && (lf != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            LazyActionMap.installLazyActionMap(l, BasicLabelUI.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                                               "Label.actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            InputMap inputMap = SwingUtilities.getUIInputMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                            (l, JComponent.WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            if (inputMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                inputMap = new ComponentInputMapUIResource(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                SwingUtilities.replaceUIInputMap(l,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            inputMap.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            inputMap.put(KeyStroke.getKeyStroke(dka, ActionEvent.ALT_MASK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                                              false), "press");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            InputMap inputMap = SwingUtilities.getUIInputMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                            (l, JComponent.WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            if (inputMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                inputMap.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    protected void uninstallDefaults(JLabel c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    protected void uninstallListeners(JLabel c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        c.removePropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    protected void uninstallComponents(JLabel c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        BasicHTML.updateRenderer(c, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    protected void uninstallKeyboardActions(JLabel c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                       null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        SwingUtilities.replaceUIActionMap(c, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (System.getSecurityManager() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            return SAFE_BASIC_LABEL_UI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            return labelUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        String name = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (name == "text" || "font" == name || "foreground" == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            // remove the old html view client property if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            // existed, and install a new one if the text installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            // into the JLabel is html source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            JLabel lbl = ((JLabel) e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            String text = lbl.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            BasicHTML.updateRenderer(lbl, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        else if (name == "labelFor" || name == "displayedMnemonic") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            installKeyboardActions((JLabel) e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    // When the accelerator is pressed, temporarily make the JLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    // focusTraversable by registering a WHEN_FOCUSED action for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    // release of the accelerator.  Then give it focus so it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    // prevent unwanted keyTyped events from getting to other components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    private static class Actions extends UIAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        private static final String PRESS = "press";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        private static final String RELEASE = "release";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        Actions(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            super(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            JLabel label = (JLabel)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            String key = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            if (key == PRESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                doPress(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            else if (key == RELEASE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                doRelease(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        private void doPress(JLabel label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            Component labelFor = label.getLabelFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            if (labelFor != null && labelFor.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                if (inputMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    inputMap = new InputMapUIResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    SwingUtilities.replaceUIInputMap(label, JComponent.WHEN_FOCUSED, inputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                int dka = label.getDisplayedMnemonic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                inputMap.put(KeyStroke.getKeyStroke(dka, ActionEvent.ALT_MASK, true), RELEASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                // Need this if ALT is released before the accelerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true), RELEASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                label.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        private void doRelease(JLabel label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            Component labelFor = label.getLabelFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            if (labelFor != null && labelFor.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                if (inputMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    // inputMap should never be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    inputMap.remove(KeyStroke.getKeyStroke(label.getDisplayedMnemonic(), ActionEvent.ALT_MASK, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                if (labelFor instanceof Container &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                        ((Container) labelFor).isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    labelFor.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    SwingUtilities2.compositeRequestFocus(labelFor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
}