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//  Based on 1.3.1's AquaHighlighter, with the main difference that an inactive selection should be gray
27//  rather than a darker version of the current highlight color.
28
29package com.apple.laf;
30
31import java.awt.*;
32
33import javax.swing.*;
34import javax.swing.plaf.UIResource;
35import javax.swing.text.*;
36
37import com.apple.laf.AquaUtils.RecyclableSingleton;
38
39public class AquaHighlighter extends DefaultHighlighter implements UIResource {
40    private static final RecyclableSingleton<LayerPainter> instance = new RecyclableSingleton<LayerPainter>() {
41        protected LayerPainter getInstance() {
42            return new AquaHighlightPainter(null);
43        }
44    };
45
46    protected static LayeredHighlighter.LayerPainter getInstance() {
47        return instance.get();
48    }
49
50    public static class AquaHighlightPainter extends DefaultHighlightPainter {
51        Color selectionColor;
52        Color disabledSelectionColor;
53
54        public AquaHighlightPainter(final Color c) {
55            super(c);
56        }
57
58        public Color getColor() {
59            return selectionColor == null ? super.getColor() : selectionColor;
60        }
61
62
63        protected Color getInactiveSelectionColor() {
64            if (disabledSelectionColor != null) return disabledSelectionColor;
65            return disabledSelectionColor = UIManager.getColor("TextComponent.selectionBackgroundInactive");
66        }
67
68        void setColor(final JTextComponent c) {
69            selectionColor = super.getColor();
70
71            if (selectionColor == null) selectionColor = c.getSelectionColor();
72
73            final Window owningWindow = SwingUtilities.getWindowAncestor(c);
74
75            // If window is not currently active selection color is a gray with RGB of (212, 212, 212).
76            if (owningWindow != null && !owningWindow.isActive()) {
77                selectionColor = getInactiveSelectionColor();
78            }
79
80            if (!c.hasFocus()) {
81                selectionColor = getInactiveSelectionColor();
82            }
83        }
84
85        public void paint(final Graphics g, final int offs0, final int offs1, final Shape bounds, final JTextComponent c) {
86            setColor(c);
87            super.paint(g, offs0, offs1, bounds, c);
88        }
89
90        public Shape paintLayer(final Graphics g, final int offs0, final int offs1, final Shape bounds, final JTextComponent c, final View view) {
91            setColor(c);
92            return super.paintLayer(g, offs0, offs1, bounds, c, view);
93        }
94    }
95}
96