Path: blob/master/src/java.desktop/macosx/classes/com/apple/laf/AquaHighlighter.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*/2425// Based on 1.3.1's AquaHighlighter, with the main difference that an inactive selection should be gray26// rather than a darker version of the current highlight color.2728package com.apple.laf;2930import java.awt.*;3132import javax.swing.*;33import javax.swing.plaf.UIResource;34import javax.swing.text.*;3536import com.apple.laf.AquaUtils.RecyclableSingleton;3738public class AquaHighlighter extends DefaultHighlighter implements UIResource {39private static final RecyclableSingleton<LayerPainter> instance = new RecyclableSingleton<LayerPainter>() {40protected LayerPainter getInstance() {41return new AquaHighlightPainter(null);42}43};4445protected static LayeredHighlighter.LayerPainter getInstance() {46return instance.get();47}4849public static class AquaHighlightPainter extends DefaultHighlightPainter {50Color selectionColor;51Color disabledSelectionColor;5253public AquaHighlightPainter(final Color c) {54super(c);55}5657public Color getColor() {58return selectionColor == null ? super.getColor() : selectionColor;59}606162protected Color getInactiveSelectionColor() {63if (disabledSelectionColor != null) return disabledSelectionColor;64return disabledSelectionColor = UIManager.getColor("TextComponent.selectionBackgroundInactive");65}6667void setColor(final JTextComponent c) {68selectionColor = super.getColor();6970if (selectionColor == null) selectionColor = c.getSelectionColor();7172final Window owningWindow = SwingUtilities.getWindowAncestor(c);7374// If window is not currently active selection color is a gray with RGB of (212, 212, 212).75if (owningWindow != null && !owningWindow.isActive()) {76selectionColor = getInactiveSelectionColor();77}7879if (!c.hasFocus()) {80selectionColor = getInactiveSelectionColor();81}82}8384public void paint(final Graphics g, final int offs0, final int offs1, final Shape bounds, final JTextComponent c) {85setColor(c);86super.paint(g, offs0, offs1, bounds, c);87}8889public Shape paintLayer(final Graphics g, final int offs0, final int offs1, final Shape bounds, final JTextComponent c, final View view) {90setColor(c);91return super.paintLayer(g, offs0, offs1, bounds, c, view);92}93}94}959697