Mercurial > dive4elements > river
diff artifacts/src/main/java/org/dive4elements/river/themes/ThemeDocument.java @ 9555:ef5754ba5573
Implemented legend aggregation based on type of themes.
Added theme-editor style configuration for aggregated legend entries.
Only configured themes get aggregated.
author | gernotbelger |
---|---|
date | Tue, 23 Oct 2018 16:26:48 +0200 |
parents | 094ed9d1f2ad |
children | 6b2496d71936 |
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/themes/ThemeDocument.java Mon Oct 22 18:26:05 2018 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/themes/ThemeDocument.java Tue Oct 23 16:26:48 2018 +0200 @@ -34,7 +34,7 @@ private static final String MSG_ISOBATH_CLASS = "floodmap.isobath.class"; private static final String MSG_ISOBATH_LASTCLASS = - "floodmap.isobath.lastclass"; + "floodmap.isobath.lastclass"; public final static String FILL_COLOR = "fillcolor"; @@ -132,35 +132,34 @@ public ThemeDocument() { } - public ThemeDocument(Document document) { - values = extractValues(document); - } - - public ThemeDocument(ThemeDocument other) { - values = new HashMap<String, String>(other.values); + public ThemeDocument(final Document document) { + this.values = extractValues(document); } - - public String getValue(String key) { - return values.get(key); + public ThemeDocument(final ThemeDocument other) { + this.values = new HashMap<>(other.values); } - public void setValue(String key, String value) { - values.put(key, value); + public String getValue(final String key) { + return this.values.get(key); } - private static Map<String, String> extractValues(Document document) { - Map<String, String> values = new HashMap<String, String>(); + public void setValue(final String key, final String value) { + this.values.put(key, value); + } + + private static Map<String, String> extractValues(final Document document) { + final Map<String, String> values = new HashMap<>(); if (document == null) { log.error("Invalid null document given."); return values; } - NodeList fields = document.getElementsByTagName("field"); + final NodeList fields = document.getElementsByTagName("field"); for (int i = 0, N = fields.getLength(); i < N; ++i) { - Element field = (Element)fields.item(i); - String name = field.getAttribute("name"); - String value = field.getAttribute("default"); + final Element field = (Element)fields.item(i); + final String name = field.getAttribute("name"); + final String value = field.getAttribute("default"); if (!name.isEmpty() && !value.isEmpty()) { values.put(name, value); } @@ -172,7 +171,7 @@ } /** Parse string to be boolean with default if empty or unrecognized. */ - private static boolean parseBoolean(String value, boolean defaultsTo) { + private static boolean parseBoolean(final String value, final boolean defaultsTo) { if (value == null) { return defaultsTo; } @@ -193,7 +192,7 @@ * @param defaultsTo Default to return if conversion failed. * @return \param value as integer or defaultsto if conversion failed. */ - private static int parseInteger(String value, int defaultsTo) { + private static int parseInteger(final String value, final int defaultsTo) { if (value == null) { return defaultsTo; } @@ -201,7 +200,7 @@ try { return Integer.parseInt(value); } - catch (NumberFormatException nfe) { + catch (final NumberFormatException nfe) { // do nothing } @@ -216,7 +215,7 @@ * @param defaultsTo Default to return if conversion failed. * @return \param value as integer or defaultsto if conversion failed. */ - private static double parseDouble(String value, double defaultsTo) { + private static double parseDouble(final String value, final double defaultsTo) { if (value == null) { return defaultsTo; } @@ -224,7 +223,7 @@ try { return Double.parseDouble(value); } - catch (NumberFormatException nfe) { + catch (final NumberFormatException nfe) { // do nothing } @@ -232,60 +231,60 @@ } public boolean parseShowLineLabel() { - String show = getValue(SHOW_LINE_LABEL); + final String show = getValue(SHOW_LINE_LABEL); return parseBoolean(show, false); } public boolean parseShowWidth() { - String show = getValue(SHOW_WIDTH); + final String show = getValue(SHOW_WIDTH); return parseBoolean(show, false); } public boolean parseShowLevel() { - String show = getValue(SHOW_LEVEL); + final String show = getValue(SHOW_LEVEL); return parseBoolean(show, false); } public String parseTextOrientation() { - String o = getValue(TEXT_ORIENTATION); + final String o = getValue(TEXT_ORIENTATION); return o != null && "true".equals(o) - ? "horizontal" - : "vertical"; + ? "horizontal" + : "vertical"; } public boolean parseShowMiddleHeight() { - String show = getValue(SHOW_MIDDLE_HEIGHT); + final String show = getValue(SHOW_MIDDLE_HEIGHT); return parseBoolean(show, false); } public boolean parseLabelShowBackground() { - String show = getValue(LABEL_SHOW_BACKGROUND); + final String show = getValue(LABEL_SHOW_BACKGROUND); return parseBoolean(show, false); } public Font parseFont() { - String font = getValue(FONT); + final String font = getValue(FONT); log.debug(" font is " + font); if (font == null) { return null; } - int size = parseFontSize(); - int style = parseFontStyle(); - Font f = new Font(font, style, size); + final int size = parseFontSize(); + final int style = parseFontStyle(); + final Font f = new Font(font, style, size); return f; } public Font parseTextFont() { - String font = getValue(LABEL_FONT_FACE); + final String font = getValue(LABEL_FONT_FACE); if (font == null) { return null; } - int size = parseTextSize(); - int style = parseTextStyle(); - Font f = new Font(font, style, size); + final int size = parseTextSize(); + final int style = parseTextStyle(); + final Font f = new Font(font, style, size); return f; } @@ -298,10 +297,10 @@ } public Color parseTextBackground() { - String color = getLabelBackgroundColorString(); + final String color = getLabelBackgroundColorString(); return color != null - ? parseRGB(color) - : Color.WHITE; + ? parseRGB(color) + : Color.WHITE; } private String getLabelBackgroundColorString() { @@ -309,7 +308,7 @@ } public int parseLineWidth() { - String size = getValue(LINE_SIZE); + final String size = getValue(LINE_SIZE); if (size == null) { return 0; } @@ -317,65 +316,65 @@ try { return Integer.parseInt(size); } - catch (NumberFormatException nfe) { + catch (final NumberFormatException nfe) { log.warn("Unable to set line size from string: '" + size + "'"); } return 0; } public float [] parseLineStyle() { - String dash = getValue(LINE_STYLE); + final String dash = getValue(LINE_STYLE); - float[] def = {10}; + final float[] def = {10}; if (dash == null) { return def; } - String[] pattern = dash.split(","); + final String[] pattern = dash.split(","); if(pattern.length == 1) { return def; } try { - float[] dashes = new float[pattern.length]; + final float[] dashes = new float[pattern.length]; for (int i = 0; i < pattern.length; i++) { dashes[i] = Float.parseFloat(pattern[i]); } return dashes; } - catch (NumberFormatException nfe) { + catch (final NumberFormatException nfe) { log.warn("Unable to set dash from string: '" + dash + "'"); return def; } } public int parsePointWidth() { - String width = getValue(POINT_SIZE); + final String width = getValue(POINT_SIZE); return parseInteger(width, 3); } public Color parsePointColor() { - String color = getValue(POINT_COLOR); + final String color = getValue(POINT_COLOR); return parseColor(color); } public boolean parseShowPoints() { - String show = getValue(SHOW_POINTS); + final String show = getValue(SHOW_POINTS); return parseBoolean(show, false); } public boolean parseShowPointsOutline() { - String show = getValue(SHOW_POINTS_OUTLINE); + final String show = getValue(SHOW_POINTS_OUTLINE); return parseBoolean(show, false); } public boolean parseShowLine() { - String show = getValue(SHOW_LINE); + final String show = getValue(SHOW_LINE); return parseBoolean(show, false); } public int parseFontStyle() { - String style = getValue(TEXT_STYLE); + final String style = getValue(TEXT_STYLE); if (style == null) { return Font.PLAIN; } @@ -390,7 +389,7 @@ } public int parseTextStyle() { - String style = getValue(LABEL_FONT_STYLE); + final String style = getValue(LABEL_FONT_STYLE); if (style == null) { return Font.PLAIN; } @@ -412,35 +411,35 @@ font = parseFont(); } return new TextStyle( - parseTextColor(), - font, - parseTextBackground(), - parseLabelShowBackground(), - !parseTextOrientation().equals("horizontal")); + parseTextColor(), + font, + parseTextBackground(), + parseLabelShowBackground(), + !parseTextOrientation().equals("horizontal")); } public LineStyle parseComplexLineStyle() { return new LineStyle( - parseLineColorField(), - Integer.valueOf(parseLineWidth())); + parseLineColorField(), + Integer.valueOf(parseLineWidth())); } public boolean parseShowVerticalLine() { - String show = getValue(SHOW_VERTICAL_LINE); + final String show = getValue(SHOW_VERTICAL_LINE); return parseBoolean(show, true); } public boolean parseShowHorizontalLine() { - String show = getValue(SHOW_HORIZONTAL_LINE); + final String show = getValue(SHOW_HORIZONTAL_LINE); return parseBoolean(show, true); } public double parseBandWidth() { - String bandWidth = getValue(BANDWIDTH); + final String bandWidth = getValue(BANDWIDTH); return parseDouble(bandWidth, 0); } - private static Color parseColor(String colorString) { + private static Color parseColor(final String colorString) { if (colorString == null) { return null; } @@ -462,35 +461,35 @@ * * @return a Color or null, if <i>hex</i> is empty. */ - private static Color parseHexColor(String hex) { + private static Color parseHexColor(final String hex) { return hex != null - ? Color.decode(hex) - : null; + ? Color.decode(hex) + : null; } public boolean parseShowArea() { - String show = getValue(SHOW_AREA); + final String show = getValue(SHOW_AREA); return parseBoolean(show, false); } public boolean parseShowAreaLabel() { - String show = getValue(SHOW_AREA_LABEL); + final String show = getValue(SHOW_AREA_LABEL); return parseBoolean(show, false); } public boolean parseShowPointLabel() { - String show = getValue(SHOW_POINT_LABEL); + final String show = getValue(SHOW_POINT_LABEL); return parseBoolean(show, false); } public boolean parseShowExtraMark() { - String show = getValue(SHOWEXTRAMARK); + final String show = getValue(SHOWEXTRAMARK); return parseBoolean(show, false); } public int parseFontSize() { - String size = getValue(TEXT_SIZE); + final String size = getValue(TEXT_SIZE); if (size == null) { return 10; } @@ -498,14 +497,14 @@ try { return Integer.parseInt(size); } - catch (NumberFormatException nfe) { + catch (final NumberFormatException nfe) { // Do nothing } return 10; } public int parseTextSize() { - String size = getValue(LABEL_FONT_SIZE); + final String size = getValue(LABEL_FONT_SIZE); if (size == null) { return 10; } @@ -513,7 +512,7 @@ try { return Integer.parseInt(size); } - catch (NumberFormatException nfe) { + catch (final NumberFormatException nfe) { // Do nothing } return 10; @@ -524,18 +523,18 @@ * @param rgbtext Color as string representation, e.g. "255,0,20". * @return Color, null in case of issues. */ - public static Color parseRGB(String rgbtext) { + public static Color parseRGB(final String rgbtext) { if (rgbtext == null) { return null; } - String rgb[] = rgbtext.split(","); + final String rgb[] = rgbtext.split(","); try { return new Color( - Integer.parseInt(rgb[0].trim()), - Integer.parseInt(rgb[1].trim()), - Integer.parseInt(rgb[2].trim())); + Integer.parseInt(rgb[0].trim()), + Integer.parseInt(rgb[1].trim()), + Integer.parseInt(rgb[2].trim())); } - catch (NumberFormatException nfe) { + catch (final NumberFormatException nfe) { // Do nothing } return null; @@ -603,24 +602,24 @@ * @return color. */ public Color parseLineColorField() { - String lineColorStr = getLineColorString(); + final String lineColorStr = getLineColorString(); if (log.isDebugEnabled()) { log.debug("parseLineColorField: lineColorStr = " + - (lineColorStr == null - ? "null" - : lineColorStr)); + (lineColorStr == null + ? "null" + : lineColorStr)); } return parseColor(lineColorStr); } // FIXME: check, this is defined in default.xml, but never used. Instead the StyledAreaSeriesCollection used lineColor etc public Color parseAreaLineColorField() { - String lineColorStr = getAreaLineColorString(); + final String lineColorStr = getAreaLineColorString(); if (log.isDebugEnabled()) { log.debug("parseLineColorField: lineColorStr = " + - (lineColorStr == null + (lineColorStr == null ? "null" - : lineColorStr)); + : lineColorStr)); } return parseColor(lineColorStr); } @@ -649,68 +648,68 @@ * @return String representation of the MapserverStyle */ public String createDynamicMapserverStyle( - float from, - float to, - float step, - CallMeta meta - ) { - MapserverStyle ms = new MapserverStyle(); - - String strStartColor = getValue(WSPLGEN_STARTCOLOR); - Color startColor = strStartColor != null - ? parseColor(strStartColor) - : new Color(178, 201, 215); - String strEndColor = getValue(WSPLGEN_ENDCOLOR); - Color endColor = strEndColor != null - ? parseColor(strEndColor) - : new Color(2, 27, 42); - - to = to >= 0 ? to : 9999; - step = to != from ? step : 1; - - int numClasses = (int)((to - from) / step + 1); + final float from, + float to, + float step, + final CallMeta meta + ) { + final MapserverStyle ms = new MapserverStyle(); - float rd = (endColor.getRed() - startColor.getRed()) - / (float)numClasses; - float gd = (endColor.getGreen() - startColor.getGreen()) - / (float)numClasses; - float bd = (endColor.getBlue() - startColor.getBlue()) - / (float)numClasses; - - for (int n = 0; n < numClasses; n++) { - StringBuilder newColor = new StringBuilder(); - newColor.append(startColor.getRed() + Math.round(n * rd)); - newColor.append(' '); - newColor.append(startColor.getGreen() + Math.round(n * gd)); - newColor.append(' '); - newColor.append(startColor.getBlue() + Math.round(n * bd)); + final String strStartColor = getValue(WSPLGEN_STARTCOLOR); + final Color startColor = strStartColor != null + ? parseColor(strStartColor) + : new Color(178, 201, 215); + final String strEndColor = getValue(WSPLGEN_ENDCOLOR); + final Color endColor = strEndColor != null + ? parseColor(strEndColor) + : new Color(2, 27, 42); - String expr = createWSPLGENClassExpression( - from + n * step, step, n + 1, numClasses); - String name = createWSPLGENClassName( - from + n * step, step, n + 1, numClasses, meta); + to = to >= 0 ? to : 9999; + step = to != from ? step : 1; - Clazz c = new Clazz(name); - Style s = new Style(); - s.setColor(newColor.toString()); - s.setSize(5); + final int numClasses = (int)((to - from) / step + 1); - c.addItem(new Expression("(" + expr + ")")); - c.addItem(s); + final float rd = (endColor.getRed() - startColor.getRed()) + / (float)numClasses; + final float gd = (endColor.getGreen() - startColor.getGreen()) + / (float)numClasses; + final float bd = (endColor.getBlue() - startColor.getBlue()) + / (float)numClasses; - ms.addClazz(c); - } + for (int n = 0; n < numClasses; n++) { + final StringBuilder newColor = new StringBuilder(); + newColor.append(startColor.getRed() + Math.round(n * rd)); + newColor.append(' '); + newColor.append(startColor.getGreen() + Math.round(n * gd)); + newColor.append(' '); + newColor.append(startColor.getBlue() + Math.round(n * bd)); - return ms.toString(); + final String expr = createWSPLGENClassExpression( + from + n * step, step, n + 1, numClasses); + final String name = createWSPLGENClassName( + from + n * step, step, n + 1, numClasses, meta); + + final Clazz c = new Clazz(name); + final Style s = new Style(); + s.setColor(newColor.toString()); + s.setSize(5); + + c.addItem(new Expression("(" + expr + ")")); + c.addItem(s); + + ms.addClazz(c); + } + + return ms.toString(); } protected static String createWSPLGENClassExpression( - float val, - float step, - int idx, - int maxIdx - ) { + final float val, + final float step, + final int idx, + final int maxIdx + ) { if (idx < maxIdx) { return "[DIFF] >= " + val + " AND [DIFF] < " + (val + step); } @@ -731,12 +730,12 @@ * @return */ protected static String createWSPLGENClassName( - float val, - float step, - int idx, - int maxIdx, - CallMeta meta - ) { + final float val, + final float step, + final int idx, + final int maxIdx, + final CallMeta meta + ) { assert meta != null : "CallMeta instance is null"; if (idx < maxIdx) { @@ -749,21 +748,21 @@ public String createMapserverStyle() { - String symbol = getSymbol(); - String backcolor = getLabelBackgroundColorString(); + final String symbol = getSymbol(); + final String backcolor = getLabelBackgroundColorString(); String linecolor = getLineColorString(); if (linecolor == null) { log.warn("createMapserverStyle: linecolor String is empty"); linecolor = "0,128,255"; } - int linewidth = parseLineWidth(); + final int linewidth = parseLineWidth(); - MapserverStyle ms = new MapserverStyle(); + final MapserverStyle ms = new MapserverStyle(); - Clazz c = new Clazz(" "); + final Clazz c = new Clazz(" "); - Style s = new Style(); + final Style s = new Style(); s.setOutlineColor(linecolor.replace(",", " ")); if (backcolor != null) { @@ -774,11 +773,11 @@ s.setSymbol(symbol); c.addItem(s); - String textcolor = getTextColorString(); - int textsize = parseTextSize(); + final String textcolor = getTextColorString(); + final int textsize = parseTextSize(); if (textcolor != null && textsize > 0) { - Label l = new Label(); + final Label l = new Label(); l.setColor(textcolor.replace(",", " ")); l.setSize(textsize); c.addItem(l); @@ -804,7 +803,7 @@ return parseAreaTransparency(50); } - public int parseAreaTransparency(int alpha) { + public int parseAreaTransparency(final int alpha) { return parseInteger(getAreaTransparencyString(), alpha); } @@ -817,13 +816,13 @@ private String getAreaShowBorderString() { return getValue(AREA_SHOW_BORDER); } - - + + public boolean parseCalculateRange() { return parseBoolean(getCalculateRangeString(), false); } - + private String getCalculateRangeString() { return getValue(CALCULATE_RANGE); } @@ -832,11 +831,11 @@ final String patternName = getValue(AREA_BACKGROUND_PATTERN); if( StringUtils.isBlank(patternName) ) return null; - + try { return AreaFillPattern.valueOf(patternName); } - catch (Exception e) { + catch (final Exception e) { log.error(String.format("%s: invalid pattern name: %s", AREA_BACKGROUND_PATTERN, patternName), e); return null; }