| 1 7 package javax.swing; 8 9 import java.awt.Component ; 10 import java.awt.Container ; 11 import java.awt.Window ; 12 import java.awt.Font ; 13 import java.awt.Color ; 14 import java.awt.Insets ; 15 import java.awt.Dimension ; 16 import java.awt.KeyboardFocusManager ; 17 import java.awt.KeyEventPostProcessor ; 18 import java.awt.Toolkit ; 19 20 import java.awt.event.KeyEvent ; 21 22 import java.security.AccessController ; 23 24 import javax.swing.plaf.ComponentUI ; 25 import javax.swing.border.Border ; 26 27 import javax.swing.event.SwingPropertyChangeSupport ; 28 import java.beans.PropertyChangeListener ; 29 import java.beans.PropertyChangeEvent ; 30 31 import java.io.FileOutputStream ; 32 import java.io.IOException ; 33 import java.io.ObjectOutputStream ; 34 import java.io.ObjectInputStream ; 35 import java.io.Serializable ; 36 import java.io.File ; 37 import java.io.FileInputStream ; 38 import java.io.BufferedInputStream ; 39 40 import java.util.ArrayList ; 41 import java.util.Enumeration ; 42 import java.util.Hashtable ; 43 import java.util.Properties ; 44 import java.util.StringTokenizer ; 45 import java.util.Vector ; 46 import java.util.Locale ; 47 48 import sun.security.action.GetPropertyAction; 49 50 92 public class UIManager implements Serializable 93 { 94 104 private static class LAFState 105 { 106 Properties swingProps; 107 private UIDefaults [] tables = new UIDefaults [2]; 108 109 boolean initialized = false; 110 MultiUIDefaults multiUIDefaults = new MultiUIDefaults (tables); 111 LookAndFeel lookAndFeel; 112 LookAndFeel multiLookAndFeel = null; 113 Vector auxLookAndFeels = null; 114 SwingPropertyChangeSupport changeSupport; 115 116 UIDefaults getLookAndFeelDefaults() { return tables[0]; } 117 void setLookAndFeelDefaults(UIDefaults x) { tables[0] = x; } 118 119 UIDefaults getSystemDefaults() { return tables[1]; } 120 void setSystemDefaults(UIDefaults x) { tables[1] = x; } 121 122 129 public synchronized SwingPropertyChangeSupport  130 getPropertyChangeSupport(boolean create) { 131 if (create && changeSupport == null) { 132 changeSupport = new SwingPropertyChangeSupport ( 133 UIManager .class); 134 } 135 return changeSupport; 136 } 137 } 138 139 140 143 private static final Object lafStateACKey = new StringBuffer ("LookAndFeel State"); 144 145 146 148 private static final Object classLock = new Object (); 149 150 151 158 private static Thread currentLAFStateThread = null; 159 private static LAFState currentLAFState = null; 160 161 162 170 private static LAFState getLAFState() { 171 Thread thisThread = Thread.currentThread(); 174 if (thisThread == currentLAFStateThread) { 175 return currentLAFState; 176 } 177 178 LAFState rv = (LAFState)SwingUtilities.appContextGet(lafStateACKey); 179 if (rv == null) { 180 synchronized (classLock) { 181 rv = (LAFState)SwingUtilities.appContextGet(lafStateACKey); 182 if (rv == null) { 183 SwingUtilities.appContextPut(lafStateACKey, 184 (rv = new LAFState())); 185 } 186 } 187 } 188 189 currentLAFStateThread = thisThread; 190 currentLAFState = rv; 191 192 return rv; 193 } 194 195 196 199 200 private static final String defaultLAFKey = "swing.defaultlaf"; 201 private static final String auxiliaryLAFsKey = "swing.auxiliarylaf"; 202 private static final String multiplexingLAFKey = "swing.plaf.multiplexinglaf"; 203 private static final String installedLAFsKey = "swing.installedlafs"; 204 private static final String disableMnemonicKey = "swing.disablenavaids"; 205 206 211 private static String makeInstalledLAFKey(String laf, String attr) { 212 return "swing.installedlaf." + laf + "." + attr; 213 } 214 215 220 private static String makeSwingPropertiesFilename() { 221 String sep = File.separator; 222 String javaHome = System.getProperty("java.home"); 225 if (javaHome == null) { 226 javaHome = "<java.home undefined>"; 227 } 228 return javaHome + sep + "lib" + sep + "swing.properties"; 229 } 230 231 232 240 public static class LookAndFeelInfo { 241 private String name; 242 private String className; 243 244 253 public LookAndFeelInfo(String name, String className) { 254 this.name = name; 255 this.className = className; 256 } 257 258 264 public String getName() { 265 return name; 266 } 267 268 274 public String getClassName() { 275 return className; 276 } 277 278 284 public String toString() { 285 return getClass().getName() + "[" + getName() + " " + getClassName() + "]"; 286 } 287 } 288 289 290 298 private static LookAndFeelInfo[] installedLAFs; 299 300 static { 301 ArrayList iLAFs = new ArrayList (4); 302 iLAFs.add(new LookAndFeelInfo( 303 "Metal", "javax.swing.plaf.metal.MetalLookAndFeel")); 304 iLAFs.add(new LookAndFeelInfo("CDE/Motif", 305 "com.sun.java.swing.plaf.motif.MotifLookAndFeel")); 306 307 String osName = (String )AccessController.doPrivileged( 309 new GetPropertyAction("os.name")); 310 if (osName != null && osName.indexOf("Windows") != -1) { 311 iLAFs.add(new LookAndFeelInfo("Windows", 312 "com.sun.java.swing.plaf.windows.WindowsLookAndFeel")); 313 if (Toolkit.getDefaultToolkit().getDesktopProperty( 314 "win.xpstyle.themeActive") != null) { 315 iLAFs.add(new LookAndFeelInfo("Windows Classic", 316 "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel")); 317 } 318 } 319 else { 320 iLAFs.add(new LookAndFeelInfo("GTK+", 322 "com.sun.java.swing.plaf.gtk.GTKLookAndFeel")); 323 } 324 installedLAFs = (LookAndFeelInfo[])iLAFs.toArray( 325 new LookAndFeelInfo[iLAFs.size()]); 326 } 327 328 329 347 public static LookAndFeelInfo[] getInstalledLookAndFeels() { 348 maybeInitialize(); 349 LookAndFeelInfo[] ilafs = installedLAFs; 350 LookAndFeelInfo[] rv = new LookAndFeelInfo[ilafs.length]; 351 System.arraycopy(ilafs, 0, rv, 0, ilafs.length); 352 return rv; 353 } 354 355 356 362 public static void setInstalledLookAndFeels(LookAndFeelInfo[] infos) 363 throws SecurityException  364 { 365 LookAndFeelInfo[] newInfos = new LookAndFeelInfo[infos.length]; 366 System.arraycopy(infos, 0, newInfos, 0, infos.length); 367 installedLAFs = newInfos; 368 } 369 370 371 377 public static void installLookAndFeel(LookAndFeelInfo info) { 378 LookAndFeelInfo[] infos = getInstalledLookAndFeels(); 379 LookAndFeelInfo[] newInfos = new LookAndFeelInfo[infos.length + 1]; 380 System.arraycopy(infos, 0, newInfos, 0, infos.length); 381 newInfos[infos.length] = info; 382 setInstalledLookAndFeels(newInfos); 383 } 384 385 386 395 public static void installLookAndFeel(String name, String className) { 396 installLookAndFeel(new LookAndFeelInfo(name, className)); 397 } 398 399 400 406 public static LookAndFeel getLookAndFeel() { 407 maybeInitialize(); 408 return getLAFState().lookAndFeel; 409 } 410 411 412 423 public static void setLookAndFeel(LookAndFeel newLookAndFeel) 424 throws UnsupportedLookAndFeelException 425 { 426 if ((newLookAndFeel != null) && !newLookAndFeel.isSupportedLookAndFeel()) { 427 String s = newLookAndFeel.toString() + " not supported on this platform"; 428 throw new UnsupportedLookAndFeelException (s); 429 } 430 431 LAFState lafState = getLAFState(); 432 LookAndFeel oldLookAndFeel = lafState.lookAndFeel; 433 if (oldLookAndFeel != null) { 434 oldLookAndFeel.uninitialize(); 435 } 436 437 lafState.lookAndFeel = newLookAndFeel; 438 if (newLookAndFeel != null) { 439 sun.swing.DefaultLookup.setDefaultLookup(null); 440 newLookAndFeel.initialize(); 441 lafState.setLookAndFeelDefaults(newLookAndFeel.getDefaults()); 442 } 443 else { 444 lafState.setLookAndFeelDefaults(null); 445 } 446 447 SwingPropertyChangeSupport changeSupport = lafState. 448 getPropertyChangeSupport(false); 449 if (changeSupport != null) { 450 changeSupport.firePropertyChange("lookAndFeel", oldLookAndFeel, 451 newLookAndFeel); 452 } 453 } 454 455 456 469 public static void setLookAndFeel(String className) 470 throws ClassNotFoundException , 471 InstantiationException , 472 IllegalAccessException , 473 UnsupportedLookAndFeelException 474 { 475 if ("javax.swing.plaf.metal.MetalLookAndFeel".equals(className)) { 476 setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel ()); 478 } 479 else { 480 Class lnfClass = SwingUtilities.loadSystemClass(className); 481 setLookAndFeel((LookAndFeel )(lnfClass.newInstance())); 482 } 483 } 484 485 486 499 public static String getSystemLookAndFeelClassName() { 500 String systemLAF = (String )AccessController.doPrivileged( 501 new GetPropertyAction("swing.systemlaf")); 502 if (systemLAF != null) { 503 return systemLAF; 504 } 505 String osName = (String )AccessController.doPrivileged( 506 new GetPropertyAction("os.name")); 507 508 if (osName != null) { 509 if (osName.indexOf("Windows") != -1) { 510 return "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; 511 } 512 else { 513 String desktop = (String )AccessController.doPrivileged( 514 new GetPropertyAction("sun.desktop")); 515 if ("gnome".equals(desktop)) { 516 return "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"; 518 } 519 if ((osName.indexOf("Solaris") != -1) || 520 (osName.indexOf("SunOS") != -1)) { 521 return "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; 522 } 523 } 524 } 525 return getCrossPlatformLookAndFeelClassName(); 526 } 527 528 529 540 public static String getCrossPlatformLookAndFeelClassName() { 541 String laf = (String )AccessController.doPrivileged( 542 new GetPropertyAction("swing.crossplatformlaf")); 543 if (laf != null) { 544 return laf; 545 } 546 return "javax.swing.plaf.metal.MetalLookAndFeel"; 547 } 548 549 550 555 public static UIDefaults getDefaults() { 556 maybeInitialize(); 557 return getLAFState().multiUIDefaults; 558 } 559 560 566 public static Font getFont(Object key) { 567 return getDefaults().getFont(key); 568 } 569 570 579 public static Font getFont(Object key, Locale l) { 580 return getDefaults().getFont(key,l); 581 } 582 583 589 public static Color getColor(Object key) { 590 return getDefaults().getColor(key); 591 } 592 593 602 public static Color getColor(Object key, Locale l) { 603 return getDefaults().getColor(key,l); 604 } 605 606 612 public static Icon getIcon(Object key) { 613 return getDefaults().getIcon(key); 614 } 615 616 625 public static Icon getIcon(Object key, Locale l) { 626 return getDefaults().getIcon(key,l); 627 } 628 629 635 public static Border getBorder(Object key) { 636 return getDefaults().getBorder(key); 637 } 638 639 648 public static Border getBorder(Object key, Locale l) { 649 return getDefaults().getBorder(key,l); 650 } 651 652 658 public static String getString(Object key) { 659 return getDefaults().getString(key); 660 } 661 662 670 public static String getString(Object key, Locale l) { 671 return getDefaults().getString(key,l); 672 } 673 674 683 static String getString(Object key, Component c) { 684 Locale l = (c == null) ? Locale.getDefault() : c.getLocale(); 685 return getString(key, l); 686 } 687 688 694 public static int getInt(Object key) { 695 return getDefaults().getInt(key); 696 } 697 698 707 public static int getInt(Object key, Locale l) { 708 return getDefaults().getInt(key,l); 709 } 710 711 722 static int getInt(Object key, int defaultValue) { 723 Object value = UIManager.get(key); 724 725 if (value instanceof Integer ) { 726 return ((Integer )value).intValue(); 727 } 728 if (value instanceof String ) { 729 try { 730 return Integer.parseInt((String )value); 731 } catch (NumberFormatException nfe) {} 732 } 733 return defaultValue; 734 } 735 736 745 public static boolean getBoolean(Object key) { 746 return getDefaults().getBoolean(key); 747 } 748 749 761 public static boolean getBoolean(Object key, Locale l) { 762 return getDefaults().getBoolean(key,l); 763 } 764 765 771 public static Insets getInsets(Object key) { 772 return getDefaults().getInsets(key); 773 } 774 775 784 public static Insets getInsets(Object key, Locale l) { 785 return getDefaults().getInsets(key,l); 786 } 787 788 794 public static Dimension getDimension(Object key) { 795 return getDefaults().getDimension(key); 796 } 797 798 807 public static Dimension getDimension(Object key, Locale l) { 808 return getDefaults().getDimension(key,l); 809 } 810 811 817 public static Object get(Object key) { 818 return getDefaults().get(key); 819 } 820 821 829 public static Object get(Object key, Locale l) { 830 return getDefaults().get(key,l); 831 } 832 833 840 public static Object put(Object key, Object value) { 841 return<
|