1 /* 2 * Copyright (c) 2011, 2012, 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 com.apple.laf; 27 28 import java.awt.*; 29 30 import javax.swing.*; 31 import javax.swing.plaf.*; 32 import javax.swing.plaf.basic.*; 33 34 import sun.swing.SwingUtilities2; 35 36 import com.apple.laf.AquaUtils.RecyclableSingleton; 37 import com.apple.laf.AquaUtils.RecyclableSingletonFromDefaultConstructor; 38 39 public class AquaLabelUI extends BasicLabelUI { 40 protected static final RecyclableSingleton<AquaLabelUI> aquaLabelUI = new RecyclableSingletonFromDefaultConstructor<AquaLabelUI>(AquaLabelUI.class); 41 42 public static ComponentUI createUI(final JComponent c) { 43 return aquaLabelUI.get(); 44 } 45 46 protected void installListeners(final JLabel c) { 47 super.installListeners(c); 48 AquaUtilControlSize.addSizePropertyListener(c); 49 } 50 51 protected void uninstallListeners(final JLabel c) { 52 AquaUtilControlSize.removeSizePropertyListener(c); 53 super.uninstallListeners(c); 54 } 55 56 protected void paintEnabledText(final JLabel l, final Graphics g, final String s, final int textX, final int textY) { 57 int mnemIndex = l.getDisplayedMnemonicIndex(); 58 if (AquaMnemonicHandler.isMnemonicHidden()) { 59 mnemIndex = -1; 60 } 61 62 g.setColor(l.getForeground()); 63 SwingUtilities2.drawStringUnderlineCharAt(l, g, s, mnemIndex, textX, textY); 64 } 65 66 /** 67 * Paint clippedText at textX, textY with background.lighter() and then 68 * shifted down and to the right by one pixel with background.darker(). 69 * 70 * @see #paint 71 * @see #paintEnabledText 72 */ 73 protected void paintDisabledText(final JLabel l, final Graphics g, final String s, final int textX, final int textY) { 74 int accChar = l.getDisplayedMnemonicIndex(); 75 if (AquaMnemonicHandler.isMnemonicHidden()) { 76 accChar = -1; 77 } 78 79 final Color background = l.getBackground(); 80 81 // if our background is still something we set then we can use our happy background color. 82 if (background instanceof UIResource) { 83 g.setColor(getDisabledLabelColor(l)); 84 SwingUtilities2.drawStringUnderlineCharAt(l, g, s, accChar, textX, textY); 85 } else { 86 super.paintDisabledText(l, g, s, textX, textY); 87 } 88 } 89 90 static final String DISABLED_COLOR_KEY = "Label.disabledForegroundColor"; 91 protected Color getDisabledLabelColor(final JLabel label) { 92 final Color fg = label.getForeground(); 93 94 final Object colorProperty = label.getClientProperty(DISABLED_COLOR_KEY); 95 if (colorProperty instanceof Color) { 96 final Color disabledColor = (Color)colorProperty; 97 if ((fg.getRGB() << 8) == (disabledColor.getRGB() << 8)) return disabledColor; 98 } 99 100 final Color newDisabledColor = new Color(fg.getRed(), fg.getGreen(), fg.getBlue(), fg.getAlpha() / 2); 101 label.putClientProperty(DISABLED_COLOR_KEY, newDisabledColor); 102 return newDisabledColor; 103 } 104 }