1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50:
51: import ;
52: import ;
53: import ;
54:
55:
59: public class UIManager implements Serializable
60: {
61:
66: public static class LookAndFeelInfo
67: {
68: String name, clazz;
69:
70:
76: public LookAndFeelInfo(String name,
77: String clazz)
78: {
79: this.name = name;
80: this.clazz = clazz;
81: }
82:
83:
88: public String getName()
89: {
90: return name;
91: }
92:
93:
98: public String getClassName()
99: {
100: return clazz;
101: }
102:
103:
108: public String toString()
109: {
110: StringBuffer s = new StringBuffer();
111: s.append(getClass().getName());
112: s.append('[');
113: s.append(getName());
114: s.append(' ');
115: s.append(getClassName());
116: s.append(']');
117: return s.toString();
118: }
119: }
120:
121:
126: private static class MultiplexUIDefaults
127: extends UIDefaults
128: {
129: private class MultiplexEnumeration
130: implements Enumeration
131: {
132: Enumeration[] enums;
133: int i;
134: MultiplexEnumeration(Enumeration e1, Enumeration e2)
135: {
136: enums = new Enumeration[]{ e1, e2 };
137: i = 0;
138: }
139:
140: public boolean hasMoreElements()
141: {
142: return enums[i].hasMoreElements() || i < enums.length - 1;
143: }
144:
145: public Object nextElement()
146: {
147: Object val = enums[i].nextElement();
148: if (! enums[i].hasMoreElements() && i < enums.length - 1)
149: i++;
150: return val;
151: }
152:
153: }
154:
155: UIDefaults fallback;
156:
157: MultiplexUIDefaults(UIDefaults d)
158: {
159: fallback = d;
160: }
161:
162: public Object get(Object key)
163: {
164: Object val = super.get(key);
165: if (val == null)
166: val = fallback.get(key);
167: return val;
168: }
169:
170: public Object get(Object key, Locale l)
171: {
172: Object val = super.get(key, l);
173: if (val == null)
174: val = fallback.get(key, l);
175: return val;
176: }
177:
178: public Object remove(Object key)
179: {
180: Object val = super.remove(key);
181: if (val == null)
182: val = fallback.remove(key);
183: return val;
184: }
185:
186: public int size()
187: {
188: return super.size() + fallback.size();
189: }
190:
191: public Enumeration keys()
192: {
193: return new MultiplexEnumeration(super.keys(), fallback.keys());
194: }
195:
196: public Enumeration elements()
197: {
198: return new MultiplexEnumeration(super.elements(), fallback.elements());
199: }
200: }
201:
202: private static final long serialVersionUID = -5547433830339189365L;
203:
204:
205: static LookAndFeelInfo [] installed = {
206: new LookAndFeelInfo("Metal", "javax.swing.plaf.metal.MetalLookAndFeel"),
207: new LookAndFeelInfo("GNU", "gnu.javax.swing.plaf.gnu.GNULookAndFeel")
208: };
209:
210:
211: static LookAndFeel[] auxLookAndFeels;
212:
213:
214: static LookAndFeel currentLookAndFeel;
215:
216: static MultiplexUIDefaults currentUIDefaults;
217:
218: static UIDefaults lookAndFeelDefaults;
219:
220:
221: static PropertyChangeSupport listeners
222: = new PropertyChangeSupport(UIManager.class);
223:
224: static
225: {
226: String defaultlaf = System.getProperty("swing.defaultlaf");
227: try
228: {
229: if (defaultlaf != null)
230: {
231: setLookAndFeel(defaultlaf);
232: }
233: else
234: {
235: setLookAndFeel(new MetalLookAndFeel());
236: }
237: }
238: catch (Exception ex)
239: {
240: System.err.println("cannot initialize Look and Feel: " + defaultlaf);
241: System.err.println("error: " + ex.toString());
242: ex.printStackTrace();
243: System.err.println("falling back to Metal Look and Feel");
244: try
245: {
246: setLookAndFeel(new MetalLookAndFeel());
247: }
248: catch (Exception ex2)
249: {
250: throw (Error) new AssertionError("There must be no problem installing"
251: + " the MetalLookAndFeel.")
252: .initCause(ex2);
253: }
254: }
255: }
256:
257:
261: public UIManager()
262: {
263:
264: }
265:
266:
271: public static void addPropertyChangeListener(PropertyChangeListener listener)
272: {
273: listeners.addPropertyChangeListener(listener);
274: }
275:
276:
281: public static void removePropertyChangeListener(PropertyChangeListener
282: listener)
283: {
284: listeners.removePropertyChangeListener(listener);
285: }
286:
287:
294: public static PropertyChangeListener[] getPropertyChangeListeners()
295: {
296: return listeners.getPropertyChangeListeners();
297: }
298:
299:
308: public static void addAuxiliaryLookAndFeel(LookAndFeel laf)
309: {
310: if (laf == null)
311: throw new NullPointerException("Null 'laf' argument.");
312: if (auxLookAndFeels == null)
313: {
314: auxLookAndFeels = new LookAndFeel[1];
315: auxLookAndFeels[0] = laf;
316: return;
317: }
318:
319: LookAndFeel[] temp = new LookAndFeel[auxLookAndFeels.length + 1];
320: System.arraycopy(auxLookAndFeels, 0, temp, 0, auxLookAndFeels.length);
321: auxLookAndFeels = temp;
322: auxLookAndFeels[auxLookAndFeels.length - 1] = laf;
323: }
324:
325:
333: public static boolean removeAuxiliaryLookAndFeel(LookAndFeel laf)
334: {
335: if (auxLookAndFeels == null)
336: return false;
337: int count = auxLookAndFeels.length;
338: if (count == 1 && auxLookAndFeels[0] == laf)
339: {
340: auxLookAndFeels = null;
341: return true;
342: }
343: for (int i = 0; i < count; i++)
344: {
345: if (auxLookAndFeels[i] == laf)
346: {
347: LookAndFeel[] temp = new LookAndFeel[auxLookAndFeels.length - 1];
348: if (i == 0)
349: {
350: System.arraycopy(auxLookAndFeels, 1, temp, 0, count - 1);
351: }
352: else if (i == count - 1)
353: {
354: System.arraycopy(auxLookAndFeels, 0, temp, 0, count - 1);
355: }
356: else
357: {
358: System.arraycopy(auxLookAndFeels, 0, temp, 0, i);
359: System.arraycopy(auxLookAndFeels, i + 1, temp, i,
360: count - i - 1);
361: }
362: auxLookAndFeels = temp;
363: return true;
364: }
365: }
366: return false;
367: }
368:
369:
378: public static LookAndFeel[] getAuxiliaryLookAndFeels()
379: {
380: return auxLookAndFeels;
381: }
382:
383:
391: public static Object get(Object key)
392: {
393: return getDefaults().get(key);
394: }
395:
396:
404: public static Object get(Object key, Locale locale)
405: {
406: return getDefaults().get(key, locale);
407: }
408:
409:
415: public static boolean getBoolean(Object key)
416: {
417: Boolean value = (Boolean) get(key);
418: return value != null ? value.booleanValue() : false;
419: }
420:
421:
427: public static boolean getBoolean(Object key, Locale locale)
428: {
429: Boolean value = (Boolean) get(key, locale);
430: return value != null ? value.booleanValue() : false;
431: }
432:
433:
436: public static Border getBorder(Object key)
437: {
438: return (Border) get(key);
439: }
440:
441:
446: public static Border getBorder(Object key, Locale locale)
447: {
448: return (Border) get(key, locale);
449: }
450:
451:
454: public static Color getColor(Object key)
455: {
456: return (Color) get(key);
457: }
458:
459:
462: public static Color getColor(Object key, Locale locale)
463: {
464: return (Color) get(key);
465: }
466:
467:
473: public static String getCrossPlatformLookAndFeelClassName()
474: {
475: return "javax.swing.plaf.metal.MetalLookAndFeel";
476: }
477:
478:
483: public static UIDefaults getDefaults()
484: {
485: if (currentUIDefaults == null)
486: currentUIDefaults = new MultiplexUIDefaults(null);
487: return currentUIDefaults;
488: }
489:
490:
493: public static Dimension getDimension(Object key)
494: {
495: return (Dimension) get(key);
496: }
497:
498:
501: public static Dimension getDimension(Object key, Locale locale)
502: {
503: return (Dimension) get(key, locale);
504: }
505:
506:
514: public static Font getFont(Object key)
515: {
516: return (Font) get(key);
517: }
518:
519:
527: public static Font getFont(Object key, Locale locale)
528: {
529: return (Font) get(key, locale);
530: }
531:
532:
535: public static Icon getIcon(Object key)
536: {
537: return (Icon) get(key);
538: }
539:
540:
543: public static Icon getIcon(Object key, Locale locale)
544: {
545: return (Icon) get(key, locale);
546: }
547:
548:
551: public static Insets getInsets(Object key)
552: {
553: Object o = get(key);
554: if (o instanceof Insets)
555: return (Insets) o;
556: else
557: return null;
558: }
559:
560:
563: public static Insets getInsets(Object key, Locale locale)
564: {
565: Object o = get(key, locale);
566: if (o instanceof Insets)
567: return (Insets) o;
568: else
569: return null;
570: }
571:
572:
578: public static LookAndFeelInfo[] getInstalledLookAndFeels()
579: {
580: return installed;
581: }
582:
583: public static int getInt(Object key)
584: {
585: Integer x = (Integer) get(key);
586: if (x == null)
587: return 0;
588: return x.intValue();
589: }
590:
591: public static int getInt(Object key, Locale locale)
592: {
593: Integer x = (Integer) get(key, locale);
594: if (x == null)
595: return 0;
596: return x.intValue();
597: }
598:
599:
606: public static LookAndFeel getLookAndFeel()
607: {
608: return currentLookAndFeel;
609: }
610:
611:
617: public static UIDefaults getLookAndFeelDefaults()
618: {
619: return lookAndFeelDefaults;
620: }
621:
622:
625: public static String getString(Object key)
626: {
627: return (String) get(key);
628: }
629:
630:
633: public static String getString(Object key, Locale locale)
634: {
635: return (String) get(key, locale);
636: }
637:
638:
647: public static String getSystemLookAndFeelClassName()
648: {
649: return getCrossPlatformLookAndFeelClassName();
650: }
651:
652:
658: public static ComponentUI getUI(JComponent target)
659: {
660: return getDefaults().getUI(target);
661: }
662:
663:
670: public static void installLookAndFeel(String name, String className)
671: {
672: installLookAndFeel(new LookAndFeelInfo(name, className));
673: }
674:
675:
679: public static void installLookAndFeel(LookAndFeelInfo info)
680: {
681: LookAndFeelInfo[] newInstalled = new LookAndFeelInfo[installed.length + 1];
682: System.arraycopy(installed, 0, newInstalled, 0, installed.length);
683: newInstalled[newInstalled.length - 1] = info;
684: setInstalledLookAndFeels(newInstalled);
685: }
686:
687:
690: public static Object put(Object key, Object value)
691: {
692: return getDefaults().put(key, value);
693: }
694:
695:
698: public static void setInstalledLookAndFeels(UIManager.LookAndFeelInfo[] infos)
699: {
700: installed = infos;
701: }
702:
703:
713: public static void setLookAndFeel(LookAndFeel newLookAndFeel)
714: throws UnsupportedLookAndFeelException
715: {
716: if (newLookAndFeel != null && ! newLookAndFeel.isSupportedLookAndFeel())
717: throw new UnsupportedLookAndFeelException(newLookAndFeel.getName()
718: + " not supported on this platform");
719: LookAndFeel oldLookAndFeel = currentLookAndFeel;
720: if (oldLookAndFeel != null)
721: oldLookAndFeel.uninitialize();
722:
723:
724: currentLookAndFeel = newLookAndFeel;
725: if (newLookAndFeel != null)
726: {
727: newLookAndFeel.initialize();
728: lookAndFeelDefaults = newLookAndFeel.getDefaults();
729: if (currentUIDefaults == null)
730: currentUIDefaults =
731: new MultiplexUIDefaults(lookAndFeelDefaults);
732: else
733: currentUIDefaults.fallback = lookAndFeelDefaults;
734: }
735: else
736: {
737: currentUIDefaults = null;
738: }
739: listeners.firePropertyChange("lookAndFeel", oldLookAndFeel, newLookAndFeel);
740:
741:
742: }
743:
744:
754: public static void setLookAndFeel(String className)
755: throws ClassNotFoundException, InstantiationException, IllegalAccessException,
756: UnsupportedLookAndFeelException
757: {
758: Class c = Class.forName(className, true,
759: Thread.currentThread().getContextClassLoader());
760: LookAndFeel a = (LookAndFeel) c.newInstance();
761: setLookAndFeel(a);
762: }
763: }