source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Instruction.java@ 7382

Last change on this file since 7382 was 7382, checked in by bastiK, 10 years ago

fix [7378]

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.mapcss;
3
4import java.util.Arrays;
5
6import org.openstreetmap.josm.gui.mappaint.Cascade;
7import org.openstreetmap.josm.gui.mappaint.Environment;
8import org.openstreetmap.josm.gui.mappaint.Keyword;
9import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
10import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
11import org.openstreetmap.josm.gui.mappaint.StyleKeys;
12
13public interface Instruction extends StyleKeys {
14
15 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 implements Instruction {
31 public final String key;
32 public final Object val;
33 public final boolean isSetInstruction;
34
35 public AssignmentInstruction(String key, Object val, boolean isSetInstruction) {
36 this.key = key;
37 this.isSetInstruction = isSetInstruction;
38 if (val instanceof LiteralExpression) {
39 Object litValue = ((LiteralExpression) val).evaluate(null);
40 if (litValue instanceof Keyword) {
41 if ("none".equals(((Keyword) litValue).val)) {
42 this.val = null;
43 } else {
44 this.val = val;
45 }
46 } else if (key.equals(TEXT)) {
47 /* Special case for declaration 'text: ...'
48 *
49 * - Treat the value 'auto' as keyword.
50 * - Treat any other literal value 'litval' as as reference to tag with key 'litval'
51 *
52 * - Accept function expressions as is. This allows for
53 * tag(a_tag_name) value of a tag
54 * eval("a static text") a static text
55 * parent_tag(a_tag_name) value of a tag of a parent relation
56 */
57 if (litValue.equals(Keyword.AUTO)) {
58 this.val = Keyword.AUTO;
59 } else {
60 String s = Cascade.convertTo(litValue, String.class);
61 if (s != null) {
62 this.val = new MapPaintStyles.TagKeyReference(s);
63 } else {
64 this.val = litValue;
65 }
66 }
67 } else {
68 this.val = litValue;
69 }
70 } else {
71 this.val = val;
72 }
73 }
74
75 @Override
76 public void execute(Environment env) {
77 Object value = null;
78 if (val instanceof Expression) {
79 value = ((Expression) val).evaluate(env);
80 } else {
81 value = val;
82 }
83 if (key.equals(ICON_IMAGE) || key.equals(FILL_IMAGE) || key.equals(REPEAT_IMAGE)) {
84 if (value instanceof String) {
85 value = new IconReference((String) value, env.source);
86 }
87 }
88 env.mc.getOrCreateCascade(env.layer).putOrClear(key, value);
89 }
90
91 @Override
92 public String toString() {
93 return key + ": " + (val instanceof float[] ? Arrays.toString((float[]) val) : val instanceof String ? "String<"+val+">" : val) + ';';
94 }
95 }
96}
Note: See TracBrowser for help on using the repository browser.