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

Last change on this file since 5801 was 5801, checked in by bastiK, 11 years ago

mapcss: new map element 'repeat-image' similar to 'pattern-image', but more flexible

  • Property svn:eol-style set to native
File size: 3.2 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
13abstract 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}
Note: See TracBrowser for help on using the repository browser.