view artifacts/src/main/java/org/dive4elements/river/themes/DefaultTheme.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 af13ceeba52a
children
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.themes;

import java.util.HashMap;
import java.util.Map;

import org.dive4elements.artifacts.common.utils.XMLUtils;
import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;


/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
final class DefaultTheme implements Theme {

    /** The name of the theme.*/
    private final String name;

    private String facet;

    private int index;

    /** The map storing the fields of this theme.*/
    private final Map<String, ThemeField> fields;

    /** The map storing the attributes of this theme.*/
    private final Map<String, String> attr;

    private final String description;

    /**
     * Initializes the components of this Theme.
     */
    public DefaultTheme(final String name, final String description) {
        this.name        = name;
        this.description = description;
        this.fields      = new HashMap<>();
        this.attr        = new HashMap<>();
    }

    @Override
    public String getName() {
        return this.name;
    }

    public String getDescription() {
        return this.description;
    }

    @Override
    public void setFacet(final String facet) {
        this.facet = facet;
    }

    @Override
    public void setIndex(final int index) {
        this.index = index;
    }

    public void addAttribute(final String name, final String value) {
        if (name != null && value != null) {
            this.attr.put(name, value);
        }
    }

    public void addField(final String name, final ThemeField field) {
        if (name != null && field != null) {
            this.fields.put(name, field);
        }
    }

    @Override
    public Document toXML() {
        final Document doc = XMLUtils.newDocument();

        final ElementCreator cr = new ElementCreator(doc, null, null);

        final Element theme = cr.create("theme");
        theme.setAttribute("facet", this.facet);
        theme.setAttribute("index", String.valueOf(this.index));

        appendAttributes(cr, theme);
        appendFields(cr, theme);

        doc.appendChild(theme);

        return doc;
    }


    /**
     * Appends the attributes configured for this theme.
     *
     * @param cr The ElementCreator.
     * @param theme The document root element.
     */
    protected void appendAttributes(final ElementCreator cr, final Element theme) {

        for (final Map.Entry<String, String> entry: this.attr.entrySet()) {
            final String key = entry.getKey();
            final String val = entry.getValue();

            if (key != null && val != null) {
                cr.addAttr(theme, key, val);
            }
        }
    }


    /**
     * Appends the fields configured for this theme.
     *
     * @param cr The ElementCreator.
     * @param theme The document root element.
     */
    protected void appendFields(final ElementCreator cr, final Element theme) {

        for (final ThemeField field: this.fields.values()) {
            final Document doc = field.toXML();
            final Node    root = doc.getFirstChild();

            theme.appendChild(theme.getOwnerDocument().importNode(root, true));
        }
    }
}

http://dive4elements.wald.intevation.org