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