jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicLabelUI.java
author weijun
Thu, 11 Dec 2014 15:19:12 +0800
changeset 28228 be83f404724d
parent 25859 3317bb8137f4
child 30939 59e64fa7aec2
permissions -rw-r--r--
8067207: Replace concat String to append in StringBuilder parameters (client) Reviewed-by: serb Contributed-by: Otavio Santana <otaviojava@java.net>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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
/**
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 12047
diff changeset
    53
 * A Windows L&amp;F implementation of LabelUI.  This implementation
2
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
     *
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
    83
     * @param label an instance of {@code JLabel}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
    84
     * @param fontMetrics a font metrics
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
    85
     * @param text a text
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
    86
     * @param icon an icon
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
    87
     * @param viewR a bounding rectangle to lay out label
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
    88
     * @param iconR a bounding rectangle to lay out icon
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
    89
     * @param textR a bounding rectangle to lay out text
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
    90
     * @return a possibly clipped version of the compound labels string
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @see SwingUtilities#layoutCompoundLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    protected String layoutCL(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        JLabel label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        FontMetrics fontMetrics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Icon icon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        Rectangle viewR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        Rectangle iconR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        Rectangle textR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return SwingUtilities.layoutCompoundLabel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            (JComponent) label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            fontMetrics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            icon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            label.getVerticalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            label.getHorizontalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            label.getVerticalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            label.getHorizontalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            viewR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            iconR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            textR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            label.getIconTextGap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Paint clippedText at textX, textY with the labels foreground color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   120
     * @param l an instance of {@code JLabel}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   121
     * @param g an instance of {@code Graphics}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   122
     * @param s a text
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   123
     * @param textX an X coordinate
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   124
     * @param textY an Y coordinate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @see #paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @see #paintDisabledText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    protected void paintEnabledText(JLabel l, Graphics g, String s, int textX, int textY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        int mnemIndex = l.getDisplayedMnemonicIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        g.setColor(l.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        SwingUtilities2.drawStringUnderlineCharAt(l, g, s, mnemIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                                     textX, textY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Paint clippedText at textX, textY with background.lighter() and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * shifted down and to the right by one pixel with background.darker().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   141
     * @param l an instance of {@code JLabel}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   142
     * @param g an instance of {@code Graphics}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   143
     * @param s a text
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   144
     * @param textX an X coordinate
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   145
     * @param textY an Y coordinate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @see #paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @see #paintEnabledText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    protected void paintDisabledText(JLabel l, Graphics g, String s, int textX, int textY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int accChar = l.getDisplayedMnemonicIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        Color background = l.getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        g.setColor(background.brighter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        SwingUtilities2.drawStringUnderlineCharAt(l, g, s, accChar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                                   textX + 1, textY + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        g.setColor(background.darker());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        SwingUtilities2.drawStringUnderlineCharAt(l, g, s, accChar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                                   textX, textY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 4225
diff changeset
   162
     * 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
   163
     * then paints the entire background with the background color. The Label
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 4225
diff changeset
   164
     * text is drawn by {@link #paintEnabledText} or {@link #paintDisabledText}.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 4225
diff changeset
   165
     * The locations of the label parts are computed by {@link #layoutCL}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @see #paintEnabledText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @see #paintDisabledText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @see #layoutCL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public void paint(Graphics g, JComponent c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        JLabel label = (JLabel)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        String text = label.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if ((icon == null) && (text == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        FontMetrics fm = SwingUtilities2.getFontMetrics(label, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        String clippedText = layout(label, fm, c.getWidth(), c.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (icon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                v.paint(g, paintTextR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                int textX = paintTextR.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                int textY = paintTextR.y + fm.getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                if (label.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    paintEnabledText(label, g, clippedText, textX, textY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    paintDisabledText(label, g, clippedText, textX, textY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private String layout(JLabel label, FontMetrics fm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                          int width, int height) {
1289
43d45f4159b6 6727829: BasicLabelUI rendering fails if multiple AppContexts exist in same java JVM
mlapshin
parents: 2
diff changeset
   208
        Insets insets = label.getInsets(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        String text = label.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        Icon icon = (label.isEnabled()) ? label.getIcon() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                          label.getDisabledIcon();
1289
43d45f4159b6 6727829: BasicLabelUI rendering fails if multiple AppContexts exist in same java JVM
mlapshin
parents: 2
diff changeset
   212
        Rectangle paintViewR = new Rectangle();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        paintViewR.x = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        paintViewR.y = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        paintViewR.width = width - (insets.left + insets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        paintViewR.height = height - (insets.top + insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return layoutCL(label, fm, text, icon, paintViewR, paintIconR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                        paintTextR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public Dimension getPreferredSize(JComponent c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        JLabel label = (JLabel)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        String text = label.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        Icon icon = (label.isEnabled()) ? label.getIcon() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                          label.getDisabledIcon();
1289
43d45f4159b6 6727829: BasicLabelUI rendering fails if multiple AppContexts exist in same java JVM
mlapshin
parents: 2
diff changeset
   229
        Insets insets = label.getInsets(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        Font font = label.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        int dx = insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        int dy = insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if ((icon == null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            ((text == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
             ((text != null) && (font == null)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return new Dimension(dx, dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        else if ((text == null) || ((icon != null) && (font == null))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            return new Dimension(icon.getIconWidth() + dx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                 icon.getIconHeight() + dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            FontMetrics fm = label.getFontMetrics(font);
1289
43d45f4159b6 6727829: BasicLabelUI rendering fails if multiple AppContexts exist in same java JVM
mlapshin
parents: 2
diff changeset
   246
            Rectangle iconR = new Rectangle();
43d45f4159b6 6727829: BasicLabelUI rendering fails if multiple AppContexts exist in same java JVM
mlapshin
parents: 2
diff changeset
   247
            Rectangle textR = new Rectangle();
43d45f4159b6 6727829: BasicLabelUI rendering fails if multiple AppContexts exist in same java JVM
mlapshin
parents: 2
diff changeset
   248
            Rectangle viewR = new Rectangle();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            iconR.x = iconR.y = iconR.width = iconR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            textR.x = textR.y = textR.width = textR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            viewR.x = dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            viewR.y = dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            viewR.width = viewR.height = Short.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            layoutCL(label, fm, text, icon, viewR, iconR, textR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            int x1 = Math.min(iconR.x, textR.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            int x2 = Math.max(iconR.x + iconR.width, textR.x + textR.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            int y1 = Math.min(iconR.y, textR.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            int y2 = Math.max(iconR.y + iconR.height, textR.y + textR.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            Dimension rv = new Dimension(x2 - x1, y2 - y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            rv.width += dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            rv.height += dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @return getPreferredSize(c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public Dimension getMinimumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        Dimension d = getPreferredSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @return getPreferredSize(c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        Dimension d = getPreferredSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Returns the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        super.getBaseline(c, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        JLabel label = (JLabel)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        String text = label.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (text == null || "".equals(text) || label.getFont() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        FontMetrics fm = label.getFontMetrics(label.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        layout(label, fm, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        return BasicHTML.getBaseline(label, paintTextR.y, fm.getAscent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                     paintTextR.width, paintTextR.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Returns an enum indicating how the baseline of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * changes as the size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        super.getBaselineResizeBehavior(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (c.getClientProperty(BasicHTML.propertyKey) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return Component.BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        switch(((JLabel)c).getVerticalAlignment()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        case JLabel.TOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        case JLabel.BOTTOM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            return Component.BaselineResizeBehavior.CONSTANT_DESCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        case JLabel.CENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            return Component.BaselineResizeBehavior.CENTER_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return Component.BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        installDefaults((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        installComponents((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        installListeners((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        installKeyboardActions((JLabel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public void uninstallUI(JComponent c) {
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   350
        uninstallDefaults((JLabel) c);
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   351
        uninstallComponents((JLabel) c);
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   352
        uninstallListeners((JLabel) c);
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   353
        uninstallKeyboardActions((JLabel) c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   356
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   357
     * Installs default properties.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   358
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   359
     * @param c an instance of {@code JLabel}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   360
     */
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   361
    protected void installDefaults(JLabel c){
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   362
        LookAndFeel.installColorsAndFont(c, "Label.background", "Label.foreground", "Label.font");
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   363
        LookAndFeel.installProperty(c, "opaque", Boolean.FALSE);
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   364
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   366
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   367
     * Registers listeners.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   368
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   369
     * @param c an instance of {@code JLabel}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   370
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    protected void installListeners(JLabel c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        c.addPropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   375
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   376
     * Registers components.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   377
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   378
     * @param c an instance of {@code JLabel}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   379
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    protected void installComponents(JLabel c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        BasicHTML.updateRenderer(c, c.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        c.setInheritsPopupMenu(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   385
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   386
     * Registers keyboard actions.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   387
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   388
     * @param l an instance of {@code JLabel}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   389
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    protected void installKeyboardActions(JLabel l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        int dka = l.getDisplayedMnemonic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        Component lf = l.getLabelFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if ((dka != 0) && (lf != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            LazyActionMap.installLazyActionMap(l, BasicLabelUI.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                               "Label.actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            InputMap inputMap = SwingUtilities.getUIInputMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                            (l, JComponent.WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            if (inputMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                inputMap = new ComponentInputMapUIResource(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                SwingUtilities.replaceUIInputMap(l,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                                JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            inputMap.clear();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9217
diff changeset
   404
            inputMap.put(KeyStroke.getKeyStroke(dka, BasicLookAndFeel.getFocusAcceleratorKeyMask(), false), "press");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            InputMap inputMap = SwingUtilities.getUIInputMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                            (l, JComponent.WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            if (inputMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                inputMap.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   415
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   416
     * Uninstalls default properties.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   417
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   418
     * @param c an instance of {@code JLabel}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   419
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    protected void uninstallDefaults(JLabel c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   423
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   424
     * Unregisters listeners.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   425
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   426
     * @param c an instance of {@code JLabel}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   427
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    protected void uninstallListeners(JLabel c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        c.removePropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   432
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   433
     * Unregisters components.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   434
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   435
     * @param c an instance of {@code JLabel}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   436
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    protected void uninstallComponents(JLabel c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        BasicHTML.updateRenderer(c, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   441
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   442
     * Unregisters keyboard actions.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   443
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   444
     * @param c an instance of {@code JLabel}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   445
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    protected void uninstallKeyboardActions(JLabel c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                                       null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        SwingUtilities.replaceUIActionMap(c, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
25763
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   453
    /**
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   454
     * Returns an instance of {@code BasicLabelUI}.
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   455
     *
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   456
     * @param c a component
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   457
     * @return an instance of {@code BasicLabelUI}
51d1f910f68d 8049704: Fix doclint warnings from javax.swing.plaf.basic package, 2 of 7
aeremeev
parents: 23010
diff changeset
   458
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        if (System.getSecurityManager() != null) {
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   461
            AppContext appContext = AppContext.getAppContext();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   462
            BasicLabelUI safeBasicLabelUI =
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   463
                    (BasicLabelUI) appContext.get(BASIC_LABEL_UI_KEY);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   464
            if (safeBasicLabelUI == null) {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   465
                safeBasicLabelUI = new BasicLabelUI();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   466
                appContext.put(BASIC_LABEL_UI_KEY, safeBasicLabelUI);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   467
            }
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   468
            return safeBasicLabelUI;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents: 1639
diff changeset
   470
        return labelUI;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        String name = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (name == "text" || "font" == name || "foreground" == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            // remove the old html view client property if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            // existed, and install a new one if the text installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            // into the JLabel is html source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            JLabel lbl = ((JLabel) e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            String text = lbl.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            BasicHTML.updateRenderer(lbl, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        else if (name == "labelFor" || name == "displayedMnemonic") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            installKeyboardActions((JLabel) e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    // When the accelerator is pressed, temporarily make the JLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    // focusTraversable by registering a WHEN_FOCUSED action for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    // release of the accelerator.  Then give it focus so it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    // prevent unwanted keyTyped events from getting to other components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    private static class Actions extends UIAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        private static final String PRESS = "press";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        private static final String RELEASE = "release";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        Actions(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            super(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            JLabel label = (JLabel)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            String key = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            if (key == PRESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                doPress(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            else if (key == RELEASE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                doRelease(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        private void doPress(JLabel label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            Component labelFor = label.getLabelFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            if (labelFor != null && labelFor.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                if (inputMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    inputMap = new InputMapUIResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    SwingUtilities.replaceUIInputMap(label, JComponent.WHEN_FOCUSED, inputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                int dka = label.getDisplayedMnemonic();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9217
diff changeset
   520
                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
   521
                // Need this when the sticky keys are enabled
f795ef267353 6596966: Some JFileChooser mnemonics do not work with sticky keys
rupashka
parents: 5506
diff changeset
   522
                inputMap.put(KeyStroke.getKeyStroke(dka, 0, true), RELEASE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                // Need this if ALT is released before the accelerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true), RELEASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                label.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        private void doRelease(JLabel label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            Component labelFor = label.getLabelFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            if (labelFor != null && labelFor.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                if (inputMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    // inputMap should never be null.
9217
f795ef267353 6596966: Some JFileChooser mnemonics do not work with sticky keys
rupashka
parents: 5506
diff changeset
   535
                    int dka = label.getDisplayedMnemonic();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9217
diff changeset
   536
                    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
   537
                    inputMap.remove(KeyStroke.getKeyStroke(dka, 0, true));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                if (labelFor instanceof Container &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        ((Container) labelFor).isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    labelFor.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    SwingUtilities2.compositeRequestFocus(labelFor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
}