jdk/src/share/classes/javax/swing/plaf/basic/BasicLabelUI.java
author michaelm
Tue, 06 Mar 2012 20:34:38 +0000
changeset 12047 320a714614e9
parent 9217 f795ef267353
child 20169 d7fa6d7586c9
permissions -rw-r--r--
7113349: Initial changeset for Macosx port to jdk Reviewed-by: jjh, alanb, dholmes, anthony, ohrstrom, ksrini, jrose, weijun, smarks Contributed-by: Alan Bateman <alan.bateman@oracle.com>, Alexander Potochkin <alexander.potochkin@oracle.com>, Alexander Zuev <alexander.zuev@oracle.com>, Andrew Brygin <andrew.brygin@oracle.com>, Artem Ananiev <artem.ananiev@oracle.com>, Alex Strange <astrange@apple.com>, Bino George <bino@apple.com>, Christine Lu <christine.lu@oracle.com>, David Katleman <david.katleman@oracle.com>, David Durrence <david_durrence@apple.com>, Dmitry Cherepanov <dmitry.cherepanov@oracle.com>, Greg Lewis <glewis@eyesbeyond.com>, Kevin Miller <kevin_m_miller@apple.com>, Kurt Miller <kurt@intricatesoftware.com>, Landon Fuller <landonf@plausiblelabs.com>, Leonid Romanov <leonid.romanov@oracle.com>, Loefty Walkowiak <loefty@apple.com>, Mark Reinhold <mark.reinhold@oracle.com>, Naoto Sato <naoto.sato@oracle.com>, Philip Race <philip.race@oracle.com>, Roger Hoover <rhoover@apple.com>, Scott Kovatch <scott.kovatch@oracle.com>, Sergey ByloKhov <sergey.bylokhov@oracle.com>, Mike Swingler <swingler@apple.com>, Tomas Hurka <tomas.hurka@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
     2
 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.swing.UIAction;
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    31
import sun.awt.AppContext;
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    32
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.text.View;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.event.ActionEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.event.ActionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.event.KeyEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.Insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.awt.FontMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * A Windows L&F implementation of LabelUI.  This implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * is completely static, i.e. there's only one UIView implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * that's shared by all JLabel objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class BasicLabelUI extends LabelUI implements  PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    * The default <code>BasicLabelUI</code> instance. This field might
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    * not be used. To change the default instance use a subclass which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    * overrides the <code>createUI</code> method, and place that class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    * name in defaults table under the key "LabelUI".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected static BasicLabelUI labelUI = new BasicLabelUI();
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
    68
    private static final Object BASIC_LABEL_UI_KEY = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
1289
43d45f4159b6 6727829: BasicLabelUI rendering fails if multiple AppContexts exist in same java JVM
mlapshin
parents: 2
diff changeset
    70
    private Rectangle paintIconR = new Rectangle();
43d45f4159b6 6727829: BasicLabelUI rendering fails if multiple AppContexts exist in same java JVM
mlapshin
parents: 2
diff changeset
    71
    private Rectangle paintTextR = new Rectangle();
