1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
3 |
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.Objects;
|
---|
6 | import java.util.stream.Collectors;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
9 | import org.openstreetmap.josm.gui.mappaint.StyleSource;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * A MapCSS rule.
|
---|
13 | *
|
---|
14 | * A MapCSS style is simply a list of MapCSS rules. Each rule has a selector
|
---|
15 | * and a declaration. Whenever the selector matches the primitive, the
|
---|
16 | * declaration block is executed for this primitive.
|
---|
17 | */
|
---|
18 | public class MapCSSRule implements Comparable<MapCSSRule> {
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * The selector. If it matches, this rule should be applied
|
---|
22 | */
|
---|
23 | public final Selector selector;
|
---|
24 | /**
|
---|
25 | * The instructions for this selector
|
---|
26 | */
|
---|
27 | public final Declaration declaration;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * A declaration is a set of {@link Instruction}s
|
---|
31 | */
|
---|
32 | public static class Declaration {
|
---|
33 | /**
|
---|
34 | * The instructions in this declaration
|
---|
35 | */
|
---|
36 | public final List<Instruction> instructions;
|
---|
37 | /**
|
---|
38 | * The index of this declaration
|
---|
39 | * <p>
|
---|
40 | * declarations in the StyleSource are numbered consecutively
|
---|
41 | */
|
---|
42 | public final int idx;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Create a new {@link Declaration}
|
---|
46 | * @param instructions The instructions for this dectlaration
|
---|
47 | * @param idx The index in the {@link StyleSource}
|
---|
48 | */
|
---|
49 | public Declaration(List<Instruction> instructions, int idx) {
|
---|
50 | this.instructions = instructions;
|
---|
51 | this.idx = idx;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * <p>Executes the instructions against the environment {@code env}</p>
|
---|
56 | *
|
---|
57 | * @param env the environment
|
---|
58 | */
|
---|
59 | public void execute(Environment env) {
|
---|
60 | for (Instruction i : instructions) {
|
---|
61 | i.execute(env);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public int hashCode() {
|
---|
67 | return Objects.hash(instructions, idx);
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public boolean equals(Object obj) {
|
---|
72 | if (this == obj) return true;
|
---|
73 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
74 | Declaration that = (Declaration) obj;
|
---|
75 | return idx == that.idx &&
|
---|
76 | Objects.equals(instructions, that.instructions);
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public String toString() {
|
---|
81 | return "Declaration [instructions=" + instructions + ", idx=" + idx + ']';
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Constructs a new {@code MapCSSRule}.
|
---|
87 | * @param selector The selector
|
---|
88 | * @param declaration The declaration
|
---|
89 | */
|
---|
90 | public MapCSSRule(Selector selector, Declaration declaration) {
|
---|
91 | this.selector = selector;
|
---|
92 | this.declaration = declaration;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * <p>Executes the instructions against the environment {@code env}</p>
|
---|
97 | *
|
---|
98 | * @param env the environment
|
---|
99 | */
|
---|
100 | public void execute(Environment env) {
|
---|
101 | declaration.execute(env);
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public int compareTo(MapCSSRule o) {
|
---|
106 | return declaration.idx - o.declaration.idx;
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public String toString() {
|
---|
111 | return selector + declaration.instructions.stream()
|
---|
112 | .map(String::valueOf)
|
---|
113 | .collect(Collectors.joining("\n ", " {\n ", "\n}"));
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|