1 7 8 package javax.swing; 9 10 11 import javax.swing.plaf.ComponentUI ; 12 import javax.swing.border.*; 13 import javax.swing.event.SwingPropertyChangeSupport ; 14 15 import java.lang.reflect.*; 16 import java.util.HashMap ; 17 import java.util.Map ; 18 import java.util.Enumeration ; 19 import java.util.Hashtable ; 20 import java.util.ResourceBundle ; 21 import java.util.Locale ; 22 import java.util.Vector ; 23 import java.util.MissingResourceException ; 24 import java.awt.Font ; 25 import java.awt.Color ; 26 import java.awt.Insets ; 27 import java.awt.Dimension ; 28 import java.lang.reflect.Method ; 29 import java.beans.PropertyChangeListener ; 30 import java.beans.PropertyChangeEvent ; 31 import java.security.AccessController ; 32 import java.security.AccessControlContext ; 33 import java.security.PrivilegedAction ; 34 35 import sun.reflect.misc.MethodUtil; 36 37 54 public class UIDefaults extends Hashtable <Object ,Object > 55 { 56 private static final Object PENDING = new String ("Pending"); 57 58 private SwingPropertyChangeSupport changeSupport; 59 60 private Vector resourceBundles; 61 62 private Locale defaultLocale = Locale.getDefault(); 63 64 70 private Map resourceCache; 71 72 75 public UIDefaults() { 76 super(700, .75f); 77 resourceCache = new HashMap (); 78 } 79 80 81 95 public UIDefaults(Object [] keyValueList) { 96 super(keyValueList.length / 2); 97 for(int i = 0; i < keyValueList.length; i += 2) { 98 super.put(keyValueList[i], keyValueList[i + 1]); 99 } 100 } 101 102 103 129 public Object get(Object key) { 130 Object value = getFromHashtable( key ); 131 return (value != null) ? value : getFromResourceBundle(key, null); 132 } 133 134 138 private Object getFromHashtable(Object key) { 139 142 Object value = super.get(key); 143 if ((value != PENDING) && 144 !(value instanceof ActiveValue) && 145 !(value instanceof LazyValue)) { 146 return value; 147 } 148 149 155 synchronized(this) { 156 value = super.get(key); 157 if (value == PENDING) { 158 do { 159 try { 160 this.wait(); 161 } 162 catch (InterruptedException e) { 163 } 164 value = super.get(key); 165 } 166 while(value == PENDING); 167 return value; 168 } 169 else if (value instanceof LazyValue) { 170 super.put(key, PENDING); 171 } 172 else if (!(value instanceof ActiveValue)) { 173 return value; 174 } 175 } 176 177 180 if (value instanceof LazyValue) { 181 try { 182 185 value = ((LazyValue)value).createValue(this); 186 } 187 finally { 188 synchronized(this) { 189 if (value == null) { 190 super.remove(key); 191 } 192 else { 193 super.put(key, value); 194 } 195 this.notifyAll(); 196 } 197 } 198 } 199 else { 200 value = ((ActiveValue)value).createValue(this); 201 } 202 203 return value; 204 } 205 206 207 232 public Object get(Object key, Locale l) { 233 Object value = getFromHashtable( key ); 234 return (value != null) ? value : getFromResourceBundle(key, l); 235 } 236 237 240 private Object getFromResourceBundle(Object key, Locale l) { 241 242 if( resourceBundles == null || 243 resourceBundles.isEmpty() || 244 !(key instanceof String ) ) { 245 return null; 246 } 247 248 if( l == null ) { 250 if( defaultLocale == null ) 251 return null; 252 else 253 l = (Locale )defaultLocale; 254 } 255 256 synchronized(this) { 257 return getResourceCache(l).get((String )key); 258 } 259 } 260 261 264 private Map getResourceCache(Locale l) { 265 Map values = (Map )resourceCache.get(l); 266 267 if (values == null) { 268 values = new HashMap (); 269 for (int i=resourceBundles.size()-1; i >= 0; i--) { 270 String bundleName = (String )resourceBundles.get(i); 271 try { 272 ResourceBundle b = ResourceBundle.getBundle(bundleName, l); 273 Enumeration keys = b.getKeys(); 274 275 while (keys.hasMoreElements()) { 276 String key = (String )keys.nextElement(); 277 278 if (values.get(key) == null) { 279 Object value = b.getObject(key); 280 281 values.put(key, value); 282 } 283 } 284 } catch( MissingResourceException mre ) { 285 } 287 } 288 resourceCache.put(l, values); 289 } 290 return values; 291 } 292 293 307 public Object put(Object key, Object value) { 308 Object oldValue = (value == null) ? super.remove(key) : super.put(key, value); 309 if (key instanceof String ) { 310 firePropertyChange((String )key, oldValue, value); 311 } 312 return oldValue; 313 } 314 315 316 327 public void putDefaults(Object [] keyValueList) { 328 for(int i = 0, max = keyValueList.length; i < max; i += 2) { 329 Object value = keyValueList[i + 1]; 330 if (value == null) { 331 super.remove(keyValueList[i]); 332 } 333 else { 334 super.put(keyValueList[i], value); 335 } 336 } 337 firePropertyChange("UIDefaults", null, null); 338 } 339 340 341 349 public Font getFont(Object key) { 350 Object value = get(key); 351 return (value instanceof Font ) ? (Font )value : null; 352 } 353 354 355 366 public Font getFont(Object key, Locale l) { 367 Object value = get(key,l); 368 return (value instanceof Font ) ? (Font )value : null; 369 } 370 371 379 public Color getColor(Object key) { 380 Object value = get(key); 381 return (value instanceof Color ) ? (Color )value : null; 382 } 383 384 385 396 public Color getColor(Object key, Locale l) { 397 Object value = get(key,l); 398 return (value instanceof Color ) ? (Color )value : null; 399 } 400 401 402 410 public Icon getIcon(Object key) { 411 Object value = get(key); 412 return (value instanceof Icon ) ? (Icon )value : null; 413 } 414 415 416 427 public Icon getIcon(Object key, Locale l) { 428 Object value = get(key,l); 429 return (value instanceof Icon ) ? (Icon )value : null; 430 } 431 432 433 441 public Border getBorder(Object key) { 442 Object value = get(key); 443 return (value instanceof Border) ? (Border)value : null; 444 } 445 446 447 458 public Border getBorder(Object key, Locale l) { 459 Object value = get(key,l); 460 return (value instanceof Border) ? (Border)value : null; 461 } 462 463 464 472 public String getString(Object key) { 473 Object value = get(key); 474 return (value instanceof String ) ? (String )value : null; 475 } 476 477 488 public String getString(Object key, Locale l) { 489 Object value = get(key,l); 490 return (value instanceof String ) ? (String )value : null; 491 } 492 493 500 public int getInt(Object key) { 501 Object value = get(key); 502 return (value instanceof Integer ) ? ((Integer )value).intValue() : 0; 503 } 504 505 506 516 public int getInt(Object key, Locale l) { 517 Object value = get(key,l); 518 return (value instanceof Integer ) ? ((Integer )value).intValue() : 0; 519 } 520 521 522 531 public boolean getBoolean(Object key) { 532 Object value = get(key); 533 return (value instanceof Boolean ) ? ((Boolean )value).booleanValue() : false; 534 } 535 536 537 548 public boolean getBoolean(Object key, Locale l) { 549 Object value = get(key,l); 550 return (value instanceof Boolean ) ? ((Boolean )value).booleanValue() : false; 551 } 552 553 554 562 public Insets getInsets(Object key) { 563 Object value = get(key); 564 return (value instanceof Insets ) ? (Insets )value : null; 565 } 566 567 568 579 public Insets getInsets(Object key, Locale l) { 580 Object value = get(key,l); 581 return (value instanceof Insets ) ? (Insets )value : null; 582 } 583 584 585 593 public Dimension getDimension(Object key) { 594 Object value = get(key); 595 return (value instanceof Dimension ) ? (Dimension )value : null; 596 } 597 598 599 610 public Dimension getDimension(Object key, Locale l) { 611 Object value = get(key,l); 612 return (value instanceof Dimension ) ? (Dimension )value : null; 613 } 614 615 616 636 public Class <? extends ComponentUI > 637 getUIClass(String uiClassID, ClassLoader uiClassLoader) 638 { 639 try { 640 String className = (String )get(uiClassID); 641 if (className != null) { 642 Class cls = (Class )get(className); 643 if (cls == null) { 644 if (uiClassLoader == null) { 645 cls = SwingUtilities.loadSystemClass(className); 646 } 647 else { 648 cls = uiClassLoader.loadClass(className); 649 } 650 if (cls != null) { 651 put(className, cls); 653 } 654 } 655 return cls; 656 } 657 } 658 catch (ClassNotFoundException e) { 659 return null; 660 } 661 catch (ClassCastException e) { 662 return null; 663 } 664 return null; 665 } 666 667 668 675 public Class <? extends ComponentUI > getUIClass(String uiClassID) { 676 return getUIClass(uiClassID, null); 677 } 678 679 680 688 protected void getUIError(String msg) { 689 System.err.println("UIDefaults.getUI() failed: " + msg); 690 try { 691 throw new Error (); 692 } 693 catch (Throwable e) { 694 e.printStackTrace(); 695 } 696 } 697 698 712 public ComponentUI getUI(JComponent target) { 713 714 Object cl = get("ClassLoader"); 715 ClassLoader uiClassLoader = 716 (cl != null) ? (ClassLoader )cl : target.getClass().getClassLoader(); 717 Class uiClass = getUIClass(target.getUIClassID(), uiClassLoader); 718 Object uiObject = null; 719 720 if (uiClass == null) { 721 getUIError("no ComponentUI class for: " + target); 722 } 723 else { 724 try { 725 Method m = (Method )get(uiClass); 726 if (m == null) { 727 Class acClass = javax.swing.JComponent .class; 728 m = uiClass.getMethod("createUI", new Class []{acClass}); 729 put(uiClass, m); 730 } 731 uiObject = MethodUtil.invoke(m, null, new Object []{target}); 732 } 733 catch (NoSuchMethodException e) { 734 getUIError("static createUI() method not found in " + uiClass); 735 } 736 catch (Exception e) { 737 getUIError("createUI() failed for " + target + " " + e); 738 } 739 } 740 741 return (ComponentUI )uiObject; 742 } 743 744 754 public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { 755 if (changeSupport == null) { 756 changeSupport = new SwingPropertyChangeSupport (this); 757 } 758 changeSupport.addPropertyChangeListener(listener); 759 } 760 761 762 770 public synchronized void removePropertyChangeListener(PropertyChangeListener listener) { 771 if (changeSupport != null) { 772 changeSupport.removePropertyChangeListener(listener); 773 } 774 } 775 776 777 785 public synchronized PropertyChangeListener [] getPropertyChangeListeners() { 786 if (changeSupport == null) { 787 return new PropertyChangeListener [0]; 788 } 789 return changeSupport.getPropertyChangeListeners(); 790 } 791 792 793 805 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 806 if (changeSupport != null) { 807 changeSupport.firePropertyChange(propertyName, oldValue, newValue); 808 } 809 } 810 811 812 823 public synchronized void addResourceBundle( String bundleName ) { 824 if( bundleName == null ) { 825 return; 826 } 827 if( resourceBundles == null ) { 828 resourceBundles = new Vector (5); 829 } 830 if (!resourceBundles.contains(bundleName)) { 831 resourceBundles.add( bundleName ); 832 resourceCache.clear(); 833 } 834 } 835 836 837 846 public synchronized void removeResourceBundle( String bundleName ) { 847 if( resourceBundles != null ) { 848 resourceBundles.remove( bundleName ); 849 } 850 resourceCache.clear(); 851 } 852 853 867 public void setDefaultLocale( Locale l ) { 868 defaultLocale = l; 869 } 870 871 885 public Locale getDefaultLocale() { 886 return defaultLocale; 887 } 888 889 914 public interface LazyValue { 915 925 Object createValue(UIDefaults table); 926 } 927 928 929 947 public interface ActiveValue { 948 955 Object createValue(UIDefaults table); 956 } 957 958 969 public static class ProxyLazyValue implements LazyValue { 970 private AccessControlContext acc; 971 private String className; 972 private String methodName; 973 private Object [] args; 974 975 982 public ProxyLazyValue(String c) { 983 this(c, (String )null); 984 } 985 996 public ProxyLazyValue(String c, String m) { 997 this(c, m, null); 998 } 999 1008 public ProxyLazyValue(String c, Object [] o) { 1009 this(c, null, o); 1010 } 1011 1024 public ProxyLazyValue(String c, String m, Object [] o) { 1025 acc = AccessController.getContext(); 1026 className = c; 1027 methodName = m; 1028 if (o != null) { 1029 args = (Object [])o.clone(); 1030 } 1031 } 1032 1033 1040 public Object createValue(final UIDefaults table) { 1041 return AccessController.doPrivileged(new PrivilegedAction () { 1045 public Object run() { 1046 try { 1047 Class c; 1048 Object cl; 1049 if (table == null || !((cl = table.get("ClassLoader")) 1051 instanceof ClassLoader )) { 1052 cl = Thread.currentThread(). 1053 getContextClassLoader(); 1054 if (cl == null) { 1055 cl = ClassLoader.getSystemClassLoader(); 1057 } 1058 } 1059 c = Class.forName(className, true, (ClassLoader )cl); 1060 if (methodName != null) { 1061 Class [] types = getClassArray(args); 1062 Method m = c.getMethod(methodName, types); 1063 return MethodUtil.invoke(m, c, args); 1064 } else { 1065 Class [] types = getClassArray(args); 1066 Constructor constructor = c.getConstructor(types); 1067 return constructor.newInstance(args); 1068 } 1069 } catch(Exception e) { 1070 } 1076 return null; 1077 } 1078 }, acc); 1079 } 1080 1081 1088 private Class [] getClassArray(Object [] args) { 1089 Class [] types = null; 1090 if (args!=null) { 1091 types = new Class [args.length]; 1092 for (int i = 0; i< args.length; i++) { 1093 1096 if (args[i] instanceof java.lang.Integer ) { 1097 types[i]=Integer.TYPE; 1098 } else if (args[i] instanceof java.lang.Boolean ) { 1099 types[i]=Boolean.TYPE; 1100 } else if (args[i] instanceof javax.swing.plaf.ColorUIResource ) { 1101 1109 types[i]=java.awt.Color .class; 1110 } else { 1111 types[i]=args[i].getClass(); 1112 } 1113 } 1114 } 1115 return types; 1116 } 1117 1118 private String printArgs(Object [] array) { 1119 String s = "{"; 1120 if (array !=null) { 1121 for (int i = 0 ; i < array.length-1; i++) { 1122 s = s.concat(array[i] + ","); 1123 } 1124 s = s.concat(array[array.length-1] + "}"); 1125 } else { 1126 s = s.concat("}"); 1127 } 1128 return s; 1129 } 1130 } 1131 1132 1133 1143 public static class LazyInputMap implements LazyValue { 1144 1145 private Object [] bindings; 1146 1147 public LazyInputMap(Object [] bindings) { 1148 this.bindings = bindings; 1149 } 1150 1151 1158 public Object createValue(UIDefaults table) { 1159 if (bindings != null) { 1160 InputMap km = LookAndFeel.makeInputMap(bindings); 1161 return km; 1162 } 1163 return null; 1164 } 1165 } 1166} 1167 | Popular Tags |