teichmann@6905: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@6905: * Software engineering by Intevation GmbH teichmann@6905: * teichmann@6905: * This file is Free Software under the GNU AGPL (>=v3) teichmann@6905: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@6905: * documentation coming with Dive4Elements River for details. teichmann@6905: */ teichmann@6905: teichmann@6905: package org.dive4elements.river.themes; teichmann@6905: teichmann@6905: import java.awt.Color; teichmann@6905: import java.awt.Font; teichmann@6908: import java.util.HashMap; teichmann@6908: import java.util.Map; teichmann@6905: gernotbelger@8910: import org.apache.commons.lang.StringUtils; teichmann@6906: import org.apache.log4j.Logger; teichmann@6905: import org.dive4elements.artifacts.CallMeta; teichmann@6906: import org.dive4elements.river.artifacts.model.MapserverStyle; teichmann@6906: import org.dive4elements.river.artifacts.model.MapserverStyle.Clazz; teichmann@6906: import org.dive4elements.river.artifacts.model.MapserverStyle.Expression; teichmann@6906: import org.dive4elements.river.artifacts.model.MapserverStyle.Label; teichmann@6906: import org.dive4elements.river.artifacts.model.MapserverStyle.Style; teichmann@6906: import org.dive4elements.river.artifacts.resources.Resources; gernotbelger@8910: import org.dive4elements.river.jfree.AreaFillPattern; teichmann@6905: import org.w3c.dom.Document; teichmann@6908: import org.w3c.dom.Element; teichmann@6908: import org.w3c.dom.NodeList; teichmann@6905: teichmann@6905: public class ThemeDocument teichmann@6905: { teichmann@8202: private static Logger log = Logger.getLogger(ThemeDocument.class); teichmann@6906: teichmann@6906: private static final String MSG_ISOBATH_CLASS = "floodmap.isobath.class"; teichmann@6906: tom@8856: private static final String MSG_ISOBATH_LASTCLASS = gernotbelger@9555: "floodmap.isobath.lastclass"; teichmann@6906: teichmann@6908: public final static String FILL_COLOR = "fillcolor"; teichmann@6906: teichmann@6908: public final static String LINE_COLOR = "linecolor"; teichmann@6906: teichmann@6908: public final static String AREA_LINE_COLOR = "areabordercolor"; teichmann@6906: teichmann@6908: public static final String LINE_SIZE = "linesize"; teichmann@6906: teichmann@6908: public static final String LINE_STYLE = "linetype"; teichmann@6906: teichmann@6908: public static final String POINT_SIZE = "pointsize"; teichmann@6906: teichmann@6908: public static final String POINT_COLOR = "pointcolor"; teichmann@6908: teichmann@6908: public final static String SHOW_BORDER = "showborder"; teichmann@6908: teichmann@6908: public final static String AREA_SHOW_BORDER = "showborder"; teichmann@6908: teichmann@6908: public final static String SHOW_POINTS = "showpoints"; teichmann@6908: gernotbelger@9325: public final static String SHOW_POINTS_OUTLINE = "showpointsoutline"; gernotbelger@9325: teichmann@6908: public final static String SHOW_LINE = "showlines"; teichmann@6908: teichmann@6908: public final static String SHOW_VERTICAL_LINE = "showverticalline"; teichmann@6908: teichmann@6908: public final static String SHOW_HORIZONTAL_LINE = "showhorizontalline"; teichmann@6908: teichmann@6908: public final static String SHOW_LINE_LABEL = "showlinelabel"; teichmann@6908: teichmann@6908: public final static String SHOW_POINT_LABEL = "showpointlabel"; teichmann@6908: teichmann@6908: public final static String SHOW_WIDTH = "showwidth"; teichmann@6908: teichmann@6908: public final static String SHOW_LEVEL = "showlevel"; teichmann@6908: teichmann@6908: public final static String TRANSPARENCY = "transparency"; teichmann@6908: teichmann@6908: public final static String AREA_TRANSPARENCY = "areatransparency"; teichmann@6908: teichmann@6908: public final static String SHOW_AREA = "showarea"; teichmann@6908: aheinecke@7119: public final static String SHOW_AREA_LABEL = "showarealabel"; aheinecke@7119: teichmann@6908: public final static String SHOW_MIDDLE_HEIGHT = "showmiddleheight"; teichmann@6908: teichmann@6908: public final static String LABEL_FONT_COLOR = "labelfontcolor"; teichmann@6908: teichmann@6908: public final static String LABEL_FONT_SIZE = "labelfontsize"; teichmann@6908: teichmann@6908: public final static String LABEL_FONT_FACE = "labelfontface"; teichmann@6908: teichmann@6908: public final static String LABEL_FONT_STYLE = "labelfontstyle"; teichmann@6908: felix@7582: public final static String FONT = "font"; felix@7582: felix@7582: public final static String TEXT_SIZE = "textsize"; felix@7582: felix@7582: public final static String TEXT_STYLE = "textstyle"; felix@7582: teichmann@6908: public final static String TEXT_ORIENTATION = "textorientation"; teichmann@6908: teichmann@6908: public final static String LABEL_BGCOLOR = "labelbgcolor"; teichmann@6908: teichmann@6908: public final static String LABEL_SHOW_BACKGROUND = "labelshowbg"; teichmann@6908: teichmann@6908: public final static String BACKGROUND_COLOR = "backgroundcolor"; teichmann@6908: teichmann@6908: public final static String AREA_BACKGROUND_COLOR = "areabgcolor"; teichmann@6908: gernotbelger@8910: private static final String AREA_BACKGROUND_PATTERN = "areabgpattern"; gernotbelger@8910: teichmann@6908: public final static String SYMBOL = "symbol"; teichmann@6908: teichmann@6908: public final static String SHOW_MINIMUM = "showminimum"; teichmann@6908: teichmann@6908: public final static String SHOW_MAXIMUM = "showmaximum"; teichmann@6908: teichmann@6908: public final static String WSPLGEN_STARTCOLOR = "startcolor"; teichmann@6908: teichmann@6908: public final static String WSPLGEN_ENDCOLOR = "endcolor"; teichmann@6908: teichmann@6908: public final static String WSPLGEN_NUMCLASSES = "numclasses"; teichmann@6908: teichmann@6908: public final static String BANDWIDTH = "bandwidth"; teichmann@6908: teichmann@6908: public final static String SHOWEXTRAMARK = "showextramark"; teichmann@6908: gernotbelger@9602: private final static String BASE_LINE_COLOR = "baselineColor"; gernotbelger@9602: gernotbelger@8885: /* boolean parameter if the range of this theme should be considered when auto-zooming. Only works for area-series at the moment */ gernotbelger@8885: private static final String CALCULATE_RANGE = "calculateRange"; gernotbelger@8885: teichmann@6908: private Map values; teichmann@6908: teichmann@6905: public ThemeDocument() { teichmann@6905: } teichmann@6905: gernotbelger@9555: public ThemeDocument(final Document document) { gernotbelger@9555: this.values = extractValues(document); teichmann@6905: } teichmann@6905: gernotbelger@9555: public ThemeDocument(final ThemeDocument other) { gernotbelger@9555: this.values = new HashMap<>(other.values); teichmann@6908: } teichmann@6908: gernotbelger@9555: public String getValue(final String key) { gernotbelger@9555: return this.values.get(key); teichmann@7545: } teichmann@7545: gernotbelger@9555: public void setValue(final String key, final String value) { gernotbelger@9555: this.values.put(key, value); gernotbelger@9555: } gernotbelger@9555: gernotbelger@9555: private static Map extractValues(final Document document) { gernotbelger@9555: final Map values = new HashMap<>(); teichmann@6962: if (document == null) { teichmann@8202: log.error("Invalid null document given."); teichmann@6962: return values; teichmann@6962: } teichmann@6962: gernotbelger@9555: final NodeList fields = document.getElementsByTagName("field"); teichmann@6908: for (int i = 0, N = fields.getLength(); i < N; ++i) { gernotbelger@9555: final Element field = (Element)fields.item(i); gernotbelger@9555: final String name = field.getAttribute("name"); gernotbelger@9555: final String value = field.getAttribute("default"); teichmann@6908: if (!name.isEmpty() && !value.isEmpty()) { teichmann@6908: values.put(name, value); teichmann@6908: } teichmann@6908: } teichmann@8202: if (log.isDebugEnabled()) { teichmann@8202: log.debug("Theme values: " + values); teichmann@6912: } teichmann@6908: return values; teichmann@6908: } teichmann@6908: teichmann@6906: /** Parse string to be boolean with default if empty or unrecognized. */ gernotbelger@9555: private static boolean parseBoolean(final String value, final boolean defaultsTo) { teichmann@6909: if (value == null) { teichmann@6906: return defaultsTo; teichmann@6906: } teichmann@6906: if (value.equals("false")) { teichmann@6906: return false; teichmann@6906: } teichmann@6908: if (value.equals("true")) { teichmann@6906: return true; teichmann@6906: } teichmann@6908: return defaultsTo; teichmann@6906: } teichmann@6906: teichmann@6906: teichmann@6906: /** teichmann@6906: * Attempt converting \param value to an integer, in failing cases, teichmann@6906: * return \param defaultsTo. teichmann@6906: * @param value String to be converted to integer. teichmann@6906: * @param defaultsTo Default to return if conversion failed. teichmann@6906: * @return \param value as integer or defaultsto if conversion failed. teichmann@6906: */ gernotbelger@9555: private static int parseInteger(final String value, final int defaultsTo) { teichmann@6909: if (value == null) { teichmann@6906: return defaultsTo; teichmann@6906: } teichmann@6906: teichmann@6906: try { teichmann@6906: return Integer.parseInt(value); teichmann@6906: } gernotbelger@9555: catch (final NumberFormatException nfe) { teichmann@6906: // do nothing teichmann@6906: } teichmann@6906: teichmann@6906: return defaultsTo; teichmann@6906: } teichmann@6906: teichmann@6906: teichmann@6906: /** teichmann@6906: * Attempt converting \param value to a double, in failing cases, teichmann@6906: * return \param defaultsTo. teichmann@6906: * @param value String to be converted to double. teichmann@6906: * @param defaultsTo Default to return if conversion failed. teichmann@6906: * @return \param value as integer or defaultsto if conversion failed. teichmann@6906: */ gernotbelger@9555: private static double parseDouble(final String value, final double defaultsTo) { teichmann@6909: if (value == null) { teichmann@6906: return defaultsTo; teichmann@6906: } teichmann@6906: teichmann@6906: try { teichmann@6906: return Double.parseDouble(value); teichmann@6906: } gernotbelger@9555: catch (final NumberFormatException nfe) { teichmann@6906: // do nothing teichmann@6906: } teichmann@6906: teichmann@6906: return defaultsTo; teichmann@6905: } teichmann@6905: teichmann@6905: public boolean parseShowLineLabel() { gernotbelger@9555: final String show = getValue(SHOW_LINE_LABEL); teichmann@6906: return parseBoolean(show, false); teichmann@6905: } teichmann@6905: teichmann@6905: public boolean parseShowWidth() { gernotbelger@9555: final String show = getValue(SHOW_WIDTH); teichmann@6906: return parseBoolean(show, false); teichmann@6905: } teichmann@6905: teichmann@6905: public boolean parseShowLevel() { gernotbelger@9555: final String show = getValue(SHOW_LEVEL); teichmann@6906: return parseBoolean(show, false); teichmann@6906: } teichmann@6906: teichmann@6906: public String parseTextOrientation() { gernotbelger@9555: final String o = getValue(TEXT_ORIENTATION); teichmann@6908: teichmann@6908: return o != null && "true".equals(o) gernotbelger@9555: ? "horizontal" gernotbelger@9555: : "vertical"; teichmann@6905: } teichmann@6905: teichmann@6905: public boolean parseShowMiddleHeight() { gernotbelger@9555: final String show = getValue(SHOW_MIDDLE_HEIGHT); teichmann@6906: return parseBoolean(show, false); teichmann@6905: } teichmann@6905: teichmann@6905: public boolean parseLabelShowBackground() { gernotbelger@9555: final String show = getValue(LABEL_SHOW_BACKGROUND); teichmann@6906: return parseBoolean(show, false); teichmann@6905: } teichmann@6905: felix@7582: public Font parseFont() { gernotbelger@9555: final String font = getValue(FONT); teichmann@8202: log.debug(" font is " + font); felix@7582: if (font == null) { felix@7582: return null; felix@7582: } felix@7582: gernotbelger@9555: final int size = parseFontSize(); gernotbelger@9555: final int style = parseFontStyle(); gernotbelger@9555: final Font f = new Font(font, style, size); felix@7582: return f; felix@7582: } felix@7582: teichmann@6905: public Font parseTextFont() { gernotbelger@9555: final String font = getValue(LABEL_FONT_FACE); teichmann@6909: if (font == null) { teichmann@6906: return null; teichmann@6906: } teichmann@6906: gernotbelger@9555: final int size = parseTextSize(); gernotbelger@9555: final int style = parseTextStyle(); gernotbelger@9555: final Font f = new Font(font, style, size); teichmann@6906: return f; teichmann@6905: } teichmann@6905: teichmann@6905: public Color parseTextColor() { teichmann@6906: return parseRGB(getTextColorString()); teichmann@6906: } teichmann@6906: teichmann@6909: private String getTextColorString() { teichmann@6909: return getValue(LABEL_FONT_COLOR); teichmann@6905: } teichmann@6905: teichmann@6905: public Color parseTextBackground() { gernotbelger@9555: final String color = getLabelBackgroundColorString(); teichmann@6909: return color != null gernotbelger@9555: ? parseRGB(color) gernotbelger@9555: : Color.WHITE; teichmann@6905: } teichmann@6905: teichmann@6908: private String getLabelBackgroundColorString() { teichmann@6908: return getValue(LABEL_BGCOLOR); teichmann@6906: } teichmann@6906: teichmann@6905: public int parseLineWidth() { gernotbelger@9555: final String size = getValue(LINE_SIZE); teichmann@6909: if (size == null) { teichmann@6906: return 0; teichmann@6906: } teichmann@6906: teichmann@6906: try { teichmann@6906: return Integer.parseInt(size); teichmann@6906: } gernotbelger@9555: catch (final NumberFormatException nfe) { teichmann@8202: log.warn("Unable to set line size from string: '" + size + "'"); teichmann@6906: } teichmann@6905: return 0; teichmann@6905: } teichmann@6905: teichmann@6905: public float [] parseLineStyle() { gernotbelger@9555: final String dash = getValue(LINE_STYLE); teichmann@6906: gernotbelger@9555: final float[] def = {10}; teichmann@6909: if (dash == null) { teichmann@6906: return def; teichmann@6906: } teichmann@6906: gernotbelger@9555: final String[] pattern = dash.split(","); teichmann@6906: if(pattern.length == 1) { teichmann@6906: return def; teichmann@6906: } teichmann@6906: teichmann@6906: try { gernotbelger@9555: final float[] dashes = new float[pattern.length]; teichmann@6906: for (int i = 0; i < pattern.length; i++) { teichmann@6906: dashes[i] = Float.parseFloat(pattern[i]); teichmann@6906: } teichmann@6906: return dashes; teichmann@6906: } gernotbelger@9555: catch (final NumberFormatException nfe) { teichmann@8202: log.warn("Unable to set dash from string: '" + dash + "'"); teichmann@6906: return def; teichmann@6906: } teichmann@6905: } teichmann@6905: teichmann@6905: public int parsePointWidth() { gernotbelger@9555: final String width = getValue(POINT_SIZE); teichmann@6906: return parseInteger(width, 3); teichmann@6905: } teichmann@6905: teichmann@6905: public Color parsePointColor() { gernotbelger@9555: final String color = getValue(POINT_COLOR); teichmann@6906: return parseColor(color); teichmann@6905: } teichmann@6905: teichmann@6905: public boolean parseShowPoints() { gernotbelger@9555: final String show = getValue(SHOW_POINTS); teichmann@6906: return parseBoolean(show, false); teichmann@6905: } teichmann@6905: gernotbelger@9325: public boolean parseShowPointsOutline() { gernotbelger@9555: final String show = getValue(SHOW_POINTS_OUTLINE); gernotbelger@9325: return parseBoolean(show, false); gernotbelger@9325: } gernotbelger@9325: teichmann@6905: public boolean parseShowLine() { gernotbelger@9555: final String show = getValue(SHOW_LINE); andre@8635: return parseBoolean(show, false); teichmann@6905: } teichmann@6905: felix@7582: public int parseFontStyle() { gernotbelger@9555: final String style = getValue(TEXT_STYLE); felix@7582: if (style == null) { felix@7582: return Font.PLAIN; felix@7582: } felix@7582: felix@7582: if (style.equals("italic")) { felix@7582: return Font.ITALIC; felix@7582: } felix@7582: if (style.equals("bold")) { felix@7582: return Font.BOLD; felix@7582: } felix@7582: return Font.PLAIN; felix@7582: } felix@7582: teichmann@6906: public int parseTextStyle() { gernotbelger@9555: final String style = getValue(LABEL_FONT_STYLE); teichmann@6909: if (style == null) { teichmann@6906: return Font.PLAIN; teichmann@6906: } teichmann@6906: teichmann@6906: if (style.equals("italic")) { teichmann@6906: return Font.ITALIC; teichmann@6906: } teichmann@6908: if (style.equals("bold")) { teichmann@6906: return Font.BOLD; teichmann@6906: } teichmann@6908: return Font.PLAIN; teichmann@6906: } teichmann@6906: felix@7583: /** Handle info for label or annotation text. */ teichmann@6906: public TextStyle parseComplexTextStyle() { felix@7583: Font font = parseTextFont(); felix@7583: // Try the annotation text styles. felix@7583: if (font == null) { felix@7583: font = parseFont(); felix@7583: } teichmann@6906: return new TextStyle( gernotbelger@9555: parseTextColor(), gernotbelger@9555: font, gernotbelger@9555: parseTextBackground(), gernotbelger@9555: parseLabelShowBackground(), gernotbelger@9555: !parseTextOrientation().equals("horizontal")); teichmann@6905: } teichmann@6905: teichmann@6905: public LineStyle parseComplexLineStyle() { teichmann@6906: return new LineStyle( gernotbelger@9555: parseLineColorField(), gernotbelger@9555: Integer.valueOf(parseLineWidth())); teichmann@6905: } teichmann@6905: teichmann@6905: public boolean parseShowVerticalLine() { gernotbelger@9555: final String show = getValue(SHOW_VERTICAL_LINE); teichmann@6906: return parseBoolean(show, true); teichmann@6905: } teichmann@6905: teichmann@6905: public boolean parseShowHorizontalLine() { gernotbelger@9555: final String show = getValue(SHOW_HORIZONTAL_LINE); teichmann@6906: return parseBoolean(show, true); teichmann@6905: } teichmann@6905: teichmann@6905: public double parseBandWidth() { gernotbelger@9555: final String bandWidth = getValue(BANDWIDTH); teichmann@6906: return parseDouble(bandWidth, 0); teichmann@6905: } teichmann@6905: gernotbelger@9555: private static Color parseColor(final String colorString) { teichmann@6909: if (colorString == null) { teichmann@6906: return null; teichmann@6906: } teichmann@6909: if (colorString.indexOf("#") == 0) { teichmann@6906: return parseHexColor(colorString); teichmann@6906: } teichmann@6909: if (colorString.indexOf(",") >= 0) { teichmann@6906: return parseRGB(colorString); teichmann@6906: } teichmann@6905: teichmann@6905: return null; teichmann@6905: } teichmann@6905: teichmann@6906: teichmann@6906: /** teichmann@6906: * Parse a string like "#00CC22" and return the corresponding color. teichmann@6906: * teichmann@6906: * @param hex The hex color value. teichmann@6906: * teichmann@6906: * @return a Color or null, if hex is empty. teichmann@6906: */ gernotbelger@9555: private static Color parseHexColor(final String hex) { teichmann@6909: return hex != null gernotbelger@9555: ? Color.decode(hex) gernotbelger@9555: : null; teichmann@6905: } teichmann@6905: teichmann@6905: teichmann@6905: public boolean parseShowArea() { gernotbelger@9555: final String show = getValue(SHOW_AREA); teichmann@6906: return parseBoolean(show, false); teichmann@6905: } teichmann@6905: aheinecke@7119: public boolean parseShowAreaLabel() { gernotbelger@9555: final String show = getValue(SHOW_AREA_LABEL); aheinecke@7119: return parseBoolean(show, false); aheinecke@7119: } aheinecke@7119: teichmann@6905: public boolean parseShowPointLabel() { gernotbelger@9555: final String show = getValue(SHOW_POINT_LABEL); teichmann@6906: return parseBoolean(show, false); teichmann@6905: } teichmann@6905: teichmann@6905: public boolean parseShowExtraMark() { gernotbelger@9555: final String show = getValue(SHOWEXTRAMARK); teichmann@6906: return parseBoolean(show, false); teichmann@6905: } teichmann@6905: felix@7582: public int parseFontSize() { gernotbelger@9555: final String size = getValue(TEXT_SIZE); felix@7582: if (size == null) { felix@7582: return 10; felix@7582: } felix@7582: felix@7582: try { felix@7582: return Integer.parseInt(size); felix@7582: } gernotbelger@9555: catch (final NumberFormatException nfe) { felix@7582: // Do nothing felix@7582: } felix@7582: return 10; felix@7582: } felix@7582: teichmann@6906: public int parseTextSize() { gernotbelger@9555: final String size = getValue(LABEL_FONT_SIZE); teichmann@6909: if (size == null) { teichmann@6906: return 10; teichmann@6906: } teichmann@6906: teichmann@6906: try { teichmann@6906: return Integer.parseInt(size); teichmann@6906: } gernotbelger@9555: catch (final NumberFormatException nfe) { teichmann@6909: // Do nothing teichmann@6906: } teichmann@6906: return 10; teichmann@6906: } teichmann@6906: teichmann@6906: /** teichmann@6906: * Parse a string like "103, 100, 0" and return a corresping color. teichmann@6906: * @param rgbtext Color as string representation, e.g. "255,0,20". teichmann@6906: * @return Color, null in case of issues. teichmann@6906: */ gernotbelger@9555: public static Color parseRGB(final String rgbtext) { teichmann@6906: if (rgbtext == null) { teichmann@6906: return null; teichmann@6906: } gernotbelger@9555: final String rgb[] = rgbtext.split(","); teichmann@6906: try { teichmann@6909: return new Color( gernotbelger@9555: Integer.parseInt(rgb[0].trim()), gernotbelger@9555: Integer.parseInt(rgb[1].trim()), gernotbelger@9555: Integer.parseInt(rgb[2].trim())); teichmann@6906: } gernotbelger@9555: catch (final NumberFormatException nfe) { teichmann@6909: // Do nothing teichmann@6906: } teichmann@6909: return null; teichmann@6906: } teichmann@6906: teichmann@6912: private String getLineColorString() { teichmann@6908: return getValue(LINE_COLOR); teichmann@6906: } teichmann@6906: teichmann@6906: teichmann@6906: /** Get show border as string. */ teichmann@6912: private String getShowBorderString() { teichmann@6908: return getValue(SHOW_BORDER); teichmann@6906: } teichmann@6906: teichmann@6906: teichmann@6906: /** Get fill color as string. */ teichmann@6908: private String getFillColorString() { teichmann@6908: return getValue(FILL_COLOR); teichmann@6908: } teichmann@6908: teichmann@6912: private String getSymbol() { teichmann@6908: return getValue(SYMBOL); teichmann@6906: } teichmann@6906: teichmann@6908: private String getTransparencyString() { teichmann@6908: return getValue(TRANSPARENCY); teichmann@6906: } teichmann@6906: teichmann@6906: teichmann@6912: private String getAreaTransparencyString() { teichmann@6908: return getValue(AREA_TRANSPARENCY); teichmann@6906: } teichmann@6906: teichmann@6906: teichmann@6912: private String getShowMinimum() { teichmann@6908: return getValue(SHOW_MINIMUM); teichmann@6906: } teichmann@6906: teichmann@6906: teichmann@6912: private String getShowMaximum() { teichmann@6908: return getValue(SHOW_MAXIMUM); teichmann@6906: } teichmann@6906: teichmann@6906: /** teichmann@6906: * Gets color from color field. teichmann@6906: * @param theme the theme document. teichmann@6906: * @return color. teichmann@6906: */ teichmann@6906: public Color parseFillColorField() { teichmann@6906: return parseRGB(getFillColorString()); teichmann@6906: } teichmann@6906: teichmann@6906: public boolean parseShowBorder() { teichmann@6906: return parseBoolean(getShowBorderString(), false); teichmann@6906: } teichmann@6906: teichmann@6906: public int parseTransparency() { teichmann@6906: return parseInteger(getTransparencyString(), 50); teichmann@6906: } teichmann@6906: gernotbelger@9602: public Color parseBaseLineColor() { gernotbelger@9602: final String colorStr = getValue(BASE_LINE_COLOR); gernotbelger@9602: return parseColor(colorStr); gernotbelger@9602: } teichmann@6906: teichmann@6906: /** teichmann@6906: * Gets color from color field. teichmann@6906: * @return color. teichmann@6906: */ teichmann@6906: public Color parseLineColorField() { gernotbelger@9555: final String lineColorStr = getLineColorString(); teichmann@8202: if (log.isDebugEnabled()) { teichmann@8202: log.debug("parseLineColorField: lineColorStr = " + gernotbelger@9555: (lineColorStr == null gernotbelger@9555: ? "null" gernotbelger@9555: : lineColorStr)); teichmann@6909: } teichmann@6906: return parseColor(lineColorStr); teichmann@6906: } teichmann@6906: gernotbelger@8885: // FIXME: check, this is defined in default.xml, but never used. Instead the StyledAreaSeriesCollection used lineColor etc teichmann@6906: public Color parseAreaLineColorField() { gernotbelger@9555: final String lineColorStr = getAreaLineColorString(); teichmann@8202: if (log.isDebugEnabled()) { teichmann@8202: log.debug("parseLineColorField: lineColorStr = " + gernotbelger@9555: (lineColorStr == null teichmann@6909: ? "null" gernotbelger@9555: : lineColorStr)); teichmann@6909: } teichmann@6906: return parseColor(lineColorStr); teichmann@6906: } teichmann@6906: teichmann@6906: teichmann@6906: private String getAreaLineColorString() { teichmann@6908: return getValue(AREA_LINE_COLOR); teichmann@6906: } teichmann@6906: teichmann@6906: teichmann@6906: public boolean parseShowMinimum() { teichmann@6906: return parseBoolean(getShowMinimum(), false); teichmann@6906: } teichmann@6906: teichmann@6906: teichmann@6906: public boolean parseShowMaximum() { teichmann@6906: return parseBoolean(getShowMaximum(), false); teichmann@6906: } teichmann@6906: teichmann@6906: teichmann@6906: /** teichmann@6906: * Creates a MapserverStyle from the given XML theme. teichmann@6906: * This method uses a start- and endcolor to interpolate a teichmann@6906: * given number of color classes for the MapserverStyle. teichmann@6906: * @param theme teichmann@6906: * @return String representation of the MapserverStyle teichmann@6906: */ teichmann@6905: public String createDynamicMapserverStyle( gernotbelger@9555: final float from, gernotbelger@9555: float to, gernotbelger@9555: float step, gernotbelger@9555: final CallMeta meta gernotbelger@9555: ) { gernotbelger@9555: final MapserverStyle ms = new MapserverStyle(); teichmann@6906: gernotbelger@9555: final String strStartColor = getValue(WSPLGEN_STARTCOLOR); gernotbelger@9555: final Color startColor = strStartColor != null gernotbelger@9555: ? parseColor(strStartColor) gernotbelger@9555: : new Color(178, 201, 215); gernotbelger@9555: final String strEndColor = getValue(WSPLGEN_ENDCOLOR); gernotbelger@9555: final Color endColor = strEndColor != null gernotbelger@9555: ? parseColor(strEndColor) gernotbelger@9555: : new Color(2, 27, 42); teichmann@6906: gernotbelger@9555: to = to >= 0 ? to : 9999; gernotbelger@9555: step = to != from ? step : 1; teichmann@6906: gernotbelger@9555: final int numClasses = (int)((to - from) / step + 1); teichmann@6906: gernotbelger@9555: final float rd = (endColor.getRed() - startColor.getRed()) gernotbelger@9555: / (float)numClasses; gernotbelger@9555: final float gd = (endColor.getGreen() - startColor.getGreen()) gernotbelger@9555: / (float)numClasses; gernotbelger@9555: final float bd = (endColor.getBlue() - startColor.getBlue()) gernotbelger@9555: / (float)numClasses; teichmann@6906: gernotbelger@9555: for (int n = 0; n < numClasses; n++) { gernotbelger@9555: final StringBuilder newColor = new StringBuilder(); gernotbelger@9555: newColor.append(startColor.getRed() + Math.round(n * rd)); gernotbelger@9555: newColor.append(' '); gernotbelger@9555: newColor.append(startColor.getGreen() + Math.round(n * gd)); gernotbelger@9555: newColor.append(' '); gernotbelger@9555: newColor.append(startColor.getBlue() + Math.round(n * bd)); teichmann@6906: gernotbelger@9555: final String expr = createWSPLGENClassExpression( gernotbelger@9555: from + n * step, step, n + 1, numClasses); gernotbelger@9555: final String name = createWSPLGENClassName( gernotbelger@9555: from + n * step, step, n + 1, numClasses, meta); gernotbelger@9555: gernotbelger@9555: final Clazz c = new Clazz(name); gernotbelger@9555: final Style s = new Style(); gernotbelger@9555: s.setColor(newColor.toString()); gernotbelger@9555: s.setSize(5); gernotbelger@9555: gernotbelger@9555: c.addItem(new Expression("(" + expr + ")")); gernotbelger@9555: c.addItem(s); gernotbelger@9555: gernotbelger@9555: ms.addClazz(c); gernotbelger@9555: } gernotbelger@9555: gernotbelger@9555: return ms.toString(); teichmann@6905: } teichmann@6905: teichmann@6906: tom@8856: protected static String createWSPLGENClassExpression( gernotbelger@9555: final float val, gernotbelger@9555: final float step, gernotbelger@9555: final int idx, gernotbelger@9555: final int maxIdx gernotbelger@9555: ) { teichmann@6906: if (idx < maxIdx) { teichmann@6906: return "[DIFF] >= " + val + " AND [DIFF] < " + (val + step); teichmann@6906: } teichmann@6906: else { teichmann@6906: return "[DIFF] >= " + val; teichmann@6906: } teichmann@6906: } teichmann@6906: teichmann@6906: /** teichmann@6906: * Creates a class name for the mapfile style that visualizes a floodmap. teichmann@6906: * The class names are used in the map's legend. teichmann@6906: * teichmann@6906: * @param val Current isobath value. teichmann@6906: * @param step Difference between to class values. teichmann@6906: * @param idx Current class index that is being processed. teichmann@6906: * @param maxIdx Highest class index. teichmann@6906: * @param meta Caller meta object used to determine locale. teichmann@6906: * @return teichmann@6906: */ teichmann@6912: protected static String createWSPLGENClassName( gernotbelger@9555: final float val, gernotbelger@9555: final float step, gernotbelger@9555: final int idx, gernotbelger@9555: final int maxIdx, gernotbelger@9555: final CallMeta meta gernotbelger@9555: ) { teichmann@6906: assert meta != null : "CallMeta instance is null"; teichmann@6906: teichmann@6906: if (idx < maxIdx) { teichmann@6906: return Resources.getMsg(meta, MSG_ISOBATH_CLASS, teichmann@6906: new Object[] {val, val + step}); teichmann@6906: } teichmann@6909: return Resources.getMsg(meta, MSG_ISOBATH_LASTCLASS, teichmann@6909: new Object[] {val}); teichmann@6906: } teichmann@6906: teichmann@6906: teichmann@6906: public String createMapserverStyle() { gernotbelger@9555: final String symbol = getSymbol(); gernotbelger@9555: final String backcolor = getLabelBackgroundColorString(); teichmann@6906: String linecolor = getLineColorString(); teichmann@6909: if (linecolor == null) { teichmann@8202: log.warn("createMapserverStyle: linecolor String is empty"); teichmann@6906: linecolor = "0,128,255"; teichmann@6906: } teichmann@6906: gernotbelger@9555: final int linewidth = parseLineWidth(); teichmann@6906: gernotbelger@9555: final MapserverStyle ms = new MapserverStyle(); teichmann@6906: gernotbelger@9555: final Clazz c = new Clazz(" "); teichmann@6906: gernotbelger@9555: final Style s = new Style(); teichmann@6906: s.setOutlineColor(linecolor.replace(",", " ")); teichmann@6906: teichmann@6909: if (backcolor != null) { teichmann@6906: s.setColor(backcolor.replace(",", " ")); teichmann@6906: } teichmann@6906: teichmann@6906: s.setSize(linewidth); teichmann@6906: s.setSymbol(symbol); teichmann@6906: c.addItem(s); teichmann@6906: gernotbelger@9555: final String textcolor = getTextColorString(); gernotbelger@9555: final int textsize = parseTextSize(); teichmann@6906: teichmann@6909: if (textcolor != null && textsize > 0) { gernotbelger@9555: final Label l = new Label(); teichmann@6906: l.setColor(textcolor.replace(",", " ")); teichmann@6906: l.setSize(textsize); teichmann@6906: c.addItem(l); teichmann@6906: } teichmann@6906: teichmann@6906: ms.addClazz(c); teichmann@6906: teichmann@6906: return ms.toString(); teichmann@6906: } teichmann@6906: teichmann@6906: teichmann@6908: private String getAreaBackgroundColorString() { teichmann@6908: return getValue(AREA_BACKGROUND_COLOR); teichmann@6906: } teichmann@6906: teichmann@6906: teichmann@6905: public Color parseAreaBackgroundColor() { teichmann@6906: return parseColor(getAreaBackgroundColorString()); teichmann@6905: } teichmann@6905: teichmann@6906: teichmann@6906: public int parseAreaTransparency() { teichmann@6908: return parseAreaTransparency(50); teichmann@6908: } teichmann@6908: gernotbelger@9555: public int parseAreaTransparency(final int alpha) { teichmann@6908: return parseInteger(getAreaTransparencyString(), alpha); teichmann@6905: } teichmann@6905: teichmann@6906: teichmann@6906: public boolean parseAreaShowBorder() { teichmann@6906: return parseBoolean(getAreaShowBorderString(), false); teichmann@6906: } teichmann@6906: teichmann@6906: teichmann@6906: private String getAreaShowBorderString() { teichmann@6908: return getValue(AREA_SHOW_BORDER); teichmann@6906: } gernotbelger@9555: gernotbelger@9555: gernotbelger@8885: public boolean parseCalculateRange() { gernotbelger@8885: return parseBoolean(getCalculateRangeString(), false); gernotbelger@8885: } gernotbelger@8885: gernotbelger@9555: gernotbelger@8885: private String getCalculateRangeString() { gernotbelger@8885: return getValue(CALCULATE_RANGE); gernotbelger@8885: } gernotbelger@8910: gernotbelger@8910: public AreaFillPattern parseAreaBackgroundPattern() { gernotbelger@8910: final String patternName = getValue(AREA_BACKGROUND_PATTERN); gernotbelger@8910: if( StringUtils.isBlank(patternName) ) gernotbelger@8910: return null; gernotbelger@9555: gernotbelger@8910: try { gernotbelger@8910: return AreaFillPattern.valueOf(patternName); gernotbelger@8910: } gernotbelger@9555: catch (final Exception e) { gernotbelger@8910: log.error(String.format("%s: invalid pattern name: %s", AREA_BACKGROUND_PATTERN, patternName), e); gernotbelger@8910: return null; gernotbelger@8910: } gernotbelger@8910: } teichmann@6905: } teichmann@6905: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :