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

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

performance - remove useless boxing of boolean constants

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