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

Last change on this file since 10600 was 10600, checked in by Don-vip, 8 years ago

see #11390 - sonar - squid:S1609 - Java 8: @FunctionalInterface annotation should be used to flag Single Abstract Method interfaces

  • Property svn:eol-style set to native
File size: 3.5 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
13@FunctionalInterface
14public interface Instruction extends StyleKeys {
15
16 void execute(Environment env);
17
18 class RelativeFloat {
19 public final float val;
20
21 public RelativeFloat(float val) {
22 this.val = val;
23 }
24
25 @Override
26 public String toString() {
27 return "RelativeFloat{" + "val=" + val + '}';
28 }
29 }
30
31 class AssignmentInstruction implements Instruction {
32 public final String key;
33 public final Object val;
34 public final boolean isSetInstruction;
35
36 public AssignmentInstruction(String key, Object val, boolean isSetInstruction) {
37 this.key = key;
38 this.isSetInstruction = isSetInstruction;
39 if (val instanceof LiteralExpression) {
40 Object litValue = ((LiteralExpression) val).evaluate(null);
41 if (litValue instanceof Keyword && "none".equals(((Keyword) litValue).val)) {
42 this.val = null;
43 } else if (TEXT.equals(key)) {
44 /* Special case for declaration 'text: ...'
45 *
46 * - Treat the value 'auto' as keyword.
47 * - Treat any other literal value 'litval' as as reference to tag with key 'litval'
48 *
49 * - Accept function expressions as is. This allows for
50 * tag(a_tag_name) value of a tag
51 * eval("a static text") a static text
52 * parent_tag(a_tag_name) value of a tag of a parent relation
53 */
54 if (litValue.equals(Keyword.AUTO)) {
55 this.val = Keyword.AUTO;
56 } else {
57 String s = Cascade.convertTo(litValue, String.class);
58 if (s != null) {
59 this.val = new MapPaintStyles.TagKeyReference(s);
60 } else {
61 this.val = litValue;
62 }
63 }
64 } else {
65 this.val = litValue;
66 }
67 } else {
68 this.val = val;
69 }
70 }
71
72 @Override
73 public void execute(Environment env) {
74 Object value;
75 if (val instanceof Expression) {
76 value = ((Expression) val).evaluate(env);
77 } else {
78 value = val;
79 }
80 if (ICON_IMAGE.equals(key) || FILL_IMAGE.equals(key) || REPEAT_IMAGE.equals(key)) {
81 if (value instanceof String) {
82 value = new IconReference((String) value, env.source);
83 }
84 }
85 env.mc.getOrCreateCascade(env.layer).putOrClear(key, value);
86 }
87
88 @Override
89 public String toString() {
90 return key + ": " + (val instanceof float[] ? Arrays.toString((float[]) val) :
91 (val instanceof String ? ("String<"+val+'>') : val)) + ';';
92 }
93 }
94}
Note: See TracBrowser for help on using the repository browser.