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 | 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 | * count of nodes in parent way or members in parent relation. Must be != null in LINK context.
|
---|
43 | */
|
---|
44 | public Integer count = null;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Creates a new uninitialized environment.
|
---|
48 | */
|
---|
49 | public Environment() {
|
---|
50 | // environment can be initialized later through with* methods
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Creates a new environment.
|
---|
55 | * @since 8415
|
---|
56 | */
|
---|
57 | public Environment(OsmPrimitive osm) {
|
---|
58 | this.osm = osm;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Creates a new environment.
|
---|
63 | */
|
---|
64 | public Environment(OsmPrimitive osm, MultiCascade mc, String layer, StyleSource source) {
|
---|
65 | this.osm = osm;
|
---|
66 | this.mc = mc;
|
---|
67 | this.layer = layer;
|
---|
68 | this.source = source;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Creates a clone of the environment {@code other}.
|
---|
73 | *
|
---|
74 | * @param other the other environment. Must not be null.
|
---|
75 | * @throws IllegalArgumentException if {@code param} is {@code null}
|
---|
76 | */
|
---|
77 | public Environment(Environment other) {
|
---|
78 | CheckParameterUtil.ensureParameterNotNull(other);
|
---|
79 | this.osm = other.osm;
|
---|
80 | this.mc = other.mc;
|
---|
81 | this.layer = other.layer;
|
---|
82 | this.parent = other.parent;
|
---|
83 | this.child = other.child;
|
---|
84 | this.source = other.source;
|
---|
85 | this.index = other.index;
|
---|
86 | this.count = other.count;
|
---|
87 | this.context = other.getContext();
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Creates a clone of this environment, with the specified primitive.
|
---|
92 | * @return A clone of this environment, with the specified primitive
|
---|
93 | * @see #osm
|
---|
94 | */
|
---|
95 | public Environment withPrimitive(OsmPrimitive osm) {
|
---|
96 | Environment e = new Environment(this);
|
---|
97 | e.osm = osm;
|
---|
98 | return e;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Creates a clone of this environment, with the specified parent.
|
---|
103 | * @param parent the matching parent object
|
---|
104 | * @return A clone of this environment, with the specified parent
|
---|
105 | * @see #parent
|
---|
106 | */
|
---|
107 | public Environment withParent(OsmPrimitive parent) {
|
---|
108 | Environment e = new Environment(this);
|
---|
109 | e.parent = parent;
|
---|
110 | return e;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Creates a clone of this environment, with the specified parent, index, and context set to {@link Context#LINK}.
|
---|
115 | * @param parent the matching parent object
|
---|
116 | * @param index index of node in parent way or member in parent relation
|
---|
117 | * @param count count of nodes in parent way or members in parent relation
|
---|
118 | * @return A clone of this environment, with the specified parent, index, and context set to {@link Context#LINK}
|
---|
119 | * @since 6175
|
---|
120 | * @see #parent
|
---|
121 | * @see #index
|
---|
122 | */
|
---|
123 | public Environment withParentAndIndexAndLinkContext(OsmPrimitive parent, int index, int count) {
|
---|
124 | Environment e = new Environment(this);
|
---|
125 | e.parent = parent;
|
---|
126 | e.index = index;
|
---|
127 | e.count = count;
|
---|
128 | e.context = Context.LINK;
|
---|
129 | return e;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Creates a clone of this environment, with the specified child.
|
---|
134 | * @param child the matching child object
|
---|
135 | * @return A clone of this environment, with the specified child
|
---|
136 | * @see #child
|
---|
137 | */
|
---|
138 | public Environment withChild(OsmPrimitive child) {
|
---|
139 | Environment e = new Environment(this);
|
---|
140 | e.child = child;
|
---|
141 | return e;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Creates a clone of this environment, with the specified child, index, and context set to {@link Context#LINK}.
|
---|
146 | * @param child the matching child object
|
---|
147 | * @param index index of node in parent way or member in parent relation
|
---|
148 | * @param count count of nodes in parent way or members in parent relation
|
---|
149 | * @return A clone of this environment, with the specified child, index, and context set to {@code Context#LINK}
|
---|
150 | * @since 6175
|
---|
151 | * @see #child
|
---|
152 | * @see #index
|
---|
153 | */
|
---|
154 | public Environment withChildAndIndexAndLinkContext(OsmPrimitive child, int index, int count) {
|
---|
155 | Environment e = new Environment(this);
|
---|
156 | e.child = child;
|
---|
157 | e.index = index;
|
---|
158 | e.count = count;
|
---|
159 | e.context = Context.LINK;
|
---|
160 | return e;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Creates a clone of this environment, with the specified index.
|
---|
165 | * @param index index of node in parent way or member in parent relation
|
---|
166 | * @param count count of nodes in parent way or members in parent relation
|
---|
167 | * @return A clone of this environment, with the specified index
|
---|
168 | * @see #index
|
---|
169 | */
|
---|
170 | public Environment withIndex(int index, int count) {
|
---|
171 | Environment e = new Environment(this);
|
---|
172 | e.index = index;
|
---|
173 | e.count = count;
|
---|
174 | return e;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Creates a clone of this environment, with the specified {@link Context}.
|
---|
179 | * @return A clone of this environment, with the specified {@code Context}
|
---|
180 | */
|
---|
181 | public Environment withContext(Context context) {
|
---|
182 | Environment e = new Environment(this);
|
---|
183 | e.context = context == null ? Context.PRIMITIVE : context;
|
---|
184 | return e;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Creates a clone of this environment, with context set to {@link Context#LINK}.
|
---|
189 | * @return A clone of this environment, with context set to {@code Context#LINK}
|
---|
190 | */
|
---|
191 | public Environment withLinkContext() {
|
---|
192 | Environment e = new Environment(this);
|
---|
193 | e.context = Context.LINK;
|
---|
194 | return e;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Determines if the context of this environment is {@link Context#LINK}.
|
---|
199 | * @return {@code true} if the context of this environment is {@code Context#LINK}, {@code false} otherwise
|
---|
200 | */
|
---|
201 | public boolean isLinkContext() {
|
---|
202 | return Context.LINK.equals(context);
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Determines if this environment has a relation as parent.
|
---|
207 | * @return {@code true} if this environment has a relation as parent, {@code false} otherwise
|
---|
208 | * @see #parent
|
---|
209 | */
|
---|
210 | public boolean hasParentRelation() {
|
---|
211 | return parent instanceof Relation;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Replies the current context.
|
---|
216 | *
|
---|
217 | * @return the current context
|
---|
218 | */
|
---|
219 | public Context getContext() {
|
---|
220 | return context == null ? Context.PRIMITIVE : context;
|
---|
221 | }
|
---|
222 |
|
---|
223 | public String getRole() {
|
---|
224 | if (getContext().equals(Context.PRIMITIVE))
|
---|
225 | return null;
|
---|
226 |
|
---|
227 | if (parent instanceof Relation)
|
---|
228 | return ((Relation) parent).getMember(index).getRole();
|
---|
229 | if (child != null && osm instanceof Relation)
|
---|
230 | return ((Relation) osm).getMember(index).getRole();
|
---|
231 | return null;
|
---|
232 | }
|
---|
233 |
|
---|
234 | public void clearSelectorMatchingInformation() {
|
---|
235 | parent = null;
|
---|
236 | child = null;
|
---|
237 | index = null;
|
---|
238 | count = null;
|
---|
239 | }
|
---|
240 |
|
---|
241 | public Cascade getCascade(String layer) {
|
---|
242 | return mc == null ? null : mc.getCascade(layer == null ? this.layer : layer);
|
---|
243 | }
|
---|
244 | }
|
---|