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: 
ingo@1793: import de.intevation.flys.artifacts.model.MapserverStyle;
ingo@1793: import de.intevation.flys.artifacts.model.MapserverStyle.Clazz;
ingo@1876: import de.intevation.flys.artifacts.model.MapserverStyle.Style;
ingo@1876: import de.intevation.flys.artifacts.model.MapserverStyle.Label;
ingo@1793: 
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@2016:     public final static String XPATH_FILL_COLOR =
felix@2016:         "/theme/field[@name='fillcolor']/@default";
felix@2016: 
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: 
felix@2016:     public final static String XPATH_SHOW_BORDER =
felix@2016:         "/theme/field[@name='showborder']/@default";
felix@2016: 
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: 
felix@2032:     public final static String XPATH_TRANSPARENCY =
felix@2032:         "/theme/field[@name='transparent']/@default";
felix@2032: 
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: 
ingo@1816:     public final static String XPATH_SYMBOL =
ingo@1816:         "/theme/field[@name='symbol']/@default";
ingo@1816: 
felix@2016: 
felix@2016:     /** Parse string to be boolean with default if empty or unrecognized. */
felix@2016:     public static boolean parseBoolean(String value, boolean defaultsTo) {
felix@2016:         if (value == null || value.length() == 0) {
felix@2016:             return defaultsTo;
felix@2016:         }
felix@2016:         if (value.equals("false")) {
felix@2016:             return false;
felix@2016:         }
felix@2016:         else if (value.equals("true")) {
felix@2016:             return true;
felix@2016:         }
felix@2016:         else {
felix@2016:             return defaultsTo;
felix@2016:         }
felix@2016:     }
felix@2016: 
felix@2016: 
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);
felix@2016:         return parseBoolean(show, false);
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);
felix@2016:         return parseBoolean(show, true);
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) {
ingo@1876:         return parseRGB(getTextColorString(theme));
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) {
ingo@1878:         String color = getBackgroundColorString(theme);
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);
felix@2016:         return parseBoolean(show, false);
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: 
ingo@1793:     public static String getLineColorString(Document theme) {
ingo@1793:         return XMLUtils.xpathString(theme, XPATH_LINE_COLOR, null);
ingo@1793:     }
ingo@1793: 
felix@2016:     /** Get show border as string. */
felix@2016:     public static String getShowBorderString(Document theme) {
felix@2016:         return XMLUtils.xpathString(theme, XPATH_SHOW_BORDER, null);
felix@2016:     }
felix@2016: 
felix@2016:     /** Get fill color as string. */
felix@2016:     public static String getFillColorString(Document theme) {
felix@2016:         return XMLUtils.xpathString(theme, XPATH_FILL_COLOR, null);
felix@2016:     }
ingo@1793: 
ingo@1878:     public static String getBackgroundColorString(Document theme) {
ingo@1878:         return XMLUtils.xpathString(theme, XPATH_TEXT_BACKGROUND, null);
ingo@1878:     }
ingo@1878: 
ingo@1878: 
ingo@1876:     public static String getTextColorString(Document theme) {
ingo@1876:         return XMLUtils.xpathString(theme, XPATH_TEXT_COLOR, null);
ingo@1876:     }
ingo@1876: 
ingo@1876: 
ingo@1816:     public static String getSymbol(Document theme) {
ingo@1816:         return XMLUtils.xpathString(theme, XPATH_SYMBOL, null);
ingo@1816:     }
ingo@1816: 
felix@2032:     public static String getTransparencyString(Document theme) {
felix@2032:         return XMLUtils.xpathString(theme, XPATH_TRANSPARENCY, null);
felix@2032:     }
felix@2032: 
ingo@1816: 
felix@1090:     /**
felix@1090:      * Gets color from color field.
felix@1090:      * @param theme    the theme document.
felix@1090:      * @return color.
felix@1090:      */
felix@2016:     public static Color parseFillColorField(Document theme) {
felix@2016:         return parseRGB(getFillColorString(theme));
felix@2016:     }
felix@2016: 
felix@2016:     public static boolean parseShowBorder(Document theme) {
felix@2016:         return parseBoolean(getShowBorderString(theme), false);
felix@2016:     }
felix@2016: 
felix@2032:     public static boolean parseTransparency(Document theme) {
felix@2032:         return parseBoolean(getTransparencyString(theme), false);
felix@2032:     }
felix@2016: 
felix@2016:     /**
felix@2016:      * Gets color from color field.
felix@2016:      * @param theme    the theme document.
felix@2016:      * @return color.
felix@2016:      */
felix@1090:     public static Color parseLineColorField(Document theme) {
ingo@1793:         return parseRGB(getLineColorString(theme));
ingo@1793:     }
ingo@1793: 
ingo@1793: 
ingo@1793:     public static String createMapserverStyle(Document theme) {
ingo@1816:         String symbol    = getSymbol(theme);
ingo@1878:         String backcolor = getBackgroundColorString(theme);
ingo@1793:         String linecolor = getLineColorString(theme);
ingo@1816: 
ingo@1816:         int linewidth = parseLineWidth(theme);
ingo@1793: 
ingo@1793:         MapserverStyle ms = new MapserverStyle();
ingo@1793: 
ingo@1793:         Clazz c = new Clazz(" ");
ingo@1876: 
ingo@1876:         Style s = new Style();
ingo@1878:         s.setOutlineColor(linecolor.replace(",", ""));
ingo@1878: 
ingo@1878:         if (backcolor != null && backcolor.length() > 0) {
ingo@1878:             s.setColor(backcolor.replace(",", ""));
ingo@1878:         }
ingo@1878: 
ingo@1876:         s.setSize(linewidth);
ingo@1876:         s.setSymbol(symbol);
ingo@1876:         c.addItem(s);
ingo@1876: 
ingo@1876:         String textcolor = getTextColorString(theme);
ingo@1876:         int    textsize  = parseTextSize(theme);
ingo@1876: 
ingo@1876:         if (textcolor != null && textcolor.length() > 0 && textsize > 0) {
ingo@1876:             Label l = new Label();
ingo@1876:             l.setColor(textcolor.replace(",", ""));
ingo@1876:             l.setSize(textsize);
ingo@1876:             c.addItem(l);
ingo@1876:         }
ingo@1793: 
ingo@1793:         ms.addClazz(c);
ingo@1793: 
ingo@1793:         return ms.toString();
felix@1090:     }
felix@1090: }
felix@2016: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :