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

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

added much improved preferences for external styles and presets

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