1 19 20 package org.netbeans.modules.form; 21 22 import java.beans.*; 23 import java.util.*; 24 import javax.swing.*; 25 import javax.swing.plaf.metal.*; 26 import org.jdesktop.layout.LayoutStyle; 27 import org.openide.util.*; 28 import org.openide.ErrorManager; 29 30 35 public class FormLAF { 36 37 private static boolean initialized = false; 38 39 private static boolean lafBlockEntered; 40 41 private static DelegatingDefaults delDefaults; 42 43 private static Map netbeansDefaults = new HashMap(); 44 45 private static Map userDefaults = new HashMap(); 46 47 private static Map lafToTheme = new HashMap(); 48 49 private static boolean ideLafIsMetal; 50 private static boolean preview; 51 private static Class previewLaf; 52 53 private FormLAF() { 54 } 55 56 static UIDefaults initPreviewLaf(Class lafClass) { 57 try { 58 boolean previewLafIsMetal = MetalLookAndFeel.class.isAssignableFrom(lafClass); 59 if (!ideLafIsMetal && previewLafIsMetal && 60 !MetalLookAndFeel.class.equals(lafClass) && (lafToTheme.get(MetalLookAndFeel.class) == null)) { 61 lafToTheme.put(MetalLookAndFeel.class, MetalLookAndFeel.getCurrentTheme()); 62 } 63 LookAndFeel previewLookAndFeel = (LookAndFeel)lafClass.newInstance(); 64 if (previewLafIsMetal) { 65 MetalTheme theme = (MetalTheme)lafToTheme.get(lafClass); 66 if (theme == null) { 67 lafToTheme.put(lafClass, MetalLookAndFeel.getCurrentTheme()); 68 } else { 69 MetalLookAndFeel.setCurrentTheme(theme); 70 } 71 } 72 previewLookAndFeel.initialize(); 73 74 UIDefaults previewDefaults = previewLookAndFeel.getDefaults(); 75 76 if (previewLafIsMetal && ideLafIsMetal) { 77 LookAndFeel ideLaf = UIManager.getLookAndFeel(); 78 MetalTheme theme = (MetalTheme)lafToTheme.get(ideLaf.getClass()); 79 MetalLookAndFeel.setCurrentTheme(theme); 80 } 81 82 ClassLoader classLoader = lafClass.getClassLoader(); 83 if (classLoader != null) previewDefaults.put("ClassLoader", classLoader); 85 if (previewDefaults.get("LayoutStyle.instance") == null) { previewDefaults.put("LayoutStyle.instance", createLayoutStyle(previewLookAndFeel.getID())); 89 } 90 91 previewDefaults.putAll(delDefaults.getCustomizedUIDefaults()); 92 93 return previewDefaults; 94 } catch (Exception ex) { 95 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 96 } catch (LinkageError ex) { 97 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 98 } 99 return null; 100 } 101 102 private static void initialize() throws Exception { 103 initialized = true; 104 UIManager.getDefaults(); 106 LookAndFeel laf = UIManager.getLookAndFeel(); 107 ideLafIsMetal = laf instanceof MetalLookAndFeel; 108 if (ideLafIsMetal) { 109 lafToTheme.put(laf.getClass(), MetalLookAndFeel.getCurrentTheme()); 110 } 111 LookAndFeel original = laf; 112 try { 113 original = laf.getClass().newInstance(); 114 } catch (Exception ex) { 115 ex.printStackTrace(); 116 } 117 118 java.lang.reflect.Method method = UIManager.class.getDeclaredMethod("getLAFState", new Class [0]); method.setAccessible(true); 120 Object lafState = method.invoke(null, new Object [0]); 121 method = lafState.getClass().getDeclaredMethod("setLookAndFeelDefaults", new Class [] {UIDefaults.class}); method.setAccessible(true); 123 124 UIDefaults ide = UIManager.getLookAndFeelDefaults(); 125 assert !(ide instanceof DelegatingDefaults); 126 127 delDefaults = new DelegatingDefaults(null, original.getDefaults(), ide); 128 method.invoke(lafState, new Object [] {delDefaults}); 129 } 130 131 static Object executeWithLookAndFeel(final Mutex.ExceptionAction act) 132 throws Exception 133 { 134 try { 135 return Mutex.EVENT.readAccess(new Mutex.ExceptionAction() { 136 public Object run() throws Exception { 137 synchronized (UIManager.getDefaults()) { 141 boolean restoreAfter = true; 142 try { 143 if (lafBlockEntered) 144 restoreAfter = false; 145 else { 146 lafBlockEntered = true; 147 useDesignerLookAndFeel(); 148 restoreAfter = true; 149 } 150 return act.run(); 151 } 152 finally { 153 if (restoreAfter) { 154 useIDELookAndFeel(); 155 lafBlockEntered = false; 156 } 157 } 158 } 159 } 160 }); 161 } 162 catch (MutexException ex) { 163 throw ex.getException(); 164 } 165 } 166 167 static void executeWithLookAndFeel(final Runnable run) { 168 Mutex.EVENT.readAccess(new Mutex.Action() { 169 public Object run() { 170 synchronized (UIManager.getDefaults()) { 174 boolean restoreAfter = true; 175 try { 176 if (lafBlockEntered) 177 restoreAfter = false; 178 else { 179 lafBlockEntered = true; 180 useDesignerLookAndFeel(); 181 restoreAfter = true; 182 } 183 run.run(); 184 } 185 finally { 186 if (restoreAfter) { 187 useIDELookAndFeel(); 188 lafBlockEntered = false; 189 } 190 } 191 } 192 return null; 193 } 194 }); 195 } 196 197 private static void useDesignerLookAndFeel() { 198 if (!initialized) { 199 try { 200 initialize(); 201 } catch (Exception ex) { 202 ex.printStackTrace(); 203 } 204 } 205 UIDefaults defaults = UIManager.getDefaults(); 206 netbeansDefaults.clear(); 207 netbeansDefaults.putAll(defaults); 208 netbeansDefaults.keySet().removeAll(userDefaults.keySet()); 209 defaults.keySet().removeAll(netbeansDefaults.keySet()); 210 211 if (!preview) { 212 setUseDesignerDefaults(true); 213 } else if (MetalLookAndFeel.class.isAssignableFrom(previewLaf)) { 214 MetalLookAndFeel.setCurrentTheme((MetalTheme)lafToTheme.get(previewLaf)); 215 } 216 } 217 218 private static void useIDELookAndFeel() { 219 userDefaults.clear(); 220 userDefaults.putAll(UIManager.getDefaults()); 221 UIManager.getDefaults().putAll(netbeansDefaults); 222 223 if (!preview) { 224 setUseDesignerDefaults(false); 225 } else if (ideLafIsMetal) { 226 MetalLookAndFeel.setCurrentTheme((MetalTheme)lafToTheme.get(UIManager.getLookAndFeel().getClass())); 227 } 228 } 229 230 236 private static LayoutStyle createLayoutStyle(String lafID) { 237 boolean useCoreLayoutStyle = false; 238 try { 239 Class.forName("javax.swing.LayoutStyle"); useCoreLayoutStyle = true; 241 } catch (ClassNotFoundException cnfex) {} 242 String layoutStyleClass; 243 if (useCoreLayoutStyle) { 244 layoutStyleClass = "Swing"; } else if ("Metal" == lafID) { layoutStyleClass = "Metal"; } 248 else if ("Windows" == lafID) { layoutStyleClass = "Windows"; } 251 else if ("GTK" == lafID) { layoutStyleClass = "Gnome"; } 254 else if ("Aqua" == lafID) { layoutStyleClass = "Aqua"; } else { 257 layoutStyleClass = ""; } 259 layoutStyleClass = "org.jdesktop.layout." + layoutStyleClass + "LayoutStyle"; LayoutStyle layoutStyle = null; 261 try { 262 Class clazz = Class.forName(layoutStyleClass); 263 java.lang.reflect.Constructor constr = clazz.getDeclaredConstructor(new Class [0]); 264 constr.setAccessible(true); 265 layoutStyle = (LayoutStyle)constr.newInstance(null); 266 } catch (Exception ex) { 267 ex.printStackTrace(); 268 } 269 return layoutStyle; 270 } 271 272 static LayoutStyle getDesignerLayoutStyle() { 273 return LayoutStyle.getSharedInstance(); 274 } 275 276 static void setUseDesignerDefaults(boolean designerDefaults) { 277 delDefaults.setDelegating(designerDefaults); 278 } 279 280 static void setUsePreviewDefaults(boolean previewing, Class previewLAF, UIDefaults uiDefaults) { 281 preview = previewing; 282 previewLaf = previewLAF; 283 if (previewing) { 284 delDefaults.setPreviewDefaults(uiDefaults); 285 } 286 delDefaults.setPreviewing(previewing); 287 } 288 289 public static boolean getUsePreviewDefaults() { 290 return preview && !delDefaults.isDelegating(); 291 } 292 293 static void setCustomizingUIClasses(boolean customizing) { 294 delDefaults.setCustomizingUIClasses(customizing); 295 } 296 297 301 static class DelegatingDefaults extends UIDefaults { 302 303 private UIDefaults preview; 304 305 private UIDefaults original; 306 307 private UIDefaults ide; 308 309 private boolean delegating; 310 311 private boolean previewing; 312 313 private boolean customizingUI; 314 315 private Map customizedUIDefaults = new HashMap(); 316 317 DelegatingDefaults(UIDefaults preview, UIDefaults original, UIDefaults ide) { 318 this.preview = preview; 319 this.original = original; 320 this.ide = ide; 321 } 322 323 public void setPreviewDefaults(UIDefaults preview) { 324 this.preview = preview; 325 } 326 327 public void setDelegating(boolean delegating){ 328 this.delegating = delegating; 329 } 330 331 public boolean isDelegating() { 332 return delegating; 333 } 334 335 public void setPreviewing(boolean previewing){ 336 this.previewing = previewing; 337 } 338 339 public boolean isPreviewing() { 340 return previewing; 341 } 342 343 public void setCustomizingUIClasses(boolean customizing) { 344 customizingUI = customizing; 345 } 346 347 public Map getCustomizedUIDefaults() { 348 return customizedUIDefaults; 349 } 350 351 353 private UIDefaults getCurrentDefaults() { 354 return delegating ? original : (previewing ? preview : ide); 355 } 356 357 public Object get(Object key) { 358 return delegating ? original.get(key) : (previewing ? preview.get(key) : ide.get(key)); 359 } 360 361 public Object put(Object key, Object value) { 362 if (delegating) { 363 if (customizingUI) { 364 customizedUIDefaults.put(key, value); 365 } 366 return original.put(key, value); 367 } else { 368 return (previewing ? preview.put(key, value) : ide.put(key, value)); 369 } 370 } 371 372 public void putDefaults(Object [] keyValueList) { 373 getCurrentDefaults().putDefaults(keyValueList); 374 } 375 376 public Object get(Object key, Locale l) { 377 return getCurrentDefaults().get(key, l); 378 } 379 380 public synchronized void addResourceBundle(String bundleName) { 381 getCurrentDefaults().addResourceBundle(bundleName); 382 } 383 384 public synchronized void removeResourceBundle(String bundleName) { 385 getCurrentDefaults().removeResourceBundle(bundleName); 386 } 387 388 public void setDefaultLocale(Locale l) { 389 getCurrentDefaults().setDefaultLocale(l); 390 } 391 392 public Locale getDefaultLocale() { 393 return getCurrentDefaults().getDefaultLocale(); 394 } 395 396 public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { 397 getCurrentDefaults().addPropertyChangeListener(listener); 398 } 399 400 public synchronized void removePropertyChangeListener(PropertyChangeListener listener) { 401 getCurrentDefaults().removePropertyChangeListener(listener); 402 } 403 404 public synchronized PropertyChangeListener[] getPropertyChangeListeners() { 405 return getCurrentDefaults().getPropertyChangeListeners(); 406 } 407 408 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 409 System.out.println("Warning: FormLAF.firePropertyChange called, but not implemented."); } 411 412 } 413 414 } 415 | Popular Tags |