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

Last change on this file since 1583 was 1415, checked in by stoecker, 15 years ago

applied patch #2185 by bruce89

  • Property svn:eol-style set to native
File size: 7.5 KB
Line 
1package org.openstreetmap.josm.gui.mappaint;
2
3import java.util.Collections;
4import java.util.HashMap;
5import java.util.LinkedList;
6import java.util.List;
7import java.util.Map;
8import java.util.Iterator;
9
10import org.openstreetmap.josm.data.osm.Node;
11import org.openstreetmap.josm.data.osm.OsmPrimitive;
12import org.openstreetmap.josm.data.osm.OsmUtils;
13import org.openstreetmap.josm.Main;
14
15public class ElemStyles
16{
17 public class StyleSet {
18 private HashMap<String, IconElemStyle> icons;
19 private HashMap<String, LineElemStyle> lines;
20 private HashMap<String, AreaElemStyle> areas;
21 private HashMap<String, LineElemStyle> modifiers;
22 public StyleSet()
23 {
24 icons = new HashMap<String, IconElemStyle>();
25 lines = new HashMap<String, LineElemStyle>();
26 modifiers = new HashMap<String, LineElemStyle>();
27 areas = new HashMap<String, AreaElemStyle>();
28 }
29 private IconElemStyle getNode(Map<String, String> keys)
30 {
31 IconElemStyle ret = null;
32 Iterator<String> iterator = keys.keySet().iterator();
33 while(iterator.hasNext())
34 {
35 String key = iterator.next();
36 String val = keys.get(key);
37 IconElemStyle style;
38 if((style = icons.get("n" + key + "=" + val)) != null)
39 {
40 if(ret == null || style.priority > ret.priority)
41 ret = style;
42 }
43 if((style = icons.get("b" + key + "=" + OsmUtils.getNamedOsmBoolean(val))) != null)
44 {
45 if(ret == null || style.priority > ret.priority)
46 ret = style;
47 }
48 if((style = icons.get("x" + key)) != null)
49 {
50 if(ret == null || style.priority > ret.priority)
51 ret = style;
52 }
53 }
54 return ret;
55 }
56 private ElemStyle get(Map<String, String> keys)
57 {
58 AreaElemStyle retArea = null;
59 LineElemStyle retLine = null;
60 String linestring = null;
61 HashMap<String, LineElemStyle> over = new HashMap<String, LineElemStyle>();
62 Iterator<String> iterator = keys.keySet().iterator();
63 while(iterator.hasNext())
64 {
65 String key = iterator.next();
66 String val = keys.get(key);
67 AreaElemStyle styleArea;
68 LineElemStyle styleLine;
69 String idx = "n" + key + "=" + val;
70 if((styleArea = areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority))
71 retArea = styleArea;
72 if((styleLine = lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority))
73 {
74 retLine = styleLine;
75 linestring = idx;
76 }
77 if((styleLine = modifiers.get(idx)) != null)
78 over.put(idx, styleLine);
79 idx = "b" + key + "=" + OsmUtils.getNamedOsmBoolean(val);
80 if((styleArea = areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority))
81 retArea = styleArea;
82 if((styleLine = lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority))
83 {
84 retLine = styleLine;
85 linestring = idx;
86 }
87 if((styleLine = modifiers.get(idx)) != null)
88 over.put(idx, styleLine);
89 idx = "x" + key;
90 if((styleArea = areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority))
91 retArea = styleArea;
92 if((styleLine = lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority))
93 {
94 retLine = styleLine;
95 linestring = idx;
96 }
97 if((styleLine = modifiers.get(idx)) != null)
98 over.put(idx, styleLine);
99 }
100 over.remove(linestring);
101 if(over.size() != 0 && retLine != null)
102 {
103 List<LineElemStyle> s = new LinkedList<LineElemStyle>(over.values());
104 Collections.sort(s);
105 retLine = new LineElemStyle(retLine, s);
106 }
107 if(retArea != null)
108 {
109 if(retLine != null)
110 return new AreaElemStyle(retArea, retLine);
111 else
112 return retArea;
113 }
114 return retLine;
115 }
116
117 public ElemStyle get(OsmPrimitive osm)
118 {
119 return (osm.keys == null) ? null :
120 ((osm instanceof Node) ? getNode(osm.keys) : get(osm.keys));
121 }
122
123 public IconElemStyle getIcon(OsmPrimitive osm)
124 {
125 return (osm.keys == null) ? null : getNode(osm.keys);
126 }
127
128 public boolean isArea(OsmPrimitive o)
129 {
130 if(o.keys != null && !(o instanceof Node))
131 {
132 Iterator<String> iterator = o.keys.keySet().iterator();
133 while(iterator.hasNext())
134 {
135 String key = iterator.next();
136 String val = o.keys.get(key);
137 if(areas.containsKey("n" + key + "=" + val)
138 || areas.containsKey("b" + key + "=" + OsmUtils.getNamedOsmBoolean(val))
139 || areas.containsKey("x" + key))
140 return true;
141 }
142 }
143 return false;
144 }
145
146 public boolean hasAreas()
147 {
148 return areas.size() > 0;
149 }
150 }
151
152 HashMap<String, StyleSet> styleSet;
153 public ElemStyles()
154 {
155 styleSet = new HashMap<String, StyleSet>();
156 }
157
158 private String getKey(String k, String v, String b)
159 {
160 if(v != null)
161 return "n" + k + "=" + v;
162 else if(b != null)
163 return "b" + k + "=" + OsmUtils.getNamedOsmBoolean(b);
164 else
165 return "x" + k;
166 }
167
168 public void add(String name, String k, String v, String b, LineElemStyle style)
169 {
170 String key = getKey(k,v,b);
171 style.code = key;
172 getStyleSet(name, true).lines.put(key, style);
173 }
174
175 public void addModifier(String name, String k, String v, String b, LineElemStyle style)
176 {
177 String key = getKey(k,v,b);
178 style.code = key;
179 getStyleSet(name, true).modifiers.put(key, style);
180 }
181
182 public void add(String name, String k, String v, String b, AreaElemStyle style)
183 {
184 String key = getKey(k,v,b);
185 style.code = key;
186 getStyleSet(name, true).areas.put(key, style);
187 }
188
189 public void add(String name, String k, String v, String b, IconElemStyle style)
190 {
191 String key = getKey(k,v,b);
192 style.code = key;
193 getStyleSet(name, true).icons.put(key, style);
194 }
195
196 private StyleSet getStyleSet(String name, boolean create)
197 {
198 if(name == null)
199 name = Main.pref.get("mappaint.style", "standard");
200
201 StyleSet s = styleSet.get(name);
202 if(create && s == null)
203 {
204 s = new StyleSet();
205 styleSet.put(name, s);
206 }
207 return s;
208 }
209
210 /* called from class users, never return null */
211 public StyleSet getStyleSet()
212 {
213 return getStyleSet(null, false);
214 }
215}
Note: See TracBrowser for help on using the repository browser.