Cleaned up font loading. Added toolkit thread generation fix

This commit is contained in:
nathan 2015-11-20 02:17:48 +01:00
parent f5a2bdc7d8
commit 8eab3332c4

View File

@ -34,12 +34,20 @@ import java.util.Enumeration;
public public
class SwingUtil { class SwingUtil {
static { static {
// loads fonts into the system, and sets the default look and feel. /*
* hack workaround for starting the Toolkit thread before any Timer stuff
* javax.swing.Timer uses the Event Dispatch Thread, which is not
* created until the Toolkit thread starts up. Using the Swing
* Timer before starting this stuff starts up may get unexpected
* results (such as taking a long time before the first timer
* event).
*/
Toolkit.getDefaultToolkit();
boolean fonts_disable = SystemProps.getBoolean(SystemProps.FontsDisable, false); // loads fonts into the system, and sets the default look and feel.
if (!fonts_disable) { if (SystemProps.LoadAllFonts) {
boolean isJava6 = OS.javaVersion == 6; boolean isJava6 = OS.javaVersion == 6;
String fontsLocation = SystemProps.getString(SystemProps.FontsLocation, "resources/fonts"); String fontsLocation = SystemProps.FontsLocation;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Enumeration<URL> fonts = LocationResolver.getResources(fontsLocation); Enumeration<URL> fonts = LocationResolver.getResources(fontsLocation);
@ -84,16 +92,15 @@ class SwingUtil {
} }
boolean LF_disable = SystemProps.getBoolean(SystemProps.LookAndFeelDisable, false); String customLandF = SystemProps.customLookAndFeel;
if (!LF_disable) { if (customLandF != null && !customLandF.isEmpty()) {
// register a better looking L&F // register a better looking L&F (default we use is Nimbus)
String nimbus = "Nimbus";
String name = UIManager.getLookAndFeel().getName(); String name = UIManager.getLookAndFeel().getName();
if (!nimbus.equals(name)) { if (!customLandF.equals(name)) {
try { try {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) { if (customLandF.equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName()); UIManager.setLookAndFeel(info.getClassName());
break; break;
} }
@ -120,35 +127,44 @@ class SwingUtil {
* For example: * For example:
* <p> * <p>
* *
* Font titleTextFont = SwingUtil.getFontFromProperty("dorkbox.growl.maintext", "Source Code Pro Bold", Font.BOLD, 16); * Font titleTextFont = SwingUtil.parseFont("Source Code Pro Bold 16");
* *
* @param propertyBaseName This is the base property to get the font info from, such as "dorkbox.growl.mainText" * @param fontInfo This is the font "name style size", as a string. For example "Source Code Pro Bold BOLD 16"
*
* @param fontName this is the default font information, if the font information is not overridden.
* @param fontHint this is the default font information, if the font information is not overridden
* @param fontSize this is the default font information, if the font information is not overridden
* *
* @return the specified font * @return the specified font
*/ */
public static public static
Font getFontFromProperty(final String propertyBaseName, final String fontName, final int fontHint, final int fontSize) { Font parseFont(final String fontInfo) {
String name = SystemProps.getString(propertyBaseName + ".name", fontName); try {
int style = SystemProps.getInt(propertyBaseName + ".style", fontHint); final int sizeIndex = fontInfo.lastIndexOf(" ");
int size = SystemProps.getInt(propertyBaseName + ".size", fontSize);
// this can be WRONG, in which case it will just error out String size = fontInfo.substring(sizeIndex + 1);
//noinspection MagicConstant
return new Font(name, style, size); // hint is at most 6 (ITALIC) before sizeIndex - we can use this to our benefit.
int styleIndex = fontInfo.indexOf(" ", sizeIndex - 7);
String styleString = fontInfo.substring(styleIndex + 1, sizeIndex);
int style = Font.PLAIN;
if (styleString.equalsIgnoreCase("bold")) {
style = Font.BOLD;
}
else if (styleString.equalsIgnoreCase("italic")) {
style = Font.ITALIC;
}
String fontName = fontInfo.substring(0, styleIndex);
// this can be WRONG, in which case it will just error out
//noinspection MagicConstant
return new Font(fontName, style, Integer.parseInt(size));
} catch (Exception e) {
throw new RuntimeException("Unable to load font info from '" + fontInfo + "'", e);
}
} }
/** used when setting various icon components in the GUI to "nothing", since null doesn't work */ /** used when setting various icon components in the GUI to "nothing", since null doesn't work */
public static final Image BLANK_ICON = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB_PRE); public static final Image BLANK_ICON = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB_PRE);
public static final Font FONT_BOLD_16 = new Font("Source Code Pro Bold", Font.BOLD, 16); // used by ??
public static final Font FONT_12 = new Font("Source Code Pro", Font.PLAIN, 12); // used by ??
public static final Font FONT_14 = new Font("Source Code Pro", Font.PLAIN, 14); // used by ??
public static public static
void showOnSameScreenAsMouse_Center(final Container frame) { void showOnSameScreenAsMouse_Center(final Container frame) {