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 getDefaults().put(key, value); 842 } 843 844 850 public static ComponentUI getUI(JComponent target) { 851 maybeInitialize(); 852 ComponentUI ui = null; 853 LookAndFeel multiLAF = getLAFState().multiLookAndFeel; 854 if (multiLAF != null) { 855 ui = multiLAF.getDefaults().getUI(target); 858 } 859 if (ui == null) { 860 ui = getDefaults().getUI(target); 861 } 862 return ui; 863 } 864 865 866 871 public static UIDefaults getLookAndFeelDefaults() { 872 maybeInitialize(); 873 return getLAFState().getLookAndFeelDefaults(); 874 } 875 876 879 private static LookAndFeel getMultiLookAndFeel() { 880 LookAndFeel multiLookAndFeel = getLAFState().multiLookAndFeel; 881 if (multiLookAndFeel == null) { 882 String defaultName = "javax.swing.plaf.multi.MultiLookAndFeel"; 883 String className = getLAFState().swingProps.getProperty(multiplexingLAFKey, defaultName); 884 try { 885 Class lnfClass = SwingUtilities.loadSystemClass(className); 886 multiLookAndFeel = (LookAndFeel )lnfClass.newInstance(); 887 } catch (Exception exc) { 888 System.err.println("UIManager: failed loading " + className); 889 } 890 } 891 return multiLookAndFeel; 892 } 893 894 910 static public void addAuxiliaryLookAndFeel(LookAndFeel laf) { 911 maybeInitialize(); 912 913 if (!laf.isSupportedLookAndFeel()) { 914 return; 917 } 918 Vector v = getLAFState().auxLookAndFeels; 919 if (v == null) { 920 v = new Vector (); 921 } 922 923 if (!v.contains(laf)) { 924 v.addElement(laf); 925 laf.initialize(); 926 getLAFState().auxLookAndFeels = v; 927 928 if (getLAFState().multiLookAndFeel == null) { 929 getLAFState().multiLookAndFeel = getMultiLookAndFeel(); 930 } 931 } 932 } 933 934 949 static public boolean removeAuxiliaryLookAndFeel(LookAndFeel laf) { 950 maybeInitialize(); 951 952 boolean result; 953 954 Vector v = getLAFState().auxLookAndFeels; 955 if ((v == null) || (v.size() == 0)) { 956 return false; 957 } 958 959 result = v.removeElement(laf); 960 if (result) { 961 if (v.size() == 0) { 962 getLAFState().auxLookAndFeels = null; 963 getLAFState().multiLookAndFeel = null; 964 } else { 965 getLAFState().auxLookAndFeels = v; 966 } 967 } 968 laf.uninitialize(); 969 970 return result; 971 } 972 973 987 static public LookAndFeel [] getAuxiliaryLookAndFeels() { 988 maybeInitialize(); 989 990 Vector v = getLAFState().auxLookAndFeels; 991 if ((v == null) || (v.size() == 0)) { 992 return null; 993 } 994 else { 995 LookAndFeel [] rv = new LookAndFeel [v.size()]; 996 for (int i = 0; i < rv.length; i++) { 997 rv[i] = (LookAndFeel )v.elementAt(i); 998 } 999 return rv; 1000 } 1001 } 1002 1003 1004 1011 public static void addPropertyChangeListener(PropertyChangeListener listener) 1012 { 1013 synchronized (classLock) { 1014 getLAFState().getPropertyChangeSupport(true). 1015 addPropertyChangeListener(listener); 1016 } 1017 } 1018 1019 1020 1028 public static void removePropertyChangeListener(PropertyChangeListener listener) 1029 { 1030 synchronized (classLock) { 1031 getLAFState().getPropertyChangeSupport(true). 1032 removePropertyChangeListener(listener); 1033 } 1034 } 1035 1036 1037 1045 public static PropertyChangeListener [] getPropertyChangeListeners() { 1046 synchronized(classLock) { 1047 return getLAFState().getPropertyChangeSupport(true). 1048 getPropertyChangeListeners(); 1049 } 1050 } 1051 1052 private static Properties loadSwingProperties() 1053 { 1054 1057 if (UIManager .class.getClassLoader() != null) { 1058 return new Properties (); 1059 } 1060 else { 1061 final Properties props = new Properties (); 1062 1063 java.security.AccessController.doPrivileged( 1064 new java.security.PrivilegedAction () { 1065 public Object run() { 1066 try { 1067 File file = new File (makeSwingPropertiesFilename()); 1068 1069 if (file.exists()) { 1070 FileInputStream ins = new FileInputStream (file); 1073 props.load(ins); 1074 ins.close(); 1075 } 1076 } 1077 catch (Exception e) { 1078 } 1080 1081 checkProperty(props, defaultLAFKey); 1084 checkProperty(props, auxiliaryLAFsKey); 1085 checkProperty(props, multiplexingLAFKey); 1086 checkProperty(props, installedLAFsKey); 1087 checkProperty(props, disableMnemonicKey); 1088 return null; 1090 } 1091 }); 1092 return props; 1093 } 1094 } 1095 1096 private static void checkProperty(Properties props, String key) { 1097 String value = System.getProperty(key); 1100 if (value != null) { 1101 props.put(key, value); 1102 } 1103 } 1104 1105 1106 1112 private static void initializeInstalledLAFs(Properties swingProps) 1113 { 1114 String ilafsString = swingProps.getProperty(installedLAFsKey); 1115 if (ilafsString == null) { 1116 return; 1117 } 1118 1119 1123 Vector lafs = new Vector (); 1124 StringTokenizer st = new StringTokenizer (ilafsString, ",", false); 1125 while (st.hasMoreTokens()) { 1126 lafs.addElement(st.nextToken()); 1127 } 1128 1129 1133 Vector ilafs = new Vector (lafs.size()); 1134 for(int i = 0; i < lafs.size(); i++) { 1135 String laf = (String )lafs.elementAt(i); 1136 String name = swingProps.getProperty(makeInstalledLAFKey(laf, "name"), laf); 1137 String cls = swingProps.getProperty(makeInstalledLAFKey(laf, "class")); 1138 if (cls != null) { 1139 ilafs.addElement(new LookAndFeelInfo(name, cls)); 1140 } 1141 } 1142 1143 installedLAFs = new LookAndFeelInfo[ilafs.size()]; 1144 for(int i = 0; i < ilafs.size(); i++) { 1145 installedLAFs[i] = (LookAndFeelInfo)(ilafs.elementAt(i)); 1146 } 1147 } 1148 1149 1150 1158 private static void initializeDefaultLAF(Properties swingProps) 1159 { 1160 if (getLAFState().lookAndFeel != null) { 1161 return; 1162 } 1163 1164 String metalLnf = getCrossPlatformLookAndFeelClassName(); 1165 String lnfDefault = metalLnf; 1166 1167 String lnfName = "<undefined>" ; 1168 try { 1169 lnfName = swingProps.getProperty(defaultLAFKey, lnfDefault); 1170 setLookAndFeel(lnfName); 1171 } catch (Exception e) { 1172 try { 1173 lnfName = swingProps.getProperty(defaultLAFKey, metalLnf); 1174 setLookAndFeel(lnfName); 1175 } catch (Exception e2) { 1176 throw new Error ("can't load " + lnfName); 1177 } 1178 } 1179 } 1180 1181 1182 private static void initializeAuxiliaryLAFs(Properties swingProps) 1183 { 1184 String auxLookAndFeelNames = swingProps.getProperty(auxiliaryLAFsKey); 1185 if (auxLookAndFeelNames == null) { 1186 return; 1187 } 1188 1189 Vector auxLookAndFeels = new Vector (); 1190 1191 StringTokenizer p = new StringTokenizer (auxLookAndFeelNames,","); 1192 String factoryName; 1193 1194 1196 1197 while (p.hasMoreTokens()) { 1198 String className = p.nextToken(); 1199 try { 1200 Class lnfClass = SwingUtilities.loadSystemClass(className); 1201 LookAndFeel newLAF = (LookAndFeel )lnfClass.newInstance(); 1202 newLAF.initialize(); 1203 auxLookAndFeels.addElement(newLAF); 1204 } 1205 catch (Exception e) { 1206 System.err.println("UIManager: failed loading auxiliary look and feel " + className); 1207 } 1208 } 1209 1210 1215 if (auxLookAndFeels.size() == 0) { 1216 auxLookAndFeels = null; 1217 } 1218 else { 1219 getLAFState().multiLookAndFeel = getMultiLookAndFeel(); 1220 if (getLAFState().multiLookAndFeel == null) { 1221 auxLookAndFeels = null; 1222 } 1223 } 1224 1225 getLAFState().auxLookAndFeels = auxLookAndFeels; 1226 } 1227 1228 1229 private static void initializeSystemDefaults(Properties swingProps) { 1230 getLAFState().swingProps = swingProps; 1231 } 1232 1233 1234 1241 private static void maybeInitialize() { 1242 synchronized (classLock) { 1243 if (!getLAFState().initialized) { 1244 getLAFState().initialized = true; 1245 initialize(); 1246 } 1247 } 1248 } 1249 1250 1251 1254 private static void initialize() { 1255 Properties swingProps = loadSwingProperties(); 1256 initializeSystemDefaults(swingProps); 1257 initializeDefaultLAF(swingProps); 1258 initializeAuxiliaryLAFs(swingProps); 1259 initializeInstalledLAFs(swingProps); 1260 1261 String toolkitName = Toolkit.getDefaultToolkit().getClass().getName(); 1263 if (!"sun.awt.X11.XToolkit".equals(toolkitName)) { 1265 if (FocusManager.isFocusManagerEnabled()) { 1266 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 1267 setDefaultFocusTraversalPolicy( 1268 new LayoutFocusTraversalPolicy ()); 1269 } 1270 } 1271 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 1279 addKeyEventPostProcessor(new KeyEventPostProcessor () { 1280 public boolean postProcessKeyEvent(KeyEvent e) { 1281 Component c = e.getComponent(); 1282 1283 if ((!(c instanceof JComponent ) || 1284 (c != null && !((JComponent )c).isEnabled())) && 1285 JComponent.KeyboardState.shouldProcess(e) && 1286 SwingUtilities.processKeyBindings(e)) { 1287 e.consume(); 1288 return true; 1289 } 1290 return false; 1291 } 1292 }); 1293 } 1294} 1295 1296 | Popular Tags |