1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
3 |
|
---|
4 | import java.util.Arrays;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
7 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
8 | import org.openstreetmap.josm.gui.mappaint.Keyword;
|
---|
9 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
---|
10 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
|
---|
11 | import org.openstreetmap.josm.gui.mappaint.StyleKeys;
|
---|
12 |
|
---|
13 | abstract public class Instruction implements StyleKeys {
|
---|
14 |
|
---|
15 | public abstract void execute(Environment env);
|
---|
16 |
|
---|
17 | public static class RelativeFloat {
|
---|
18 | public final float val;
|
---|
19 |
|
---|
20 | public RelativeFloat(float val) {
|
---|
21 | this.val = val;
|
---|
22 | }
|
---|
23 |
|
---|
24 | @Override
|
---|
25 | public String toString() {
|
---|
26 | return "RelativeFloat{" + "val=" + val + '}';
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | public static class AssignmentInstruction extends Instruction {
|
---|
31 | public final String key;
|
---|
32 | public final Object val;
|
---|
33 |
|
---|
34 | public AssignmentInstruction(String key, Object val) {
|
---|
35 | this.key = key;
|
---|
36 | if (val instanceof LiteralExpression) {
|
---|
37 | Object litValue = ((LiteralExpression) val).evaluate(null);
|
---|
38 | if (key.equals(TEXT)) {
|
---|
39 | /* Special case for declaration 'text: ...'
|
---|
40 | *
|
---|
41 | * - Treat the value 'auto' as keyword.
|
---|
42 | * - Treat any other literal value 'litval' as as reference to tag with key 'litval'
|
---|
43 | *
|
---|
44 | * - Accept function expressions as is. This allows for
|
---|
45 | * tag(a_tag_name) value of a tag
|
---|
46 | * eval("a static text") a static text
|
---|
47 | * parent_tag(a_tag_name) value of a tag of a parent relation
|
---|
48 | */
|
---|
49 | if (litValue.equals(Keyword.AUTO)) {
|
---|
50 | this.val = Keyword.AUTO;
|
---|
51 | } else {
|
---|
52 | String s = Cascade.convertTo(litValue, String.class);
|
---|
53 | if (s != null) {
|
---|
54 | this.val = new MapPaintStyles.TagKeyReference(s);
|
---|
55 | } else {
|
---|
56 | this.val = litValue;
|
---|
57 | }
|
---|
58 | }
|
---|
59 | } else {
|
---|
60 | this.val = litValue;
|
---|
61 | }
|
---|
62 | } else {
|
---|
63 | this.val = val;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public void execute(Environment env) {
|
---|
69 | Object value = null;
|
---|
70 | if (val instanceof Expression) {
|
---|
71 | value = ((Expression) val).evaluate(env);
|
---|
72 | } else {
|
---|
73 | value = val;
|
---|
74 | }
|
---|
75 | if (key.equals(ICON_IMAGE) || key.equals(FILL_IMAGE) || key.equals("pattern-image") || key.equals(REPEAT_IMAGE)) {
|
---|
76 | if (value instanceof String) {
|
---|
77 | value = new IconReference((String) value, env.source);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | env.mc.getOrCreateCascade(env.layer).putOrClear(key, value);
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public String toString() {
|
---|
85 | return key + ':' + (val instanceof float[] ? Arrays.toString((float[]) val) : val) + ';';
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|