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

Last change on this file since 874 was 874, checked in by stoecker, 16 years ago

allow to combine mappaint line and area styles.

  • Property svn:eol-style set to native
File size: 2.4 KB
Line 
1package org.openstreetmap.josm.gui.mappaint;
2
3import java.util.HashMap;
4import java.util.Iterator;
5
6import org.openstreetmap.josm.data.osm.OsmPrimitive;
7import org.openstreetmap.josm.data.osm.OsmUtils;
8
9public class ElemStyles
10{
11 private HashMap<String, ElemStyle> styles;
12
13 public ElemStyles()
14 {
15 styles = new HashMap<String, ElemStyle>();
16 }
17
18 public void add(String k, String v, String b, ElemStyle style)
19 {
20 ElemStyle old_style;
21 String key;
22
23 /* unfortunately, there don't seem to be an efficient way to */
24 /* find out, if a given OsmPrimitive is an area or not, */
25 /* so distinguish only between way and node here - for now */
26 if (style instanceof AreaElemStyle)
27 key = "w";
28 else if (style instanceof LineElemStyle)
29 key = "w";
30 else if (style instanceof IconElemStyle)
31 key = "n";
32 else
33 key = "";
34
35 if(v != null)
36 key += "n" + k + "=" + v;
37 else if(b != null)
38 key += "b" + k + "=" + OsmUtils.getNamedOsmBoolean(b);
39 else
40 key += "x" + k;
41
42 /* avoid duplicates - for now */
43 old_style = styles.get(key);
44 if (old_style == null) {
45 /* new key/value, insert */
46 styles.put(key, style);
47 } else {
48 if (style.getMaxScale() < old_style.getMaxScale()) {
49 /* existing larger scale key/value, replace */
50 styles.remove(old_style);
51 styles.put(key, style);
52 }
53 }
54 }
55
56 public ElemStyle get(OsmPrimitive p, Boolean area)
57 {
58 if (p.keys!=null) {
59 String classname;
60 String kv = null;
61
62 if (p instanceof org.openstreetmap.josm.data.osm.Node) {
63 if(area)
64 return null;
65 classname = "n";
66 } else {
67 classname = "w";
68 }
69 Iterator<String> iterator = p.keys.keySet().iterator();
70 while (iterator.hasNext())
71 {
72 String key = iterator.next();
73 ElemStyle style = null;
74 kv = classname + "n" + key + "=" + p.keys.get(key);
75 if (styles.containsKey(kv))
76 {
77 style = styles.get(kv);
78 if(area == style instanceof AreaElemStyle)
79 return style;
80 }
81 kv = classname + "b" + key + "=" + OsmUtils.getNamedOsmBoolean(p.keys.get(key));
82 if (styles.containsKey(kv))
83 {
84 style = styles.get(kv);
85 if(area == style instanceof AreaElemStyle)
86 return style;
87 }
88 kv = classname + "x" + key;
89 if (styles.containsKey(kv))
90 {
91 style = styles.get(kv);
92 if(area == style instanceof AreaElemStyle)
93 return style;
94 }
95 }
96 }
97
98 return null;
99 }
100
101 public boolean isArea(OsmPrimitive p)
102 {
103 return get(p, true) instanceof AreaElemStyle;
104 }
105}
Note: See TracBrowser for help on using the repository browser.