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

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

allow configurable mappaint colors

  • Property svn:eol-style set to native
File size: 6.0 KB
Line 
1package org.openstreetmap.josm.gui.mappaint;
2
3import java.awt.Color;
4import java.awt.Toolkit;
5import java.io.File;
6import java.net.URL;
7
8import javax.swing.ImageIcon;
9
10import org.openstreetmap.josm.tools.ColorHelper;
11import org.openstreetmap.josm.Main;
12import org.xml.sax.Attributes;
13import org.xml.sax.helpers.DefaultHandler;
14
15public class ElemStyleHandler extends DefaultHandler
16{
17 boolean inDoc, inRule, inCondition, inElemStyle, inLine, inIcon, inArea, inScaleMax, inScaleMin;
18 String curKey = null;
19 String curValue = null;
20 String curBoolean = null;
21 int curLineWidth = -1;
22 int curLineRealWidth = 0;
23 boolean curLineDashed = false;
24 Color curLineColour = null;
25 Color curAreaColour = null;
26 ImageIcon curIcon = null;
27 boolean curIconAnnotate = true;
28 long curScaleMax = 1000000000;
29 long curScaleMin = 0;
30
31 public ElemStyleHandler() {
32 inDoc=inRule=inCondition=inElemStyle=inLine=inIcon=inArea=false;
33 }
34
35 Color convertColor(String colString)
36 {
37 int i = colString.indexOf("#");
38 String colorString;
39 if(i < 0) // name only
40 colorString = Main.pref.get("color.mappaint."+colString);
41 else if(i == 0) // value only
42 colorString = colString;
43 else // value and name
44 colorString = Main.pref.get("color.mappaint."+colString.substring(0,i), colString.substring(i));
45 return ColorHelper.html2color(colorString);
46 }
47
48 @Override public void startDocument() {
49 inDoc = true;
50 }
51
52 @Override public void endDocument() {
53 inDoc = false;
54 }
55
56 @Override public void startElement(String uri,String name, String qName,
57 Attributes atts) {
58 if (inDoc==true) {
59 if (qName.equals("rule")) {
60 inRule=true;
61 }
62 else if (qName.equals("condition") && inRule) {
63 inCondition=true;
64 for (int count=0; count<atts.getLength(); count++) {
65 if(atts.getQName(count).equals("k"))
66 {
67 curKey = atts.getValue(count);
68 curBoolean = null;
69 curValue = null;
70 }
71 else if(atts.getQName(count).equals("v"))
72 curValue = atts.getValue(count);
73 else if(atts.getQName(count).equals("b"))
74 curBoolean = atts.getValue(count);
75 }
76 } else if (qName.equals("line")) {
77 inLine = true;
78 for (int count=0; count<atts.getLength(); count++) {
79 if(atts.getQName(count).equals("width"))
80 curLineWidth = Integer.parseInt(atts.getValue(count));
81 else if (atts.getQName(count).equals("colour"))
82 curLineColour=convertColor(atts.getValue(count));
83 else if (atts.getQName(count).equals("realwidth"))
84 curLineRealWidth=Integer.parseInt(atts.getValue(count));
85 else if (atts.getQName(count).equals("dashed"))
86 curLineDashed=Boolean.parseBoolean(atts.getValue(count));
87 }
88 } else if (qName.equals("scale_max")) {
89 inScaleMax = true;
90 } else if (qName.equals("scale_min")) {
91 inScaleMin = true;
92 } else if (qName.equals("icon")) {
93 inIcon = true;
94 for (int count=0; count<atts.getLength(); count++) {
95 if (atts.getQName(count).equals("src")) {
96 if(!MapPaintStyles.isInternal())
97 {
98 String imageFile = MapPaintStyles.getImageDir()+atts.getValue(count);
99 File f = new File(imageFile);
100 if (f.exists()) {
101 //open icon from user directory
102 curIcon = new ImageIcon(imageFile);
103 continue;
104 }
105 }
106 try {
107 URL path = getClass().getResource(MapPaintStyles.getInternalImageDir()+atts.getValue(count));
108 if (path == null) {
109 /* icon not found, using default */
110 System.out.println("Mappaint: Icon " + atts.getValue(count) + " not found, using default icon");
111 path = getClass().getResource(MapPaintStyles.getInternalImageDir()+"misc/no_icon.png");
112 curIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(path));
113 } else {
114 curIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(path));
115 }
116 }
117 catch (Exception e){
118 URL path = getClass().getResource(MapPaintStyles.getInternalImageDir()+"incomming/amenity.png");
119 curIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(path));
120 }
121 } else if (atts.getQName(count).equals("annotate")) {
122 curIconAnnotate = Boolean.parseBoolean (atts.getValue(count));
123 }
124 }
125 }
126 else if (qName.equals("area"))
127 {
128 inArea = true;
129 for (int count=0; count<atts.getLength(); count++)
130 {
131 if (atts.getQName(count).equals("colour"))
132 curAreaColour=convertColor(atts.getValue(count));
133 }
134 }
135 }
136 }
137
138 @Override public void endElement(String uri,String name, String qName)
139 {
140 if (inRule && qName.equals("rule")) {
141 ElemStyle newStyle;
142 inRule = false;
143 if (curLineWidth != -1) {
144 newStyle = new LineElemStyle(curLineWidth, curLineRealWidth, curLineColour,
145 curLineDashed, curScaleMax, curScaleMin);
146 MapPaintStyles.add(curKey, curValue, curBoolean, newStyle);
147 curLineWidth = -1;
148 curLineRealWidth= 0;
149 curLineDashed = false;
150 curLineColour = null;
151 }
152
153 if (curIcon != null) {
154 newStyle = new IconElemStyle(curIcon, curIconAnnotate, curScaleMax, curScaleMin);
155 MapPaintStyles.add(curKey, curValue, curBoolean, newStyle);
156 curIcon = null;
157 curIconAnnotate = true;
158 }
159 if (curAreaColour != null) {
160 newStyle = new AreaElemStyle (curAreaColour, curScaleMax, curScaleMin);
161 MapPaintStyles.add(curKey, curValue, curBoolean, newStyle);
162 curAreaColour = null;
163 }
164 curScaleMax = 1000000000;
165 curScaleMin = 0;
166
167 }
168 else if (inCondition && qName.equals("condition"))
169 inCondition = false;
170 else if (inLine && qName.equals("line"))
171 inLine = false;
172 else if (inIcon && qName.equals("icon"))
173 inIcon = false;
174 else if (inArea && qName.equals("area"))
175 inArea = false;
176 else if (qName.equals("scale_max"))
177 inScaleMax = false;
178 else if (qName.equals("scale_min"))
179 inScaleMin = false;
180 }
181
182 @Override public void characters(char ch[], int start, int length) {
183 if (inScaleMax == true) {
184 String content = new String(ch, start, length);
185 curScaleMax = Long.parseLong(content);
186 }
187 if (inScaleMin == true) {
188 String content = new String(ch, start, length);
189 curScaleMin = Long.parseLong(content);
190 }
191 }
192}
193
194
195
196
Note: See TracBrowser for help on using the repository browser.