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:
136: static SwingPropertyChangeSupport listeners
137: = new SwingPropertyChangeSupport(UIManager.class);
138:
139: static
140: {
141: String defaultlaf = System.getProperty("swing.defaultlaf");
142: try {
143: if (defaultlaf != null)
144: {
145: Class lafClass = Class.forName(defaultlaf);
146: LookAndFeel laf = (LookAndFeel) lafClass.newInstance();
147: setLookAndFeel(laf);
148: }
149: else
150: {
151: setLookAndFeel(new MetalLookAndFeel());
152: }
153: }
154: catch (Exception ex)
155: {
156: System.err.println("cannot initialize Look and Feel: " + defaultlaf);
157: System.err.println("error: " + ex.toString());
158: System.err.println("falling back to Metal Look and Feel");
159: try
160: {
161: setLookAndFeel(new MetalLookAndFeel());
162: }
163: catch (Exception ex2)
164: {
165: throw (Error) new AssertionError("There must be no problem installing"
166: + " the MetalLookAndFeel.")
167: .initCause(ex2);
168: }
169: }
170: }
171:
172:
176: public UIManager()
177: {
178:
179: }
180:
181:
186: public static void addPropertyChangeListener(PropertyChangeListener listener)
187: {
188: listeners.addPropertyChangeListener(listener);
189: }
190:
191:
196: public static void removePropertyChangeListener(PropertyChangeListener
197: listener)
198: {
199: listeners.removePropertyChangeListener(listener);
200: }
201:
202:
209: public static PropertyChangeListener[] getPropertyChangeListeners()
210: {
211: return listeners.getPropertyChangeListeners();
212: }
213:
214:
223: public static void addAuxiliaryLookAndFeel(LookAndFeel laf)
224: {
225: if (laf == null)
226: throw new NullPointerException("Null 'laf' argument.");
227: if (auxLookAndFeels == null)
228: {
229: auxLookAndFeels = new LookAndFeel[1];
230: auxLookAndFeels[0] = laf;
231: return;
232: }
233:
234: LookAndFeel[] temp = new LookAndFeel[auxLookAndFeels.length + 1];
235: System.arraycopy(auxLookAndFeels, 0, temp, 0, auxLookAndFeels.length);
236: auxLookAndFeels = temp;
237: auxLookAndFeels[auxLookAndFeels.length - 1] = laf;
238: }
239:
240:
248: public static boolean removeAuxiliaryLookAndFeel(LookAndFeel laf)
249: {
250: if (auxLookAndFeels == null)
251: return false;
252: int count = auxLookAndFeels.length;
253: if (count == 1 && auxLookAndFeels[0] == laf)
254: {
255: auxLookAndFeels = null;
256: return true;
257: }
258: for (int i = 0; i < count; i++)
259: {
260: if (auxLookAndFeels[i] == laf)
261: {
262: LookAndFeel[] temp = new LookAndFeel[auxLookAndFeels.length - 1];
263: if (i == 0)
264: {
265: System.arraycopy(auxLookAndFeels, 1, temp, 0, count - 1);
266: }
267: else if (i == count - 1)
268: {
269: System.arraycopy(auxLookAndFeels, 0, temp, 0, count - 1);
270: }
271: else
272: {
273: System.arraycopy(auxLookAndFeels, 0, temp, 0, i);
274: System.arraycopy(auxLookAndFeels, i + 1, temp, i,
275: count - i - 1);
276: }
277: auxLookAndFeels = temp;
278: return true;
279: }
280: }
281: return false;
282: }
283:
284:
293: public static LookAndFeel[] getAuxiliaryLookAndFeels()
294: {
295: return auxLookAndFeels;
296: }
297:
298:
306: public static Object get(Object key)
307: {
308: return getLookAndFeelDefaults().get(key);
309: }
310:
311:
319: public static Object get(Object key, Locale locale)
320: {
321: return getLookAndFeelDefaults().get(key ,locale);
322: }
323:
324:
330: public static boolean getBoolean(Object key)
331: {
332: Boolean value = (Boolean) getLookAndFeelDefaults().get(key);
333: return value != null ? value.booleanValue() : false;
334: }
335:
336:
342: public static boolean getBoolean(Object key, Locale locale)
343: {
344: Boolean value = (Boolean) getLookAndFeelDefaults().get(key, locale);
345: return value != null ? value.booleanValue() : false;
346: }
347:
348:
351: public static Border getBorder(Object key)
352: {
353: return (Border) getLookAndFeelDefaults().get(key);
354: }
355:
356:
361: public static Border getBorder(Object key, Locale locale)
362: {
363: return (Border) getLookAndFeelDefaults().get(key, locale);
364: }
365:
366:
369: public static Color getColor(Object key)
370: {
371: return (Color) getLookAndFeelDefaults().get(key);
372: }
373:
374:
377: public static Color getColor(Object key, Locale locale)
378: {
379: return (Color) getLookAndFeelDefaults().get(key);
380: }
381:
382:
388: public static String getCrossPlatformLookAndFeelClassName()
389: {
390: return "javax.swing.plaf.metal.MetalLookAndFeel";
391: }
392:
393:
398: public static UIDefaults getDefaults()
399: {
400: return currentUIDefaults;
401: }
402:
403:
406: public static Dimension getDimension(Object key)
407: {
408: return (Dimension) getLookAndFeelDefaults().get(key);
409: }
410:
411:
414: public static Dimension getDimension(Object key, Locale locale)
415: {
416: return (Dimension) getLookAndFeelDefaults().get(key, locale);
417: }
418:
419:
427: public static Font getFont(Object key)
428: {
429: return (Font) getLookAndFeelDefaults().get(key);
430: }
431:
432:
440: public static Font getFont(Object key, Locale locale)
441: {
442: return (Font) getLookAndFeelDefaults().get(key ,locale);
443: }
444:
445:
448: public static Icon getIcon(Object key)
449: {
450: return (Icon) getLookAndFeelDefaults().get(key);
451: }
452:
453:
456: public static Icon getIcon(Object key, Locale locale)
457: {
458: return (Icon) getLookAndFeelDefaults().get(key, locale);
459: }
460:
461:
464: public static Insets getInsets(Object key)
465: {
466: return getLookAndFeelDefaults().getInsets(key);
467: }
468:
469:
472: public static Insets getInsets(Object key, Locale locale)
473: {
474: return getLookAndFeelDefaults().getInsets(key, locale);
475: }
476:
477:
483: public static LookAndFeelInfo[] getInstalledLookAndFeels()
484: {
485: return installed;
486: }
487:
488: public static int getInt(Object key)
489: {
490: Integer x = (Integer) getLookAndFeelDefaults().get(key);
491: if (x == null)
492: return 0;
493: return x.intValue();
494: }
495:
496: public static int getInt(Object key, Locale locale)
497: {
498: Integer x = (Integer) getLookAndFeelDefaults().get(key, locale);
499: if (x == null)
500: return 0;
501: return x.intValue();
502: }
503:
504:
511: public static LookAndFeel getLookAndFeel()
512: {
513: return currentLookAndFeel;
514: }
515:
516:
522: public static UIDefaults getLookAndFeelDefaults()
523: {
524: return currentUIDefaults;
525: }
526:
527:
530: public static String getString(Object key)
531: {
532: return (String) getLookAndFeelDefaults().get(key);
533: }
534:
535:
538: public static String getString(Object key, Locale locale)
539: {
540: return (String) getLookAndFeelDefaults().get(key, locale);
541: }
542:
543:
552: public static String getSystemLookAndFeelClassName()
553: {
554: return getCrossPlatformLookAndFeelClassName();
555: }
556:
557:
563: public static ComponentUI getUI(JComponent target)
564: {
565: return getLookAndFeelDefaults().getUI(target);
566: }
567:
568:
575: public static void installLookAndFeel(String name, String className)
576: {
577: installLookAndFeel(new LookAndFeelInfo(name, className));
578: }
579:
580:
584: public static void installLookAndFeel(LookAndFeelInfo info)
585: {
586:
587: }
588:
589:
592: public static Object put(Object key, Object value)
593: {
594: return getLookAndFeelDefaults().put(key,value);
595: }
596:
597:
600: public static void setInstalledLookAndFeels(UIManager.LookAndFeelInfo[] infos)
601: {
602:
603: }
604:
605:
615: public static void setLookAndFeel(LookAndFeel newLookAndFeel)
616: throws UnsupportedLookAndFeelException
617: {
618: if (newLookAndFeel != null && ! newLookAndFeel.isSupportedLookAndFeel())
619: throw new UnsupportedLookAndFeelException(newLookAndFeel.getName());
620:
621: LookAndFeel oldLookAndFeel = currentLookAndFeel;
622: if (oldLookAndFeel != null)
623: oldLookAndFeel.uninitialize();
624:
625:
626: currentLookAndFeel = newLookAndFeel;
627: if (newLookAndFeel != null)
628: {
629: newLookAndFeel.initialize();
630: currentUIDefaults = newLookAndFeel.getDefaults();
631: }
632: else
633: {
634: currentUIDefaults = null;
635: }
636: listeners.firePropertyChange("lookAndFeel", oldLookAndFeel, newLookAndFeel);
637:
638:
639: }
640:
641:
651: public static void setLookAndFeel(String className)
652: throws ClassNotFoundException, InstantiationException, IllegalAccessException,
653: UnsupportedLookAndFeelException
654: {
655: Class c = Class.forName(className);
656: LookAndFeel a = (LookAndFeel) c.newInstance();
657: setLookAndFeel(a);
658: }
659: }