1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
3 |
|
---|
4 | import java.util.stream.Collectors;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * A MapCSS rule.
|
---|
10 | *
|
---|
11 | * A MapCSS style is simply a list of MapCSS rules. Each rule has a selector
|
---|
12 | * and a declaration. Whenever the selector matches the primitive, the
|
---|
13 | * declaration block is executed for this primitive.
|
---|
14 | */
|
---|
15 | public class MapCSSRule implements Comparable<MapCSSRule> {
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * The selector. If it matches, this rule should be applied
|
---|
19 | */
|
---|
20 | public final Selector selector;
|
---|
21 | /**
|
---|
22 | * The instructions for this selector
|
---|
23 | */
|
---|
24 | public final Declaration declaration;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Constructs a new {@code MapCSSRule}.
|
---|
28 | * @param selector The selector
|
---|
29 | * @param declaration The declaration
|
---|
30 | */
|
---|
31 | public MapCSSRule(Selector selector, Declaration declaration) {
|
---|
32 | this.selector = selector;
|
---|
33 | this.declaration = declaration;
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Test whether the selector of this rule applies to the primitive.
|
---|
38 | *
|
---|
39 | * @param env the Environment. env.mc and env.layer are read-only when matching a selector.
|
---|
40 | * env.source is not needed. This method will set the matchingReferrers field of env as
|
---|
41 | * a side effect! Make sure to clear it before invoking this method.
|
---|
42 | * @return true, if the selector applies
|
---|
43 | * @see Selector#matches
|
---|
44 | */
|
---|
45 | public boolean matches(Environment env) {
|
---|
46 | return selector.matches(env);
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * <p>Executes the instructions against the environment {@code env}</p>
|
---|
51 | *
|
---|
52 | * @param env the environment
|
---|
53 | * @see Declaration#execute
|
---|
54 | */
|
---|
55 | public void execute(Environment env) {
|
---|
56 | declaration.execute(env);
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public int compareTo(MapCSSRule o) {
|
---|
61 | return declaration.idx - o.declaration.idx;
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public String toString() {
|
---|
66 | return selector + declaration.instructions.stream()
|
---|
67 | .map(String::valueOf)
|
---|
68 | .collect(Collectors.joining("\n ", " {\n ", "\n}"));
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|