source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/Environment.java@ 6611

Last change on this file since 6611 was 6611, checked in by simon04, 10 years ago

see #9516 - Replace BuildingInBuilding by a corresponding MapCSS test

  • Property svn:eol-style set to native
File size: 7.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import org.openstreetmap.josm.data.osm.OsmPrimitive;
5import org.openstreetmap.josm.data.osm.Relation;
6import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
7import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.LinkSelector;
8import 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 */
15public 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 public static final String DEFAULT_LAYER = "default";
24
25 /**
26 * If not null, this is the matching parent object if a condition or an expression
27 * is evaluated in a {@link LinkSelector} (within a child selector)
28 */
29 public OsmPrimitive parent;
30
31 /**
32 * The same for parent selector. Only one of the 2 fields (parent or child) is not null in any environment.
33 */
34 public OsmPrimitive child;
35
36 /**
37 * index of node in parent way or member in parent relation. Must be != null in LINK context.
38 */
39 public Integer index = null;
40
41 /**
42 * Creates a new uninitialized environment.
43 */
44 public Environment() {}
45
46 /**
47 * Creates a new environment.
48 */
49 public Environment(OsmPrimitive osm, MultiCascade mc, String layer, StyleSource source) {
50 this.osm = osm;
51 this.mc = mc;
52 this.layer = layer;
53 this.source = source;
54 }
55
56 /**
57 * Creates a clone of the environment {@code other}.
58 *
59 * @param other the other environment. Must not be null.
60 * @throws IllegalArgumentException if {@code param} is {@code null}
61 */
62 public Environment(Environment other) throws IllegalArgumentException {
63 CheckParameterUtil.ensureParameterNotNull(other);
64 this.osm = other.osm;
65 this.mc = other.mc;
66 this.layer = other.layer;
67 this.parent = other.parent;
68 this.child = other.child;
69 this.source = other.source;
70 this.index = other.index;
71 this.context = other.getContext();
72 }
73
74 /**
75 * Creates a clone of this environment, with the specified primitive.
76 * @return A clone of this environment, with the specified primitive
77 * @see #osm
78 */
79 public Environment withPrimitive(OsmPrimitive osm) {
80 Environment e = new Environment(this);
81 e.osm = osm;
82 return e;
83 }
84
85 /**
86 * Creates a clone of this environment, with the specified parent.
87 * @param parent the matching parent object
88 * @return A clone of this environment, with the specified parent
89 * @see #parent
90 */
91 public Environment withParent(OsmPrimitive parent) {
92 Environment e = new Environment(this);
93 e.parent = parent;
94 return e;
95 }
96
97 /**
98 * Creates a clone of this environment, with the specified parent, index, and context set to {@link Context#LINK}.
99 * @param parent the matching parent object
100 * @param index index of node in parent way or member in parent relation
101 * @return A clone of this environment, with the specified parent, index, and context set to {@link Context#LINK}
102 * @since 6175
103 * @see #parent
104 * @see #index
105 */
106 public Environment withParentAndIndexAndLinkContext(OsmPrimitive parent, int index) {
107 Environment e = new Environment(this);
108 e.parent = parent;
109 e.index = index;
110 e.context = Context.LINK;
111 return e;
112 }
113
114 /**
115 * Creates a clone of this environment, with the specified child.
116 * @param child the matching child object
117 * @return A clone of this environment, with the specified child
118 * @see #child
119 */
120 public Environment withChild(OsmPrimitive child) {
121 Environment e = new Environment(this);
122 e.child = child;
123 return e;
124 }
125
126 /**
127 * Creates a clone of this environment, with the specified child, index, and context set to {@link Context#LINK}.
128 * @param child the matching child object
129 * @param index index of node in parent way or member in parent relation
130 * @return A clone of this environment, with the specified child, index, and context set to {@code Context#LINK}
131 * @since 6175
132 * @see #child
133 * @see #index
134 */
135 public Environment withChildAndIndexAndLinkContext(OsmPrimitive child, int index) {
136 Environment e = new Environment(this);
137 e.child = child;
138 e.index = index;
139 e.context = Context.LINK;
140 return e;
141 }
142
143 /**
144 * Creates a clone of this environment, with the specified index.
145 * @param index index of node in parent way or member in parent relation
146 * @return A clone of this environment, with the specified index
147 * @see #index
148 */
149 public Environment withIndex(int index) {
150 Environment e = new Environment(this);
151 e.index = index;
152 return e;
153 }
154
155 /**
156 * Creates a clone of this environment, with the specified {@link Context}.
157 * @return A clone of this environment, with the specified {@code Context}
158 */
159 public Environment withContext(Context context) {
160 Environment e = new Environment(this);
161 e.context = context == null ? Context.PRIMITIVE : context;
162 return e;
163 }
164
165 /**
166 * Creates a clone of this environment, with context set to {@link Context#LINK}.
167 * @return A clone of this environment, with context set to {@code Context#LINK}
168 */
169 public Environment withLinkContext() {
170 Environment e = new Environment(this);
171 e.context = Context.LINK;
172 return e;
173 }
174
175 /**
176 * Determines if the context of this environment is {@link Context#LINK}.
177 * @return {@code true} if the context of this environment is {@code Context#LINK}, {@code false} otherwise
178 */
179 public boolean isLinkContext() {
180 return Context.LINK.equals(context);
181 }
182
183 /**
184 * Determines if this environment has a relation as parent.
185 * @return {@code true} if this environment has a relation as parent, {@code false} otherwise
186 * @see #parent
187 */
188 public boolean hasParentRelation() {
189 return parent instanceof Relation;
190 }
191
192 /**
193 * Replies the current context.
194 *
195 * @return the current context
196 */
197 public Context getContext() {
198 return context == null ? Context.PRIMITIVE : context;
199 }
200
201 public String getRole() {
202 if (getContext().equals(Context.PRIMITIVE))
203 return null;
204
205 if (parent instanceof Relation)
206 return ((Relation) parent).getMember(index).getRole();
207 if (child != null && osm instanceof Relation)
208 return ((Relation) osm).getMember(index).getRole();
209 return null;
210 }
211
212 public void clearSelectorMatchingInformation() {
213 parent = null;
214 child = null;
215 index = null;
216 }
217
218 public Cascade getCascade(String layer) {
219 return mc == null ? null : mc.getCascade(layer == null ? this.layer : layer);
220 }
221}
Note: See TracBrowser for help on using the repository browser.