1 19 20 package org.netbeans.swing.plaf.util; 21 22 23 import javax.imageio.ImageIO ; 24 import javax.swing.*; 25 import java.awt.*; 26 import java.lang.reflect.Method ; 27 import java.net.URL ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 31 35 public final class UIUtils { 36 private static HashMap <RenderingHints.Key, Object > hintsMap = null; 37 private static final boolean noAntialias = 38 Boolean.getBoolean("nb.no.antialias"); 40 41 private static boolean colorsReady = false; 42 43 44 private UIUtils() { 45 } 46 47 49 public static boolean isWindowsLF () { 50 if (Boolean.getBoolean("netbeans.winsys.forceclassic")) { 51 return false; 52 } 53 String lfID = UIManager.getLookAndFeel().getID(); 54 return lfID.endsWith("Windows"); } 57 58 60 public static boolean isXPLF () { 61 if (!isWindowsLF()) { 62 return false; 63 } 64 Boolean isXP = (Boolean )Toolkit.getDefaultToolkit(). 65 getDesktopProperty("win.xpstyle.themeActive"); return isXP == null ? false : isXP.booleanValue(); 67 } 68 69 private static final Map <RenderingHints.Key, Object > getHints() { 70 if (hintsMap == null) { 74 hintsMap = new HashMap <RenderingHints.Key, Object >(); 75 hintsMap.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 76 hintsMap.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 77 } 78 return hintsMap; 79 } 80 81 public static final void configureRenderingHints (Graphics g) { 82 if (noAntialias) return; 83 Graphics2D g2d = (Graphics2D) g; 84 85 g2d.addRenderingHints(getHints()); 86 } 87 88 public static Image loadImage (String s) { 89 if (openideAvailable == null) { 90 checkOpenide(); 91 } 92 if (Boolean.TRUE.equals(openideAvailable)) { 93 return loadWithUtilities(s); 94 } else { 95 return loadWithImageIO(s); 96 } 97 } 98 99 102 public static Color getMiddle (Color c1, Color c2) { 103 return new Color((c1.getRed() + c2.getRed()) / 2, 104 (c1.getGreen() + c2.getGreen()) / 2, 105 (c1.getBlue() + c2.getBlue()) / 2); 106 } 107 108 private static void checkOpenide() { 109 try { 110 utilsClass = Class.forName("org.openide.util.Utilities"); utilsMethod = utilsClass.getDeclaredMethod ( "loadImage", new Class [] {String .class}); openideAvailable = Boolean.TRUE; 113 } catch (Exception e) { 114 openideAvailable = Boolean.FALSE; 115 } 116 } 117 118 private static Image loadWithUtilities (String s) { 119 Image result = null; 120 try { 121 result = (Image) utilsMethod.invoke ( null, new Object [] {s} ); 122 } catch (Exception e) { 123 System.err.println ("Error loading image " + s); e.printStackTrace(); } 126 return result; 127 } 128 129 private static Image loadWithImageIO (String s) { 130 Image result = null; 131 try { 132 URL url = UIUtils.class.getResource ( s ); 133 result = ImageIO.read ( url ); 134 } catch (Exception e) { 135 System.err.println ("Error loading image using ImageIO " + s); e.printStackTrace(); 137 } 138 return result; 139 } 140 141 private static Boolean openideAvailable = null; 142 private static Class <?> utilsClass = null; 143 private static Method utilsMethod = null; 144 145 public static GradientPaint getGradientPaint ( float x1, float y1, Color upper, float x2, float y2, Color lower, 147 boolean repeats ) { 148 return new GradientPaint ( x1, y1, upper, x2, y2, lower, repeats ); 149 } 150 151 152 public static Color adjustColor (Color c, int rDiff, int gDiff, int bDiff) { 153 int red = Math.max(0, Math.min(255, c.getRed() + rDiff)); 155 int green = Math.max(0, Math.min(255, c.getGreen() + gDiff)); 156 int blue = Math.max(0, Math.min(255, c.getBlue() + bDiff)); 157 return new Color(red, green, blue); 158 } 159 160 163 private static float minMax(float f) { 164 return Math.max(0, Math.min(1, f)); 165 } 166 167 public static boolean isBrighter(Color a, Color b) { 168 int[] ac = new int[]{a.getRed(), a.getGreen(), a.getBlue()}; 169 int[] bc = new int[]{b.getRed(), b.getGreen(), b.getBlue()}; 170 int dif = 0; 171 172 for (int i = 0; i < 3; i++) { 173 int currDif = ac[i] - bc[i]; 174 if (Math.abs(currDif) > Math.abs(dif)) { 175 dif = currDif; 176 } 177 } 178 return dif > 0; 179 } 180 } 181 | Popular Tags |