1 /* 2 * Copyright (c) 2005, 2006, 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 package sun.tools.jconsole; 27 28 import java.awt.*; 29 30 import javax.accessibility.*; 31 import javax.swing.*; 32 import javax.swing.border.*; 33 import javax.swing.tree.*; 34 35 import sun.tools.jconsole.inspector.*; 36 37 import static java.lang.Math.*; 38 39 /** 40 * Miscellaneous utility methods for JConsole 41 */ 42 public class Utilities { 43 private static final String windowsLaF = 44 "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; 45 updateTransparency(JComponent comp)46 public static void updateTransparency(JComponent comp) { 47 LookAndFeel laf = UIManager.getLookAndFeel(); 48 boolean transparent = laf.getClass().getName().equals(windowsLaF); 49 setTabbedPaneTransparency(comp, transparent); 50 } 51 setTabbedPaneTransparency(JComponent comp, boolean transparent)52 private static void setTabbedPaneTransparency(JComponent comp, boolean transparent) { 53 for (Component child : comp.getComponents()) { 54 if (comp instanceof JTabbedPane) { 55 setTransparency((JComponent)child, transparent); 56 } else if (child instanceof JComponent) { 57 setTabbedPaneTransparency((JComponent)child, transparent); 58 } 59 } 60 } 61 setTransparency(JComponent comp, boolean transparent)62 private static void setTransparency(JComponent comp, boolean transparent) { 63 comp.setOpaque(!transparent); 64 for (Component child : comp.getComponents()) { 65 if (child instanceof JPanel || 66 child instanceof JSplitPane || 67 child instanceof JScrollPane || 68 child instanceof JViewport || 69 child instanceof JCheckBox) { 70 71 setTransparency((JComponent)child, transparent); 72 } 73 if (child instanceof XTree) { 74 XTree t = (XTree)child; 75 DefaultTreeCellRenderer cr = (DefaultTreeCellRenderer)t.getCellRenderer(); 76 77 cr.setBackground(null); 78 cr.setBackgroundNonSelectionColor(new Color(0, 0, 0, 1)); 79 t.setCellRenderer(cr); 80 setTransparency((JComponent)child, transparent); 81 } 82 } 83 } 84 85 86 /** 87 * A slightly modified border for JScrollPane to be used with a JTable inside 88 * a JTabbedPane. It has only top part and the rest is clipped to make the 89 * overall border less thick. 90 * The top border helps differentiating the containing table from its container. 91 */ newTableScrollPane(JComponent comp)92 public static JScrollPane newTableScrollPane(JComponent comp) { 93 return new TableScrollPane(comp); 94 } 95 96 @SuppressWarnings("serial") 97 private static class TableScrollPane extends JScrollPane { TableScrollPane(JComponent comp)98 public TableScrollPane(JComponent comp) { 99 super(comp); 100 } 101 paintBorder(Graphics g)102 protected void paintBorder(Graphics g) { 103 Border border = getBorder(); 104 if (border != null) { 105 Insets insets = border.getBorderInsets(this); 106 if (insets != null) { 107 Shape oldClip = g.getClip(); 108 g.clipRect(0, 0, getWidth(), insets.top); 109 super.paintBorder(g); 110 g.setClip(oldClip); 111 } 112 } 113 } 114 } 115 setAccessibleName(Accessible comp, String name)116 public static void setAccessibleName(Accessible comp, String name) { 117 comp.getAccessibleContext().setAccessibleName(name); 118 } 119 setAccessibleDescription(Accessible comp, String description)120 public static void setAccessibleDescription(Accessible comp, String description) { 121 comp.getAccessibleContext().setAccessibleDescription(description); 122 } 123 124 125 /** 126 * Modifies color c1 to ensure it has acceptable contrast 127 * relative to color c2. 128 * 129 * http://www.w3.org/TR/AERT#color-contrast 130 * http://www.cs.rit.edu/~ncs/color/t_convert.html#RGB%20to%20YIQ%20&%20YIQ%20to%20RGB 131 */ ensureContrast(Color c1, Color c2)132 public static Color ensureContrast(Color c1, Color c2) { 133 double y1 = getColorBrightness(c1); 134 double y2 = getColorBrightness(c2); 135 136 if (abs(y1 - y2) < 125.0) { 137 if (y2 < 128.0) { 138 c1 = setColorBrightness(c1, y2 + 125.0); 139 } else { 140 c1 = setColorBrightness(c1, y2 - 125.0); 141 } 142 } 143 144 return c1; 145 } 146 getColorBrightness(Color c)147 public static double getColorBrightness(Color c) { 148 // Convert RGB -> YIQ and return the Y value 149 return (c.getRed() * 0.299 + c.getGreen() * 0.587 + c.getBlue() * 0.114); 150 } 151 setColorBrightness(Color c, double y)152 private static Color setColorBrightness(Color c, double y) { 153 // Convert YIQ -> RGB 154 double i = (c.getRed() * 0.596 - c.getGreen() * 0.275 - c.getBlue() * 0.321); 155 double q = (c.getRed() * 0.212 - c.getGreen() * 0.523 + c.getBlue() * 0.311); 156 157 // Keep values in legal range. This may reduce the 158 // achieved contrast somewhat. 159 int r = max(0, min(255, (int)round(y + i * 0.956 + q * 0.621))); 160 int g = max(0, min(255, (int)round(y - i * 0.272 - q * 0.647))); 161 int b = max(0, min(255, (int)round(y - i * 1.105 + q * 1.702))); 162 163 return new Color(r, g, b); 164 } 165 166 } 167