43d45f4159b6 6727829: BasicLabelUI rendering fails if multiple AppContexts exist in same java JVM
mlapshin
parents: 2
diff changeset
    72
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static void loadActionMap(LazyActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        map.put(new Actions(Actions.PRESS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        map.put(new Actions(Actions.RELEASE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Forwards the call to SwingUtilities.layoutCompoundLabel().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * This method is here so that a subclass could do Label specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * layout and to shorten the method name a little.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @see SwingUtilities#layoutCompoundLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    protected String layoutCL(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        JLabel label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        FontMetrics fontMetrics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        Icon icon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        Rectangle viewR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        Rectangle iconR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        Rectangle textR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return SwingUtilities.layoutCompoundLabel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            (JComponent) label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            fontMetrics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            icon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            label.getVerticalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            label.getHorizontalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            label.getVerticalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            label.getHorizontalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            viewR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            iconR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            textR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            label.getIconTextGap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Paint clippedText at textX, textY with the labels foreground color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see #paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @see #paintDisabledText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    protected void paintEnabledText(JLabel l, Graphics g, String s, int textX, int textY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        int mnemIndex = l.getDisplayedMnemonicIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        g.setColor(l.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        SwingUtilities2.drawStringUnderlineCharAt(l, g, s, mnemIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                                     textX, textY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Paint clippedText at textX, textY with background.lighter() and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * shifted down and to the right by one pixel with background.darker().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @see #paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @see #paintEnabledText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    protected void paintDisabledText(JLabel l, Graphics g, String s, int textX, int textY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        int accChar = l.getDisplayedMnemonicIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Color background = l.getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        g.setColor(background.brighter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        SwingUtilities2.drawStringUnderlineCharAt(l, g, s, accChar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                                   textX + 1, textY + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        g.setColor(background.darker());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        SwingUtilities2.drawStringUnderlineCharAt(l, g, s, accChar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                                   textX, textY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 4225
diff changeset
   144
     * Paints the label text with the foreground color, if the label is opaque
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 4225
diff changeset
   145
     * then paints the entire background with the background color. The Label
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 4225
diff changeset
   146
     * text is drawn by {@link #paintEnabledText} or {@link #paintDisabledText}.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 4225
diff changeset
   147
     * The locations of the label parts are computed by {@link #layoutCL}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @see #paintEnabledText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @see #paintDisabledText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @see #layoutCL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public void paint(Graphics g, JComponent c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        JLabel label = (JLabel)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        String text = label.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if ((icon == null) && (text == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        FontMetrics fm = SwingUtilities2.getFontMetrics(label, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        String clippedText = layout(label, fm, c.getWidth(), c.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (icon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                v.paint(g, paintTextR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                int textX = paintTextR.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                int textY = paintTextR.y + fm.getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                if (label.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    paintEnabledText(label, g, clippedText, textX, textY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    paintDisabledText(label, g, clippedText, textX, textY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private String layout(JLabel label, FontMetrics fm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                          int width, int height) {
1289
43d45f4159b6 6727829: BasicLabelUI rendering fails if multiple AppContexts exist in same java JVM
mlapshin
parents: 2
diff changeset
   190
        Insets insets = label.getInsets(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        String text = label.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        Icon icon = (label.isEnabled()) ? label.getIcon() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                          label.getDisabledIcon();
1289
43d45f4159b6 6727829: BasicLabelUI rendering fails if multiple AppContexts exist in same java JVM
mlapshin
parents: 2
diff changeset
   194
        Rectangle paintViewR = new Rectangle();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        paintViewR.x = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        paintViewR.y = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        paintViewR.width = width - (insets.left + insets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        paintViewR.height = height - (insets.top + insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return layoutCL(label, fm, text, icon, paintViewR, paintIconR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        paintTextR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public Dimension getPreferredSize(JComponent c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        JLabel label = (JLabel)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        String text = label.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        Icon icon = (label.isEnabled()) ? label.getIcon() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                          label.getDisabledIcon();
1289
43d45f4159b6 6727829: BasicLabelUI rendering fails if multiple AppContexts exist in same java JVM
mlapshin
parents: 2
diff changeset
   211
        Insets insets = label.getInsets(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        Font font = label.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        int dx = insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        int dy = insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if ((icon == null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            ((text == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
             ((text != null) && (font == null)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return new Dimension(dx, dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        else if ((text == null) || ((icon != null) && (font == null))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return new Dimension(icon.getIconWidth() + dx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                 icon.getIconHeight() + dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            FontMetrics fm = label.getFontMetrics(font);
1289
43d45f4159b6 6727829: BasicLabelUI rendering fails if multiple AppContexts exist in same java JVM
mlapshin
parents: 2
diff changeset
   228
            Rectangle iconR = new Rectangle();
43d45f4159b6 6727829: BasicLabelUI rendering fails if multiple AppContexts exist in same java JVM
mlapshin
parents: 2
diff changeset
   229
            Rectangle textR = new Rectangle();
43d45f4159b6 6727829: BasicLabelUI rendering fails if multiple AppContexts exist in same java JVM
mlapshin
parents: 2
diff changeset
   230
            Rectangle viewR = new Rectangle();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            iconR.x = iconR.y = iconR.width = iconR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            textR.x = textR.y = textR.width = textR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            viewR.x = dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            viewR.y = dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            viewR.width = viewR.height = Short.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            layoutCL(label, fm, text, icon, viewR, iconR, textR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            int x1 = Math.min(iconR.x, textR.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            int x2 = Math.max(iconR.x + iconR.width, textR.x + textR.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            int y1 = Math.min(iconR.y, textR.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            int y2 = Math.max(iconR.y + iconR.height, textR.y + textR.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            Dimension rv = new Dimension(x2 - x1, y2 - y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            rv.width += dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            rv.height += dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @return getPreferredSize(c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public Dimension getMinimumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        Dimension d = getPreferredSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return d;
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
     * @return getPreferredSize(c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        Dimension d = getPreferredSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Returns the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        super.getBaseline(c, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        JLabel label = (JLabel)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        String text = label.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (text == null || "".equals(text) || label.getFont() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        FontMetrics fm = label.getFontMetrics(label.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        layout(label, fm, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return BasicHTML.getBaseline(label, paintTextR.y, fm.getAscent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                     paintTextR.width, paintTextR.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Returns an enum indicating how the baseline of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * changes as the size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        super.getBaselineResizeBehavior(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (c.getClientProperty(BasicHTML.propertyKey) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return Component.BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        switch(((JLabel)c).getVerticalAlignment()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        case JLabel.TOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        case JLabel.BOTTOM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return Component.BaselineResizeBehavior.CONSTANT_DESCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        case JLabel.CENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            return Component.BaselineResizeBehavior.CENTER_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return Component.BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        installDefaults((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        installComponents((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        installListeners((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        installKeyboardActions((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        uninstallDefaults((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        uninstallComponents((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        uninstallListeners((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        uninstallKeyboardActions((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     protected void installDefaults(JLabel c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         LookAndFeel.installColorsAndFont(c, "Label.background", "Label.foreground", "Label.font");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
         LookAndFeel.installProperty(c, "opaque", Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    protected void installListeners(JLabel c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        c.addPropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    protected void installComponents(JLabel c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        BasicHTML.updateRenderer(c, c.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        c.setInheritsPopupMenu(true);
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 installKeyboardActions(JLabel l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        int dka = l.getDisplayedMnemonic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        Component lf = l.getLabelFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if ((dka != 0) && (lf != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            LazyActionMap.installLazyActionMap(l, BasicLabelUI.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                               "Label.actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            InputMap inputMap = SwingUtilities.getUIInputMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                            (l, JComponent.WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            if (inputMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                inputMap = new ComponentInputMapUIResource(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                SwingUtilities.replaceUIInputMap(l,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            inputMap.clear();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9217
diff changeset
   366
            inputMap.put(KeyStroke.getKeyStroke(dka, BasicLookAndFeel.getFocusAcceleratorKeyMask(), false), "press");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            InputMap inputMap = SwingUtilities.getUIInputMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                            (l, JComponent.WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            if (inputMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                inputMap.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    protected void uninstallDefaults(JLabel c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    protected void uninstallListeners(JLabel c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        c.removePropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    protected void uninstallComponents(JLabel c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        BasicHTML.updateRenderer(c, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    protected void uninstallKeyboardActions(JLabel c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                       null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        SwingUtilities.replaceUIActionMap(c, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (System.getSecurityManager() != null) {
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   397
            AppContext appContext = AppContext.getAppContext();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   398
            BasicLabelUI safeBasicLabelUI =
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   399
                    (BasicLabelUI) appContext.get(BASIC_LABEL_UI_KEY);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   400
            if (safeBasicLabelUI == null) {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   401
                safeBasicLabelUI = new BasicLabelUI();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   402
                appContext.put(BASIC_LABEL_UI_KEY, safeBasicLabelUI);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   403
            }
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   404
            return safeBasicLabelUI;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   406
        return labelUI;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        String name = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (name == "text" || "font" == name || "foreground" == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            // remove the old html view client property if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            // existed, and install a new one if the text installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            // into the JLabel is html source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            JLabel lbl = ((JLabel) e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            String text = lbl.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            BasicHTML.updateRenderer(lbl, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        else if (name == "labelFor" || name == "displayedMnemonic") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            installKeyboardActions((JLabel) e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    // When the accelerator is pressed, temporarily make the JLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    // focusTraversable by registering a WHEN_FOCUSED action for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    // release of the accelerator.  Then give it focus so it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    // prevent unwanted keyTyped events from getting to other components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    private static class Actions extends UIAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        private static final String PRESS = "press";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        private static final String RELEASE = "release";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        Actions(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            super(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            JLabel label = (JLabel)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            String key = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            if (key == PRESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                doPress(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            else if (key == RELEASE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                doRelease(label);
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
        private void doPress(JLabel label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            Component labelFor = label.getLabelFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            if (labelFor != null && labelFor.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                if (inputMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    inputMap = new InputMapUIResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    SwingUtilities.replaceUIInputMap(label, JComponent.WHEN_FOCUSED, inputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                int dka = label.getDisplayedMnemonic();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9217
diff changeset
   456
                inputMap.put(KeyStroke.getKeyStroke(dka, BasicLookAndFeel.getFocusAcceleratorKeyMask(), true), RELEASE);
9217
f795ef267353 6596966: Some JFileChooser mnemonics do not work with sticky keys
rupashka
parents: 5506
diff changeset
   457
                // Need this when the sticky keys are enabled
f795ef267353 6596966: Some JFileChooser mnemonics do not work with sticky keys
rupashka
parents: 5506
diff changeset
   458
                inputMap.put(KeyStroke.getKeyStroke(dka, 0, true), RELEASE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                // Need this if ALT is released before the accelerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true), RELEASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                label.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        private void doRelease(JLabel label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            Component labelFor = label.getLabelFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            if (labelFor != null && labelFor.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                if (inputMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    // inputMap should never be null.
9217
f795ef267353 6596966: Some JFileChooser mnemonics do not work with sticky keys
rupashka
parents: 5506
diff changeset
   471
                    int dka = label.getDisplayedMnemonic();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9217
diff changeset
   472
                    inputMap.remove(KeyStroke.getKeyStroke(dka, BasicLookAndFeel.getFocusAcceleratorKeyMask(), true));
9217
f795ef267353 6596966: Some JFileChooser mnemonics do not work with sticky keys
rupashka
parents: 5506
diff changeset
   473
                    inputMap.remove(KeyStroke.getKeyStroke(dka, 0, true));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                if (labelFor instanceof Container &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                        ((Container) labelFor).isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    labelFor.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    SwingUtilities2.compositeRequestFocus(labelFor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
}