| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.mappaint;
|
|---|
| 3 |
|
|---|
| 4 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 5 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 6 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
|
|---|
| 7 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.LinkSelector;
|
|---|
| 8 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
|---|
| 9 |
|
|---|
| 10 | /**
|
|---|
| 11 | * Environment is a data object to provide access to various "global" parameters.
|
|---|
| 12 | * It is used during processing of MapCSS rules and for the generation of
|
|---|
| 13 | * style elements.
|
|---|
| 14 | */
|
|---|
| 15 | public class Environment {
|
|---|
| 16 |
|
|---|
| 17 | public OsmPrimitive osm;
|
|---|
| 18 |
|
|---|
| 19 | public MultiCascade mc;
|
|---|
| 20 | public String layer;
|
|---|
| 21 | public StyleSource source;
|
|---|
| 22 | private Context context = Context.PRIMITIVE;
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * If not null, this is the matching parent object if an condition or an expression
|
|---|
| 26 | * is evaluated in a {@link LinkSelector} (within a child selector)
|
|---|
| 27 | */
|
|---|
| 28 | public OsmPrimitive parent;
|
|---|
| 29 | /**
|
|---|
| 30 | * The same for parent selector. Only one of the 2 fields (parent or child) is not null in any environment.
|
|---|
| 31 | */
|
|---|
| 32 | public OsmPrimitive child;
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * index of node in parent way or member in parent relation. Must be != null in LINK context.
|
|---|
| 36 | */
|
|---|
| 37 | public Integer index = null;
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * Creates a new uninitialized environment
|
|---|
| 41 | */
|
|---|
| 42 | public Environment() {}
|
|---|
| 43 |
|
|---|
| 44 | public Environment(OsmPrimitive osm, MultiCascade mc, String layer, StyleSource source) {
|
|---|
| 45 | this.osm = osm;
|
|---|
| 46 | this.mc = mc;
|
|---|
| 47 | this.layer = layer;
|
|---|
| 48 | this.source = source;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | /**
|
|---|
| 52 | * Creates a clone of the environment {@code other}
|
|---|
| 53 | *
|
|---|
| 54 | * @param other the other environment. Must not be null.
|
|---|
| 55 | */
|
|---|
| 56 | public Environment(Environment other) throws IllegalArgumentException{
|
|---|
| 57 | CheckParameterUtil.ensureParameterNotNull(other);
|
|---|
| 58 | this.osm = other.osm;
|
|---|
| 59 | this.mc = other.mc;
|
|---|
| 60 | this.layer = other.layer;
|
|---|
| 61 | this.parent = other.parent;
|
|---|
| 62 | this.child = other.child;
|
|---|
| 63 | this.source = other.source;
|
|---|
| 64 | this.index = other.index;
|
|---|
| 65 | this.context = other.getContext();
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | public Environment withPrimitive(OsmPrimitive osm) {
|
|---|
| 69 | Environment e = new Environment(this);
|
|---|
| 70 | e.osm = osm;
|
|---|
| 71 | return e;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | public Environment withParent(OsmPrimitive parent) {
|
|---|
| 75 | Environment e = new Environment(this);
|
|---|
| 76 | e.parent = parent;
|
|---|
| 77 | return e;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | public Environment withChild(OsmPrimitive child) {
|
|---|
| 81 | Environment e = new Environment(this);
|
|---|
| 82 | e.child = child;
|
|---|
| 83 | return e;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | public Environment withIndex(int index) {
|
|---|
| 87 | Environment e = new Environment(this);
|
|---|
| 88 | e.index = index;
|
|---|
| 89 | return e;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | public Environment withContext(Context context) {
|
|---|
| 93 | Environment e = new Environment(this);
|
|---|
| 94 | e.context = context == null ? Context.PRIMITIVE : context;
|
|---|
| 95 | return e;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | public Environment withLinkContext() {
|
|---|
| 99 | Environment e = new Environment(this);
|
|---|
| 100 | e.context = Context.LINK;
|
|---|
| 101 | return e;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | public boolean isLinkContext() {
|
|---|
| 105 | return Context.LINK.equals(context);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | public boolean hasParentRelation() {
|
|---|
| 109 | return parent instanceof Relation;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /**
|
|---|
| 113 | * Replies the current context.
|
|---|
| 114 | *
|
|---|
| 115 | * @return the current context
|
|---|
| 116 | */
|
|---|
| 117 | public Context getContext() {
|
|---|
| 118 | return context == null ? Context.PRIMITIVE : context;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | public String getRole() {
|
|---|
| 122 | if (getContext().equals(Context.PRIMITIVE))
|
|---|
| 123 | return null;
|
|---|
| 124 |
|
|---|
| 125 | if (parent instanceof Relation)
|
|---|
| 126 | return ((Relation) parent).getMember(index).getRole();
|
|---|
| 127 | if (child != null && osm instanceof Relation)
|
|---|
| 128 | return ((Relation) osm).getMember(index).getRole();
|
|---|
| 129 | return null;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | public void clearSelectorMatchingInformation() {
|
|---|
| 133 | parent = null;
|
|---|
| 134 | child = null;
|
|---|
| 135 | index = null;
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|