source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyles.java@ 5766

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

see #8490 - fix NPE

  • Property svn:eol-style set to native
File size: 17.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import java.awt.Color;
5import java.util.ArrayList;
6import java.util.Collection;
7import java.util.Collections;
8import java.util.Iterator;
9import java.util.List;
10import java.util.Map.Entry;
11
12import org.openstreetmap.josm.data.osm.Node;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.Relation;
15import org.openstreetmap.josm.data.osm.Way;
16import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon;
17import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
18import org.openstreetmap.josm.gui.NavigatableComponent;
19import org.openstreetmap.josm.gui.mappaint.StyleCache.StyleList;
20import org.openstreetmap.josm.tools.Pair;
21import org.openstreetmap.josm.tools.Utils;
22
23public class ElemStyles {
24 private List<StyleSource> styleSources;
25 private boolean drawMultipolygon;
26
27 private int cacheIdx = 1;
28
29 private boolean defaultNodes, defaultLines;
30 private int defaultNodesIdx, defaultLinesIdx;
31
32 public ElemStyles()
33 {
34 styleSources = new ArrayList<StyleSource>();
35 }
36
37 public void clearCached() {
38 cacheIdx++;
39 }
40
41 public List<StyleSource> getStyleSources() {
42 return Collections.<StyleSource>unmodifiableList(styleSources);
43 }
44
45 /**
46 * Create the list of styles for one primitive.
47 *
48 * @param osm the primitive
49 * @param scale the scale (in meters per 100 pixel)
50 * @param nc
51 * @return
52 */
53 public StyleList get(OsmPrimitive osm, double scale, NavigatableComponent nc) {
54 return getStyleCacheWithRange(osm, scale, nc).a;
55 }
56
57 /**
58 * Create the list of styles and its valid scale range for one primitive.
59 *
60 * Automatically adds default styles in case no proper style was found.
61 * Uses the cache, if possible, and saves the results to the cache.
62 */
63 public Pair<StyleList, Range> getStyleCacheWithRange(OsmPrimitive osm, double scale, NavigatableComponent nc) {
64 if (osm.mappaintStyle == null || osm.mappaintCacheIdx != cacheIdx || scale <= 0) {
65 osm.mappaintStyle = StyleCache.EMPTY_STYLECACHE;
66 } else {
67 Pair<StyleList, Range> lst = osm.mappaintStyle.getWithRange(scale);
68 if (lst.a != null)
69 return lst;
70 }
71 Pair<StyleList, Range> p = getImpl(osm, scale, nc);
72 if (osm instanceof Node && isDefaultNodes()) {
73 if (p.a.isEmpty()) {
74 if (TextElement.AUTO_LABEL_COMPOSITION_STRATEGY.compose(osm) != null) {
75 p.a = NodeElemStyle.DEFAULT_NODE_STYLELIST_TEXT;
76 } else {
77 p.a = NodeElemStyle.DEFAULT_NODE_STYLELIST;
78 }
79 } else {
80 boolean hasNonModifier = false;
81 boolean hasText = false;
82 for (ElemStyle s : p.a) {
83 if (s instanceof BoxTextElemStyle) {
84 hasText = true;
85 } else {
86 if (!s.isModifier) {
87 hasNonModifier = true;
88 }
89 }
90 }
91 if (!hasNonModifier) {
92 p.a = new StyleList(p.a, NodeElemStyle.SIMPLE_NODE_ELEMSTYLE);
93 if (!hasText) {
94 if (TextElement.AUTO_LABEL_COMPOSITION_STRATEGY.compose(osm) != null) {
95 p.a = new StyleList(p.a, BoxTextElemStyle.SIMPLE_NODE_TEXT_ELEMSTYLE);
96 }
97 }
98 }
99 }
100 } else if (osm instanceof Way && isDefaultLines()) {
101 boolean hasProperLineStyle = false;
102 for (ElemStyle s : p.a) {
103 if (s.isProperLineStyle()) {
104 hasProperLineStyle = true;
105 break;
106 }
107 }
108 if (!hasProperLineStyle) {
109 AreaElemStyle area = Utils.find(p.a, AreaElemStyle.class);
110 LineElemStyle line = area == null ? LineElemStyle.UNTAGGED_WAY : LineElemStyle.createSimpleLineStyle(area.color, true);
111 p.a = new StyleList(p.a, line);
112 }
113 }
114 if (p != null) {
115 osm.mappaintStyle = osm.mappaintStyle.put(p.a, p.b);
116 }
117 osm.mappaintCacheIdx = cacheIdx;
118 return p;
119 }
120
121 /**
122 * Create the list of styles and its valid scale range for one primitive.
123 *
124 * This method does multipolygon handling.
125 *
126 *
127 * There are different tagging styles for multipolygons, that have to be respected:
128 * - tags on the relation
129 * - tags on the outer way
130 * - tags on both, the outer and the inner way (very old style)
131 *
132 * If the primitive is a way, look for multipolygon parents. In case it
133 * is indeed member of some multipolygon as role "outer", all area styles
134 * are removed. (They apply to the multipolygon area.)
135 * Outer ways can have their own independent line styles, e.g. a road as
136 * boundary of a forest. Otherwise, in case, the way does not have an
137 * independent line style, take a line style from the multipolygon.
138 * If the multipolygon does not have a line style either, at least create a
139 * default line style from the color of the area.
140 *
141 * Now consider the case that the way is not an outer way of any multipolygon,
142 * but is member of a multipolygon as "inner".
143 * First, the style list is regenerated, considering only tags of this way
144 * minus the tags of outer way of the multipolygon (to care for the "very
145 * old style").
146 * Then check, if the way describes something in its own right. (linear feature
147 * or area) If not, add a default line style from the area color of the multipolygon.
148 *
149 */
150 private Pair<StyleList, Range> getImpl(OsmPrimitive osm, double scale, NavigatableComponent nc) {
151 if (osm instanceof Node)
152 return generateStyles(osm, scale, null, false);
153 else if (osm instanceof Way)
154 {
155 Pair<StyleList, Range> p = generateStyles(osm, scale, null, false);
156
157 boolean isOuterWayOfSomeMP = false;
158 Color wayColor = null;
159
160 for (OsmPrimitive referrer : osm.getReferrers()) {
161 Relation r = (Relation) referrer;
162 if (!drawMultipolygon || !r.isMultipolygon() || !r.isUsable()) {
163 continue;
164 }
165 Multipolygon multipolygon = MultipolygonCache.getInstance().get(nc, r);
166
167 if (multipolygon.getOuterWays().contains(osm)) {
168 boolean hasIndependentLineStyle = false;
169 if (!isOuterWayOfSomeMP) { // do this only one time
170 List<ElemStyle> tmp = new ArrayList<ElemStyle>(p.a.size());
171 for (ElemStyle s : p.a) {
172 if (s instanceof AreaElemStyle) {
173 wayColor = ((AreaElemStyle) s).color;
174 } else {
175 tmp.add(s);
176 if (s.isProperLineStyle()) {
177 hasIndependentLineStyle = true;
178 }
179 }
180 }
181 p.a = new StyleList(tmp);
182 isOuterWayOfSomeMP = true;
183 }
184
185 if (!hasIndependentLineStyle) {
186 Pair<StyleList, Range> mpElemStyles = getStyleCacheWithRange(r, scale, nc);
187 ElemStyle mpLine = null;
188 for (ElemStyle s : mpElemStyles.a) {
189 if (s.isProperLineStyle()) {
190 mpLine = s;
191 break;
192 }
193 }
194 p.b = Range.cut(p.b, mpElemStyles.b);
195 if (mpLine != null) {
196 p.a = new StyleList(p.a, mpLine);
197 break;
198 } else if (wayColor == null && isDefaultLines()) {
199 AreaElemStyle mpArea = Utils.find(mpElemStyles.a, AreaElemStyle.class);
200 if (mpArea != null) {
201 wayColor = mpArea.color;
202 }
203 }
204 }
205 }
206 }
207 if (isOuterWayOfSomeMP) {
208 if (isDefaultLines()) {
209 boolean hasLineStyle = false;
210 for (ElemStyle s : p.a) {
211 if (s.isProperLineStyle()) {
212 hasLineStyle = true;
213 break;
214 }
215 }
216 if (!hasLineStyle) {
217 p.a = new StyleList(p.a, LineElemStyle.createSimpleLineStyle(wayColor, true));
218 }
219 }
220 return p;
221 }
222
223 if (!isDefaultLines()) return p;
224
225 for (OsmPrimitive referrer : osm.getReferrers()) {
226 Relation ref = (Relation) referrer;
227 if (!drawMultipolygon || !ref.isMultipolygon() || !ref.isUsable()) {
228 continue;
229 }
230 final Multipolygon multipolygon = MultipolygonCache.getInstance().get(nc, ref);
231
232 if (multipolygon.getInnerWays().contains(osm)) {
233 Iterator<Way> it = multipolygon.getOuterWays().iterator();
234 p = generateStyles(osm, scale, it.hasNext() ? it.next() : null, false);
235 boolean hasIndependentElemStyle = false;
236 for (ElemStyle s : p.a) {
237 if (s.isProperLineStyle() || s instanceof AreaElemStyle) {
238 hasIndependentElemStyle = true;
239 }
240 }
241 if (!hasIndependentElemStyle && !multipolygon.getOuterWays().isEmpty()) {
242 StyleList mpElemStyles = get(ref, scale, nc);
243 Color mpColor = null;
244 for (ElemStyle mpS : mpElemStyles) {
245 if (mpS instanceof AreaElemStyle) {
246 mpColor = ((AreaElemStyle) mpS).color;
247 break;
248 }
249 }
250 p.a = new StyleList(p.a, LineElemStyle.createSimpleLineStyle(mpColor, true));
251 }
252 return p;
253 }
254 }
255 return p;
256 }
257 else if (osm instanceof Relation)
258 {
259 Pair<StyleList, Range> p = generateStyles(osm, scale, null, true);
260 if (drawMultipolygon && ((Relation)osm).isMultipolygon()) {
261 if (!Utils.exists(p.a, AreaElemStyle.class)) {
262 // look at outer ways to find area style
263 Multipolygon multipolygon = MultipolygonCache.getInstance().get(nc, (Relation) osm);
264 for (Way w : multipolygon.getOuterWays()) {
265 Pair<StyleList, Range> wayStyles = generateStyles(w, scale, null, false);
266 p.b = Range.cut(p.b, wayStyles.b);
267 ElemStyle area = Utils.find(wayStyles.a, AreaElemStyle.class);
268 if (area != null) {
269 p.a = new StyleList(p.a, area);
270 break;
271 }
272 }
273 }
274 }
275 return p;
276 }
277 return null;
278 }
279
280 /**
281 * Create the list of styles and its valid scale range for one primitive.
282 *
283 * Loops over the list of style sources, to generate the map of properties.
284 * From these properties, it generates the different types of styles.
285 *
286 * @param osm the primitive to create styles for
287 * @param scale the scale (in meters per 100 px), must be > 0
288 * @param multipolyOuterWay support for a very old multipolygon tagging style
289 * where you add the tags both to the outer and the inner way.
290 * However, independent inner way style is also possible.
291 * @param pretendWayIsClosed For styles that require the way to be closed,
292 * we pretend it is. This is useful for generating area styles from the (segmented)
293 * outer ways of a multipolygon.
294 * @return the generated styles and the valid range as a pair
295 */
296 public Pair<StyleList, Range> generateStyles(OsmPrimitive osm, double scale, OsmPrimitive multipolyOuterWay, boolean pretendWayIsClosed) {
297
298 List<ElemStyle> sl = new ArrayList<ElemStyle>();
299 MultiCascade mc = new MultiCascade();
300 Environment env = new Environment(osm, mc, null, null);
301
302 for (StyleSource s : styleSources) {
303 if (s.active) {
304 s.apply(mc, osm, scale, multipolyOuterWay, pretendWayIsClosed);
305 }
306 }
307
308 for (Entry<String, Cascade> e : mc.getLayers()) {
309 if ("*".equals(e.getKey())) {
310 continue;
311 }
312 env.layer = e.getKey();
313 Cascade c = e.getValue();
314 if (osm instanceof Way) {
315 addIfNotNull(sl, AreaElemStyle.create(c));
316 addIfNotNull(sl, LinePatternElemStyle.create(env));
317 addIfNotNull(sl, LineElemStyle.createLine(env));
318 addIfNotNull(sl, LineElemStyle.createLeftCasing(env));
319 addIfNotNull(sl, LineElemStyle.createRightCasing(env));
320 addIfNotNull(sl, LineElemStyle.createCasing(env));
321 addIfNotNull(sl, LineTextElemStyle.create(env));
322 } else if (osm instanceof Node) {
323 NodeElemStyle nodeStyle = NodeElemStyle.create(env);
324 if (nodeStyle != null) {
325 sl.add(nodeStyle);
326 addIfNotNull(sl, BoxTextElemStyle.create(env, nodeStyle.getBoxProvider()));
327 } else {
328 addIfNotNull(sl, BoxTextElemStyle.create(env, NodeElemStyle.SIMPLE_NODE_ELEMSTYLE.getBoxProvider()));
329 }
330 } else if (osm instanceof Relation) {
331 if (((Relation)osm).isMultipolygon()) {
332 addIfNotNull(sl, AreaElemStyle.create(c));
333 addIfNotNull(sl, LinePatternElemStyle.create(env));
334 addIfNotNull(sl, LineElemStyle.createLine(env));
335 addIfNotNull(sl, LineElemStyle.createCasing(env));
336 addIfNotNull(sl, LineTextElemStyle.create(env));
337 } else if ("restriction".equals(osm.get("type"))) {
338 addIfNotNull(sl, NodeElemStyle.create(env));
339 }
340 }
341 }
342 return new Pair<StyleList, Range>(new StyleList(sl), mc.range);
343 }
344
345 private static <T> void addIfNotNull(List<T> list, T obj) {
346 if (obj != null) {
347 list.add(obj);
348 }
349 }
350
351 /**
352 * Draw a default node symbol for nodes that have no style?
353 */
354 private boolean isDefaultNodes() {
355 if (defaultNodesIdx == cacheIdx)
356 return defaultNodes;
357 defaultNodes = fromCanvas("default-points", true, Boolean.class);
358 defaultNodesIdx = cacheIdx;
359 return defaultNodes;
360 }
361
362 /**
363 * Draw a default line for ways that do not have an own line style?
364 */
365 private boolean isDefaultLines() {
366 if (defaultLinesIdx == cacheIdx)
367 return defaultLines;
368 defaultLines = fromCanvas("default-lines", true, Boolean.class);
369 defaultLinesIdx = cacheIdx;
370 return defaultLines;
371 }
372
373 private <T> T fromCanvas(String key, T def, Class<T> c) {
374 MultiCascade mc = new MultiCascade();
375 Relation r = new Relation();
376 r.put("#canvas", "query");
377
378 for (StyleSource s : styleSources) {
379 if (s.active) {
380 s.apply(mc, r, 1, null, false);
381 }
382 }
383 T res = mc.getCascade("default").get(key, def, c);
384 return res;
385 }
386
387 public boolean isDrawMultipolygon() {
388 return drawMultipolygon;
389 }
390
391 public void setDrawMultipolygon(boolean drawMultipolygon) {
392 this.drawMultipolygon = drawMultipolygon;
393 }
394
395 /**
396 * remove all style sources; only accessed from MapPaintStyles
397 */
398 void clear() {
399 styleSources.clear();
400 }
401
402 /**
403 * add a style source; only accessed from MapPaintStyles
404 */
405 void add(StyleSource style) {
406 styleSources.add(style);
407 }
408
409 /**
410 * set the style sources; only accessed from MapPaintStyles
411 */
412 void setStyleSources(Collection<StyleSource> sources) {
413 styleSources.clear();
414 styleSources.addAll(sources);
415 }
416
417 /**
418 * Returns the first AreaElemStyle for a given primitive.
419 * @param p the OSM primitive
420 * @param pretendWayIsClosed For styles that require the way to be closed,
421 * we pretend it is. This is useful for generating area styles from the (segmented)
422 * outer ways of a multipolygon.
423 * @return first AreaElemStyle found or {@code null}.
424 */
425 public static AreaElemStyle getAreaElemStyle(OsmPrimitive p, boolean pretendWayIsClosed) {
426 if (MapPaintStyles.getStyles() == null)
427 return null;
428 for (ElemStyle s : MapPaintStyles.getStyles().generateStyles(p, 1.0, null, pretendWayIsClosed).a) {
429 if (s instanceof AreaElemStyle)
430 return (AreaElemStyle) s;
431 }
432 return null;
433 }
434
435 /**
436 * Determines whether primitive has an AreaElemStyle.
437 * @param p the OSM primitive
438 * @param pretendWayIsClosed For styles that require the way to be closed,
439 * we pretend it is. This is useful for generating area styles from the (segmented)
440 * outer ways of a multipolygon.
441 * @return {@code true} iff primitive has an AreaElemStyle
442 */
443 public static boolean hasAreaElemStyle(OsmPrimitive p, boolean pretendWayIsClosed) {
444 return getAreaElemStyle(p, pretendWayIsClosed) != null;
445 }
446}
Note: See TracBrowser for help on using the repository browser.