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