Path: blob/master/src/java.desktop/macosx/classes/com/apple/laf/AquaLabelUI.java
41154 views
/*1* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.apple.laf;2627import java.awt.*;2829import javax.swing.*;30import javax.swing.plaf.*;31import javax.swing.plaf.basic.*;3233import sun.swing.SwingUtilities2;3435import com.apple.laf.AquaUtils.RecyclableSingleton;36import com.apple.laf.AquaUtils.RecyclableSingletonFromDefaultConstructor;3738public class AquaLabelUI extends BasicLabelUI {39private static final RecyclableSingleton<AquaLabelUI> aquaLabelUI = new RecyclableSingletonFromDefaultConstructor<AquaLabelUI>(AquaLabelUI.class);4041public static ComponentUI createUI(final JComponent c) {42return aquaLabelUI.get();43}4445protected void installListeners(final JLabel c) {46super.installListeners(c);47AquaUtilControlSize.addSizePropertyListener(c);48}4950protected void uninstallListeners(final JLabel c) {51AquaUtilControlSize.removeSizePropertyListener(c);52super.uninstallListeners(c);53}5455protected void paintEnabledText(final JLabel l, final Graphics g, final String s, final int textX, final int textY) {56int mnemIndex = l.getDisplayedMnemonicIndex();57if (AquaMnemonicHandler.isMnemonicHidden()) {58mnemIndex = -1;59}6061g.setColor(l.getForeground());62SwingUtilities2.drawStringUnderlineCharAt(l, g, s, mnemIndex, textX, textY);63}6465/**66* Paint clippedText at textX, textY with background.lighter() and then67* shifted down and to the right by one pixel with background.darker().68*69* @see #paint70* @see #paintEnabledText71*/72protected void paintDisabledText(final JLabel l, final Graphics g, final String s, final int textX, final int textY) {73int accChar = l.getDisplayedMnemonicIndex();74if (AquaMnemonicHandler.isMnemonicHidden()) {75accChar = -1;76}7778final Color background = l.getBackground();7980// if our background is still something we set then we can use our happy background color.81if (background instanceof UIResource) {82g.setColor(getDisabledLabelColor(l));83SwingUtilities2.drawStringUnderlineCharAt(l, g, s, accChar, textX, textY);84} else {85super.paintDisabledText(l, g, s, textX, textY);86}87}8889static final String DISABLED_COLOR_KEY = "Label.disabledForegroundColor";90protected Color getDisabledLabelColor(final JLabel label) {91final Color fg = label.getForeground();9293final Object colorProperty = label.getClientProperty(DISABLED_COLOR_KEY);94if (colorProperty instanceof Color) {95final Color disabledColor = (Color)colorProperty;96if ((fg.getRGB() << 8) == (disabledColor.getRGB() << 8)) return disabledColor;97}9899final Color newDisabledColor = new Color(fg.getRed(), fg.getGreen(), fg.getBlue(), fg.getAlpha() / 2);100label.putClientProperty(DISABLED_COLOR_KEY, newDisabledColor);101return newDisabledColor;102}103}104105106