Commit 0397581c authored by Nico Mack's avatar Nico Mack

Added getContentAsFont method

parent a0080fb5
......@@ -35,8 +35,12 @@ import org.slf4j.LoggerFactory;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -129,6 +133,13 @@ public class BootstrappingUtils {
SPECIAL_INTEGERS.put(POSITIVE_INFINITY, Integer.MAX_VALUE);
}
private static final List<String> AVAILABLE_FONTS = new ArrayList<>();
static {
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
AVAILABLE_FONTS.addAll(Arrays.asList(environment.getAvailableFontFamilyNames()));
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Primitives
......@@ -343,24 +354,6 @@ public class BootstrappingUtils {
}
return value;
}
// ---------------------------------------------------------------------------
private static Font getContentAsFont(Element rootElement, String childName, boolean optional, Font defaultFont, BootstrapContext context) throws BuildException {
Font font = defaultFont;
Element fontNode = (childName != null) ? rootElement.getChild(childName) : rootElement;
if (fontNode != null) {
String fontName = getContentAsString(fontNode, null, BootstrappingUtils.MANDATORY, null, context);
int fontSize = getContentAsInteger(fontNode, Externalization.SIZE_NODE, BootstrappingUtils.OPTIONAL, 12, context);
String fontStyle = getContentAsString(fontNode, Externalization.STYLE_NODE, BootstrappingUtils.OPTIONAL, PLAIN, context);
if (!FONT_STYLES.containsKey(fontStyle)) {
logAndThrowBuildException(new BuildException(StringUtils.build("Specified font style {} does match accepted values. Accepted values are {}!", fontStyle, FONT_STYLES.keySet())), rootElement, childName); //$NON-NLS-1$
}
font = new Font(fontName, FONT_STYLES.get(fontStyle.toLowerCase()), fontSize);
}
return font;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
......@@ -872,6 +865,38 @@ public class BootstrappingUtils {
return colour;
}
// ---------------------------------------------------------------------------
/**
* @param rootElement
* @param childName
* @param optional
* @param defaultFont
* @param context
* @return
* @throws BuildException
*/
// ---------------------------------------------------------------------------
public static Font getContentAsFont(Element rootElement, String childName, boolean optional, Font defaultFont, BootstrapContext context) throws BuildException {
Font font = defaultFont;
Element fontNode = (childName != null) ? rootElement.getChild(childName) : rootElement;
if (fontNode != null) {
String fontName = getContentAsString(fontNode, null, BootstrappingUtils.MANDATORY, null, context);
int fontSize = getContentAsInteger(fontNode, Externalization.SIZE_NODE, BootstrappingUtils.OPTIONAL, 12, context);
String fontStyle = getContentAsString(fontNode, Externalization.STYLE_NODE, BootstrappingUtils.OPTIONAL, PLAIN, context);
if (!AVAILABLE_FONTS.contains(fontName)) {
logAndThrowBuildException(new BuildException(StringUtils.build("Specified font {} is unknown!", fontName)), rootElement, childName); //$NON-NLS-1$
}
if (!FONT_STYLES.containsKey(fontStyle)) {
logAndThrowBuildException(new BuildException(StringUtils.build("Specified font style {} does match accepted values. Accepted values are {}!", fontStyle, FONT_STYLES.keySet())), rootElement, childName); //$NON-NLS-1$
}
font = new Font(fontName, FONT_STYLES.get(fontStyle.toLowerCase()), fontSize);
}
return font;
}
// ---------------------------------------------------------------------------
/**
* @param rootElement
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment