This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
UIManager.java
1558 lines (1421 loc) · 59.5 KB
/
UIManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.swing;
import java.awt.Component;
import java.awt.Font;
import java.awt.Color;
import java.awt.Insets;
import java.awt.Dimension;
import java.awt.KeyboardFocusManager;
import java.awt.KeyEventPostProcessor;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.security.AccessController;
import javax.swing.plaf.ComponentUI;
import javax.swing.border.Border;
import javax.swing.event.SwingPropertyChangeSupport;
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.Locale;
import sun.awt.SunToolkit;
import sun.awt.OSInfo;
import sun.security.action.GetPropertyAction;
import sun.swing.SwingUtilities2;
import java.util.HashMap;
import java.util.Objects;
import sun.awt.AppContext;
import sun.awt.AWTAccessor;
/**
* {@code UIManager} manages the current look and feel, the set of
* available look and feels, {@code PropertyChangeListeners} that
* are notified when the look and feel changes, look and feel defaults, and
* convenience methods for obtaining various default values.
*
* <h2>Specifying the look and feel</h2>
*
* The look and feel can be specified in two distinct ways: by
* specifying the fully qualified name of the class for the look and
* feel, or by creating an instance of {@code LookAndFeel} and passing
* it to {@code setLookAndFeel}. The following example illustrates
* setting the look and feel to the system look and feel:
* <pre>
* UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
* </pre>
* The following example illustrates setting the look and feel based on
* class name:
* <pre>
* UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
* </pre>
* Once the look and feel has been changed it is imperative to invoke
* {@code updateUI} on all {@code JComponents}. The method {@link
* SwingUtilities#updateComponentTreeUI} makes it easy to apply {@code
* updateUI} to a containment hierarchy. Refer to it for
* details. The exact behavior of not invoking {@code
* updateUI} after changing the look and feel is
* unspecified. It is very possible to receive unexpected exceptions,
* painting problems, or worse.
*
* <h2>Default look and feel</h2>
*
* The class used for the default look and feel is chosen in the following
* manner:
* <ol>
* <li>If the system property <code>swing.defaultlaf</code> is
* {@code non-null}, use its value as the default look and feel class
* name.
* <li>If the {@link java.util.Properties} file <code>swing.properties</code>
* exists and contains the key <code>swing.defaultlaf</code>,
* use its value as the default look and feel class name. The location
* that is checked for <code>swing.properties</code> may vary depending
* upon the implementation of the Java platform. Typically the
* <code>swing.properties</code> file is located in the <code>conf</code>
* subdirectory of the Java installation directory.
* Refer to the release notes of the implementation being used for
* further details.
* <li>Otherwise use the cross platform look and feel.
* </ol>
*
* <h2>Defaults</h2>
*
* {@code UIManager} manages three sets of {@code UIDefaults}. In order, they
* are:
* <ol>
* <li>Developer defaults. With few exceptions Swing does not
* alter the developer defaults; these are intended to be modified
* and used by the developer.
* <li>Look and feel defaults. The look and feel defaults are
* supplied by the look and feel at the time it is installed as the
* current look and feel ({@code setLookAndFeel()} is invoked). The
* look and feel defaults can be obtained using the {@code
* getLookAndFeelDefaults()} method.
* <li>System defaults. The system defaults are provided by Swing.
* </ol>
* Invoking any of the various {@code get} methods
* results in checking each of the defaults, in order, returning
* the first {@code non-null} value. For example, invoking
* {@code UIManager.getString("Table.foreground")} results in first
* checking developer defaults. If the developer defaults contain
* a value for {@code "Table.foreground"} it is returned, otherwise
* the look and feel defaults are checked, followed by the system defaults.
* <p>
* It's important to note that {@code getDefaults} returns a custom
* instance of {@code UIDefaults} with this resolution logic built into it.
* For example, {@code UIManager.getDefaults().getString("Table.foreground")}
* is equivalent to {@code UIManager.getString("Table.foreground")}. Both
* resolve using the algorithm just described. In many places the
* documentation uses the word defaults to refer to the custom instance
* of {@code UIDefaults} with the resolution logic as previously described.
* <p>
* When the look and feel is changed, {@code UIManager} alters only the
* look and feel defaults; the developer and system defaults are not
* altered by the {@code UIManager} in any way.
* <p>
* The set of defaults a particular look and feel supports is defined
* and documented by that look and feel. In addition, each look and
* feel, or {@code ComponentUI} provided by a look and feel, may
* access the defaults at different times in their life cycle. Some
* look and feels may aggressively look up defaults, so that changing a
* default may not have an effect after installing the look and feel.
* Other look and feels may lazily access defaults so that a change to
* the defaults may effect an existing look and feel. Finally, other look
* and feels might not configure themselves from the defaults table in
* any way. None-the-less it is usually the case that a look and feel
* expects certain defaults, so that in general
* a {@code ComponentUI} provided by one look and feel will not
* work with another look and feel.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*
* @author Thomas Ball
* @author Hans Muller
* @since 1.2
*/
@SuppressWarnings("serial") // Same-version serialization only
public class UIManager implements Serializable
{
/**
* This class defines the state managed by the <code>UIManager</code>. For
* Swing applications the fields in this class could just as well
* be static members of <code>UIManager</code> however we give them
* "AppContext"
* scope instead so that applets (and potentially multiple lightweight
* applications running in a single VM) have their own state. For example,
* an applet can alter its look and feel, see <code>setLookAndFeel</code>.
* Doing so has no affect on other applets (or the browser).
*/
private static class LAFState
{
Properties swingProps;
private UIDefaults[] tables = new UIDefaults[2];
boolean initialized = false;
boolean focusPolicyInitialized = false;
MultiUIDefaults multiUIDefaults = new MultiUIDefaults(tables);
LookAndFeel lookAndFeel;
LookAndFeel multiLookAndFeel = null;
Vector<LookAndFeel> auxLookAndFeels = null;
SwingPropertyChangeSupport changeSupport;
LookAndFeelInfo[] installedLAFs;
UIDefaults getLookAndFeelDefaults() { return tables[0]; }
void setLookAndFeelDefaults(UIDefaults x) { tables[0] = x; }
UIDefaults getSystemDefaults() { return tables[1]; }
void setSystemDefaults(UIDefaults x) { tables[1] = x; }
/**
* Returns the SwingPropertyChangeSupport for the current
* AppContext. If <code>create</code> is a true, a non-null
* <code>SwingPropertyChangeSupport</code> will be returned, if
* <code>create</code> is false and this has not been invoked
* with true, null will be returned.
*/
public synchronized SwingPropertyChangeSupport
getPropertyChangeSupport(boolean create) {
if (create && changeSupport == null) {
changeSupport = new SwingPropertyChangeSupport(
UIManager.class);
}
return changeSupport;
}
}
/* Lock object used in place of class object for synchronization. (4187686)
*/
private static final Object classLock = new Object();
/**
* Constructs a {@code UIManager}.
*/
public UIManager() {}
/**
* Return the <code>LAFState</code> object, lazily create one if necessary.
* All access to the <code>LAFState</code> fields is done via this method,
* for example:
* <pre>
* getLAFState().initialized = true;
* </pre>
*/
private static LAFState getLAFState() {
LAFState rv = (LAFState)SwingUtilities.appContextGet(
SwingUtilities2.LAF_STATE_KEY);
if (rv == null) {
synchronized (classLock) {
rv = (LAFState)SwingUtilities.appContextGet(
SwingUtilities2.LAF_STATE_KEY);
if (rv == null) {
SwingUtilities.appContextPut(
SwingUtilities2.LAF_STATE_KEY,
(rv = new LAFState()));
}
}
}
return rv;
}
/* Keys used in the <code>swing.properties</code> properties file.
* See loadUserProperties(), initialize().
*/
private static final String defaultLAFKey = "swing.defaultlaf";
private static final String auxiliaryLAFsKey = "swing.auxiliarylaf";
private static final String multiplexingLAFKey = "swing.plaf.multiplexinglaf";
private static final String installedLAFsKey = "swing.installedlafs";
private static final String disableMnemonicKey = "swing.disablenavaids";
/**
* Return a <code>swing.properties</code> file key for the attribute of specified
* look and feel. The attr is either "name" or "class", a typical
* key would be: "swing.installedlaf.windows.name"
*/
private static String makeInstalledLAFKey(String laf, String attr) {
return "swing.installedlaf." + laf + "." + attr;
}
/**
* The location of the <code>swing.properties</code> property file is
* implementation-specific.
* It is typically located in the <code>conf</code> subdirectory of the Java
* installation directory. This method returns a bogus filename
* if <code>java.home</code> isn't defined.
*/
private static String makeSwingPropertiesFilename() {
String sep = File.separator;
// No need to wrap this in a doPrivileged as it's called from
// a doPrivileged.
String javaHome = System.getProperty("java.home");
if (javaHome == null) {
javaHome = "<java.home undefined>";
}
return javaHome + sep + "conf" + sep + "swing.properties";
}
/**
* Provides a little information about an installed
* <code>LookAndFeel</code> for the sake of configuring a menu or
* for initial application set up.
*
* @see UIManager#getInstalledLookAndFeels
* @see LookAndFeel
*/
public static class LookAndFeelInfo {
private String name;
private String className;
/**
* Constructs a <code>UIManager</code>s
* <code>LookAndFeelInfo</code> object.
*
* @param name a <code>String</code> specifying the name of
* the look and feel
* @param className a <code>String</code> specifying the name of
* the class that implements the look and feel
*/
public LookAndFeelInfo(String name, String className) {
this.name = name;
this.className = className;
}
/**
* Returns the name of the look and feel in a form suitable
* for a menu or other presentation
* @return a <code>String</code> containing the name
* @see LookAndFeel#getName
*/
public String getName() {
return name;
}
/**
* Returns the name of the class that implements this look and feel.
* @return the name of the class that implements this
* <code>LookAndFeel</code>
* @see LookAndFeel
*/
public String getClassName() {
return className;
}
/**
* Returns a string that displays and identifies this
* object's properties.
*
* @return a <code>String</code> representation of this object
*/
public String toString() {
return getClass().getName() + "[" + getName() + " " + getClassName() + "]";
}
}
/**
* The default value of <code>installedLAFS</code> is used when no
* <code>swing.properties</code>
* file is available or if the file doesn't contain a "swing.installedlafs"
* property.
*
* @see #initializeInstalledLAFs
*/
private static LookAndFeelInfo[] installedLAFs;
static {
ArrayList<LookAndFeelInfo> iLAFs = new ArrayList<LookAndFeelInfo>(4);
iLAFs.add(new LookAndFeelInfo(
"Metal", "javax.swing.plaf.metal.MetalLookAndFeel"));
iLAFs.add(new LookAndFeelInfo(
"Nimbus", "javax.swing.plaf.nimbus.NimbusLookAndFeel"));
iLAFs.add(new LookAndFeelInfo("CDE/Motif",
"com.sun.java.swing.plaf.motif.MotifLookAndFeel"));
// Only include windows on Windows boxs.
@SuppressWarnings("removal")
OSInfo.OSType osType = AccessController.doPrivileged(OSInfo.getOSTypeAction());
if (osType == OSInfo.OSType.WINDOWS) {
iLAFs.add(new LookAndFeelInfo("Windows",
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"));
if (Toolkit.getDefaultToolkit().getDesktopProperty(
"win.xpstyle.themeActive") != null) {
iLAFs.add(new LookAndFeelInfo("Windows Classic",
"com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"));
}
}
else if (osType == OSInfo.OSType.MACOSX) {
iLAFs.add(new LookAndFeelInfo("Mac OS X", "com.apple.laf.AquaLookAndFeel"));
}
else {
// GTK is not shipped on Windows.
iLAFs.add(new LookAndFeelInfo("GTK+",
"com.sun.java.swing.plaf.gtk.GTKLookAndFeel"));
}
installedLAFs = iLAFs.toArray(new LookAndFeelInfo[iLAFs.size()]);
}
/**
* Returns an array of {@code LookAndFeelInfo}s representing the
* {@code LookAndFeel} implementations currently available. The
* <code>LookAndFeelInfo</code> objects can be used by an
* application to construct a menu of look and feel options for
* the user, or to determine which look and feel to set at startup
* time. To avoid the penalty of creating numerous {@code
* LookAndFeel} objects, {@code LookAndFeelInfo} maintains the
* class name of the {@code LookAndFeel} class, not the actual
* {@code LookAndFeel} instance.
* <p>
* The following example illustrates setting the current look and feel
* from an instance of {@code LookAndFeelInfo}:
* <pre>
* UIManager.setLookAndFeel(info.getClassName());
* </pre>
*
* @return an array of <code>LookAndFeelInfo</code> objects
* @see #setLookAndFeel
*/
public static LookAndFeelInfo[] getInstalledLookAndFeels() {
maybeInitialize();
LookAndFeelInfo[] ilafs = getLAFState().installedLAFs;
if (ilafs == null) {
ilafs = installedLAFs;
}
LookAndFeelInfo[] rv = new LookAndFeelInfo[ilafs.length];
System.arraycopy(ilafs, 0, rv, 0, ilafs.length);
return rv;
}
/**
* Sets the set of available look and feels. While this method does
* not check to ensure all of the {@code LookAndFeelInfos} are
* {@code non-null}, it is strongly recommended that only {@code non-null}
* values are supplied in the {@code infos} array.
*
* @param infos set of <code>LookAndFeelInfo</code> objects specifying
* the available look and feels
*
* @see #getInstalledLookAndFeels
* @throws NullPointerException if {@code infos} is {@code null}
*/
public static void setInstalledLookAndFeels(LookAndFeelInfo[] infos)
throws SecurityException
{
maybeInitialize();
LookAndFeelInfo[] newInfos = new LookAndFeelInfo[infos.length];
System.arraycopy(infos, 0, newInfos, 0, infos.length);
getLAFState().installedLAFs = newInfos;
}
/**
* Adds the specified look and feel to the set of available look
* and feels. While this method allows a {@code null} {@code info},
* it is strongly recommended that a {@code non-null} value be used.
*
* @param info a <code>LookAndFeelInfo</code> object that names the
* look and feel and identifies the class that implements it
* @see #setInstalledLookAndFeels
*/
public static void installLookAndFeel(LookAndFeelInfo info) {
LookAndFeelInfo[] infos = getInstalledLookAndFeels();
LookAndFeelInfo[] newInfos = new LookAndFeelInfo[infos.length + 1];
System.arraycopy(infos, 0, newInfos, 0, infos.length);
newInfos[infos.length] = info;
setInstalledLookAndFeels(newInfos);
}
/**
* Adds the specified look and feel to the set of available look
* and feels. While this method does not check the
* arguments in any way, it is strongly recommended that {@code
* non-null} values be supplied.
*
* @param name descriptive name of the look and feel
* @param className name of the class that implements the look and feel
* @see #setInstalledLookAndFeels
*/
public static void installLookAndFeel(String name, String className) {
installLookAndFeel(new LookAndFeelInfo(name, className));
}
/**
* Returns the current look and feel or <code>null</code>.
*
* @return current look and feel, or <code>null</code>
* @see #setLookAndFeel
*/
public static LookAndFeel getLookAndFeel() {
maybeInitialize();
return getLAFState().lookAndFeel;
}
/**
* Creates a supported built-in Java {@code LookAndFeel} specified
* by the given {@code L&F name} name.
*
* @param name a {@code String} specifying the name of the built-in
* look and feel
* @return the built-in {@code LookAndFeel} object
* @throws NullPointerException if {@code name} is {@code null}
* @throws UnsupportedLookAndFeelException if the built-in Java {@code L&F}
* is not found for the given name or it is not supported by the
* underlying platform
*
* @see LookAndFeel#getName
* @see LookAndFeel#isSupportedLookAndFeel
*
* @since 9
*/
@SuppressWarnings("deprecation")
public static LookAndFeel createLookAndFeel(String name)
throws UnsupportedLookAndFeelException {
Objects.requireNonNull(name);
if ("GTK look and feel".equals(name)) {
name = "GTK+";
}
try {
for (LookAndFeelInfo info : installedLAFs) {
if (info.getName().equals(name)) {
Class<?> cls = Class.forName(UIManager.class.getModule(),
info.getClassName());
LookAndFeel laf =
(LookAndFeel) cls.newInstance();
if (!laf.isSupportedLookAndFeel()) {
break;
}
return laf;
}
}
} catch (ReflectiveOperationException |
IllegalArgumentException ignore) {
}
throw new UnsupportedLookAndFeelException(name);
}
/**
* Sets the current look and feel to {@code newLookAndFeel}.
* If the current look and feel is {@code non-null} {@code
* uninitialize} is invoked on it. If {@code newLookAndFeel} is
* {@code non-null}, {@code initialize} is invoked on it followed
* by {@code getDefaults}. The defaults returned from {@code
* newLookAndFeel.getDefaults()} replace those of the defaults
* from the previous look and feel. If the {@code newLookAndFeel} is
* {@code null}, the look and feel defaults are set to {@code null}.
* <p>
* A value of {@code null} can be used to set the look and feel
* to {@code null}. As the {@code LookAndFeel} is required for
* most of Swing to function, setting the {@code LookAndFeel} to
* {@code null} is strongly discouraged.
* <p>
* This is a JavaBeans bound property.
*
* @param newLookAndFeel {@code LookAndFeel} to install
* @throws UnsupportedLookAndFeelException if
* {@code newLookAndFeel} is {@code non-null} and
* {@code newLookAndFeel.isSupportedLookAndFeel()} returns
* {@code false}
* @see #getLookAndFeel
*/
public static void setLookAndFeel(LookAndFeel newLookAndFeel)
throws UnsupportedLookAndFeelException
{
if ((newLookAndFeel != null) && !newLookAndFeel.isSupportedLookAndFeel()) {
String s = newLookAndFeel.toString() + " not supported on this platform";
throw new UnsupportedLookAndFeelException(s);
}
LAFState lafState = getLAFState();
LookAndFeel oldLookAndFeel = lafState.lookAndFeel;
if (oldLookAndFeel != null) {
oldLookAndFeel.uninitialize();
}
lafState.lookAndFeel = newLookAndFeel;
if (newLookAndFeel != null) {
sun.swing.DefaultLookup.setDefaultLookup(null);
newLookAndFeel.initialize();
lafState.setLookAndFeelDefaults(newLookAndFeel.getDefaults());
}
else {
lafState.setLookAndFeelDefaults(null);
}
SwingPropertyChangeSupport changeSupport = lafState.
getPropertyChangeSupport(false);
if (changeSupport != null) {
changeSupport.firePropertyChange("lookAndFeel", oldLookAndFeel,
newLookAndFeel);
}
}
/**
* Loads the {@code LookAndFeel} specified by the given class
* name, using the current thread's context class loader, and
* passes it to {@code setLookAndFeel(LookAndFeel)}.
*
* @param className a string specifying the name of the class that implements
* the look and feel
* @throws ClassNotFoundException if the <code>LookAndFeel</code>
* class could not be found
* @throws InstantiationException if a new instance of the class
* couldn't be created
* @throws IllegalAccessException if the class or initializer isn't accessible
* @throws UnsupportedLookAndFeelException if
* <code>lnf.isSupportedLookAndFeel()</code> is false
* @throws ClassCastException if {@code className} does not identify
* a class that extends {@code LookAndFeel}
* @throws NullPointerException if {@code className} is {@code null}
*/
@SuppressWarnings("deprecation")
public static void setLookAndFeel(String className)
throws ClassNotFoundException,
InstantiationException,
IllegalAccessException,
UnsupportedLookAndFeelException
{
if ("javax.swing.plaf.metal.MetalLookAndFeel".equals(className)) {
// Avoid reflection for the common case of metal.
setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
}
else {
Class<?> lnfClass = SwingUtilities.loadSystemClass(className);
setLookAndFeel((LookAndFeel)(lnfClass.newInstance()));
}
}
/**
* Returns the name of the <code>LookAndFeel</code> class that implements
* the native system look and feel if there is one, otherwise
* the name of the default cross platform <code>LookAndFeel</code>
* class. This value can be overriden by setting the
* <code>swing.systemlaf</code> system property.
*
* @return the <code>String</code> of the <code>LookAndFeel</code>
* class
*
* @see #setLookAndFeel
* @see #getCrossPlatformLookAndFeelClassName
*/
public static String getSystemLookAndFeelClassName() {
@SuppressWarnings("removal")
String systemLAF = AccessController.doPrivileged(
new GetPropertyAction("swing.systemlaf"));
if (systemLAF != null) {
return systemLAF;
}
@SuppressWarnings("removal")
OSInfo.OSType osType = AccessController.doPrivileged(OSInfo.getOSTypeAction());
if (osType == OSInfo.OSType.WINDOWS) {
return "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
} else {
Toolkit toolkit = Toolkit.getDefaultToolkit();
if (toolkit instanceof SunToolkit) {
SunToolkit suntk = (SunToolkit)toolkit;
String desktop = suntk.getDesktop();
boolean gtkAvailable = suntk.isNativeGTKAvailable();
if ("gnome".equals(desktop) && gtkAvailable) {
return "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
}
}
if (osType == OSInfo.OSType.MACOSX) {
if (toolkit.getClass() .getName()
.equals("sun.lwawt.macosx.LWCToolkit")) {
return "com.apple.laf.AquaLookAndFeel";
}
}
}
return getCrossPlatformLookAndFeelClassName();
}
/**
* Returns the name of the <code>LookAndFeel</code> class that implements
* the default cross platform look and feel -- the Java
* Look and Feel (JLF). This value can be overriden by setting the
* <code>swing.crossplatformlaf</code> system property.
*
* @return a string with the JLF implementation-class
* @see #setLookAndFeel
* @see #getSystemLookAndFeelClassName
*/
public static String getCrossPlatformLookAndFeelClassName() {
@SuppressWarnings("removal")
String laf = AccessController.doPrivileged(
new GetPropertyAction("swing.crossplatformlaf"));
if (laf != null) {
return laf;
}
return "javax.swing.plaf.metal.MetalLookAndFeel";
}
/**
* Returns the defaults. The returned defaults resolve using the
* logic specified in the class documentation.
*
* @return a <code>UIDefaults</code> object containing the default values
*/
public static UIDefaults getDefaults() {
maybeInitialize();
return getLAFState().multiUIDefaults;
}
/**
* Returns a font from the defaults. If the value for {@code key} is
* not a {@code Font}, {@code null} is returned.
*
* @param key an <code>Object</code> specifying the font
* @return the <code>Font</code> object
* @throws NullPointerException if {@code key} is {@code null}
*/
public static Font getFont(Object key) {
return getDefaults().getFont(key);
}
/**
* Returns a font from the defaults that is appropriate
* for the given locale. If the value for {@code key} is
* not a {@code Font}, {@code null} is returned.
*
* @param key an <code>Object</code> specifying the font
* @param l the <code>Locale</code> for which the font is desired; refer
* to {@code UIDefaults} for details on how a {@code null}
* {@code Locale} is handled
* @return the <code>Font</code> object
* @throws NullPointerException if {@code key} is {@code null}
* @since 1.4
*/
public static Font getFont(Object key, Locale l) {
return getDefaults().getFont(key,l);
}
/**
* Returns a color from the defaults. If the value for {@code key} is
* not a {@code Color}, {@code null} is returned.
*
* @param key an <code>Object</code> specifying the color
* @return the <code>Color</code> object
* @throws NullPointerException if {@code key} is {@code null}
*/
public static Color getColor(Object key) {
return getDefaults().getColor(key);
}
/**
* Returns a color from the defaults that is appropriate
* for the given locale. If the value for {@code key} is
* not a {@code Color}, {@code null} is returned.
*
* @param key an <code>Object</code> specifying the color
* @param l the <code>Locale</code> for which the color is desired; refer
* to {@code UIDefaults} for details on how a {@code null}
* {@code Locale} is handled
* @return the <code>Color</code> object
* @throws NullPointerException if {@code key} is {@code null}
* @since 1.4
*/
public static Color getColor(Object key, Locale l) {
return getDefaults().getColor(key,l);
}
/**
* Returns an <code>Icon</code> from the defaults. If the value for
* {@code key} is not an {@code Icon}, {@code null} is returned.
*
* @param key an <code>Object</code> specifying the icon
* @return the <code>Icon</code> object
* @throws NullPointerException if {@code key} is {@code null}
*/
public static Icon getIcon(Object key) {
return getDefaults().getIcon(key);
}
/**
* Returns an <code>Icon</code> from the defaults that is appropriate
* for the given locale. If the value for
* {@code key} is not an {@code Icon}, {@code null} is returned.
*
* @param key an <code>Object</code> specifying the icon
* @param l the <code>Locale</code> for which the icon is desired; refer
* to {@code UIDefaults} for details on how a {@code null}
* {@code Locale} is handled
* @return the <code>Icon</code> object
* @throws NullPointerException if {@code key} is {@code null}
* @since 1.4
*/
public static Icon getIcon(Object key, Locale l) {
return getDefaults().getIcon(key,l);
}
/**
* Returns a border from the defaults. If the value for
* {@code key} is not a {@code Border}, {@code null} is returned.
*
* @param key an <code>Object</code> specifying the border
* @return the <code>Border</code> object
* @throws NullPointerException if {@code key} is {@code null}
*/
public static Border getBorder(Object key) {
return getDefaults().getBorder(key);
}
/**
* Returns a border from the defaults that is appropriate
* for the given locale. If the value for
* {@code key} is not a {@code Border}, {@code null} is returned.
*
* @param key an <code>Object</code> specifying the border
* @param l the <code>Locale</code> for which the border is desired; refer
* to {@code UIDefaults} for details on how a {@code null}
* {@code Locale} is handled
* @return the <code>Border</code> object
* @throws NullPointerException if {@code key} is {@code null}
* @since 1.4
*/
public static Border getBorder(Object key, Locale l) {
return getDefaults().getBorder(key,l);
}
/**
* Returns a string from the defaults. If the value for
* {@code key} is not a {@code String}, {@code null} is returned.
*
* @param key an <code>Object</code> specifying the string
* @return the <code>String</code>
* @throws NullPointerException if {@code key} is {@code null}
*/
public static String getString(Object key) {
return getDefaults().getString(key);
}
/**
* Returns a string from the defaults that is appropriate for the
* given locale. If the value for
* {@code key} is not a {@code String}, {@code null} is returned.
*
* @param key an <code>Object</code> specifying the string
* @param l the <code>Locale</code> for which the string is desired; refer
* to {@code UIDefaults} for details on how a {@code null}
* {@code Locale} is handled
* @return the <code>String</code>
* @since 1.4
* @throws NullPointerException if {@code key} is {@code null}
*/
public static String getString(Object key, Locale l) {
return getDefaults().getString(key,l);
}
/**
* Returns a string from the defaults that is appropriate for the
* given locale. If the value for
* {@code key} is not a {@code String}, {@code null} is returned.
*
* @param key an <code>Object</code> specifying the string
* @param c {@code Component} used to determine the locale;
* {@code null} implies the default locale as
* returned by {@code Locale.getDefault()}
* @return the <code>String</code>
* @throws NullPointerException if {@code key} is {@code null}
*/
static String getString(Object key, Component c) {
Locale l = (c == null) ? Locale.getDefault() : c.getLocale();
return getString(key, l);
}
/**
* Returns an integer from the defaults. If the value for
* {@code key} is not an {@code Integer}, or does not exist,
* {@code 0} is returned.
*
* @param key an <code>Object</code> specifying the int
* @return the int
* @throws NullPointerException if {@code key} is {@code null}
*/
public static int getInt(Object key) {
return getDefaults().getInt(key);
}
/**
* Returns an integer from the defaults that is appropriate
* for the given locale. If the value for
* {@code key} is not an {@code Integer}, or does not exist,
* {@code 0} is returned.
*
* @param key an <code>Object</code> specifying the int
* @param l the <code>Locale</code> for which the int is desired; refer
* to {@code UIDefaults} for details on how a {@code null}
* {@code Locale} is handled
* @return the int
* @throws NullPointerException if {@code key} is {@code null}
* @since 1.4
*/
public static int getInt(Object key, Locale l) {
return getDefaults().getInt(key,l);
}
/**
* Returns a boolean from the defaults which is associated with
* the key value. If the key is not found or the key doesn't represent
* a boolean value then {@code false} is returned.
*
* @param key an <code>Object</code> specifying the key for the desired boolean value
* @return the boolean value corresponding to the key
* @throws NullPointerException if {@code key} is {@code null}
* @since 1.4
*/
public static boolean getBoolean(Object key) {
return getDefaults().getBoolean(key);
}
/**
* Returns a boolean from the defaults which is associated with
* the key value and the given <code>Locale</code>. If the key is not
* found or the key doesn't represent
* a boolean value then {@code false} will be returned.
*
* @param key an <code>Object</code> specifying the key for the desired
* boolean value
* @param l the <code>Locale</code> for which the boolean is desired; refer
* to {@code UIDefaults} for details on how a {@code null}
* {@code Locale} is handled
* @return the boolean value corresponding to the key
* @throws NullPointerException if {@code key} is {@code null}
* @since 1.4
*/
public static boolean getBoolean(Object key, Locale l) {
return getDefaults().getBoolean(key,l);
}
/**
* Returns an <code>Insets</code> object from the defaults. If the value
* for {@code key} is not an {@code Insets}, {@code null} is returned.
*
* @param key an <code>Object</code> specifying the <code>Insets</code> object
* @return the <code>Insets</code> object
* @throws NullPointerException if {@code key} is {@code null}
*/
public static Insets getInsets(Object key) {
return getDefaults().getInsets(key);
}
/**
* Returns an <code>Insets</code> object from the defaults that is
* appropriate for the given locale. If the value
* for {@code key} is not an {@code Insets}, {@code null} is returned.
*
* @param key an <code>Object</code> specifying the <code>Insets</code> object
* @param l the <code>Locale</code> for which the object is desired; refer
* to {@code UIDefaults} for details on how a {@code null}
* {@code Locale} is handled
* @return the <code>Insets</code> object
* @throws NullPointerException if {@code key} is {@code null}
* @since 1.4
*/
public static Insets getInsets(Object key, Locale l) {
return getDefaults().getInsets(key,l);
}
/**
* Returns a dimension from the defaults. If the value
* for {@code key} is not a {@code Dimension}, {@code null} is returned.
*
* @param key an <code>Object</code> specifying the dimension object
* @return the <code>Dimension</code> object
* @throws NullPointerException if {@code key} is {@code null}
*/
public static Dimension getDimension(Object key) {
return getDefaults().getDimension(key);
}
/**
* Returns a dimension from the defaults that is appropriate
* for the given locale. If the value
* for {@code key} is not a {@code Dimension}, {@code null} is returned.
*
* @param key an <code>Object</code> specifying the dimension object
* @param l the <code>Locale</code> for which the object is desired; refer
* to {@code UIDefaults} for details on how a {@code null}
* {@code Locale} is handled
* @return the <code>Dimension</code> object
* @throws NullPointerException if {@code key} is {@code null}
* @since 1.4
*/
public static Dimension getDimension(Object key, Locale l) {
return getDefaults().getDimension(key,l);