Cannot do things in during static initialization that depends on
static fields. Moved logic to methods.
This commit is contained in:
parent
896744ae1e
commit
d6885798c5
|
@ -53,18 +53,11 @@ import javax.swing.UIManager;
|
||||||
|
|
||||||
public
|
public
|
||||||
class SwingUtil {
|
class SwingUtil {
|
||||||
/** All of the fonts in the {@link #FONTS_LOCATION} will be loaded by the Font manager */
|
|
||||||
@Property
|
|
||||||
public static boolean LOAD_ALL_FONTS = false;
|
|
||||||
|
|
||||||
/** Default location where all the fonts are stored */
|
/** Default location where all the fonts are stored */
|
||||||
@Property
|
@Property
|
||||||
public static String FONTS_LOCATION = "resources/fonts";
|
public static String FONTS_LOCATION = "resources/fonts";
|
||||||
|
|
||||||
/** Sets the entire L&F to the Nimbus L&F. Set this to a different one (or null to disable) */
|
|
||||||
@Property
|
|
||||||
public static String CUSTOM_LOOK_AND_FEEL = null;
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
/*
|
/*
|
||||||
* hack workaround for starting the Toolkit thread before any Timer stuff
|
* hack workaround for starting the Toolkit thread before any Timer stuff
|
||||||
|
@ -75,13 +68,52 @@ class SwingUtil {
|
||||||
* event).
|
* event).
|
||||||
*/
|
*/
|
||||||
Toolkit.getDefaultToolkit();
|
Toolkit.getDefaultToolkit();
|
||||||
|
}
|
||||||
|
|
||||||
// loads fonts into the system, and sets the default look and feel.
|
/**
|
||||||
if (LOAD_ALL_FONTS) {
|
* Sets the entire L&F based on "simple" name. Null to set to the system L&F. If this is not called (or set), Swing will use the
|
||||||
|
* default CrossPlatform L&F, which is 'Metal'
|
||||||
|
*
|
||||||
|
* @param lookAndFeel the simple name or null for the system default
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
void setLookAndFeel(final String lookAndFeel) {
|
||||||
|
if (lookAndFeel == null) {
|
||||||
|
try {
|
||||||
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// register a different L&F
|
||||||
|
String specified = lookAndFeel.toLowerCase(Locale.US).trim();
|
||||||
|
String current = UIManager.getLookAndFeel().getName().toLowerCase(Locale.US);
|
||||||
|
|
||||||
|
if (!specified.equals(current)) {
|
||||||
|
try {
|
||||||
|
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
|
||||||
|
if (specified.equals(info.getName().toLowerCase(Locale.US))) {
|
||||||
|
UIManager.setLookAndFeel(info.getClassName());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
// whoops. something isn't right!
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** All of the fonts in the {@link #FONTS_LOCATION} will be loaded by the Font manager */
|
||||||
|
public static
|
||||||
|
void loadAllFonts() {
|
||||||
boolean isJava6 = OS.javaVersion == 6;
|
boolean isJava6 = OS.javaVersion == 6;
|
||||||
|
|
||||||
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||||
Enumeration<URL> fonts = LocationResolver.getResources(FONTS_LOCATION);
|
Enumeration<URL> fonts = LocationResolver.getResources(FONTS_LOCATION);
|
||||||
|
|
||||||
if (fonts.hasMoreElements()) {
|
if (fonts.hasMoreElements()) {
|
||||||
// skip the FIRST one, since we always know that the first one is the directory we asked for
|
// skip the FIRST one, since we always know that the first one is the directory we asked for
|
||||||
fonts.nextElement();
|
fonts.nextElement();
|
||||||
|
@ -122,28 +154,6 @@ class SwingUtil {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (CUSTOM_LOOK_AND_FEEL != null && !CUSTOM_LOOK_AND_FEEL.isEmpty()) {
|
|
||||||
// register a different L&F
|
|
||||||
String specified = CUSTOM_LOOK_AND_FEEL.toLowerCase(Locale.US);
|
|
||||||
String current = UIManager.getLookAndFeel().getName().toLowerCase(Locale.US);
|
|
||||||
|
|
||||||
if (!specified.equals(current)) {
|
|
||||||
try {
|
|
||||||
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
|
|
||||||
if (specified.equals(info.getName().toLowerCase(Locale.US))) {
|
|
||||||
UIManager.setLookAndFeel(info.getClassName());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
// whoops. something isn't right!
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets (or creates) a Font based on a specific system property. Remember: the FontManager caches system/loaded fonts, so we don't need
|
* Gets (or creates) a Font based on a specific system property. Remember: the FontManager caches system/loaded fonts, so we don't need
|
||||||
* to ALSO cache them as well. see: https://stackoverflow.com/questions/6102602/java-awt-is-font-a-lightweight-object
|
* to ALSO cache them as well. see: https://stackoverflow.com/questions/6102602/java-awt-is-font-a-lightweight-object
|
||||||
|
|
Loading…
Reference in New Issue
Block a user