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 |
|
---|
7 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
8 | import org.openstreetmap.josm.tools.Utils;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * A MapCSS rule.
|
---|
12 | *
|
---|
13 | * A MapCSS style is simply a list of MapCSS rules. Each rule has a selector
|
---|
14 | * and a declaration. Whenever the selector matches the primitive, the
|
---|
15 | * declaration block is executed for this primitive.
|
---|
16 | */
|
---|
17 | public class MapCSSRule implements Comparable<MapCSSRule> {
|
---|
18 |
|
---|
19 | public final Selector selector;
|
---|
20 | public final Declaration declaration;
|
---|
21 |
|
---|
22 | public static class Declaration {
|
---|
23 | public final List<Instruction> instructions;
|
---|
24 | // declarations in the StyleSource are numbered consecutively
|
---|
25 | public final int idx;
|
---|
26 |
|
---|
27 | public Declaration(List<Instruction> instructions, int idx) {
|
---|
28 | this.instructions = instructions;
|
---|
29 | this.idx = idx;
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * <p>Executes the instructions against the environment {@code env}</p>
|
---|
34 | *
|
---|
35 | * @param env the environment
|
---|
36 | */
|
---|
37 | public void execute(Environment env) {
|
---|
38 | for (Instruction i : instructions) {
|
---|
39 | i.execute(env);
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public int hashCode() {
|
---|
45 | return Objects.hash(instructions, idx);
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public boolean equals(Object obj) {
|
---|
50 | if (this == obj) return true;
|
---|
51 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
52 | Declaration that = (Declaration) obj;
|
---|
53 | return idx == that.idx &&
|
---|
54 | Objects.equals(instructions, that.instructions);
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public String toString() {
|
---|
59 | return "Declaration [instructions=" + instructions + ", idx=" + idx + ']';
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Constructs a new {@code MapCSSRule}.
|
---|
65 | * @param selector The selector
|
---|
66 | * @param declaration The declaration
|
---|
67 | */
|
---|
68 | public MapCSSRule(Selector selector, Declaration declaration) {
|
---|
69 | this.selector = selector;
|
---|
70 | this.declaration = declaration;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * <p>Executes the instructions against the environment {@code env}</p>
|
---|
75 | *
|
---|
76 | * @param env the environment
|
---|
77 | */
|
---|
78 | public void execute(Environment env) {
|
---|
79 | declaration.execute(env);
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public int compareTo(MapCSSRule o) {
|
---|
84 | return declaration.idx - o.declaration.idx;
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public String toString() {
|
---|
89 | return selector + " {\n " + Utils.join("\n ", declaration.instructions) + "\n}";
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|