1 /*
2  * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package javax.swing.plaf.metal;
27 
28 import sun.swing.SwingUtilities2;
29 import sun.awt.AppContext;
30 
31 import javax.swing.*;
32 import javax.swing.plaf.*;
33 import javax.swing.plaf.basic.*;
34 
35 
36 import java.awt.*;
37 
38 
39 /**
40  * A Windows L&F implementation of LabelUI.  This implementation
41  * is completely static, i.e. there's only one UIView implementation
42  * that's shared by all JLabel objects.
43  *
44  * @author Hans Muller
45  */
46 
47 public class MetalLabelUI extends BasicLabelUI
48 {
49    /**
50     * The default <code>MetalLabelUI</code> instance. This field might
51     * not be used. To change the default instance use a subclass which
52     * overrides the <code>createUI</code> method, and place that class
53     * name in defaults table under the key "LabelUI".
54     */
55     protected static MetalLabelUI metalLabelUI = new MetalLabelUI();
56 
57     private static final Object METAL_LABEL_UI_KEY = new Object();
58 
59     /**
60      * Returns an instance of {@code MetalLabelUI}.
61      *
62      * @param c a component
63      * @return an instance of {@code MetalLabelUI}
64      */
createUI(JComponent c)65     public static ComponentUI createUI(JComponent c) {
66         if (System.getSecurityManager() != null) {
67             AppContext appContext = AppContext.getAppContext();
68             MetalLabelUI safeMetalLabelUI =
69                     (MetalLabelUI) appContext.get(METAL_LABEL_UI_KEY);
70             if (safeMetalLabelUI == null) {
71                 safeMetalLabelUI = new MetalLabelUI();
72                 appContext.put(METAL_LABEL_UI_KEY, safeMetalLabelUI);
73             }
74             return safeMetalLabelUI;
75         }
76         return metalLabelUI;
77     }
78 
79     /**
80      * Just paint the text gray (Label.disabledForeground) rather than
81      * in the labels foreground color.
82      *
83      * @see #paint
84      * @see #paintEnabledText
85      */
paintDisabledText(JLabel l, Graphics g, String s, int textX, int textY)86     protected void paintDisabledText(JLabel l, Graphics g, String s, int textX, int textY)
87     {
88         int mnemIndex = l.getDisplayedMnemonicIndex();
89         g.setColor(UIManager.getColor("Label.disabledForeground"));
90         SwingUtilities2.drawStringUnderlineCharAt(l, g, s, mnemIndex,
91                                                    textX, textY);
92     }
93 }
94