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 } | 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 private TextUIDrawing textUIDrawing; 42 43 public static ComponentUI createUI(final JComponent c) { 44 return aquaLabelUI.get(); 45 } 46 47 protected void installListeners(final JLabel c) { 48 super.installListeners(c); 49 AquaUtilControlSize.addSizePropertyListener(c); 50 } 51 52 protected void uninstallListeners(final JLabel c) { 53 AquaUtilControlSize.removeSizePropertyListener(c); 54 super.uninstallListeners(c); 55 } 56 57 @Override 58 protected void installDefaults(JLabel c) { 59 super.installDefaults(c); 60 textUIDrawing = SwingUtilities2.getTextUIDrawing(textUIDrawing); 61 } 62 63 @Override 64 protected void uninstallDefaults(JLabel c) { 65 super.uninstallDefaults(c); 66 if (textUIDrawing != SwingUtilities2.DEFAULT_UI_TEXT_DRAWING 67 && textUIDrawing instanceof UIResource) { 68 textUIDrawing = SwingUtilities2.DEFAULT_UI_TEXT_DRAWING; 69 } 70 } 71 72 protected void paintEnabledText(final JLabel l, final Graphics g, final String s, final int textX, final int textY) { 73 int mnemIndex = l.getDisplayedMnemonicIndex(); 74 if (AquaMnemonicHandler.isMnemonicHidden()) { 75 mnemIndex = -1; 76 } 77 78 g.setColor(l.getForeground()); 79 textUIDrawing.drawStringUnderlineCharAt(l, g, s, mnemIndex, textX, textY); 80 } 81 82 /** 83 * Paint clippedText at textX, textY with background.lighter() and then 84 * shifted down and to the right by one pixel with background.darker(). 85 * 86 * @see #paint 87 * @see #paintEnabledText 88 */ 89 protected void paintDisabledText(final JLabel l, final Graphics g, final String s, final int textX, final int textY) { 90 int accChar = l.getDisplayedMnemonicIndex(); 91 if (AquaMnemonicHandler.isMnemonicHidden()) { 92 accChar = -1; 93 } 94 95 final Color background = l.getBackground(); 96 97 // if our background is still something we set then we can use our happy background color. 98 if (background instanceof UIResource) { 99 g.setColor(getDisabledLabelColor(l)); 100 textUIDrawing.drawStringUnderlineCharAt(l, g, s, accChar, textX, textY); 101 } else { 102 super.paintDisabledText(l, g, s, textX, textY); 103 } 104 } 105 106 static final String DISABLED_COLOR_KEY = "Label.disabledForegroundColor"; 107 protected Color getDisabledLabelColor(final JLabel label) { 108 final Color fg = label.getForeground(); 109 110 final Object colorProperty = label.getClientProperty(DISABLED_COLOR_KEY); 111 if (colorProperty instanceof Color) { 112 final Color disabledColor = (Color)colorProperty; 113 if ((fg.getRGB() << 8) == (disabledColor.getRGB() << 8)) return disabledColor; 114 } 115 116 final Color newDisabledColor = new Color(fg.getRed(), fg.getGreen(), fg.getBlue(), fg.getAlpha() / 2); 117 label.putClientProperty(DISABLED_COLOR_KEY, newDisabledColor); 118 return newDisabledColor; 119 } 120 } |