1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
3 |
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.regex.PatternSyntaxException;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.data.osm.Node;
|
---|
8 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
9 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
10 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
11 | import org.openstreetmap.josm.data.osm.Way;
|
---|
12 | import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
|
---|
13 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
14 | import org.openstreetmap.josm.gui.mappaint.Range;
|
---|
15 | import org.openstreetmap.josm.tools.Pair;
|
---|
16 | import org.openstreetmap.josm.tools.Utils;
|
---|
17 |
|
---|
18 | public interface Selector {
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Apply the selector to the primitive and check if it matches.
|
---|
22 | *
|
---|
23 | * @param env the Environment. env.mc and env.layer are read-only when matching a selector.
|
---|
24 | * env.source is not needed. This method will set the matchingReferrers field of env as
|
---|
25 | * a side effect! Make sure to clear it before invoking this method.
|
---|
26 | * @return true, if the selector applies
|
---|
27 | */
|
---|
28 | public boolean matches(Environment env);
|
---|
29 |
|
---|
30 | public String getSubpart();
|
---|
31 |
|
---|
32 | public Range getRange();
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * <p>Represents a child selector or a parent selector.</p>
|
---|
36 | *
|
---|
37 | * <p>In addition to the standard CSS notation for child selectors, JOSM also supports
|
---|
38 | * an "inverse" notation:</p>
|
---|
39 | * <pre>
|
---|
40 | * selector_a > selector_b { ... } // the standard notation (child selector)
|
---|
41 | * relation[type=route] > way { ... } // example (all ways of a route)
|
---|
42 | *
|
---|
43 | * selector_a < selector_b { ... } // the inverse notation (parent selector)
|
---|
44 | * node[traffic_calming] < way { ... } // example (way that has a traffic calming node)
|
---|
45 | * </pre>
|
---|
46 | *
|
---|
47 | */
|
---|
48 | public static class ChildOrParentSelector implements Selector {
|
---|
49 | private final Selector left;
|
---|
50 | private final LinkSelector link;
|
---|
51 | private final Selector right;
|
---|
52 | /** true, if this represents a parent selector (otherwise it is a child selector)
|
---|
53 | */
|
---|
54 | private final boolean parentSelector;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | *
|
---|
58 | * @param a the first selector
|
---|
59 | * @param b the second selector
|
---|
60 | * @param parentSelector if true, this is a parent selector; otherwise a child selector
|
---|
61 | */
|
---|
62 | public ChildOrParentSelector(Selector a, LinkSelector link, Selector b, boolean parentSelector) {
|
---|
63 | this.left = a;
|
---|
64 | this.link = link;
|
---|
65 | this.right = b;
|
---|
66 | this.parentSelector = parentSelector;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * <p>Finds the first referrer matching {@link #left}</p>
|
---|
71 | *
|
---|
72 | * <p>The visitor works on an environment and it saves the matching
|
---|
73 | * referrer in {@code e.parent} and its relative position in the
|
---|
74 | * list referrers "child list" in {@code e.index}.</p>
|
---|
75 | *
|
---|
76 | * <p>If after execution {@code e.parent} is null, no matching
|
---|
77 | * referrer was found.</p>
|
---|
78 | *
|
---|
79 | */
|
---|
80 | private class MatchingReferrerFinder extends AbstractVisitor{
|
---|
81 | private Environment e;
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Constructor
|
---|
85 | * @param e the environment against which we match
|
---|
86 | */
|
---|
87 | public MatchingReferrerFinder(Environment e){
|
---|
88 | this.e = e;
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public void visit(Node n) {
|
---|
93 | // node should never be a referrer
|
---|
94 | throw new AssertionError();
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | public void visit(Way w) {
|
---|
99 | /*
|
---|
100 | * If e.parent is already set to the first matching referrer. We skip any following
|
---|
101 | * referrer injected into the visitor.
|
---|
102 | */
|
---|
103 | if (e.parent != null) return;
|
---|
104 |
|
---|
105 | if (!left.matches(e.withPrimitive(w)))
|
---|
106 | return;
|
---|
107 | for (int i=0; i<w.getNodesCount(); i++) {
|
---|
108 | Node n = w.getNode(i);
|
---|
109 | if (n.equals(e.osm)) {
|
---|
110 | if (link.matches(e.withParent(w).withIndex(i).withLinkContext())) {
|
---|
111 | e.parent = w;
|
---|
112 | e.index = i;
|
---|
113 | return;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Override
|
---|
120 | public void visit(Relation r) {
|
---|
121 | /*
|
---|
122 | * If e.parent is already set to the first matching referrer. We skip any following
|
---|
123 | * referrer injected into the visitor.
|
---|
124 | */
|
---|
125 | if (e.parent != null) return;
|
---|
126 |
|
---|
127 | if (!left.matches(e.withPrimitive(r)))
|
---|
128 | return;
|
---|
129 | for (int i=0; i < r.getMembersCount(); i++) {
|
---|
130 | RelationMember m = r.getMember(i);
|
---|
131 | if (m.getMember().equals(e.osm)) {
|
---|
132 | if (link.matches(e.withParent(r).withIndex(i).withLinkContext())) {
|
---|
133 | e.parent = r;
|
---|
134 | e.index = i;
|
---|
135 | return;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Override
|
---|
143 | public boolean matches(Environment e) {
|
---|
144 | if (!right.matches(e))
|
---|
145 | return false;
|
---|
146 |
|
---|
147 | if (!parentSelector) {
|
---|
148 | MatchingReferrerFinder collector = new MatchingReferrerFinder(e);
|
---|
149 | e.osm.visitReferrers(collector);
|
---|
150 | if (e.parent != null)
|
---|
151 | return true;
|
---|
152 | } else {
|
---|
153 | if (e.osm instanceof Way) {
|
---|
154 | List<Node> wayNodes = ((Way) e.osm).getNodes();
|
---|
155 | for (int i=0; i<wayNodes.size(); i++) {
|
---|
156 | Node n = wayNodes.get(i);
|
---|
157 | if (left.matches(e.withPrimitive(n))) {
|
---|
158 | if (link.matches(e.withChild(n).withIndex(i).withLinkContext())) {
|
---|
159 | e.child = n;
|
---|
160 | e.index = i;
|
---|
161 | return true;
|
---|
162 | }
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|
166 | else if (e.osm instanceof Relation) {
|
---|
167 | List<RelationMember> members = ((Relation) e.osm).getMembers();
|
---|
168 | for (int i=0; i<members.size(); i++) {
|
---|
169 | OsmPrimitive member = members.get(i).getMember();
|
---|
170 | if (left.matches(e.withPrimitive(member))) {
|
---|
171 | if (link.matches(e.withChild(member).withIndex(i).withLinkContext())) {
|
---|
172 | e.child = member;
|
---|
173 | e.index = i;
|
---|
174 | return true;
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|
180 | return false;
|
---|
181 | }
|
---|
182 |
|
---|
183 | @Override
|
---|
184 | public String getSubpart() {
|
---|
185 | return right.getSubpart();
|
---|
186 | }
|
---|
187 |
|
---|
188 | @Override
|
---|
189 | public Range getRange() {
|
---|
190 | return right.getRange();
|
---|
191 | }
|
---|
192 |
|
---|
193 | @Override
|
---|
194 | public String toString() {
|
---|
195 | return left +" "+ (parentSelector? "<" : ">")+link+" " +right;
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Super class of {@link GeneralSelector} and {@link LinkSelector}
|
---|
201 | * @since 5841
|
---|
202 | */
|
---|
203 | public static abstract class AbstractSelector implements Selector {
|
---|
204 |
|
---|
205 | protected final List<Condition> conds;
|
---|
206 |
|
---|
207 | protected AbstractSelector(List<Condition> conditions) {
|
---|
208 | if (conditions == null || conditions.isEmpty()) {
|
---|
209 | this.conds = null;
|
---|
210 | } else {
|
---|
211 | this.conds = conditions;
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Determines if all conditions match the given environment.
|
---|
217 | * @param env The environment to check
|
---|
218 | * @return {@code true} if all conditions apply, false otherwise.
|
---|
219 | */
|
---|
220 | public final boolean matchesConditions(Environment env) {
|
---|
221 | if (conds == null) return true;
|
---|
222 | for (Condition c : conds) {
|
---|
223 | try {
|
---|
224 | if (!c.applies(env)) return false;
|
---|
225 | } catch (PatternSyntaxException e) {
|
---|
226 | System.err.println("PatternSyntaxException while applying condition" + c +": "+e.getMessage());
|
---|
227 | return false;
|
---|
228 | }
|
---|
229 | }
|
---|
230 | return true;
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | public static class LinkSelector extends AbstractSelector {
|
---|
235 |
|
---|
236 | public LinkSelector(List<Condition> conditions) {
|
---|
237 | super(conditions);
|
---|
238 | }
|
---|
239 |
|
---|
240 | @Override
|
---|
241 | public boolean matches(Environment env) {
|
---|
242 | Utils.ensure(env.isLinkContext(), "Requires LINK context in environment, got ''{0}''", env.getContext());
|
---|
243 | return matchesConditions(env);
|
---|
244 | }
|
---|
245 |
|
---|
246 | @Override
|
---|
247 | public String getSubpart() {
|
---|
248 | throw new UnsupportedOperationException("Not supported yet.");
|
---|
249 | }
|
---|
250 |
|
---|
251 | @Override
|
---|
252 | public Range getRange() {
|
---|
253 | throw new UnsupportedOperationException("Not supported yet.");
|
---|
254 | }
|
---|
255 |
|
---|
256 | @Override
|
---|
257 | public String toString() {
|
---|
258 | return "LinkSelector{" + "conditions=" + conds + '}';
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | public static class GeneralSelector extends AbstractSelector {
|
---|
263 | private String base;
|
---|
264 | public Range range;
|
---|
265 | private String subpart;
|
---|
266 |
|
---|
267 | public GeneralSelector(String base, Pair<Integer, Integer> zoom, List<Condition> conds, String subpart) {
|
---|
268 | super(conds);
|
---|
269 | this.base = base;
|
---|
270 | if (zoom != null) {
|
---|
271 | int a = zoom.a == null ? 0 : zoom.a;
|
---|
272 | int b = zoom.b == null ? Integer.MAX_VALUE : zoom.b;
|
---|
273 | if (a <= b) {
|
---|
274 | range = fromLevel(a, b);
|
---|
275 | }
|
---|
276 | }
|
---|
277 | if (range == null) {
|
---|
278 | range = new Range();
|
---|
279 | }
|
---|
280 | this.subpart = subpart;
|
---|
281 | }
|
---|
282 |
|
---|
283 | @Override
|
---|
284 | public String getSubpart() {
|
---|
285 | return subpart;
|
---|
286 | }
|
---|
287 | @Override
|
---|
288 | public Range getRange() {
|
---|
289 | return range;
|
---|
290 | }
|
---|
291 |
|
---|
292 | public boolean matchesBase(Environment e){
|
---|
293 | if (e.osm instanceof Node) {
|
---|
294 | return base.equals("node") || base.equals("*");
|
---|
295 | } else if (e.osm instanceof Way) {
|
---|
296 | return base.equals("way") || base.equals("area") || base.equals("*");
|
---|
297 | } else if (e.osm instanceof Relation) {
|
---|
298 | if (base.equals("area")) {
|
---|
299 | return ((Relation) e.osm).isMultipolygon();
|
---|
300 | } else if (base.equals("relation")) {
|
---|
301 | return true;
|
---|
302 | } else if (base.equals("canvas")) {
|
---|
303 | return e.osm.get("#canvas") != null;
|
---|
304 | }
|
---|
305 | }
|
---|
306 | return false;
|
---|
307 | }
|
---|
308 |
|
---|
309 | @Override
|
---|
310 | public boolean matches(Environment e) {
|
---|
311 | if (!matchesBase(e)) return false;
|
---|
312 | return matchesConditions(e);
|
---|
313 | }
|
---|
314 |
|
---|
315 | public String getBase() {
|
---|
316 | return base;
|
---|
317 | }
|
---|
318 |
|
---|
319 | public static Range fromLevel(int a, int b) {
|
---|
320 | if (a > b)
|
---|
321 | throw new AssertionError();
|
---|
322 | double lower = 0;
|
---|
323 | double upper = Double.POSITIVE_INFINITY;
|
---|
324 | if (b != Integer.MAX_VALUE) {
|
---|
325 | lower = level2scale(b + 1);
|
---|
326 | }
|
---|
327 | if (a != 0) {
|
---|
328 | upper = level2scale(a);
|
---|
329 | }
|
---|
330 | return new Range(lower, upper);
|
---|
331 | }
|
---|
332 |
|
---|
333 | final static double R = 6378135;
|
---|
334 |
|
---|
335 | public static double level2scale(int lvl) {
|
---|
336 | if (lvl < 0)
|
---|
337 | throw new IllegalArgumentException();
|
---|
338 | // preliminary formula - map such that mapnik imagery tiles of the same
|
---|
339 | // or similar level are displayed at the given scale
|
---|
340 | return 2.0 * Math.PI * R / Math.pow(2.0, lvl) / 2.56;
|
---|
341 | }
|
---|
342 |
|
---|
343 | @Override
|
---|
344 | public String toString() {
|
---|
345 | return base + (range == null ? "" : range) + Utils.join("", conds) + (subpart != null ? ("::" + subpart) : "");
|
---|
346 | }
|
---|
347 | }
|
---|
348 | }
|
---|