1 | package org.openstreetmap.josm.gui.mappaint;
|
---|
2 |
|
---|
3 | import java.util.Collections;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.LinkedList;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Iterator;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.data.osm.Node;
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
12 | import org.openstreetmap.josm.data.osm.Way;
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 |
|
---|
15 | public class ElemStyles
|
---|
16 | {
|
---|
17 | private class StyleSet {
|
---|
18 | HashMap<String, IconElemStyle> icons;
|
---|
19 | HashMap<String, LineElemStyle> lines;
|
---|
20 | HashMap<String, AreaElemStyle> areas;
|
---|
21 | 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 | }
|
---|
30 | HashMap<String, StyleSet> styleSet;
|
---|
31 |
|
---|
32 | public ElemStyles()
|
---|
33 | {
|
---|
34 | styleSet = new HashMap<String, StyleSet>();
|
---|
35 | }
|
---|
36 |
|
---|
37 | private String getKey(String k, String v, String b)
|
---|
38 | {
|
---|
39 | if(v != null)
|
---|
40 | return "n" + k + "=" + v;
|
---|
41 | else if(b != null)
|
---|
42 | return "b" + k + "=" + OsmUtils.getNamedOsmBoolean(b);
|
---|
43 | else
|
---|
44 | return "x" + k;
|
---|
45 | }
|
---|
46 |
|
---|
47 | public void add(String name, String k, String v, String b, LineElemStyle style)
|
---|
48 | {
|
---|
49 | getStyleSet(name, true).lines.put(getKey(k,v,b), style);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void addModifier(String name, String k, String v, String b, LineElemStyle style)
|
---|
53 | {
|
---|
54 | getStyleSet(name, true).modifiers.put(getKey(k,v,b), style);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public void add(String name, String k, String v, String b, AreaElemStyle style)
|
---|
58 | {
|
---|
59 | getStyleSet(name, true).areas.put(getKey(k,v,b), style);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public void add(String name, String k, String v, String b, IconElemStyle style)
|
---|
63 | {
|
---|
64 | getStyleSet(name, true).icons.put(getKey(k,v,b), style);
|
---|
65 | }
|
---|
66 |
|
---|
67 | private StyleSet getStyleSet(String name, boolean create)
|
---|
68 | {
|
---|
69 | if(name == null)
|
---|
70 | name = Main.pref.get("mappaint.style", "standard");
|
---|
71 | StyleSet s = styleSet.get(name);
|
---|
72 | if(create && s == null)
|
---|
73 | {
|
---|
74 | s = new StyleSet();
|
---|
75 | styleSet.put(name, s);
|
---|
76 | }
|
---|
77 | return s;
|
---|
78 | }
|
---|
79 |
|
---|
80 | public IconElemStyle get(Node n)
|
---|
81 | {
|
---|
82 | StyleSet ss = getStyleSet(null, false);
|
---|
83 | IconElemStyle ret = null;
|
---|
84 | if(ss != null && n.keys != null)
|
---|
85 | {
|
---|
86 | Iterator<String> iterator = n.keys.keySet().iterator();
|
---|
87 | while(iterator.hasNext())
|
---|
88 | {
|
---|
89 | String key = iterator.next();
|
---|
90 | String val = n.keys.get(key);
|
---|
91 | IconElemStyle style;
|
---|
92 | if((style = ss.icons.get("n" + key + "=" + val)) != null)
|
---|
93 | {
|
---|
94 | if(ret == null || style.priority > ret.priority)
|
---|
95 | ret = style;
|
---|
96 | }
|
---|
97 | if((style = ss.icons.get("b" + key + "=" + OsmUtils.getNamedOsmBoolean(val))) != null)
|
---|
98 | {
|
---|
99 | if(ret == null || style.priority > ret.priority)
|
---|
100 | ret = style;
|
---|
101 | }
|
---|
102 | if((style = ss.icons.get("x" + key)) != null)
|
---|
103 | {
|
---|
104 | if(ret == null || style.priority > ret.priority)
|
---|
105 | ret = style;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 | return ret;
|
---|
110 | }
|
---|
111 |
|
---|
112 | public ElemStyle get(Way w)
|
---|
113 | {
|
---|
114 | StyleSet ss = getStyleSet(null, false);
|
---|
115 | if(ss == null || w.keys == null)
|
---|
116 | return null;
|
---|
117 | AreaElemStyle retArea = null;
|
---|
118 | LineElemStyle retLine = null;
|
---|
119 | List<LineElemStyle> over = new LinkedList<LineElemStyle>();
|
---|
120 | Iterator<String> iterator = w.keys.keySet().iterator();
|
---|
121 | while(iterator.hasNext())
|
---|
122 | {
|
---|
123 | String key = iterator.next();
|
---|
124 | String val = w.keys.get(key);
|
---|
125 | AreaElemStyle styleArea;
|
---|
126 | LineElemStyle styleLine;
|
---|
127 | String idx = "n" + key + "=" + val;
|
---|
128 | if((styleArea = ss.areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority))
|
---|
129 | retArea = styleArea;
|
---|
130 | if((styleLine = ss.lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority))
|
---|
131 | retLine = styleLine;
|
---|
132 | if((styleLine = ss.modifiers.get(idx)) != null)
|
---|
133 | over.add(styleLine);
|
---|
134 | idx = "b" + key + "=" + OsmUtils.getNamedOsmBoolean(val);
|
---|
135 | if((styleArea = ss.areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority))
|
---|
136 | retArea = styleArea;
|
---|
137 | if((styleLine = ss.lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority))
|
---|
138 | retLine = styleLine;
|
---|
139 | if((styleLine = ss.modifiers.get(idx)) != null)
|
---|
140 | over.add(styleLine);
|
---|
141 | idx = "x" + key;
|
---|
142 | if((styleArea = ss.areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority))
|
---|
143 | retArea = styleArea;
|
---|
144 | if((styleLine = ss.lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority))
|
---|
145 | retLine = styleLine;
|
---|
146 | if((styleLine = ss.modifiers.get(idx)) != null)
|
---|
147 | over.add(styleLine);
|
---|
148 | }
|
---|
149 | if(over.size() != 0 && retLine != null)
|
---|
150 | {
|
---|
151 | Collections.sort(over);
|
---|
152 | retLine = new LineElemStyle(retLine, over);
|
---|
153 | }
|
---|
154 | if(retArea != null)
|
---|
155 | {
|
---|
156 | if(retLine != null)
|
---|
157 | return new AreaElemStyle(retArea, retLine);
|
---|
158 | else
|
---|
159 | return retArea;
|
---|
160 | }
|
---|
161 | return retLine;
|
---|
162 | }
|
---|
163 |
|
---|
164 | public boolean isArea(Way w)
|
---|
165 | {
|
---|
166 | StyleSet ss = getStyleSet(null, false);
|
---|
167 | if(ss != null && w.keys != null)
|
---|
168 | {
|
---|
169 | Iterator<String> iterator = w.keys.keySet().iterator();
|
---|
170 | while(iterator.hasNext())
|
---|
171 | {
|
---|
172 | String key = iterator.next();
|
---|
173 | String val = w.keys.get(key);
|
---|
174 | if(ss.areas.containsKey("n" + key + "=" + val)
|
---|
175 | || ss.areas.containsKey("n" + key + "=" + OsmUtils.getNamedOsmBoolean(val))
|
---|
176 | || ss.areas.containsKey("x" + key))
|
---|
177 | return true;
|
---|
178 | }
|
---|
179 | }
|
---|
180 | return false;
|
---|
181 | }
|
---|
182 | public boolean hasAreas()
|
---|
183 | {
|
---|
184 | StyleSet ss = getStyleSet(null, false);
|
---|
185 | return ss != null && ss.areas.size() > 0;
|
---|
186 | }
|
---|
187 | }
|
---|