1 7 package javax.swing.plaf.synth; 8 9 import sun.swing.DefaultLookup; 10 import java.awt.*; 11 import java.awt.event.*; 12 import java.beans.*; 13 import java.io.*; 14 import java.lang.ref.*; 15 import java.text.*; 16 import java.util.*; 17 import javax.swing.*; 18 import javax.swing.border.*; 19 import javax.swing.plaf.*; 20 import javax.swing.plaf.basic.*; 21 import sun.awt.AppContext; 22 import sun.swing.plaf.synth.*; 23 24 47 public class SynthLookAndFeel extends BasicLookAndFeel { 48 51 static final Insets EMPTY_UIRESOURCE_INSETS = new InsetsUIResource( 52 0, 0, 0, 0); 53 54 57 private static final Object STYLE_FACTORY_KEY = 58 new StringBuffer ("com.sun.java.swing.plaf.gtk.StyleCache"); 59 60 64 private static SynthStyleFactory lastFactory; 65 71 private static boolean multipleApps; 72 75 private static AppContext lastContext; 76 77 static ComponentUI selectedUI; 79 static int selectedUIState; 81 82 85 private SynthStyleFactory factory; 86 87 91 private Map defaultsMap; 92 93 94 102 static void setSelectedUI(ComponentUI uix, boolean selected, 103 boolean focused, boolean enabled) { 104 selectedUI = uix; 105 selectedUIState = 0; 106 if (selected) { 107 selectedUIState = SynthConstants.SELECTED; 108 if (focused) { 109 selectedUIState |= SynthConstants.FOCUSED; 110 } 111 } 112 else { 113 selectedUIState = SynthConstants.FOCUSED; 114 if (enabled) { 115 selectedUIState |= SynthConstants.ENABLED; 116 } 117 else { 118 selectedUIState |= SynthConstants.DISABLED; 119 } 120 } 121 } 122 123 126 static void resetSelectedUI() { 127 selectedUI = null; 128 } 129 130 131 137 public static void setStyleFactory(SynthStyleFactory cache) { 138 synchronized(SynthLookAndFeel .class) { 141 AppContext context = AppContext.getAppContext(); 142 if (!multipleApps && context != lastContext && 143 lastContext != null) { 144 multipleApps = true; 145 } 146 lastFactory = cache; 147 lastContext = context; 148 context.put(STYLE_FACTORY_KEY, cache); 149 } 150 } 151 152 157 public static SynthStyleFactory getStyleFactory() { 158 synchronized(SynthLookAndFeel .class) { 159 if (!multipleApps) { 160 return lastFactory; 161 } 162 AppContext context = AppContext.getAppContext(); 163 164 if (lastContext == context) { 165 return lastFactory; 166 } 167 lastContext = context; 168 lastFactory = (SynthStyleFactory )AppContext.getAppContext().get 169 (STYLE_FACTORY_KEY); 170 return lastFactory; 171 } 172 } 173 174 180 static int getComponentState(Component c) { 181 if (c.isEnabled()) { 182 if (c.isFocusOwner()) { 183 return SynthUI.ENABLED | SynthUI.FOCUSED; 184 } 185 return SynthUI.ENABLED; 186 } 187 return SynthUI.DISABLED; 188 } 189 190 199 public static SynthStyle getStyle(JComponent c, Region region) { 200 return getStyleFactory().getStyle(c, region); 201 } 202 203 208 static boolean shouldUpdateStyle(PropertyChangeEvent event) { 209 String eName = event.getPropertyName(); 210 if ("name" == eName) { 211 return true; 213 } 214 if ("ancestor" == eName && event.getNewValue() != null) { 215 LookAndFeel laf = UIManager.getLookAndFeel(); 218 return (laf instanceof SynthLookAndFeel && 219 ((SynthLookAndFeel )laf). 220 shouldUpdateStyleOnAncestorChanged()); 221 } 222 return false; 223 } 224 225 231 static SynthStyle updateStyle(SynthContext context, SynthUI ui) { 232 SynthStyle newStyle = getStyle(context.getComponent(), 233 context.getRegion()); 234 SynthStyle oldStyle = context.getStyle(); 235 236 if (newStyle != oldStyle) { 237 if (oldStyle != null) { 238 oldStyle.uninstallDefaults(context); 239 } 240 context.setStyle(newStyle); 241 newStyle.installDefaults(context, ui); 242 } 243 return newStyle; 244 } 245 246 253 public static void updateStyles(Component c) { 254 _updateStyles(c); 255 c.repaint(); 256 } 257 258 private static void _updateStyles(Component c) { 260 if (c instanceof JComponent) { 261 String name = c.getName(); 265 c.setName(null); 266 if (name != null) { 267 c.setName(name); 268 } 269 ((JComponent)c).revalidate(); 270 } 271 Component[] children = null; 272 if (c instanceof JMenu) { 273 children = ((JMenu)c).getMenuComponents(); 274 } 275 else if (c instanceof Container) { 276 children = ((Container)c).getComponents(); 277 } 278 if (children != null) { 279 for(int i = 0; i < children.length; i++) { 280 updateStyles(children[i]); 281 } 282 } 283 } 284 285 291 public static Region getRegion(JComponent c) { 292 return Region.getRegion(c); 293 } 294 295 300 static Insets getPaintingInsets(SynthContext state, Insets insets) { 301 if (state.isSubregion()) { 302 insets = state.getStyle().getInsets(state, insets); 303 } 304 else { 305 insets = state.getComponent().getInsets(insets); 306 } 307 return insets; 308 } 309 310 315 static void update(SynthContext state, Graphics g) { 316 paintRegion(state, g, null); 317 } 318 319 324 static void updateSubregion(SynthContext state, Graphics g, 325 Rectangle bounds) { 326 paintRegion(state, g, bounds); 327 } 328 329 private static void paintRegion(SynthContext state, Graphics g, 330 Rectangle bounds) { 331 JComponent c = state.getComponent(); 332 SynthStyle style = state.getStyle(); 333 int x, y, width, height; 334 335 if (bounds == null) { 336 x = 0; 337 y = 0; 338 width = c.getWidth(); 339 height = c.getHeight(); 340 } 341 else { 342 x = bounds.x; 343 y = bounds.y; 344 width = bounds.width; 345 height = bounds.height; 346 } 347 348 boolean subregion = state.isSubregion(); 350 if ((subregion && style.isOpaque(state)) || 351 (!subregion && c.isOpaque())) { 352 g.setColor(style.getColor(state, ColorType.BACKGROUND)); 353 g.fillRect(x, y, width, height); 354 } 355 } 356 357 static boolean isLeftToRight(Component c) { 358 return c.getComponentOrientation().isLeftToRight(); 359 } 360 361 365 static Object getUIOfType(ComponentUI ui, Class klass) { 366 if (klass.isInstance(ui)) { 367 return ui; 368 } 369 return null; 370 } 371 372 379 public static ComponentUI createUI(JComponent c) { 380 String key = c.getUIClassID().intern(); 381 382 if (key == "ButtonUI") { 383 return SynthButtonUI.createUI(c); 384 } 385 else if (key == "CheckBoxUI") { 386 return SynthCheckBoxUI.createUI(c); 387 } 388 else if (key == "CheckBoxMenuItemUI") { 389 return SynthCheckBoxMenuItemUI.createUI(c); 390 } 391 else if (key == "ColorChooserUI") { 392 return SynthColorChooserUI.createUI(c); 393 } 394 else if (key == "ComboBoxUI") { 395 return SynthComboBoxUI.createUI(c); 396 } 397 else if (key == "DesktopPaneUI") { 398 return SynthDesktopPaneUI.createUI(c); 399 } 400 else if (key == "DesktopIconUI") { 401 return SynthDesktopIconUI.createUI(c); 402 } 403 else if (key == "EditorPaneUI") { 404 return SynthEditorPaneUI.createUI(c); 405 } 406 else if (key == "FileChooserUI") { 407 return SynthFileChooserUI.createUI(c); 408 } 409 else if (key == "FormattedTextFieldUI") { 410 return SynthFormattedTextFieldUI.createUI(c); 411 } 412 else if (key == "InternalFrameUI") { 413 return SynthInternalFrameUI.createUI(c); 414 } 415 else if (key == "LabelUI") { 416 return SynthLabelUI.createUI(c); 417 } 418 else if (key == "ListUI") { 419 return SynthListUI.createUI(c); 420 } 421 else if (key == "MenuBarUI") { 422 return SynthMenuBarUI.createUI(c); 423 } 424 else if (key == "MenuUI") { 425 return SynthMenuUI.createUI(c); 426 } 427 else if (key == "MenuItemUI") { 428 return SynthMenuItemUI.createUI(c); 429 } 430 else if (key == "OptionPaneUI") { 431 return SynthOptionPaneUI.createUI(c); 432 } 433 else if (key == "PanelUI") { 434 return SynthPanelUI.createUI(c); 435 } 436 else if (key == "PasswordFieldUI") { 437 return SynthPasswordFieldUI.createUI(c); 438 } 439 else if (key == "PopupMenuSeparatorUI") { 440 return SynthSeparatorUI.createUI(c); 441 } 442 else if (key == "PopupMenuUI") { 443 return SynthPopupMenuUI.createUI(c); 444 } 445 else if (key == "ProgressBarUI") { 446 return SynthProgressBarUI.createUI(c); 447 } 448 else if (key == "RadioButtonUI") { 449 return SynthRadioButtonUI.createUI(c); 450 } 451 else if (key == "RadioButtonMenuItemUI") { 452 return SynthRadioButtonMenuItemUI.createUI(c); 453 } 454 else if (key == "RootPaneUI") { 455 return SynthRootPaneUI.createUI(c); 456 } 457 else if (key == "ScrollBarUI") { 458 return SynthScrollBarUI.createUI(c); 459 } 460 else if (key == "ScrollPaneUI") { 461 return SynthScrollPaneUI.createUI(c); 462 } 463 else if (key == "SeparatorUI") { 464 return SynthSeparatorUI.createUI(c); 465 } 466 else if (key == "SliderUI") { 467 return SynthSliderUI.createUI(c); 468 } 469 else if (key == "SpinnerUI") { 470 return SynthSpinnerUI.createUI(c); 471 } 472 else if (key == "SplitPaneUI") { 473 return SynthSplitPaneUI.createUI(c); 474 } 475 else if (key == "TabbedPaneUI") { 476 return SynthTabbedPaneUI.createUI(c); 477 } 478 else if (key == "TableUI") { 479 return SynthTableUI.createUI(c); 480 } 481 else if (key == "TableHeaderUI") { 482 return SynthTableHeaderUI.createUI(c); 483 } 484 else if (key == "TextAreaUI") { 485 return SynthTextAreaUI.createUI(c); 486 } 487 else if (key == "TextFieldUI") { 488 return SynthTextFieldUI.createUI(c); 489 } 490 else if (key == "TextPaneUI") { 491 return SynthTextPaneUI.createUI(c); 492 } 493 else if (key == "ToggleButtonUI") { 494 return SynthToggleButtonUI.createUI(c); 495 } 496 else if (key == "ToolBarSeparatorUI") { 497 return SynthSeparatorUI.createUI(c); 498 } 499 else if (key == "ToolBarUI") { 500 return SynthToolBarUI.createUI(c); 501 } 502 else if (key == "ToolTipUI") { 503 return SynthToolTipUI.createUI(c); 504 } 505 else if (key == "TreeUI") { 506 return SynthTreeUI.createUI(c); 507 } 508 else if (key == "ViewportUI") { 509 return SynthViewportUI.createUI(c); 510 } 511 return null; 512 } 513 514 515 525 public SynthLookAndFeel() { 526 factory = new DefaultSynthStyleFactory (); 527 } 528 529 543 public void load(InputStream input, Class <?> resourceBase) throws 544 ParseException, IllegalArgumentException { 545 if (defaultsMap == null) { 546 defaultsMap = new HashMap(); 547 } 548 new SynthParser ().parse(input, (DefaultSynthStyleFactory )factory, 549 resourceBase, defaultsMap); 550 } 551 552 555 public void initialize() { 556 super.initialize(); 557 DefaultLookup.setDefaultLookup(new SynthDefaultLookup ()); 558 setStyleFactory(factory); 559 } 560 561 564 public void uninitialize() { 565 super.uninitialize(); 569 } 570 571 576 public UIDefaults getDefaults() { 577 UIDefaults table = new UIDefaults(); 578 Region.registerUIs(table); 579 table.setDefaultLocale(Locale.getDefault()); 580 table.addResourceBundle( 581 "com.sun.swing.internal.plaf.basic.resources.basic" ); 582 table.addResourceBundle("com.sun.swing.internal.plaf.synth.resources.synth"); 583 584 table.put("ColorChooser.swatchesRecentSwatchSize", 586 new Dimension(10, 10)); 587 table.put("ColorChooser.swatchesDefaultRecentColor", Color.RED); 588 table.put("ColorChooser.swatchesSwatchSize", new Dimension(10, 10)); 589 590 table.put("PopupMenu.selectedWindowInputMapBindings", new Object [] { 592 "ESCAPE", "cancel", 593 "DOWN", "selectNext", 594 "KP_DOWN", "selectNext", 595 "UP", "selectPrevious", 596 "KP_UP", "selectPrevious", 597 "LEFT", "selectParent", 598 "KP_LEFT", "selectParent", 599 "RIGHT", "selectChild", 600 "KP_RIGHT", "selectChild", 601 "ENTER", "return", 602 "SPACE", "return" 603 }); 604 table.put("PopupMenu.selectedWindowInputMapBindings.RightToLeft", 605 new Object [] { 606 "LEFT", "selectChild", 607 "KP_LEFT", "selectChild", 608 "RIGHT", "selectParent", 609 "KP_RIGHT", "selectParent", 610 }); 611 612 if (defaultsMap != null) { 613 table.putAll(defaultsMap); 614 } 615 return table; 616 } 617 618 623 public boolean isSupportedLookAndFeel() { 624 return true; 625 } 626 627 632 public boolean isNativeLookAndFeel() { 633 return false; 634 } 635 636 641 public String getDescription() { 642 return "Synth look and feel"; 643 } 644 645 650 public String getName() { 651 return "Synth look and feel"; 652 } 653 654 659 public String getID() { 660 return "Synth"; 661 } 662 663 675 public boolean shouldUpdateStyleOnAncestorChanged() { 676 return false; 677 } 678 679 private void writeObject(java.io.ObjectOutputStream out) 680 throws IOException { 681 throw new NotSerializableException(this.getClass().getName()); 682 } 683 } 684 | Popular Tags |