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

Last change on this file since 13810 was 13810, checked in by Don-vip, 6 years ago

support rendering of IPrimitive

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