KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > plaf > util > UIUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.swing.plaf.util;
21
22
23 import javax.imageio.ImageIO JavaDoc;
24 import javax.swing.*;
25 import java.awt.*;
26 import java.lang.reflect.Method JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /** XP color scheme installer.
32  *
33  * @author Dafe Simonek
34  */

35 public final class UIUtils {
36     private static HashMap JavaDoc<RenderingHints.Key, Object JavaDoc> hintsMap = null;
37     private static final boolean noAntialias =
38         Boolean.getBoolean("nb.no.antialias"); //NOI18N
39

40     /** true when XP style colors are installed into UI manager, false otherwise */
41     private static boolean colorsReady = false;
42             
43     /** No need to instantiate this utility class. */
44     private UIUtils() {
45     }
46     
47     /** Finds if windows LF is active.
48      * @return true if windows LF is active, false otherwise */

49     public static boolean isWindowsLF () {
50         if (Boolean.getBoolean("netbeans.winsys.forceclassic")) {
51             return false;
52         }
53         String JavaDoc lfID = UIManager.getLookAndFeel().getID();
54         // #79401 - return true also for "JGoodies Windows" LF
55
return lfID.endsWith("Windows"); //NOI18N
56
}
57     
58     /** Finds if windows LF with XP theme is active.
59      * @return true if windows LF and XP theme is active, false otherwise */

60     public static boolean isXPLF () {
61         if (!isWindowsLF()) {
62             return false;
63         }
64         Boolean JavaDoc isXP = (Boolean JavaDoc)Toolkit.getDefaultToolkit().
65                         getDesktopProperty("win.xpstyle.themeActive"); //NOI18N
66
return isXP == null ? false : isXP.booleanValue();
67     }
68
69      private static final Map JavaDoc<RenderingHints.Key, Object JavaDoc> getHints() {
70         //XXX should do this in update() in the UI instead
71
//Note for this method we do NOT want only text antialiasing - we
72
//want antialiased curves.
73
if (hintsMap == null) {
74             hintsMap = new HashMap JavaDoc<RenderingHints.Key, Object JavaDoc>();
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 JavaDoc 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     /** Computes "middle" color in terms of rgb color space. Ignores alpha
100      * (transparency) channel
101      */

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"); //NOI18N
111
utilsMethod = utilsClass.getDeclaredMethod ( "loadImage", new Class JavaDoc[] {String JavaDoc.class}); //NOI18N
112
openideAvailable = Boolean.TRUE;
113         } catch (Exception JavaDoc e) {
114             openideAvailable = Boolean.FALSE;
115         }
116     }
117
118     private static Image loadWithUtilities (String JavaDoc s) {
119         Image result = null;
120         try {
121             result = (Image) utilsMethod.invoke ( null, new Object JavaDoc[] {s} );
122         } catch (Exception JavaDoc e) {
123             System.err.println ("Error loading image " + s); //NOI18N
124
e.printStackTrace(); //XXX
125
}
126         return result;
127     }
128
129     private static Image loadWithImageIO (String JavaDoc s) {
130         Image result = null;
131         try {
132             URL JavaDoc url = UIUtils.class.getResource ( s );
133             result = ImageIO.read ( url );
134         } catch (Exception JavaDoc e) {
135             System.err.println ("Error loading image using ImageIO " + s); //NOI18N
136
e.printStackTrace();
137         }
138         return result;
139     }
140
141     private static Boolean JavaDoc openideAvailable = null;
142     private static Class JavaDoc<?> utilsClass = null;
143     private static Method JavaDoc utilsMethod = null;
144
145     //XXX move/duplicate org.netbeans.swing.tabcontrol.plaf.ColorUtil gradient paint caching?
146
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         //XXX deleteme once converted to relative colors
154
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     /**
161      * Rotates a float value around 0-1
162      */

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