1 30 31 package com.jgoodies.looks.windows; 32 33 import java.awt.Color ; 34 import java.awt.Font ; 35 import java.lang.reflect.Method ; 36 37 import javax.swing.Icon ; 38 import javax.swing.UIDefaults ; 39 import javax.swing.border.Border ; 40 import javax.swing.plaf.DimensionUIResource ; 41 import javax.swing.plaf.FontUIResource ; 42 import javax.swing.plaf.InsetsUIResource ; 43 import javax.swing.plaf.basic.BasicBorders ; 44 45 import com.jgoodies.looks.FontSizeHints; 46 import com.jgoodies.looks.FontUtils; 47 import com.jgoodies.looks.LookUtils; 48 import com.jgoodies.looks.Options; 49 import com.jgoodies.looks.common.MinimumSizedIcon; 50 import com.jgoodies.looks.common.ShadowPopupFactory; 51 52 61 public final class WindowsLookAndFeel extends com.sun.java.swing.plaf.windows.WindowsLookAndFeel { 62 63 66 public static final String BORDER_STYLE_KEY = "jgoodies.windows.borderStyle"; 67 68 private static FontSizeHints fontSizeHints = null; 70 71 public String getID() { 72 return "JGoodies Windows"; 73 } 74 75 public String getName() { 76 return "JGoodies Windows"; 77 } 78 79 public String getDescription() { 80 return "The JGoodies Windows Look and Feel" 81 + " - \u00a9 2001-2005 JGoodies Karsten Lentzsch"; 82 } 83 84 86 96 public static FontSizeHints getFontSizeHints() { 97 return fontSizeHints != null 98 ? fontSizeHints 99 : Options.getGlobalFontSizeHints(); 100 } 101 102 108 public static void setFontSizeHints(FontSizeHints newHints) { 109 fontSizeHints = newHints; 110 } 111 112 113 115 121 public void initialize() { 122 super.initialize(); 123 ShadowPopupFactory.install(); 124 } 125 126 127 133 public void uninitialize() { 134 super.uninitialize(); 135 ShadowPopupFactory.uninstall(); 136 } 137 138 139 143 protected void initClassDefaults(UIDefaults table) { 144 super.initClassDefaults(table); 145 String WINDOWS_PREFIX = "com.jgoodies.looks.windows.Windows"; 146 String COMMON_PREFIX = "com.jgoodies.looks.common.ExtBasic"; 147 148 String menuUIPrefix = LookUtils.IS_LAF_WINDOWS_XP_ENABLED 149 ? WINDOWS_PREFIX 150 : COMMON_PREFIX; 151 152 Object [] uiDefaults = { 154 "ButtonUI", WINDOWS_PREFIX + "ButtonUI", 156 "ToggleButtonUI", WINDOWS_PREFIX + "ToggleButtonUI", 157 158 "ComboBoxUI", WINDOWS_PREFIX + "ComboBoxUI", 160 161 "ScrollPaneUI", WINDOWS_PREFIX + "ScrollPaneUI", 163 164 "MenuBarUI", WINDOWS_PREFIX + "MenuBarUI", 166 167 "MenuUI", menuUIPrefix + "MenuUI", 169 "MenuItemUI", COMMON_PREFIX + "MenuItemUI", 170 "CheckBoxMenuItemUI", COMMON_PREFIX + "CheckBoxMenuItemUI", 171 "RadioButtonMenuItemUI", COMMON_PREFIX + "RadioButtonMenuItemUI", 172 173 "PopupMenuSeparatorUI", COMMON_PREFIX + "PopupMenuSeparatorUI", 175 176 "OptionPaneUI", WINDOWS_PREFIX + "OptionPaneUI", 178 179 "SplitPaneUI", WINDOWS_PREFIX + "SplitPaneUI", 181 182 "TabbedPaneUI", WINDOWS_PREFIX + "TabbedPaneUI", 184 185 "TreeUI", WINDOWS_PREFIX + "TreeUI", 187 188 "SeparatorUI", WINDOWS_PREFIX + "SeparatorUI"}; 190 191 if (LookUtils.IS_JAVA_1_4_2_OR_LATER) { 192 uiDefaults = append(uiDefaults, 194 "SpinnerUI", WINDOWS_PREFIX + "SpinnerUI"); 195 } 196 197 198 if (LookUtils.IS_LAF_WINDOWS_XP_ENABLED) { 199 uiDefaults = append(uiDefaults, 201 "PasswordFieldUI", WINDOWS_PREFIX + "XPPasswordFieldUI"); 202 203 uiDefaults = append(uiDefaults, 206 "ToolBarUI", WINDOWS_PREFIX + "XPToolBarUI"); 207 208 uiDefaults = append(uiDefaults, 210 "TableHeaderUI", WINDOWS_PREFIX + "XPTableHeaderUI"); 211 } else { 212 uiDefaults = append(uiDefaults, 215 "ToolBarUI", WINDOWS_PREFIX + "ToolBarUI"); 216 217 uiDefaults = append(uiDefaults, 219 "ScrollBarUI", WINDOWS_PREFIX + "ScrollBarUI"); 220 221 if (!LookUtils.IS_JAVA_1_4_2_OR_LATER) { 222 uiDefaults = append(uiDefaults, 224 "ToolBarSeparatorUI", WINDOWS_PREFIX + "ToolBarSeparatorUI"); 225 } 226 } 227 table.putDefaults(uiDefaults); 228 } 229 230 233 protected void initComponentDefaults(UIDefaults table) { 234 super.initComponentDefaults(table); 235 236 final boolean isXP = LookUtils.IS_LAF_WINDOWS_XP_ENABLED; 237 238 if (FontUtils.useSystemFontSettings()) { 240 initFontDefaults(table); 241 } 242 243 if (!isXP) { 244 initComponentDefaultsNoXP(table); 245 } 246 247 Object marginBorder = new BasicBorders.MarginBorder (); 248 Object checkBoxMargin = new InsetsUIResource (2, 0, 2, 0); 249 250 Object etchedBorder = new UIDefaults.ProxyLazyValue ( 251 "javax.swing.plaf.BorderUIResource", 252 "getEtchedBorderUIResource"); 253 Object buttonBorder = new SimpleProxyLazyValue( 254 "com.jgoodies.looks.windows.WindowsLookAndFeel", 255 "getButtonBorder"); 256 Object menuBorder = WindowsBorders.getMenuBorder(); 257 258 Object menuBarEmptyBorder = marginBorder; 259 Object menuBarSeparatorBorder = WindowsBorders.getSeparatorBorder(); 260 Object menuBarEtchedBorder = WindowsBorders.getEtchedBorder(); 261 Object menuBarHeaderBorder = WindowsBorders.getMenuBarHeaderBorder(); 262 263 Object toolBarEmptyBorder = marginBorder; 264 Object toolBarSeparatorBorder = WindowsBorders.getSeparatorBorder(); 265 Object toolBarEtchedBorder = WindowsBorders.getEtchedBorder(); 266 Object toolBarHeaderBorder = WindowsBorders.getToolBarHeaderBorder(); 267 268 Object defaultButtonMargin = LookUtils.createButtonMargin(false); 269 Object narrowButtonMargin = LookUtils.createButtonMargin(true); 270 271 Object toolBarSeparatorSize = LookUtils.IS_JAVA_1_4_2_OR_LATER 272 ? null 273 : new DimensionUIResource (6, Options.getDefaultIconSize().height); 274 275 Object textInsets = new InsetsUIResource (2, 2, 2, 2); 276 277 Object comboRendererMargin = LookUtils.IS_JAVA_1_4 278 ? textInsets 279 : new InsetsUIResource (0, 0, 0, 0); 280 281 Object menuItemMargin = LookUtils.IS_LOW_RESOLUTION 282 ? new InsetsUIResource (3, 0, 3, 0) 283 : new InsetsUIResource (2, 0, 2, 0); 284 Object menuMargin = LookUtils.IS_LOW_RESOLUTION 285 ? new InsetsUIResource (2, 3, 2, 3) 286 : new InsetsUIResource (2, 4, 2, 4); 287 288 int pad = isXP ? 3 : 0; 289 Object popupMenuSeparatorMargin = LookUtils.IS_LOW_RESOLUTION 290 ? new InsetsUIResource (2, pad, 3, pad) 291 : new InsetsUIResource (3, pad, 4, pad); 292 293 Icon menuItemCheckIcon = new MinimumSizedIcon(); 294 295 int treeFontSize = table.getFont("Tree.font").getSize(); 297 Integer rowHeight = new Integer (treeFontSize + 6); 298 299 Class superclass = getClass().getSuperclass(); 300 Color controlColor = table.getColor("control"); 301 302 Object menuBarBackground = isXP 303 ? table.get("control") 304 : table.get("menu"); 305 Object menuSelectionBackground = isXP 306 ? table.get("MenuItem.selectionBackground") 307 : table.get("Menu.background"); 308 Object menuSelectionForeground = isXP 309 ? table.get("MenuItem.selectionForeground") 310 : table.get("Menu.foreground"); 311 312 Object [] defaults = { 313 "Button.border", buttonBorder, 314 "Button.margin", defaultButtonMargin, "Button.narrowMargin", narrowButtonMargin, 317 "CheckBox.border", marginBorder, 320 "CheckBox.margin", checkBoxMargin, 321 322 "ComboBox.editorBorder", marginBorder, 323 "ComboBox.editorColumns", new Integer (5), 324 "ComboBox.rendererMargin", comboRendererMargin, 326 "EditorPane.margin", textInsets, 327 328 "InternalFrame.icon", makeIcon(superclass, "icons/JavaCup.gif"), 330 331 332 "Menu.border", menuBorder, "Menu.borderPainted", Boolean.TRUE, 335 "Menu.background", menuBarBackground, 336 "Menu.selectionForeground", menuSelectionForeground, 337 "Menu.selectionBackground", menuSelectionBackground, 338 340 "Menu.margin", menuMargin, 342 "MenuBar.background", menuBarBackground, 343 "MenuBar.border", menuBarSeparatorBorder, "MenuBar.emptyBorder", menuBarEmptyBorder, "MenuBar.separatorBorder", menuBarSeparatorBorder, "MenuBar.etchedBorder", menuBarEtchedBorder, "MenuBar.headerBorder", menuBarHeaderBorder, 349 "MenuItem.borderPainted", Boolean.TRUE, 350 "MenuItem.checkIcon", menuItemCheckIcon, "MenuItem.margin", menuItemMargin, "CheckBoxMenuItem.margin", menuItemMargin, "RadioButtonMenuItem.margin", menuItemMargin, 355 "OptionPane.errorIcon", isXP ? makeIcon(getClass(), "icons/xp/Error.png") 356 : makeIcon(superclass, "icons/Error.gif"), 357 "OptionPane.informationIcon", isXP ? makeIcon(getClass(), "icons/xp/Inform.png") 358 : makeIcon(superclass, "icons/Inform.gif"), 359 "OptionPane.warningIcon", isXP ? makeIcon(getClass(), "icons/xp/Warn.png") 360 : makeIcon(superclass, "icons/Warn.gif"), 361 "OptionPane.questionIcon", isXP ? makeIcon(getClass(), "icons/xp/Inform.png") 362 : makeIcon(superclass, "icons/Question.gif"), 363 "FormattedTextField.margin", textInsets, "PasswordField.margin", textInsets, 366 "PopupMenu.border", WindowsBorders.getPopupMenuBorder(), 367 "PopupMenuSeparator.margin", popupMenuSeparatorMargin, 368 369 "ScrollPane.etchedBorder", etchedBorder, 371 "RadioButton.border", marginBorder, 374 "RadioButton.margin", checkBoxMargin, 375 376 "Table.gridColor", controlColor, "TextArea.margin", textInsets, "TextField.margin", textInsets, "ToggleButton.margin", defaultButtonMargin, "ToggleButton.narrowMargin", narrowButtonMargin, 382 "ToolBar.border", toolBarEmptyBorder, 383 "ToolBar.emptyBorder", toolBarEmptyBorder, "ToolBar.separatorBorder", toolBarSeparatorBorder, "ToolBar.etchedBorder", toolBarEtchedBorder, "ToolBar.headerBorder", toolBarHeaderBorder, "ToolBar.separatorSize", toolBarSeparatorSize, 388 "ToolBar.margin", new InsetsUIResource (0, 10, 0, 0), 389 390 "Tree.selectionBorderColor", controlColor, "Tree.rowHeight", rowHeight, "Tree.openIcon", isXP ? makeIcon(getClass(), "icons/xp/TreeOpen.png") 393 : makeIcon(getClass(), "icons/TreeOpen.gif"), 394 "Tree.closedIcon", isXP ? makeIcon(getClass(), "icons/xp/TreeClosed.png") 395 : makeIcon(getClass(), "icons/TreeClosed.gif"), 396 }; 397 table.putDefaults(defaults); 398 } 399 400 403 private void initComponentDefaultsNoXP(UIDefaults table) { 404 Object checkBoxIcon = new SimpleProxyLazyValue( 405 "com.jgoodies.looks.windows.WindowsLookAndFeel", 406 "getCheckBoxIcon"); 407 408 Object radioButtonIcon = new SimpleProxyLazyValue( 409 "com.jgoodies.looks.windows.WindowsLookAndFeel", 410 "getRadioButtonIcon"); 411 412 Border winInsetBorder = new BasicBorders.FieldBorder (table 413 .getColor("controlShadow"), table 414 .getColor("controlDkShadow"), 415 table.getColor("controlHighlight"), table 416 .getColor("controlLtHighlight")); 417 418 Object [] defaults = { 419 "CheckBox.checkColor", table.get("controlText"), "CheckBox.icon", checkBoxIcon, 421 "RadioButton.checkColor", table.get("controlText"), "RadioButton.icon", radioButtonIcon, 423 "Table.scrollPaneBorder", winInsetBorder, 425 }; 426 table.putDefaults(defaults); 427 } 428 429 434 private void initFontDefaults(UIDefaults table) { 435 Font messageFont; 436 Font toolTipFont; 437 Font windowFont; 438 439 Font menuFont = FontUtils.getMenuFont(table, getFontSizeHints()); 441 Font controlFont = FontUtils.getControlFont(table, getFontSizeHints()); 442 443 Font controlBoldFont = new FontUIResource (controlFont 445 .deriveFont(Font.BOLD)); 446 447 messageFont = table.getFont("OptionPane.font"); 448 toolTipFont = table.getFont("ToolTip.font"); 449 windowFont = table.getFont("InternalFrame.titleFont"); 450 451 FontUtils.initFontDefaults(table, controlFont, controlBoldFont, 452 controlFont, menuFont, messageFont, toolTipFont, windowFont); 453 } 454 455 457 public static Border getButtonBorder() { 458 return WindowsBorders.getButtonBorder(); 459 } 460 461 public static Icon getCheckBoxIcon() { 462 return WindowsIconFactory.getCheckBoxIcon(); 463 } 464 465 public static Icon getRadioButtonIcon() { 466 return WindowsIconFactory.getRadioButtonIcon(); 467 } 468 469 471 477 private static Object [] append(Object [] source, String key, Object value) { 478 int length = source.length; 479 Object [] destination = new Object [length + 2]; 480 System.arraycopy(source, 0, destination, 0, length); 481 destination[length] = key; 482 destination[length + 1] = value; 483 return destination; 484 } 485 486 488 498 private static class SimpleProxyLazyValue implements UIDefaults.LazyValue { 499 500 private final String className; 501 private final String methodName; 502 503 512 public SimpleProxyLazyValue(String c, String m) { 513 className = c; 514 methodName = m; 515 } 516 517 524 public Object createValue(UIDefaults table) { 525 Object instance = null; 526 try { 527 Class c; 528 ClassLoader classLoader = table != null 530 ? (ClassLoader ) table.get("ClassLoader") 531 : Thread.currentThread().getContextClassLoader(); 532 if (classLoader == null) { 533 classLoader = getClass().getClassLoader(); 534 } 535 c = Class.forName(className, true, classLoader); 536 Method m = c.getMethod(methodName, null); 537 instance = m.invoke(c, null); 538 } catch (Throwable t) { 539 LookUtils.log("Problem creating " + className + " with method " 540 + methodName + t); 541 } 542 return instance; 543 } 544 } 545 546 } | Popular Tags |