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

Last change on this file since 15938 was 15938, checked in by GerdP, 5 years ago

see #16707: improve highlighting of overlapping areas and "zoom to error"

  • store the overlapping area with the TestError
  • return also the intersection area in Geometry.polygonIntersectionResult()
  • Property svn:eol-style set to native
File size: 9.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import java.awt.geom.Area;
5import java.util.HashMap;
6import java.util.LinkedHashSet;
7import java.util.Map;
8import java.util.Set;
9
10import org.openstreetmap.josm.data.osm.IPrimitive;
11import org.openstreetmap.josm.data.osm.Relation;
12import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
13import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.LinkSelector;
14import org.openstreetmap.josm.tools.CheckParameterUtil;
15
16/**
17 * Environment is a data object to provide access to various "global" parameters.
18 * It is used during processing of MapCSS rules and for the generation of
19 * style elements.
20 */
21public class Environment {
22
23 /**
24 * The primitive that is currently evaluated
25 */
26 public IPrimitive osm;
27
28 /**
29 * The cascades that are currently evaluated
30 */
31 public MultiCascade mc;
32 /**
33 * The current MapCSS layer
34 */
35 public String layer;
36 /**
37 * The style source that is evaluated
38 */
39 public StyleSource source;
40 private Context context = Context.PRIMITIVE;
41
42 /**
43 * The name of the default layer. It is used if no layer is specified in the MapCSS rule
44 */
45 public static final String DEFAULT_LAYER = "default";
46
47 /**
48 * If not null, this is the matching parent object if a condition or an expression
49 * is evaluated in a {@link LinkSelector} (within a child selector)
50 */
51 public IPrimitive parent;
52
53 /**
54 * The same for parent selector. Only one of the 2 fields (parent or child) is not null in any environment.
55 */
56 public IPrimitive child;
57
58 /**
59 * index of node in parent way or member in parent relation. Must be != null in LINK context.
60 */
61 public Integer index;
62
63 /**
64 * count of nodes in parent way or members in parent relation. Must be != null in LINK context.
65 */
66 public Integer count;
67
68 /**
69 * Set of matched children filled by ContainsFinder and CrossingFinder, null if nothing matched
70 */
71 public Set<IPrimitive> children;
72
73 /**
74 * Intersection areas (only filled with CrossingFinder if children is not null)
75 */
76 public Map<IPrimitive, Area> intersections;
77
78 /**
79 * Creates a new uninitialized environment.
80 */
81 public Environment() {
82 // environment can be initialized later through with* methods
83 }
84
85 /**
86 * Creates a new environment.
87 * @param osm OSM primitive
88 * @since 8415
89 * @since 13810 (signature)
90 */
91 public Environment(IPrimitive osm) {
92 this.osm = osm;
93 }
94
95 /**
96 * Creates a new environment.
97 * @param osm OSM primitive
98 * @param mc multi cascade
99 * @param layer layer
100 * @param source style source
101 * @since 13810 (signature)
102 */
103 public Environment(IPrimitive osm, MultiCascade mc, String layer, StyleSource source) {
104 this.osm = osm;
105 this.mc = mc;
106 this.layer = layer;
107 this.source = source;
108 }
109
110 /**
111 * Creates a clone of the environment {@code other}.
112 *
113 * @param other the other environment. Must not be null.
114 * @throws IllegalArgumentException if {@code param} is {@code null}
115 */
116 public Environment(Environment other) {
117 CheckParameterUtil.ensureParameterNotNull(other);
118 this.osm = other.osm;
119 this.mc = other.mc;
120 this.layer = other.layer;
121 this.parent = other.parent;
122 this.child = other.child;
123 this.source = other.source;
124 this.index = other.index;
125 this.count = other.count;
126 this.context = other.getContext();
127 this.children = other.children == null ? null : new LinkedHashSet<>(other.children);
128 this.intersections = other.intersections == null ? null : new HashMap<>(other.intersections);
129 }
130
131 /**
132 * Creates a clone of this environment, with the specified primitive.
133 * @param osm OSM primitive
134 * @return A clone of this environment, with the specified primitive
135 * @see #osm
136 * @since 13810 (signature)
137 */
138 public Environment withPrimitive(IPrimitive osm) {
139 Environment e = new Environment(this);
140 e.osm = osm;
141 return e;
142 }
143
144 /**
145 * Creates a clone of this environment, with the specified parent.
146 * @param parent the matching parent object
147 * @return A clone of this environment, with the specified parent
148 * @see #parent
149 * @since 13810 (signature)
150 */
151 public Environment withParent(IPrimitive parent) {
152 Environment e = new Environment(this);
153 e.parent = parent;
154 return e;
155 }
156
157 /**
158 * Creates a clone of this environment, with the specified parent, index, and context set to {@link Context#LINK}.
159 * @param parent the matching parent object
160 * @param index index of node in parent way or member in parent relation
161 * @param count count of nodes in parent way or members in parent relation
162 * @return A clone of this environment, with the specified parent, index, and context set to {@link Context#LINK}
163 * @see #parent
164 * @see #index
165 * @since 6175
166 * @since 13810 (signature)
167 */
168 public Environment withParentAndIndexAndLinkContext(IPrimitive parent, int index, int count) {
169 Environment e = new Environment(this);
170 e.parent = parent;
171 e.index = index;
172 e.count = count;
173 e.context = Context.LINK;
174 return e;
175 }
176
177 /**
178 * Creates a clone of this environment, with the specified child.
179 * @param child the matching child object
180 * @return A clone of this environment, with the specified child
181 * @see #child
182 * @since 13810 (signature)
183 */
184 public Environment withChild(IPrimitive child) {
185 Environment e = new Environment(this);
186 e.child = child;
187 return e;
188 }
189
190 /**
191 * Creates a clone of this environment, with the specified child, index, and context set to {@link Context#LINK}.
192 * @param child the matching child object
193 * @param index index of node in parent way or member in parent relation
194 * @param count count of nodes in parent way or members in parent relation
195 * @return A clone of this environment, with the specified child, index, and context set to {@code Context#LINK}
196 * @see #child
197 * @see #index
198 * @since 6175
199 * @since 13810 (signature)
200 */
201 public Environment withChildAndIndexAndLinkContext(IPrimitive child, int index, int count) {
202 Environment e = new Environment(this);
203 e.child = child;
204 e.index = index;
205 e.count = count;
206 e.context = Context.LINK;
207 return e;
208 }
209
210 /**
211 * Creates a clone of this environment, with the specified index.
212 * @param index index of node in parent way or member in parent relation
213 * @param count count of nodes in parent way or members in parent relation
214 * @return A clone of this environment, with the specified index
215 * @see #index
216 */
217 public Environment withIndex(int index, int count) {
218 Environment e = new Environment(this);
219 e.index = index;
220 e.count = count;
221 return e;
222 }
223
224 /**
225 * Creates a clone of this environment, with the specified {@link Context}.
226 * @param context context
227 * @return A clone of this environment, with the specified {@code Context}
228 */
229 public Environment withContext(Context context) {
230 Environment e = new Environment(this);
231 e.context = context == null ? Context.PRIMITIVE : context;
232 return e;
233 }
234
235 /**
236 * Creates a clone of this environment, with context set to {@link Context#LINK}.
237 * @return A clone of this environment, with context set to {@code Context#LINK}
238 */
239 public Environment withLinkContext() {
240 Environment e = new Environment(this);
241 e.context = Context.LINK;
242 return e;
243 }
244
245 /**
246 * Determines if the context of this environment is {@link Context#LINK}.
247 * @return {@code true} if the context of this environment is {@code Context#LINK}, {@code false} otherwise
248 */
249 public boolean isLinkContext() {
250 return Context.LINK == context;
251 }
252
253 /**
254 * Determines if this environment has a relation as parent.
255 * @return {@code true} if this environment has a relation as parent, {@code false} otherwise
256 * @see #parent
257 */
258 public boolean hasParentRelation() {
259 return parent instanceof Relation;
260 }
261
262 /**
263 * Replies the current context.
264 *
265 * @return the current context
266 */
267 public Context getContext() {
268 return context == null ? Context.PRIMITIVE : context;
269 }
270
271 /**
272 * Gets the role of the matching primitive in the relation
273 * @return The role
274 */
275 public String getRole() {
276 if (getContext() == Context.PRIMITIVE)
277 return null;
278
279 if (parent instanceof Relation)
280 return ((Relation) parent).getMember(index).getRole();
281 if (child != null && osm instanceof Relation)
282 return ((Relation) osm).getMember(index).getRole();
283 return null;
284 }
285
286 /**
287 * Clears all matching context information
288 */
289 public void clearSelectorMatchingInformation() {
290 parent = null;
291 child = null;
292 index = null;
293 count = null;
294 children = null;
295 intersections = null;
296 }
297
298 /**
299 * Gets the current cascade for a given layer
300 * @param layer The layer to use, <code>null</code> to use the layer of the {@link Environment}
301 * @return The cascade
302 */
303 public Cascade getCascade(String layer) {
304 return mc == null ? null : mc.getCascade(layer == null ? this.layer : layer);
305 }
306}
Note: See TracBrowser for help on using the repository browser.