1 7 package com.sun.java.swing.plaf.nimbus; 8 9 import javax.swing.JComponent ; 10 import javax.swing.UIDefaults ; 11 import javax.swing.UIManager ; 12 import javax.swing.plaf.synth.Region ; 13 import javax.swing.plaf.synth.SynthLookAndFeel ; 14 import javax.swing.plaf.synth.SynthStyle ; 15 import javax.swing.plaf.synth.SynthStyleFactory ; 16 import javax.swing.plaf.UIResource ; 17 import java.security.AccessController ; 18 import java.awt.Color ; 19 import java.awt.Graphics2D ; 20 import java.awt.image.BufferedImage ; 21 import javax.swing.GrayFilter ; 22 import javax.swing.Icon ; 23 import javax.swing.border.TitledBorder ; 24 import javax.swing.plaf.BorderUIResource ; 25 import sun.swing.ImageIconUIResource; 26 import sun.swing.plaf.synth.SynthIcon; 27 import sun.swing.plaf.GTKKeybindings; 28 import sun.swing.plaf.WindowsKeybindings; 29 import sun.security.action.GetPropertyAction; 30 31 37 public class NimbusLookAndFeel extends SynthLookAndFeel { 38 39 40 private static final String [] COMPONENT_KEYS = new String []{"ArrowButton", "Button", 41 "CheckBox", "CheckBoxMenuItem", "ColorChooser", "ComboBox", 42 "DesktopPane", "DesktopIcon", "EditorPane", "FileChooser", 43 "FormattedTextField", "InternalFrame", 44 "InternalFrameTitlePane", "Label", "List", "Menu", 45 "MenuBar", "MenuItem", "OptionPane", "Panel", 46 "PasswordField", "PopupMenu", "PopupMenuSeparator", 47 "ProgressBar", "RadioButton", "RadioButtonMenuItem", 48 "RootPane", "ScrollBar", "ScrollBarTrack", "ScrollBarThumb", 49 "ScrollPane", "Separator", "Slider", "SliderTrack", 50 "SliderThumb", "Spinner", "SplitPane", "TabbedPane", 51 "Table", "TableHeader", "TextArea", "TextField", "TextPane", 52 "ToggleButton", "ToolBar", "ToolTip", "Tree", "Viewport"}; 53 54 59 private NimbusDefaults defaults; 60 61 64 private UIDefaults uiDefaults; 65 66 69 public NimbusLookAndFeel() { 70 super(); 71 defaults = new NimbusDefaults(); 72 } 73 74 75 @Override public void initialize() { 76 super.initialize(); 77 defaults.initialize(); 78 setStyleFactory(new SynthStyleFactory () { 80 @Override 81 public SynthStyle getStyle(JComponent c, Region r) { 82 return defaults.getStyle(c, r); 83 } 84 }); 85 } 86 87 88 89 @Override public void uninitialize() { 90 super.uninitialize(); 91 defaults.uninitialize(); 92 ImageCache.getInstance().flush(); 94 } 95 96 99 @Override public UIDefaults getDefaults() { 100 if (uiDefaults== null){ 101 String osName = getSystemProperty("os.name"); 103 boolean isWindows = osName != null && osName.contains("Windows"); 104 105 uiDefaults = super.getDefaults(); 107 defaults.initializeDefaults(uiDefaults); 108 109 if (isWindows) { 111 WindowsKeybindings.installKeybindings(uiDefaults); 112 } else { 113 GTKKeybindings.installKeybindings(uiDefaults); 114 } 115 116 uiDefaults.put("TitledBorder.titlePosition", 118 TitledBorder.ABOVE_TOP); 119 uiDefaults.put("TitledBorder.border", new BorderUIResource ( 120 new LoweredBorder())); 121 uiDefaults.put("TitledBorder.titleColor", 122 getDerivedColor("text",0.0f,0.0f,0.23f,0,true)); 123 uiDefaults.put("TitledBorder.font", 124 new NimbusDefaults.DerivedFont("defaultFont", 125 1f, true, null)); 126 127 uiDefaults.put("OptionPane.isYesLast", !isWindows); 129 130 uiDefaults.put("Table.scrollPaneCornerComponent", 132 TableScrollPaneCorner.class); 133 134 for (String componentKey : COMPONENT_KEYS) { 136 String key = componentKey+".foreground"; 137 if (!uiDefaults.containsKey(key)){ 138 uiDefaults.put(key, 139 new NimbusProperty(componentKey,"textForeground")); 140 } 141 key = componentKey+".background"; 142 if (!uiDefaults.containsKey(key)){ 143 uiDefaults.put(key, 144 new NimbusProperty(componentKey,"background")); 145 } 146 key = componentKey+".font"; 147 if (!uiDefaults.containsKey(key)){ 148 uiDefaults.put(key, 149 new NimbusProperty(componentKey,"font")); 150 } 151 key = componentKey+".disabledText"; 152 if (!uiDefaults.containsKey(key)){ 153 uiDefaults.put(key, 154 new NimbusProperty(componentKey,"Disabled", 155 "textForeground")); 156 } 157 key = componentKey+".disabled"; 158 if (!uiDefaults.containsKey(key)){ 159 uiDefaults.put(key, 160 new NimbusProperty(componentKey,"Disabled", 161 "background")); 162 } 163 } 164 165 uiDefaults.put("FileView.computerIcon", 168 new LinkProperty("FileChooser.homeFolderIcon")); 169 uiDefaults.put("FileView.directoryIcon", 170 new LinkProperty("FileChooser.directoryIcon")); 171 uiDefaults.put("FileView.fileIcon", 172 new LinkProperty("FileChooser.fileIcon")); 173 uiDefaults.put("FileView.floppyDriveIcon", 174 new LinkProperty("FileChooser.floppyDriveIcon")); 175 uiDefaults.put("FileView.hardDriveIcon", 176 new LinkProperty("FileChooser.hardDriveIcon")); 177 } 178 return uiDefaults; 179 } 180 181 186 @Override public String getName() { 187 return "Nimbus"; 188 } 189 190 195 @Override public String getID() { 196 return "Nimbus"; 197 } 198 199 204 @Override public String getDescription() { 205 return "Nimbus Look and Feel"; 206 } 207 208 212 @Override public boolean shouldUpdateStyleOnAncestorChanged() { 213 return true; 214 } 215 216 260 public void register(Region region, String prefix) { 261 defaults.register(region, prefix); 262 } 263 264 267 private String getSystemProperty(String key) { 268 return AccessController.doPrivileged(new GetPropertyAction(key)); 269 } 270 271 @Override 272 public Icon getDisabledIcon(JComponent component, Icon icon) { 273 if (icon instanceof SynthIcon) { 274 SynthIcon si = (SynthIcon)icon; 275 BufferedImage img = EffectUtils.createCompatibleTranslucentImage( 276 si.getIconWidth(), si.getIconHeight()); 277 Graphics2D gfx = img.createGraphics(); 278 si.paintIcon(component, gfx, 0, 0); 279 gfx.dispose(); 280 return new ImageIconUIResource(GrayFilter.createDisabledImage(img)); 281 } else { 282 return super.getDisabledIcon(component, icon); 283 } 284 } 285 286 299 public Color getDerivedColor(String uiDefaultParentName, 300 float hOffset, float sOffset, 301 float bOffset, int aOffset, 302 boolean uiResource) { 303 return defaults.getDerivedColor(uiDefaultParentName, hOffset, sOffset, 304 bOffset, aOffset, uiResource); 305 } 306 307 321 protected final Color getDerivedColor(Color color1, Color color2, 322 float midPoint, boolean uiResource) { 323 if (uiResource) { 324 return new DuoDerivedColor.UIResource(color1, color2, midPoint); 325 } else { 326 return new DuoDerivedColor(color1, color2, midPoint); 327 } 328 } 329 330 343 protected final Color getDerivedColor(Color color1, Color color2, 344 float midPoint) { 345 return getDerivedColor(color1, color2, midPoint, true); 346 } 347 348 351 private class LinkProperty implements UIDefaults.ActiveValue , UIResource { 352 private String dstPropName; 353 354 private LinkProperty(String dstPropName) { 355 this.dstPropName = dstPropName; 356 } 357 358 public Object createValue(UIDefaults table) { 359 return UIManager.get(dstPropName); 360 } 361 } 362 363 367 private class NimbusProperty implements UIDefaults.ActiveValue , UIResource { 368 private String prefix; 369 private String state = null; 370 private String suffix; 371 private boolean isFont; 372 373 private NimbusProperty(String prefix, String suffix) { 374 this.prefix = prefix; 375 this.suffix = suffix; 376 isFont = "font".equals(suffix); 377 } 378 379 private NimbusProperty(String prefix, String state, String suffix) { 380 this(prefix,suffix); 381 this.state = state; 382 } 383 384 391 public Object createValue(UIDefaults table) { 392 Object obj = null; 393 if (state!=null){ 395 obj = uiDefaults.get(prefix+"["+state+"]."+suffix); 396 } 397 if (obj==null){ 399 obj = uiDefaults.get(prefix+"[Enabled]."+suffix); 400 } 401 if (obj==null){ 403 if (isFont) { 404 obj = uiDefaults.get("defaultFont"); 405 } else { 406 obj = uiDefaults.get(suffix); 407 } 408 } 409 return obj; 410 } 411 } 412 } 413 | Popular Tags |