Path: blob/master/src/jdk.jconsole/share/classes/sun/tools/jconsole/Utilities.java
40948 views
/*1* Copyright (c) 2005, 2006, 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*/2425package sun.tools.jconsole;2627import java.awt.*;2829import javax.accessibility.*;30import javax.swing.*;31import javax.swing.border.*;32import javax.swing.tree.*;3334import sun.tools.jconsole.inspector.*;3536import static java.lang.Math.*;3738/**39* Miscellaneous utility methods for JConsole40*/41public class Utilities {42private static final String windowsLaF =43"com.sun.java.swing.plaf.windows.WindowsLookAndFeel";4445public static void updateTransparency(JComponent comp) {46LookAndFeel laf = UIManager.getLookAndFeel();47boolean transparent = laf.getClass().getName().equals(windowsLaF);48setTabbedPaneTransparency(comp, transparent);49}5051private static void setTabbedPaneTransparency(JComponent comp, boolean transparent) {52for (Component child : comp.getComponents()) {53if (comp instanceof JTabbedPane) {54setTransparency((JComponent)child, transparent);55} else if (child instanceof JComponent) {56setTabbedPaneTransparency((JComponent)child, transparent);57}58}59}6061private static void setTransparency(JComponent comp, boolean transparent) {62comp.setOpaque(!transparent);63for (Component child : comp.getComponents()) {64if (child instanceof JPanel ||65child instanceof JSplitPane ||66child instanceof JScrollPane ||67child instanceof JViewport ||68child instanceof JCheckBox) {6970setTransparency((JComponent)child, transparent);71}72if (child instanceof XTree) {73XTree t = (XTree)child;74DefaultTreeCellRenderer cr = (DefaultTreeCellRenderer)t.getCellRenderer();7576cr.setBackground(null);77cr.setBackgroundNonSelectionColor(new Color(0, 0, 0, 1));78t.setCellRenderer(cr);79setTransparency((JComponent)child, transparent);80}81}82}838485/**86* A slightly modified border for JScrollPane to be used with a JTable inside87* a JTabbedPane. It has only top part and the rest is clipped to make the88* overall border less thick.89* The top border helps differentiating the containing table from its container.90*/91public static JScrollPane newTableScrollPane(JComponent comp) {92return new TableScrollPane(comp);93}9495@SuppressWarnings("serial")96private static class TableScrollPane extends JScrollPane {97public TableScrollPane(JComponent comp) {98super(comp);99}100101protected void paintBorder(Graphics g) {102Border border = getBorder();103if (border != null) {104Insets insets = border.getBorderInsets(this);105if (insets != null) {106Shape oldClip = g.getClip();107g.clipRect(0, 0, getWidth(), insets.top);108super.paintBorder(g);109g.setClip(oldClip);110}111}112}113}114115public static void setAccessibleName(Accessible comp, String name) {116comp.getAccessibleContext().setAccessibleName(name);117}118119public static void setAccessibleDescription(Accessible comp, String description) {120comp.getAccessibleContext().setAccessibleDescription(description);121}122123124/**125* Modifies color c1 to ensure it has acceptable contrast126* relative to color c2.127*128* http://www.w3.org/TR/AERT#color-contrast129* http://www.cs.rit.edu/~ncs/color/t_convert.html#RGB%20to%20YIQ%20&%20YIQ%20to%20RGB130*/131public static Color ensureContrast(Color c1, Color c2) {132double y1 = getColorBrightness(c1);133double y2 = getColorBrightness(c2);134135if (abs(y1 - y2) < 125.0) {136if (y2 < 128.0) {137c1 = setColorBrightness(c1, y2 + 125.0);138} else {139c1 = setColorBrightness(c1, y2 - 125.0);140}141}142143return c1;144}145146public static double getColorBrightness(Color c) {147// Convert RGB -> YIQ and return the Y value148return (c.getRed() * 0.299 + c.getGreen() * 0.587 + c.getBlue() * 0.114);149}150151private static Color setColorBrightness(Color c, double y) {152// Convert YIQ -> RGB153double i = (c.getRed() * 0.596 - c.getGreen() * 0.275 - c.getBlue() * 0.321);154double q = (c.getRed() * 0.212 - c.getGreen() * 0.523 + c.getBlue() * 0.311);155156// Keep values in legal range. This may reduce the157// achieved contrast somewhat.158int r = max(0, min(255, (int)round(y + i * 0.956 + q * 0.621)));159int g = max(0, min(255, (int)round(y - i * 0.272 - q * 0.647)));160int b = max(0, min(255, (int)round(y - i * 1.105 + q * 1.702)));161162return new Color(r, g, b);163}164165}166167168