source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyleHandler.java@ 2675

Last change on this file since 2675 was 2675, checked in by jttt, 14 years ago

MapPaintVisitor - delegate drawing to styles, MapPaintVisitor should only select correct style and then let primitives draw in correct order. (not finished yet)

  • Property svn:eol-style set to native
File size: 11.0 KB
Line 
1package org.openstreetmap.josm.gui.mappaint;
2
3import java.awt.Color;
4import java.util.Collection;
5import java.util.LinkedList;
6
7import javax.swing.ImageIcon;
8
9import org.openstreetmap.josm.Main;
10import org.openstreetmap.josm.tools.ColorHelper;
11import org.xml.sax.Attributes;
12import org.xml.sax.helpers.DefaultHandler;
13
14public class ElemStyleHandler extends DefaultHandler
15{
16 boolean inDoc, inRule, inCondition, inLine, inLineMod, inIcon, inArea, inScaleMax, inScaleMin;
17 boolean hadLine, hadLineMod, hadIcon, hadArea;
18 ElemStyles styles;
19 String styleName;
20 RuleElem rule = new RuleElem();
21
22 static class RuleElem {
23 Rule rule = new Rule();
24 Collection<Rule> rules;
25 long scaleMax;
26 long scaleMin;
27 LineElemStyle line = new LineElemStyle();
28 LineElemStyle linemod = new LineElemStyle();
29 AreaElemStyle area = new AreaElemStyle();
30 IconElemStyle icon = new IconElemStyle();
31 public void init()
32 {
33 rules = null;
34 scaleMax = 1000000000;
35 scaleMin = 0;
36 line.init();
37 rule.init();
38 linemod.init();
39 area.init();
40 icon.init();
41 }
42 }
43
44 public ElemStyleHandler(String name) {
45 styleName = name;
46 inDoc=inRule=inCondition=inLine=inIcon=inArea=false;
47 rule.init();
48 styles = MapPaintStyles.getStyles();
49 }
50
51 Color convertColor(String colString)
52 {
53 int i = colString.indexOf("#");
54 Color ret;
55 if(i < 0) {
56 ret = Main.pref.getColor("mappaint."+styleName+"."+colString, Color.red);
57 } else if(i == 0) {
58 ret = ColorHelper.html2color(colString);
59 } else {
60 ret = Main.pref.getColor("mappaint."+styleName+"."+colString.substring(0,i),
61 ColorHelper.html2color(colString.substring(i)));
62 }
63 return ret;
64 }
65
66 @Override public void startDocument() {
67 inDoc = true;
68 }
69
70 @Override public void endDocument() {
71 inDoc = false;
72 }
73
74 private void error(String message) {
75 System.out.println(styleName + " (" + rule.rule.key + "=" + rule.rule.value + "): " + message);
76 }
77
78 private void startElementLine(String qName, Attributes atts, LineElemStyle line) {
79 for (int count=0; count<atts.getLength(); count++)
80 {
81 if(atts.getQName(count).equals("width"))
82 {
83 String val = atts.getValue(count);
84 if(val.startsWith("+"))
85 {
86 line.setWidth(Integer.parseInt(val.substring(1)));
87 line.widthMode = LineElemStyle.WidthMode.OFFSET;
88 }
89 else if(val.startsWith("-"))
90 {
91 line.setWidth(Integer.parseInt(val));
92 line.widthMode = LineElemStyle.WidthMode.OFFSET;
93 }
94 else if(val.endsWith("%"))
95 {
96 line.setWidth(Integer.parseInt(val.substring(0, val.length()-1)));
97 line.widthMode = LineElemStyle.WidthMode.PERCENT;
98 } else {
99 line.setWidth(Integer.parseInt(val));
100 }
101 }
102 else if (atts.getQName(count).equals("colour")) {
103 line.color=convertColor(atts.getValue(count));
104 } else if (atts.getQName(count).equals("realwidth")) {
105 line.realWidth=Integer.parseInt(atts.getValue(count));
106 } else if (atts.getQName(count).equals("dashed")) {
107 float[] dashed;
108 try {
109 String[] parts = atts.getValue(count).split(",");
110 dashed = new float[parts.length];
111 for (int i = 0; i < parts.length; i++) {
112 dashed[i] = (Integer.parseInt(parts[i]));
113 }
114 } catch (NumberFormatException nfe) {
115 boolean isDashed = Boolean.parseBoolean(atts.getValue(count));
116 if(isDashed) {
117 dashed = new float[]{9};
118 } else {
119 dashed = new float[0];
120 }
121 }
122 line.setDashed(dashed);
123 } else if (atts.getQName(count).equals("dashedcolour")) {
124 line.dashedColor=convertColor(atts.getValue(count));
125 } else if(atts.getQName(count).equals("priority")) {
126 line.priority = Integer.parseInt(atts.getValue(count));
127 } else if(atts.getQName(count).equals("mode")) {
128 line.over = !atts.getValue(count).equals("under");
129 } else {
130 error("The element \"" + qName + "\" has unknown attribute \"" + atts.getQName(count) + "\"!");
131 }
132 }
133 }
134
135 @Override public void startElement(String uri,String name, String qName, Attributes atts) {
136 if (inDoc==true)
137 {
138 if (qName.equals("rule")) {
139 inRule=true;
140 } else if (qName.equals("rules"))
141 {
142 if(styleName == null)
143 {
144 String n = atts.getValue("name");
145 if(n == null) {
146 n = "standard";
147 }
148 styleName = n;
149 }
150 }
151 else if (qName.equals("scale_max")) {
152 inScaleMax = true;
153 } else if (qName.equals("scale_min")) {
154 inScaleMin = true;
155 } else if (qName.equals("condition") && inRule)
156 {
157 inCondition=true;
158 Rule r = rule.rule;
159 if(r.key != null)
160 {
161 if(rule.rules == null) {
162 rule.rules = new LinkedList<Rule>();
163 }
164 rule.rules.add(new Rule(rule.rule));
165 r = new Rule();
166 rule.rules.add(r);
167 }
168 for (int count=0; count<atts.getLength(); count++)
169 {
170 if(atts.getQName(count).equals("k")) {
171 r.key = atts.getValue(count);
172 } else if(atts.getQName(count).equals("v")) {
173 r.value = atts.getValue(count);
174 } else if(atts.getQName(count).equals("b")) {
175 r.boolValue = atts.getValue(count);
176 } else {
177 error("The element \"" + qName + "\" has unknown attribute \"" + atts.getQName(count) + "\"!");
178 }
179 }
180 if(r.key == null) {
181 error("The condition has no key!");
182 }
183 }
184 else if (qName.equals("line"))
185 {
186 hadLine = inLine = true;
187 startElementLine(qName, atts, rule.line);
188 if(rule.line.widthMode != LineElemStyle.WidthMode.ABSOLUTE) {
189 error("Relative widths are not possible for normal lines");
190 rule.line.widthMode = LineElemStyle.WidthMode.ABSOLUTE;
191 }
192 }
193 else if (qName.equals("linemod"))
194 {
195 hadLineMod = inLineMod = true;
196 startElementLine(qName, atts, rule.linemod);
197 }
198 else if (qName.equals("icon"))
199 {
200 inIcon = true;
201 for (int count=0; count<atts.getLength(); count++)
202 {
203 if (atts.getQName(count).equals("src")) {
204 ImageIcon icon = MapPaintStyles.getIcon(atts.getValue(count), styleName);
205 hadIcon = (icon != null);
206 rule.icon.icon = icon;
207 } else if (atts.getQName(count).equals("annotate")) {
208 rule.icon.annotate = Boolean.parseBoolean (atts.getValue(count));
209 } else if(atts.getQName(count).equals("priority")) {
210 rule.icon.priority = Integer.parseInt(atts.getValue(count));
211 } else {
212 error("The element \"" + qName + "\" has unknown attribute \"" + atts.getQName(count) + "\"!");
213 }
214 }
215 }
216 else if (qName.equals("area"))
217 {
218 hadArea = inArea = true;
219 for (int count=0; count<atts.getLength(); count++)
220 {
221 if (atts.getQName(count).equals("colour")) {
222 rule.area.color=convertColor(atts.getValue(count));
223 } else if (atts.getQName(count).equals("closed")) {
224 rule.area.closed=Boolean.parseBoolean(atts.getValue(count));
225 } else if(atts.getQName(count).equals("priority")) {
226 rule.area.priority = Integer.parseInt(atts.getValue(count));
227 } else {
228 error("The element \"" + qName + "\" has unknown attribute \"" + atts.getQName(count) + "\"!");
229 }
230 }
231 } else {
232 error("The element \"" + qName + "\" is unknown!");
233 }
234 }
235 }
236
237 @Override public void endElement(String uri,String name, String qName)
238 {
239 if (inRule && qName.equals("rule"))
240 {
241 if(hadLine)
242 {
243 styles.add(styleName, rule.rule, rule.rules,
244 new LineElemStyle(rule.line, rule.scaleMax, rule.scaleMin));
245 }
246 if(hadLineMod)
247 {
248 styles.addModifier(styleName, rule.rule, rule.rules,
249 new LineElemStyle(rule.linemod, rule.scaleMax, rule.scaleMin));
250 }
251 if(hadIcon)
252 {
253 styles.add(styleName, rule.rule, rule.rules,
254 new IconElemStyle(rule.icon, rule.scaleMax, rule.scaleMin));
255 }
256 if(hadArea)
257 {
258 styles.add(styleName, rule.rule, rule.rules,
259 new AreaElemStyle(rule.area, rule.scaleMax, rule.scaleMin));
260 }
261 inRule = false;
262 hadLine = hadLineMod = hadIcon = hadArea = false;
263 rule.init();
264 }
265 else if (inCondition && qName.equals("condition")) {
266 inCondition = false;
267 } else if (inLine && qName.equals("line")) {
268 inLine = false;
269 } else if (inLineMod && qName.equals("linemod")) {
270 inLineMod = false;
271 } else if (inIcon && qName.equals("icon")) {
272 inIcon = false;
273 } else if (inArea && qName.equals("area")) {
274 inArea = false;
275 } else if (qName.equals("scale_max")) {
276 inScaleMax = false;
277 } else if (qName.equals("scale_min")) {
278 inScaleMin = false;
279 }
280 }
281
282 @Override public void characters(char ch[], int start, int length)
283 {
284 if (inScaleMax == true) {
285 rule.scaleMax = Long.parseLong(new String(ch, start, length));
286 } else if (inScaleMin == true) {
287 rule.scaleMin = Long.parseLong(new String(ch, start, length));
288 }
289 }
290}
Note: See TracBrowser for help on using the repository browser.