felix@1090: package de.intevation.flys.utils; felix@1090: raimund@1738: import org.apache.log4j.Logger; raimund@1738: felix@1090: import java.awt.Color; raimund@1738: import java.awt.Font; felix@1090: felix@1090: import org.w3c.dom.Document; felix@1090: felix@1090: import de.intevation.artifacts.common.utils.XMLUtils; felix@1090: felix@1090: felix@1090: /** felix@1090: * Utility to deal with themes and their representations. felix@1090: */ felix@1090: public class ThemeUtil { raimund@1738: raimund@1738: private static Logger logger = raimund@1738: Logger.getLogger(ThemeUtil.class); raimund@1738: felix@1711: public final static String XPATH_LINE_COLOR = felix@1711: "/theme/field[@name='linecolor']/@default"; felix@1711: felix@1711: public static final String XPATH_LINE_SIZE = felix@1711: "/theme/field[@name='linesize']/@default"; felix@1711: raimund@1753: public static final String XPATH_LINE_STYLE = raimund@1753: "/theme/field[@name='linetype']/@default"; raimund@1753: raimund@1748: public final static String XPATH_SHOW_POINTS = raimund@1748: "/theme/field[@name='showpoints']/@default"; raimund@1748: raimund@1753: public final static String XPATH_SHOW_LINE = raimund@1753: "/theme/field[@name='showlines']/@default"; raimund@1753: raimund@1738: public final static String XPATH_TEXT_COLOR = raimund@1738: "/theme/field[@name='textcolor']/@default"; raimund@1738: raimund@1738: public final static String XPATH_TEXT_SIZE = raimund@1738: "/theme/field[@name='textsize']/@default"; raimund@1738: raimund@1738: public final static String XPATH_TEXT_FONT = raimund@1738: "/theme/field[@name='font']/@default"; felix@1711: raimund@1748: public final static String XPATH_TEXT_STYLE = raimund@1748: "/theme/field[@name='textstyle']/@default"; raimund@1748: raimund@1748: public final static String XPATH_TEXT_ORIENTATION = raimund@1748: "/theme/field[@name='textorientation']/@default"; raimund@1748: raimund@1748: public final static String XPATH_TEXT_BACKGROUND = raimund@1750: "/theme/field[@name='backgroundcolor']/@default"; raimund@1748: raimund@1748: public final static String XPATH_SHOW_BACKGROUND = raimund@1748: "/theme/field[@name='showbackground']/@default"; raimund@1748: felix@1711: /** felix@1711: * Parses line width, defaulting to 0. felix@1711: * @param theme the theme felix@1711: */ felix@1711: public static int parseLineWidth(Document theme) { felix@1711: String size = XMLUtils.xpathString(theme, XPATH_LINE_SIZE, null); felix@1711: if (size == null || size.length() == 0) { felix@1711: return 0; felix@1711: } felix@1711: felix@1711: try { felix@1711: return Integer.valueOf(size); felix@1711: } felix@1711: catch (NumberFormatException nfe) { raimund@1753: logger.warn("Unable to set line size from string: '" + size + "'"); felix@1711: } felix@1711: return 0; felix@1711: } felix@1711: felix@1090: felix@1090: /** raimund@1753: * Parses the line style, defaulting to '10'. raimund@1753: * @param theme The theme. raimund@1753: */ raimund@1753: public static float[] parseLineStyle(Document theme) { raimund@1753: String dash = XMLUtils.xpathString(theme, XPATH_LINE_STYLE, null); raimund@1753: raimund@1753: float[] def = {10}; raimund@1753: if (dash == null || dash.length() == 0) { raimund@1753: return def; raimund@1753: } raimund@1753: raimund@1753: String[] pattern = dash.split(","); raimund@1753: if(pattern.length == 1) { raimund@1753: return def; raimund@1753: } raimund@1753: raimund@1753: try { raimund@1753: float[] dashes = new float[pattern.length]; raimund@1753: for (int i = 0; i < pattern.length; i++) { raimund@1753: dashes[i] = Float.parseFloat(pattern[i]); raimund@1753: } raimund@1753: return dashes; raimund@1753: } raimund@1753: catch(NumberFormatException nfe) { raimund@1753: logger.warn("Unable to set dash from string: '" + dash + "'"); raimund@1753: return def; raimund@1753: } raimund@1753: } raimund@1753: raimund@1753: raimund@1753: /** raimund@1738: * Parses text size, defaulting to 10. raimund@1738: * @param theme The theme. raimund@1738: */ raimund@1738: public static int parseTextSize(Document theme) { raimund@1738: String size = XMLUtils.xpathString(theme, XPATH_TEXT_SIZE, null); raimund@1738: if (size == null || size.length() == 0) { raimund@1738: return 10; raimund@1738: } raimund@1738: raimund@1738: try { raimund@1738: return Integer.valueOf(size); raimund@1738: } raimund@1738: catch (NumberFormatException nfe) { raimund@1738: } raimund@1738: return 10; raimund@1738: } raimund@1738: raimund@1738: raimund@1738: /** raimund@1748: * Parses the attribute 'showpoints', defaults to false. raimund@1748: * @param theme The theme. raimund@1748: */ raimund@1748: public static boolean parseShowPoints(Document theme) { raimund@1748: String show = XMLUtils.xpathString(theme, XPATH_SHOW_POINTS, null); raimund@1748: if (show == null || show.length() == 0) { raimund@1748: return false; raimund@1748: } raimund@1748: if (show.equals("true")) { raimund@1748: return true; raimund@1748: } raimund@1748: else { raimund@1748: return false; raimund@1748: } raimund@1748: } raimund@1748: raimund@1748: raimund@1748: /** raimund@1753: * Parses the attribute 'showlines', defaults to true. raimund@1753: * @param theme The theme. raimund@1753: */ raimund@1753: public static boolean parseShowLine(Document theme) { raimund@1753: String show = XMLUtils.xpathString(theme, XPATH_SHOW_LINE, null); raimund@1753: if (show == null || show.length() == 0) { raimund@1753: return true; raimund@1753: } raimund@1753: if (show.equals("false")) { raimund@1753: return false; raimund@1753: } raimund@1753: else { raimund@1753: return true; raimund@1753: } raimund@1753: } raimund@1753: raimund@1753: raimund@1753: /** raimund@1738: * Parses text color. raimund@1738: * @param theme The theme. raimund@1738: */ raimund@1738: public static Color parseTextColor(Document theme) { raimund@1738: String color = XMLUtils.xpathString(theme, XPATH_TEXT_COLOR, null); raimund@1738: return parseRGB(color); raimund@1738: } raimund@1738: raimund@1738: raimund@1738: /** raimund@1738: * Parses the font. raimund@1738: * @param theme The theme. raimund@1738: */ raimund@1738: public static Font parseTextFont(Document theme) { raimund@1738: String font = XMLUtils.xpathString(theme, XPATH_TEXT_FONT, null); raimund@1738: if (font == null || font.length() == 0) { raimund@1738: return null; raimund@1738: } raimund@1738: raimund@1738: int size = parseTextSize(theme); raimund@1750: int style = parseTextStyle(theme); raimund@1750: Font f = new Font (font, style, size); raimund@1738: return f; raimund@1738: } raimund@1738: raimund@1738: raimund@1738: /** raimund@1748: * Parses the text style, defaults to 'Font.PLAIN'. raimund@1748: * @param theme The theme. raimund@1748: */ raimund@1748: public static int parseTextStyle(Document theme) { raimund@1748: String style = XMLUtils.xpathString(theme, XPATH_TEXT_STYLE, null); raimund@1748: if (style == null || style.length() == 0) { raimund@1748: return Font.PLAIN; raimund@1748: } raimund@1748: raimund@1748: if (style.equals("italic")) { raimund@1748: return Font.ITALIC; raimund@1748: } raimund@1748: else if (style.equals("bold")) { raimund@1748: return Font.BOLD; raimund@1748: } raimund@1748: else { raimund@1748: return Font.PLAIN; raimund@1748: } raimund@1748: } raimund@1748: raimund@1748: raimund@1748: /** raimund@1748: * Parses the textorientation, defaults to 'vertical'. raimund@1748: * @param theme The theme. raimund@1748: */ raimund@1748: public static String parseTextOrientation(Document theme) { raimund@1748: String o = XMLUtils.xpathString(theme, XPATH_TEXT_ORIENTATION, null); raimund@1748: if (o == null || o.length() == 0) { raimund@1748: return "vertical"; raimund@1748: } raimund@1750: if(o.equals("true")) { raimund@1750: return "horizontal"; raimund@1750: } raimund@1750: else { raimund@1750: return "vertical"; raimund@1750: } raimund@1748: } raimund@1748: raimund@1748: raimund@1748: /** raimund@1748: * Parses the text background color, defaults to white. raimund@1748: * @param theme The theme. raimund@1748: */ raimund@1748: public static Color parseTextBackground(Document theme) { raimund@1748: String color = XMLUtils.xpathString(theme, XPATH_TEXT_BACKGROUND, null); raimund@1748: if (color == null || color.length() == 0) { raimund@1748: return Color.WHITE; raimund@1748: } raimund@1748: return parseRGB(color); raimund@1748: } raimund@1748: raimund@1748: raimund@1748: /** raimund@1748: * Parses the attribute whether to show background or not, defaults to raimund@1748: * false. raimund@1748: * @param theme The theme. raimund@1748: */ raimund@1748: public static boolean parseShowTextBackground(Document theme) { raimund@1748: String show = XMLUtils.xpathString(theme, XPATH_SHOW_BACKGROUND, null); raimund@1748: if(show == null || show.length() == 0) { raimund@1748: return false; raimund@1748: } raimund@1748: raimund@1748: if(show.equals("true")) { raimund@1748: return true; raimund@1748: } raimund@1748: else { raimund@1748: return false; raimund@1748: } raimund@1748: } raimund@1748: raimund@1748: raimund@1748: /** felix@1090: * Parse a string like "103, 100, 0" and return a corresping color. felix@1090: * @param rgbtext Color as string representation, e.g. "255,0,20". felix@1090: * @return Color, null in case of issues. felix@1090: */ felix@1090: public static Color parseRGB(String rgbtext) { felix@1090: if (rgbtext == null) { felix@1090: return null; felix@1090: } felix@1090: String rgb[] = rgbtext.split(","); felix@1090: Color c = null; felix@1090: try { felix@1090: c = new Color( felix@1090: Integer.valueOf(rgb[0].trim()), felix@1090: Integer.valueOf(rgb[1].trim()), felix@1090: Integer.valueOf(rgb[2].trim())); felix@1090: } felix@1090: catch (NumberFormatException nfe) { felix@1090: c = null; felix@1090: } felix@1090: return c; felix@1090: } felix@1090: raimund@1748: felix@1090: /** felix@1090: * Gets color from color field. felix@1090: * @param theme the theme document. felix@1090: * @return color. felix@1090: */ felix@1090: public static Color parseLineColorField(Document theme) { felix@1090: String color = XMLUtils.xpathString(theme, XPATH_LINE_COLOR, null); felix@1090: return parseRGB(color); felix@1090: } felix@1090: }