001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.mappaint;
003
004import java.util.Collection;
005import java.util.HashMap;
006import java.util.Map;
007import java.util.Map.Entry;
008
009import org.openstreetmap.josm.tools.CheckParameterUtil;
010
011/**
012 * Several layers / cascades, e.g. one for the main Line and one for each overlay.
013 * The range is (0,Infinity) at first and it shrinks in the process when
014 * StyleSources apply zoom level dependent properties.
015 */
016public class MultiCascade implements StyleKeys {
017
018    private Map<String, Cascade> layers;
019    public Range range;
020
021    /**
022     * Constructs a new {@code MultiCascade}.
023     */
024    public MultiCascade() {
025        layers = new HashMap<>();
026        range = Range.ZERO_TO_INFINITY;
027    }
028
029    /**
030     * Return the cascade with the given name. If it doesn't exist, create
031     * a new layer with that name and return it. The new layer will be
032     * a clone of the "*" layer, if it exists.
033     */
034    public Cascade getOrCreateCascade(String layer) {
035        CheckParameterUtil.ensureParameterNotNull(layer);
036        Cascade c = layers.get(layer);
037        if (c == null) {
038            if (layers.containsKey("*")) {
039                c = layers.get("*").clone();
040            } else {
041                c = new Cascade();
042                // Everything that is not on the default layer is assumed to
043                // be a modifier. Can be overridden in style definition.
044                if (!"default".equals(layer) && !"*".equals(layer)) {
045                    c.put(MODIFIER, true);
046                }
047            }
048            layers.put(layer, c);
049        }
050        return c;
051    }
052
053    /**
054     * Read-only version of getOrCreateCascade. For convenience, it returns an
055     * empty cascade for non-existing layers. However this empty (read-only) cascade
056     * is not added to this MultiCascade object.
057     */
058    public Cascade getCascade(String layer) {
059        if (layer == null) {
060            layer = "default";
061        }
062        Cascade c = layers.get(layer);
063        if (c == null) {
064            c = new Cascade();
065            if (!"default".equals(layer) && !"*".equals(layer)) {
066                c.put(MODIFIER, true);
067            }
068        }
069        return c;
070    }
071
072    public Collection<Entry<String, Cascade>> getLayers() {
073        return layers.entrySet();
074    }
075
076    public boolean hasLayer(String layer) {
077        return layers.containsKey(layer);
078    }
079}