src/jdk.jconsole/share/classes/sun/tools/jconsole/Utilities.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/jdk.jconsole/share/classes/sun/tools/jconsole/Utilities.java@3317bb8137f4
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jconsole;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.tree.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.tools.jconsole.inspector.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import static java.lang.Math.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Miscellaneous utility methods for JConsole
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class Utilities {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final String windowsLaF =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static void updateTransparency(JComponent comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        LookAndFeel laf = UIManager.getLookAndFeel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        boolean transparent = laf.getClass().getName().equals(windowsLaF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        setTabbedPaneTransparency(comp, transparent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static void setTabbedPaneTransparency(JComponent comp, boolean transparent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        for (Component child : comp.getComponents()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            if (comp instanceof JTabbedPane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                setTransparency((JComponent)child, transparent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            } else if (child instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                setTabbedPaneTransparency((JComponent)child, transparent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static void setTransparency(JComponent comp, boolean transparent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        comp.setOpaque(!transparent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        for (Component child : comp.getComponents()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if (child instanceof JPanel ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                child instanceof JSplitPane ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                child instanceof JScrollPane ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                child instanceof JViewport ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                child instanceof JCheckBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                setTransparency((JComponent)child, transparent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (child instanceof XTree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                XTree t = (XTree)child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                DefaultTreeCellRenderer cr = (DefaultTreeCellRenderer)t.getCellRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                cr.setBackground(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                cr.setBackgroundNonSelectionColor(new Color(0, 0, 0, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                t.setCellRenderer(cr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                setTransparency((JComponent)child, transparent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * A slightly modified border for JScrollPane to be used with a JTable inside
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * a JTabbedPane. It has only top part and the rest is clipped to make the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * overall border less thick.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * The top border helps differentiating the containing table from its container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static JScrollPane newTableScrollPane(JComponent comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return new TableScrollPane(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    @SuppressWarnings("serial")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static class TableScrollPane extends JScrollPane {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        public TableScrollPane(JComponent comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            super(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        protected void paintBorder(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            Border border = getBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (border != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                Insets insets = border.getBorderInsets(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                if (insets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    Shape oldClip = g.getClip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    g.clipRect(0, 0, getWidth(), insets.top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    super.paintBorder(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    g.setClip(oldClip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public static void setAccessibleName(Accessible comp, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        comp.getAccessibleContext().setAccessibleName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public static void setAccessibleDescription(Accessible comp, String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        comp.getAccessibleContext().setAccessibleDescription(description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Modifies color c1 to ensure it has acceptable contrast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * relative to color c2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * http://www.w3.org/TR/AERT#color-contrast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * http://www.cs.rit.edu/~ncs/color/t_convert.html#RGB%20to%20YIQ%20&%20YIQ%20to%20RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public static Color ensureContrast(Color c1, Color c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        double y1 = getColorBrightness(c1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        double y2 = getColorBrightness(c2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (abs(y1 - y2) < 125.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (y2 < 128.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                c1 = setColorBrightness(c1, y2 + 125.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                c1 = setColorBrightness(c1, y2 - 125.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return c1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public static double getColorBrightness(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // Convert RGB -> YIQ and return the Y value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return (c.getRed() * 0.299 + c.getGreen() * 0.587 + c.getBlue() * 0.114);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private static Color setColorBrightness(Color c, double y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // Convert YIQ -> RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        double i = (c.getRed() * 0.596 - c.getGreen() * 0.275 - c.getBlue() * 0.321);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        double q = (c.getRed() * 0.212 - c.getGreen() * 0.523 + c.getBlue() * 0.311);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // Keep values in legal range. This may reduce the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // achieved contrast somewhat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        int r = max(0, min(255, (int)round(y + i * 0.956 + q * 0.621)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        int g = max(0, min(255, (int)round(y - i * 0.272 - q * 0.647)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        int b = max(0, min(255, (int)round(y - i * 1.105 + q * 1.702)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return new Color(r, g, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}