1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.Map;
|
---|
7 | import java.util.Map.Entry;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Several layers / cascades, e.g. one for the main Line and one for each overlay.
|
---|
13 | * The range is (0,Infinity) at first and it shrinks in the process when
|
---|
14 | * StyleSources apply zoom level dependent properties.
|
---|
15 | */
|
---|
16 | public class MultiCascade implements StyleKeys {
|
---|
17 |
|
---|
18 | private final Map<String, Cascade> layers;
|
---|
19 | /**
|
---|
20 | * The scale range this cascade is valid for
|
---|
21 | */
|
---|
22 | public Range range;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Constructs a new {@code MultiCascade}.
|
---|
26 | */
|
---|
27 | public MultiCascade() {
|
---|
28 | layers = new HashMap<>();
|
---|
29 | range = Range.ZERO_TO_INFINITY;
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Return the cascade with the given name. If it doesn't exist, create
|
---|
34 | * a new layer with that name and return it. The new layer will be
|
---|
35 | * a clone of the "*" layer, if it exists.
|
---|
36 | * @param layer layer
|
---|
37 | * @return cascade
|
---|
38 | */
|
---|
39 | public Cascade getOrCreateCascade(String layer) {
|
---|
40 | CheckParameterUtil.ensureParameterNotNull(layer);
|
---|
41 | Cascade c = layers.get(layer);
|
---|
42 | if (c == null) {
|
---|
43 | if (layers.containsKey("*")) {
|
---|
44 | c = new Cascade(layers.get("*"));
|
---|
45 | } else {
|
---|
46 | c = new Cascade();
|
---|
47 | // Everything that is not on the default layer is assumed to
|
---|
48 | // be a modifier. Can be overridden in style definition.
|
---|
49 | if (!"default".equals(layer) && !"*".equals(layer)) {
|
---|
50 | c.put(MODIFIER, Boolean.TRUE);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | layers.put(layer, c);
|
---|
54 | }
|
---|
55 | return c;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Read-only version of {@link #getOrCreateCascade}. For convenience, it returns an
|
---|
60 | * empty cascade for non-existing layers. However this empty (read-only) cascade
|
---|
61 | * is not added to this MultiCascade object.
|
---|
62 | * @param layer layer
|
---|
63 | * @return cascade
|
---|
64 | */
|
---|
65 | public Cascade getCascade(String layer) {
|
---|
66 | if (layer == null) {
|
---|
67 | layer = "default";
|
---|
68 | }
|
---|
69 | Cascade c = layers.get(layer);
|
---|
70 | if (c == null) {
|
---|
71 | c = new Cascade();
|
---|
72 | if (!"default".equals(layer) && !"*".equals(layer)) {
|
---|
73 | c.put(MODIFIER, Boolean.TRUE);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | return c;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Gets all cascades for the known layers
|
---|
81 | * @return The cascades for the layers
|
---|
82 | */
|
---|
83 | public Collection<Entry<String, Cascade>> getLayers() {
|
---|
84 | return layers.entrySet();
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Check whether this cascade has a given layer
|
---|
89 | * @param layer The layer to check for
|
---|
90 | * @return <code>true</code> if it has that layer
|
---|
91 | */
|
---|
92 | public boolean hasLayer(String layer) {
|
---|
93 | return layers.containsKey(layer);
|
---|
94 | }
|
---|
95 | }
|
---|