Cannot do things in during static initialization that depends on

static fields. Moved logic to methods.
This commit is contained in:
nathan 2017-02-26 00:07:45 +01:00
parent 896744ae1e
commit d6885798c5

View File

@ -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,70 +68,87 @@ 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
boolean isJava6 = OS.javaVersion == 6; * default CrossPlatform L&F, which is 'Metal'
*
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); * @param lookAndFeel the simple name or null for the system default
Enumeration<URL> fonts = LocationResolver.getResources(FONTS_LOCATION); */
if (fonts.hasMoreElements()) { public static
// skip the FIRST one, since we always know that the first one is the directory we asked for void setLookAndFeel(final String lookAndFeel) {
fonts.nextElement(); if (lookAndFeel == null) {
try {
while (fonts.hasMoreElements()) { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
URL url = fonts.nextElement(); } catch (Exception e) {
InputStream is = null; e.printStackTrace();
//noinspection TryWithIdenticalCatches
try {
String path = url.toURI()
.getPath();
// only support TTF fonts (java6) and OTF fonts (7+).
if (path.endsWith(".ttf") || (!isJava6 && path.endsWith(".otf"))) {
is = url.openStream();
Font newFont = Font.createFont(Font.TRUETYPE_FONT, is);
// fonts that ALREADY exist are not re-registered
ge.registerFont(newFont);
}
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (FontFormatException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException 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 (CUSTOM_LOOK_AND_FEEL != null && !CUSTOM_LOOK_AND_FEEL.isEmpty()) { if (!specified.equals(current)) {
// register a different L&F try {
String specified = CUSTOM_LOOK_AND_FEEL.toLowerCase(Locale.US); for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
String current = UIManager.getLookAndFeel().getName().toLowerCase(Locale.US); if (specified.equals(info.getName().toLowerCase(Locale.US))) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// whoops. something isn't right!
e.printStackTrace();
}
}
}
if (!specified.equals(current)) { /** 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;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Enumeration<URL> fonts = LocationResolver.getResources(FONTS_LOCATION);
if (fonts.hasMoreElements()) {
// skip the FIRST one, since we always know that the first one is the directory we asked for
fonts.nextElement();
while (fonts.hasMoreElements()) {
URL url = fonts.nextElement();
InputStream is = null;
//noinspection TryWithIdenticalCatches
try { try {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { String path = url.toURI()
if (specified.equals(info.getName().toLowerCase(Locale.US))) { .getPath();
UIManager.setLookAndFeel(info.getClassName());
break; // only support TTF fonts (java6) and OTF fonts (7+).
if (path.endsWith(".ttf") || (!isJava6 && path.endsWith(".otf"))) {
is = url.openStream();
Font newFont = Font.createFont(Font.TRUETYPE_FONT, is);
// fonts that ALREADY exist are not re-registered
ge.registerFont(newFont);
}
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (FontFormatException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
} }
} }
} catch (Exception e) {
// whoops. something isn't right!
e.printStackTrace();
} }
} }
} }