1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
50: import ;
51: import ;
52: import ;
53:
54:
58: public class UIManager implements Serializable
59: {
60:
65: public static class LookAndFeelInfo
66: {
67: String name, clazz;
68:
69:
75: public LookAndFeelInfo(String name,
76: String clazz)
77: {
78: this.name = name;
79: this.clazz = clazz;
80: }
81:
82:
87: public String getName()
88: {
89: return name;
90: }
91:
92:
97: public String getClassName()
98: {
99: return clazz;
100: }
101:
102:
107: public String toString()
108: {
109: StringBuffer s = new StringBuffer();
110: s.append(getClass().getName());
111: s.append('[');
112: s.append(getName());
113: s.append(' ');
114: s.append(getClassName());
115: s.append(']');
116: return s.toString();
117: }
118: }
119:
120: private static final long serialVersionUID = -5547433830339189365L;
121:
122:
123: static LookAndFeelInfo [] installed = {
124: new LookAndFeelInfo("Metal", "javax.swing.plaf.metal.MetalLookAndFeel")
125: };
126:
127:
128: static LookAndFeel[] auxLookAndFeels;
129:
130:
131: static LookAndFeel currentLookAndFeel;
132:
133: static UIDefaults currentUIDefaults;
134:
135:
138: static UIDefaults userUIDefaults;
139:
140:
141: static SwingPropertyChangeSupport listeners
142: = new SwingPropertyChangeSupport(UIManager.class);
143:
144: static
145: {
146: String defaultlaf = System.getProperty("swing.defaultlaf");
147: try {
148: if (defaultlaf != null)
149: {
150: Class lafClass = Class.forName(defaultlaf);
151: LookAndFeel laf = (LookAndFeel) lafClass.newInstance();
152: setLookAndFeel(laf);
153: }
154: else
155: {
156: setLookAndFeel(new MetalLookAndFeel());
157: }
158: }
159: catch (Exception ex)
160: {
161: System.err.println("cannot initialize Look and Feel: " + defaultlaf);
162: System.err.println("error: " + ex.toString());
163: System.err.println("falling back to Metal Look and Feel");
164: try
165: {
166: setLookAndFeel(new MetalLookAndFeel());
167: }
168: catch (Exception ex2)
169: {
170: throw (Error) new AssertionError("There must be no problem installing"
171: + " the MetalLookAndFeel.")
172: .initCause(ex2);
173: }
174: }
175: }
176:
177:
181: public UIManager()
182: {
183:
184: }
185:
186:
191: public static void addPropertyChangeListener(PropertyChangeListener listener)
192: {
193: listeners.addPropertyChangeListener(listener);
194: }
195:
196:
201: public static void removePropertyChangeListener(PropertyChangeListener
202: listener)
203: {
204: listeners.removePropertyChangeListener(listener);
205: }
206:
207:
214: public static PropertyChangeListener[] getPropertyChangeListeners()
215: {
216: return listeners.getPropertyChangeListeners();
217: }
218:
219:
228: public static void addAuxiliaryLookAndFeel(LookAndFeel laf)
229: {
230: if (laf == null)
231: throw new NullPointerException("Null 'laf' argument.");
232: if (auxLookAndFeels == null)
233: {
234: auxLookAndFeels = new LookAndFeel[1];
235: auxLookAndFeels[0] = laf;
236: return;
237: }
238:
239: LookAndFeel[] temp = new LookAndFeel[auxLookAndFeels.length + 1];
240: System.arraycopy(auxLookAndFeels, 0, temp, 0, auxLookAndFeels.length);
241: auxLookAndFeels = temp;
242: auxLookAndFeels[auxLookAndFeels.length - 1] = laf;
243: }
244:
245:
253: public static boolean removeAuxiliaryLookAndFeel(LookAndFeel laf)
254: {
255: if (auxLookAndFeels == null)
256: return false;
257: int count = auxLookAndFeels.length;
258: if (count == 1 && auxLookAndFeels[0] == laf)
259: {
260: auxLookAndFeels = null;
261: return true;
262: }
263: for (int i = 0; i < count; i++)
264: {
265: if (auxLookAndFeels[i] == laf)
266: {
267: LookAndFeel[] temp = new LookAndFeel[auxLookAndFeels.length - 1];
268: if (i == 0)
269: {
270: System.arraycopy(auxLookAndFeels, 1, temp, 0, count - 1);
271: }
272: else if (i == count - 1)
273: {
274: System.arraycopy(auxLookAndFeels, 0, temp, 0, count - 1);
275: }
276: else
277: {
278: System.arraycopy(auxLookAndFeels, 0, temp, 0, i);
279: System.arraycopy(auxLookAndFeels, i + 1, temp, i,
280: count - i - 1);
281: }
282: auxLookAndFeels = temp;
283: return true;
284: }
285: }
286: return false;
287: }
288:
289:
298: public static LookAndFeel[] getAuxiliaryLookAndFeels()
299: {
300: return auxLookAndFeels;
301: }
302:
303:
311: public static Object get(Object key)
312: {
313: Object val = null;
314: if (userUIDefaults != null)
315: val = userUIDefaults.get(key);
316: if (val == null)
317: val = getLookAndFeelDefaults().get(key);
318: return val;
319: }
320:
321:
329: public static Object get(Object key, Locale locale)
330: {
331: Object val = null;
332: if (userUIDefaults != null)
333: val = userUIDefaults.get(key, locale);
334: if (val == null)
335: val = getLookAndFeelDefaults().get(key, locale);
336: return val;
337: }
338:
339:
345: public static boolean getBoolean(Object key)
346: {
347: Boolean value = (Boolean) get(key);
348: return value != null ? value.booleanValue() : false;
349: }
350:
351:
357: public static boolean getBoolean(Object key, Locale locale)
358: {
359: Boolean value = (Boolean) get(key, locale);
360: return value != null ? value.booleanValue() : false;
361: }
362:
363:
366: public static Border getBorder(Object key)
367: {
368: return (Border) get(key);
369: }
370:
371:
376: public static Border getBorder(Object key, Locale locale)
377: {
378: return (Border) get(key, locale);
379: }
380:
381:
384: public static Color getColor(Object key)
385: {
386: return (Color) get(key);
387: }
388:
389:
392: public static Color getColor(Object key, Locale locale)
393: {
394: return (Color) get(key);
395: }
396:
397:
403: public static String getCrossPlatformLookAndFeelClassName()
404: {
405: return "javax.swing.plaf.metal.MetalLookAndFeel";
406: }
407:
408:
413: public static UIDefaults getDefaults()
414: {
415: return currentUIDefaults;
416: }
417:
418:
421: public static Dimension getDimension(Object key)
422: {
423: return (Dimension) get(key);
424: }
425:
426:
429: public static Dimension getDimension(Object key, Locale locale)
430: {
431: return (Dimension) get(key, locale);
432: }
433:
434:
442: public static Font getFont(Object key)
443: {
444: return (Font) get(key);
445: }
446:
447:
455: public static Font getFont(Object key, Locale locale)
456: {
457: return (Font) get(key ,locale);
458: }
459:
460:
463: public static Icon getIcon(Object key)
464: {
465: return (Icon) get(key);
466: }
467:
468:
471: public static Icon getIcon(Object key, Locale locale)
472: {
473: return (Icon) get(key, locale);
474: }
475:
476:
479: public static Insets getInsets(Object key)
480: {
481: Object o = get(key);
482: if (o instanceof Insets)
483: return (Insets) o;
484: else
485: return null;
486: }
487:
488:
491: public static Insets getInsets(Object key, Locale locale)
492: {
493: Object o = get(key, locale);
494: if (o instanceof Insets)
495: return (Insets) o;
496: else
497: return null;
498: }
499:
500:
506: public static LookAndFeelInfo[] getInstalledLookAndFeels()
507: {
508: return installed;
509: }
510:
511: public static int getInt(Object key)
512: {
513: Integer x = (Integer) get(key);
514: if (x == null)
515: return 0;
516: return x.intValue();
517: }
518:
519: public static int getInt(Object key, Locale locale)
520: {
521: Integer x = (Integer) get(key, locale);
522: if (x == null)
523: return 0;
524: return x.intValue();
525: }
526:
527:
534: public static LookAndFeel getLookAndFeel()
535: {
536: return currentLookAndFeel;
537: }
538:
539:
545: public static UIDefaults getLookAndFeelDefaults()
546: {
547: return currentUIDefaults;
548: }
549:
550:
553: public static String getString(Object key)
554: {
555: return (String) get(key);
556: }
557:
558:
561: public static String getString(Object key, Locale locale)
562: {
563: return (String) get(key, locale);
564: }
565:
566:
575: public static String getSystemLookAndFeelClassName()
576: {
577: return getCrossPlatformLookAndFeelClassName();
578: }
579:
580:
586: public static ComponentUI getUI(JComponent target)
587: {
588: ComponentUI ui = null;
589: if (userUIDefaults != null
590: && userUIDefaults.get(target.getUIClassID()) != null)
591: ui = userUIDefaults.getUI(target);
592: if (ui == null)
593: ui = currentUIDefaults.getUI(target);
594: return ui;
595: }
596:
597:
604: public static void installLookAndFeel(String name, String className)
605: {
606: installLookAndFeel(new LookAndFeelInfo(name, className));
607: }
608:
609:
613: public static void installLookAndFeel(LookAndFeelInfo info)
614: {
615:
616: }
617:
618:
621: public static Object put(Object key, Object value)
622: {
623: Object old = get(key);
624: if (userUIDefaults == null)
625: userUIDefaults = new UIDefaults();
626: userUIDefaults.put(key, value);
627: return old;
628: }
629:
630:
633: public static void setInstalledLookAndFeels(UIManager.LookAndFeelInfo[] infos)
634: {
635:
636: }
637:
638:
648: public static void setLookAndFeel(LookAndFeel newLookAndFeel)
649: throws UnsupportedLookAndFeelException
650: {
651: if (newLookAndFeel != null && ! newLookAndFeel.isSupportedLookAndFeel())
652: throw new UnsupportedLookAndFeelException(newLookAndFeel.getName());
653: LookAndFeel oldLookAndFeel = currentLookAndFeel;
654: if (oldLookAndFeel != null)
655: oldLookAndFeel.uninitialize();
656:
657:
658: currentLookAndFeel = newLookAndFeel;
659: if (newLookAndFeel != null)
660: {
661: newLookAndFeel.initialize();
662: currentUIDefaults = newLookAndFeel.getDefaults();
663: }
664: else
665: {
666: currentUIDefaults = null;
667: }
668: listeners.firePropertyChange("lookAndFeel", oldLookAndFeel, newLookAndFeel);
669:
670:
671: }
672:
673:
683: public static void setLookAndFeel(String className)
684: throws ClassNotFoundException, InstantiationException, IllegalAccessException,
685: UnsupportedLookAndFeelException
686: {
687: Class c = Class.forName(className);
688: LookAndFeel a = (LookAndFeel) c.newInstance();
689: setLookAndFeel(a);
690: }
691: }