1 19 20 package org.netbeans.swing.plaf; 21 22 import java.awt.Toolkit ; 23 import org.netbeans.swing.plaf.aqua.AquaLFCustoms; 24 import org.netbeans.swing.plaf.gtk.GtkLFCustoms; 25 import org.netbeans.swing.plaf.metal.MetalLFCustoms; 26 import org.netbeans.swing.plaf.util.NbTheme; 27 import org.netbeans.swing.plaf.util.RelativeColor; 28 import org.netbeans.swing.plaf.util.UIBootstrapValue; 29 import org.netbeans.swing.plaf.util.UIUtils; 30 31 import javax.swing.*; 32 import javax.swing.plaf.metal.MetalLookAndFeel ; 33 import java.beans.PropertyChangeEvent ; 34 import java.beans.PropertyChangeListener ; 35 import java.net.URL ; 36 import java.util.Arrays ; 37 import java.util.HashSet ; 38 import java.util.Set ; 39 import org.netbeans.swing.plaf.winclassic.WindowsLFCustoms; 40 import org.netbeans.swing.plaf.winvista.VistaLFCustoms; 41 import org.netbeans.swing.plaf.winxp.XPLFCustoms; 42 43 50 public final class Startup { 51 53 55 private static final String FORCED_CUSTOMS = System.getProperty("nb.forceui"); 57 60 private static final boolean NO_CUSTOMIZATIONS = Boolean.getBoolean("netbeans.plaf.disable.ui.customizations"); 62 63 private static Startup instance = null; 64 65 66 private LFCustoms curCustoms = null; 67 private LFCustoms globalCustoms = null; 68 69 private static URL themeURL = null; 70 private static Class uiClass = null; 71 72 private boolean installed = false; 73 74 75 private Startup() { 76 initialize(); 77 } 78 79 82 private void initialize() { 83 LookAndFeel lf = getLookAndFeel(); 84 if (lf instanceof MetalLookAndFeel ) { 85 installTheme(lf); 87 } 88 91 try { 92 if (lf != UIManager.getLookAndFeel()) { 93 UIManager.setLookAndFeel (lf); 94 } 95 } catch (Exception e) { 96 System.err.println ("Could not install look and feel " + lf); 97 } 98 } 99 100 private LookAndFeel getLookAndFeel() { 101 if (uiClass == null) { 102 String uiClassName; 103 if (isWindows()) { 104 uiClassName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; } else if (isMac()){ 106 uiClassName = "apple.laf.AquaLookAndFeel"; 107 } else if (shouldUseMetal()) { 108 uiClassName = "javax.swing.plaf.metal.MetalLookAndFeel"; } else { 110 uiClassName = UIManager.getSystemLookAndFeelClassName(); 112 113 String javaVersion = System.getProperty("java.version"); 116 if ("1.6.0_01".compareTo(javaVersion) > 0) { 117 if (uiClassName.indexOf("gtk") >= 0 && !Boolean.getBoolean("useGtk")) { 119 uiClassName = "javax.swing.plaf.metal.MetalLookAndFeel"; 120 } 121 } else { 122 if (uiClassName.indexOf("gtk") >= 0 && System.getProperty("useGtk") != null && !Boolean.getBoolean("useGtk")) { 124 uiClassName = "javax.swing.plaf.metal.MetalLookAndFeel"; 125 } 126 } 127 } 128 try { 129 uiClass = Class.forName(uiClassName); 130 } catch (ClassNotFoundException e) { 131 System.err.println("Custom UI class " + uiClassName + " not on classpath."); } catch (Exception e) { 133 System.err.println("While loading: " + uiClassName); e.printStackTrace(); 135 } 136 } 137 LookAndFeel result = null; 138 if (uiClass != null) { 139 try { 140 141 LookAndFeel lf = UIManager.getLookAndFeel(); 142 if (uiClass != lf.getClass()) { 143 result = (LookAndFeel ) uiClass.newInstance(); 144 } else { 145 result = UIManager.getLookAndFeel(); 146 } 147 } catch (Exception e) { 148 System.err.println("Cannot load custom UI class " + uiClass); e.printStackTrace(); 150 result = UIManager.getLookAndFeel(); 151 } 152 } 153 return result; 154 } 155 156 private void installTheme(LookAndFeel lf) { 157 if (themeURL != null) { 159 NbTheme nbTheme = new NbTheme(themeURL, lf); 160 MetalLookAndFeel.setCurrentTheme(nbTheme); 161 } 162 } 163 164 165 private void install () { 166 if (installed) { 167 return; 168 } 169 if (globalCustoms == null) { 170 globalCustoms = new AllLFCustoms(); 171 installLFCustoms (globalCustoms); 172 } 173 installPerLFDefaults(); 174 installTheme(UIManager.getLookAndFeel()); 175 attachListener(); 176 } 177 178 private void installPerLFDefaults() { 179 boolean isLFChange = curCustoms != null; 180 181 curCustoms = findCustoms(); 182 if (curCustoms != null) { 183 Integer in = (Integer ) UIManager.get(LFCustoms.CUSTOM_FONT_SIZE); if (in == null && UIManager.getLookAndFeel().getClass() == MetalLookAndFeel .class) { 185 in = new Integer (11); 186 } 187 188 if (in != null) { 189 AllLFCustoms.initCustomFontSize (in.intValue()); 190 } 191 installLFCustoms (curCustoms); 192 if (isLFChange) { 193 loadAllLazyValues (curCustoms); 196 } 197 curCustoms.disposeValues(); 198 } 199 } 200 201 private void loadAllLazyValues (LFCustoms customs) { 202 if (globalCustoms != null) { 203 loadLazy (globalCustoms.getApplicationSpecificKeysAndValues()); 204 loadLazy (globalCustoms.getGuaranteedKeysAndValues()); 205 loadLazy (globalCustoms.getLookAndFeelCustomizationKeysAndValues()); 206 } 207 loadLazy (customs.getApplicationSpecificKeysAndValues()); 208 loadLazy (customs.getGuaranteedKeysAndValues()); 209 loadLazy (customs.getLookAndFeelCustomizationKeysAndValues()); 210 } 211 212 private void loadLazy (Object [] o) { 213 if (o.length > 0) { 214 UIDefaults uidefaults = UIManager.getDefaults(); 215 for (int i=1; i < o.length; i+=2) { 216 if (o[i] instanceof UIBootstrapValue.Lazy) { 217 ((UIBootstrapValue.Lazy) o[i]).createValue(uidefaults); 218 } 219 if (o[i] instanceof RelativeColor) { 220 ((RelativeColor) o[i]).clear(); 221 } 222 } 223 } 224 } 225 226 private void uninstallPerLFDefaults() { 227 assert globalCustoms != null; 228 229 if (curCustoms != null) { 230 Set <Object > keep = new HashSet <Object > (Arrays.asList(globalCustoms.allKeys())); 231 Object [] arr = curCustoms.allKeys(); 232 233 for (int i=0; i < arr.length; i++) { 234 Object key = arr[i]; 235 if (!keep.contains(key)) { 236 UIManager.put (key, null); 237 } 238 } 239 } 240 } 241 242 private void attachListener() { 243 assert listener == null; 244 listener = new LFListener(); 245 UIManager.addPropertyChangeListener(listener); 246 Toolkit.getDefaultToolkit().addPropertyChangeListener( 247 "win.xpstyle.themeActive", listener); } 249 250 private void installLFCustoms (LFCustoms customs) { 251 UIDefaults defaults = UIManager.getDefaults(); 252 253 defaults.put("ClassLoader", new CLValue()); 256 defaults.putDefaults (customs.getGuaranteedKeysAndValues()); 259 defaults.putDefaults (customs.getApplicationSpecificKeysAndValues()); 262 263 if (!NO_CUSTOMIZATIONS) { 264 defaults.putDefaults (customs.getLookAndFeelCustomizationKeysAndValues()); 267 } 268 269 } 270 271 273 private static final class CLValue implements UIDefaults.ActiveValue { 274 public Object createValue (UIDefaults defs) { 275 return Thread.currentThread().getContextClassLoader(); 276 } 277 } 278 279 282 private LFCustoms findCustoms () { 283 if (FORCED_CUSTOMS != null) { 284 System.err.println("Using explicitly set UI customizations: " + FORCED_CUSTOMS); 286 if ("Vista".equals(FORCED_CUSTOMS)) { return new VistaLFCustoms(); 288 } else if ("XP".equals(FORCED_CUSTOMS)) { return new XPLFCustoms(); 290 } else if ("Aqua".equals(FORCED_CUSTOMS)) { return new AquaLFCustoms(); 292 } else if ("Metal".equals(FORCED_CUSTOMS)) { return new MetalLFCustoms(); 294 } else if ("Windows".equals(FORCED_CUSTOMS)) { return new WindowsLFCustoms(); 296 } else if ("GTK".equals(FORCED_CUSTOMS)) { return new GtkLFCustoms(); 298 } else { 299 try { 300 return (LFCustoms) Class.forName(FORCED_CUSTOMS).newInstance(); 301 } catch (Exception e) { 302 System.err.println("UI customizations class not found: " + FORCED_CUSTOMS); } 305 } 306 } 307 308 StringBuffer buf = new StringBuffer (40); 309 buf.append("Nb."); buf.append(UIManager.getLookAndFeel().getID()); 311 if (UIUtils.isXPLF()) { 312 if (isWindowsVista()) { 313 buf.append("VistaLFCustoms"); } else { 315 buf.append("XPLFCustoms"); } 317 } else { 318 buf.append("LFCustoms"); } 320 321 LFCustoms result = null; 322 try { 323 result = (LFCustoms)UIManager.get(buf.toString()); 324 } catch (ClassCastException cce) { 325 } 328 if (result == null) { 329 String [] knownLFs = new String [] { 330 "Metal", "Windows", "Aqua", "GTK" }; 332 switch (Arrays.asList(knownLFs).indexOf(UIManager.getLookAndFeel().getID())) { 333 case 1 : 334 if (UIUtils.isXPLF()) { 335 if (isWindowsVista()) { 336 result = new VistaLFCustoms(); 337 } else { 338 result = new XPLFCustoms(); 339 } 340 } else { 341 result = new WindowsLFCustoms(); 342 } 343 break; 344 case 0 : 345 result = new MetalLFCustoms(); 346 break; 347 case 2 : 348 result = new AquaLFCustoms(); 349 break; 350 case 3 : 351 result = new GtkLFCustoms(); 352 break; 353 default : 354 if (UIUtils.isXPLF()) { 356 if (isWindowsVista()) { 357 result = new VistaLFCustoms(); 358 } else { 359 result = new XPLFCustoms(); 360 } 361 } else { 362 result = new WindowsLFCustoms(); 363 } 364 } 365 } 366 return result; 367 } 368 369 381 public static void run (Class uiClass, int uiFontSize, URL themeURL) { 382 if (instance == null) { 383 if(uiFontSize>0) { 385 Integer customFontSize = new Integer (uiFontSize); 386 UIManager.put ("customFontSize", customFontSize); 387 } 388 Startup.uiClass = uiClass; 389 Startup.themeURL = themeURL; 390 instance = new Startup(); 391 instance.install(); 392 } 393 } 394 395 private boolean isWindows() { 396 String osName = System.getProperty ("os.name"); 397 return osName.startsWith("Windows"); 398 } 399 400 private boolean isWindowsVista() { 401 String osName = System.getProperty ("os.name"); 402 return osName.indexOf("Vista") >= 0 403 || (osName.equals( "Windows NT (unknown)" ) && "6.0".equals( System.getProperty("os.version") )); 404 } 405 406 private boolean isMac() { 407 String osName = System.getProperty ("os.name"); 408 boolean result = osName.startsWith ("Darwin") || "Mac OS X".equals(osName); 409 return result; 410 } 411 412 418 private boolean shouldUseMetal() { 419 String osName = System.getProperty ("os.name"); 420 boolean result = !"Solaris".equals (osName) && 421 !osName.startsWith ("SunOS") && 422 !osName.endsWith ("Linux") || 423 UIManager.getSystemLookAndFeelClassName().indexOf("Motif") > -1; 424 return result; 425 } 426 427 private LFListener listener = null; 428 private class LFListener implements PropertyChangeListener { 429 public void propertyChange (PropertyChangeEvent pcl) { 430 if ("lookAndFeel".equals(pcl.getPropertyName()) || "win.xpstyle.themeActive".equals(pcl.getPropertyName())) { uninstallPerLFDefaults(); 432 installPerLFDefaults(); 433 } 434 } 435 } 436 437 } 438 | Popular Tags |