1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49:
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: new LookAndFeelInfo("GNU", "gnu.javax.swing.plaf.gnu.GNULookAndFeel")
126: };
127:
128:
129: static LookAndFeel[] auxLookAndFeels;
130:
131:
132: static LookAndFeel currentLookAndFeel;
133:
134: static UIDefaults currentUIDefaults;
135:
136:
139: static UIDefaults userUIDefaults;
140:
141:
142: static PropertyChangeSupport listeners
143: = new PropertyChangeSupport(UIManager.class);
144:
145: static
146: {
147: String defaultlaf = System.getProperty("swing.defaultlaf");
148: try {
149: if (defaultlaf != null)
150: {
151: Class lafClass = Class.forName(defaultlaf);
152: LookAndFeel laf = (LookAndFeel) lafClass.newInstance();
153: setLookAndFeel(laf);
154: }
155: else
156: {
157: setLookAndFeel(new MetalLookAndFeel());
158: }
159: }
160: catch (Exception ex)
161: {
162: System.err.println("cannot initialize Look and Feel: " + defaultlaf);
163: System.err.println("error: " + ex.toString());
164: System.err.println("falling back to Metal Look and Feel");
165: try
166: {
167: setLookAndFeel(new MetalLookAndFeel());
168: }
169: catch (Exception ex2)
170: {
171: throw (Error) new AssertionError("There must be no problem installing"
172: + " the MetalLookAndFeel.")
173: .initCause(ex2);
174: }
175: }
176: }
177:
178:
182: public UIManager()
183: {
184:
185: }
186:
187:
192: public static void addPropertyChangeListener(PropertyChangeListener listener)
193: {
194: listeners.addPropertyChangeListener(listener);
195: }
196:
197:
202: public static void removePropertyChangeListener(PropertyChangeListener
203: listener)
204: {
205: listeners.removePropertyChangeListener(listener);
206: }
207:
208:
215: public static PropertyChangeListener[] getPropertyChangeListeners()
216: {
217: return listeners.getPropertyChangeListeners();
218: }
219:
220:
229: public static void addAuxiliaryLookAndFeel(LookAndFeel laf)
230: {
231: if (laf == null)
232: throw new NullPointerException("Null 'laf' argument.");
233: if (auxLookAndFeels == null)
234: {
235: auxLookAndFeels = new LookAndFeel[1];
236: auxLookAndFeels[0] = laf;
237: return;
238: }
239:
240: LookAndFeel[] temp = new LookAndFeel[auxLookAndFeels.length + 1];
241: System.arraycopy(auxLookAndFeels, 0, temp, 0, auxLookAndFeels.length);
242: auxLookAndFeels = temp;
243: auxLookAndFeels[auxLookAndFeels.length - 1] = laf;
244: }
245:
246:
254: public static boolean removeAuxiliaryLookAndFeel(LookAndFeel laf)
255: {
256: if (auxLookAndFeels == null)
257: return false;
258: int count = auxLookAndFeels.length;
259: if (count == 1 && auxLookAndFeels[0] == laf)
260: {
261: auxLookAndFeels = null;
262: return true;
263: }
264: for (int i = 0; i < count; i++)
265: {
266: if (auxLookAndFeels[i] == laf)
267: {
268: LookAndFeel[] temp = new LookAndFeel[auxLookAndFeels.length - 1];
269: if (i == 0)
270: {
271: System.arraycopy(auxLookAndFeels, 1, temp, 0, count - 1);
272: }
273: else if (i == count - 1)
274: {
275: System.arraycopy(auxLookAndFeels, 0, temp, 0, count - 1);
276: }
277: else
278: {
279: System.arraycopy(auxLookAndFeels, 0, temp, 0, i);
280: System.arraycopy(auxLookAndFeels, i + 1, temp, i,
281: count - i - 1);
282: }
283: auxLookAndFeels = temp;
284: return true;
285: }
286: }
287: return false;
288: }
289:
290:
299: public static LookAndFeel[] getAuxiliaryLookAndFeels()
300: {
301: return auxLookAndFeels;
302: }
303:
304:
312: public static Object get(Object key)
313: {
314: Object val = null;
315: if (userUIDefaults != null)
316: val = userUIDefaults.get(key);
317: if (val == null)
318: val = getLookAndFeelDefaults().get(key);
319: return val;
320: }
321:
322:
330: public static Object get(Object key, Locale locale)
331: {
332: Object val = null;
333: if (userUIDefaults != null)
334: val = userUIDefaults.get(key, locale);
335: if (val == null)
336: val = getLookAndFeelDefaults().get(key, locale);
337: return val;
338: }
339:
340:
346: public static boolean getBoolean(Object key)
347: {
348: Boolean value = (Boolean) get(key);
349: return value != null ? value.booleanValue() : false;
350: }
351:
352:
358: public static boolean getBoolean(Object key, Locale locale)
359: {
360: Boolean value = (Boolean) get(key, locale);
361: return value != null ? value.booleanValue() : false;
362: }
363:
364:
367: public static Border getBorder(Object key)
368: {
369: return (Border) get(key);
370: }
371:
372:
377: public static Border getBorder(Object key, Locale locale)
378: {
379: return (Border) get(key, locale);
380: }
381:
382:
385: public static Color getColor(Object key)
386: {
387: return (Color) get(key);
388: }
389:
390:
393: public static Color getColor(Object key, Locale locale)
394: {
395: return (Color) get(key);
396: }
397:
398:
404: public static String getCrossPlatformLookAndFeelClassName()
405: {
406: return "javax.swing.plaf.metal.MetalLookAndFeel";
407: }
408:
409:
414: public static UIDefaults getDefaults()
415: {
416: return currentUIDefaults;
417: }
418:
419:
422: public static Dimension getDimension(Object key)
423: {
424: return (Dimension) get(key);
425: }
426:
427:
430: public static Dimension getDimension(Object key, Locale locale)
431: {
432: return (Dimension) get(key, locale);
433: }
434:
435:
443: public static Font getFont(Object key)
444: {
445: return (Font) get(key);
446: }
447:
448:
456: public static Font getFont(Object key, Locale locale)
457: {
458: return (Font) get(key ,locale);
459: }
460:
461:
464: public static Icon getIcon(Object key)
465: {
466: return (Icon) get(key);
467: }
468:
469:
472: public static Icon getIcon(Object key, Locale locale)
473: {
474: return (Icon) get(key, locale);
475: }
476:
477:
480: public static Insets getInsets(Object key)
481: {
482: Object o = get(key);
483: if (o instanceof Insets)
484: return (Insets) o;
485: else
486: return null;
487: }
488:
489:
492: public static Insets getInsets(Object key, Locale locale)
493: {
494: Object o = get(key, locale);
495: if (o instanceof Insets)
496: return (Insets) o;
497: else
498: return null;
499: }
500:
501:
507: public static LookAndFeelInfo[] getInstalledLookAndFeels()
508: {
509: return installed;
510: }
511:
512: public static int getInt(Object key)
513: {
514: Integer x = (Integer) get(key);
515: if (x == null)
516: return 0;
517: return x.intValue();
518: }
519:
520: public static int getInt(Object key, Locale locale)
521: {
522: Integer x = (Integer) get(key, locale);
523: if (x == null)
524: return 0;
525: return x.intValue();
526: }
527:
528:
535: public static LookAndFeel getLookAndFeel()
536: {
537: return currentLookAndFeel;
538: }
539:
540:
546: public static UIDefaults getLookAndFeelDefaults()
547: {
548: return currentUIDefaults;
549: }
550:
551:
554: public static String getString(Object key)
555: {
556: return (String) get(key);
557: }
558:
559:
562: public static String getString(Object key, Locale locale)
563: {
564: return (String) get(key, locale);
565: }
566:
567:
576: public static String getSystemLookAndFeelClassName()
577: {
578: return getCrossPlatformLookAndFeelClassName();
579: }
580:
581:
587: public static ComponentUI getUI(JComponent target)
588: {
589: ComponentUI ui = null;
590: if (userUIDefaults != null
591: && userUIDefaults.get(target.getUIClassID()) != null)
592: ui = userUIDefaults.getUI(target);
593: if (ui == null)
594: ui = currentUIDefaults.getUI(target);
595: return ui;
596: }
597:
598:
605: public static void installLookAndFeel(String name, String className)
606: {
607: installLookAndFeel(new LookAndFeelInfo(name, className));
608: }
609:
610:
614: public static void installLookAndFeel(LookAndFeelInfo info)
615: {
616: LookAndFeelInfo[] newInstalled = new LookAndFeelInfo[installed.length + 1];
617: System.arraycopy(installed, 0, newInstalled, 0, installed.length);
618: newInstalled[newInstalled.length - 1] = info;
619: setInstalledLookAndFeels(newInstalled);
620: }
621:
622:
625: public static Object put(Object key, Object value)
626: {
627: Object old = get(key);
628: if (userUIDefaults == null)
629: userUIDefaults = new UIDefaults();
630: userUIDefaults.put(key, value);
631: return old;
632: }
633:
634:
637: public static void setInstalledLookAndFeels(UIManager.LookAndFeelInfo[] infos)
638: {
639: installed = infos;
640: }
641:
642:
652: public static void setLookAndFeel(LookAndFeel newLookAndFeel)
653: throws UnsupportedLookAndFeelException
654: {
655: if (newLookAndFeel != null && ! newLookAndFeel.isSupportedLookAndFeel())
656: throw new UnsupportedLookAndFeelException(newLookAndFeel.getName());
657: LookAndFeel oldLookAndFeel = currentLookAndFeel;
658: if (oldLookAndFeel != null)
659: oldLookAndFeel.uninitialize();
660:
661:
662: currentLookAndFeel = newLookAndFeel;
663: if (newLookAndFeel != null)
664: {
665: newLookAndFeel.initialize();
666: currentUIDefaults = newLookAndFeel.getDefaults();
667: }
668: else
669: {
670: currentUIDefaults = null;
671: }
672: listeners.firePropertyChange("lookAndFeel", oldLookAndFeel, newLookAndFeel);
673:
674:
675: }
676:
677:
687: public static void setLookAndFeel(String className)
688: throws ClassNotFoundException, InstantiationException, IllegalAccessException,
689: UnsupportedLookAndFeelException
690: {
691: Class c = Class.forName(className);
692: LookAndFeel a = (LookAndFeel) c.newInstance();
693: setLookAndFeel(a);
694: }
695: }