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

Last change on this file since 627 was 627, checked in by framm, 16 years ago
  • Property svn:eol-style set to native
File size: 5.4 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.xml.sax.Attributes;
12import org.xml.sax.helpers.DefaultHandler;
13
14public class ElemStyleHandler extends DefaultHandler
15{
16 boolean inDoc, inRule, inCondition, inElemStyle, inLine, inIcon, inArea, inScaleMax, inScaleMin;
17 String curKey = null;
18 String curValue = null;
19 int curLineWidth = -1;
20 int curLineRealWidth = 0;
21 boolean curLineDashed = false;
22 Color curLineColour = null;
23 Color curAreaColour = null;
24 ImageIcon curIcon = null;
25 boolean curIconAnnotate = true;
26 long curScaleMax = 1000000000;
27 long curScaleMin = 0;
28
29 public ElemStyleHandler() {
30 inDoc=inRule=inCondition=inElemStyle=inLine=inIcon=inArea=false;
31 }
32
33 /*
34 ElemStyles getElemStyles()
35 {
36 return styles;
37 }
38 */
39
40 @Override public void startDocument() {
41 inDoc = true;
42 }
43
44 @Override public void endDocument() {
45 inDoc = false;
46 }
47
48 @Override public void startElement(String uri,String name, String qName,
49 Attributes atts) {
50 if (inDoc==true) {
51 if (qName.equals("rule")) {
52 inRule=true;
53 }
54 else if (qName.equals("condition") && inRule) {
55 inCondition=true;
56 for (int count=0; count<atts.getLength(); count++) {
57 if(atts.getQName(count).equals("k"))
58 curKey = atts.getValue(count);
59 else if(atts.getQName(count).equals("v"))
60 curValue = atts.getValue(count);
61 }
62 } else if (qName.equals("line")) {
63 inLine = true;
64 for (int count=0; count<atts.getLength(); count++) {
65 if(atts.getQName(count).equals("width"))
66 curLineWidth = Integer.parseInt(atts.getValue(count));
67 else if (atts.getQName(count).equals("colour"))
68 curLineColour=ColorHelper.html2color(atts.getValue(count));
69 else if (atts.getQName(count).equals("realwidth"))
70 curLineRealWidth=Integer.parseInt(atts.getValue(count));
71 else if (atts.getQName(count).equals("dashed"))
72 curLineDashed=Boolean.parseBoolean(atts.getValue(count));
73 }
74 } else if (qName.equals("scale_max")) {
75 inScaleMax = true;
76 } else if (qName.equals("scale_min")) {
77 inScaleMin = true;
78 } else if (qName.equals("icon")) {
79 inIcon = true;
80 for (int count=0; count<atts.getLength(); count++) {
81 if (atts.getQName(count).equals("src")) {
82 String imageFile = MapPaintStyles.getStyleDir()+"icons/"+atts.getValue(count);
83 File f = new File(imageFile);
84 if (f.exists()) {
85 //open icon from user directory
86 curIcon = new ImageIcon(imageFile);
87 } else {
88 try {
89 URL path = getClass().getResource("/styles/standard/icons/"+atts.getValue(count));
90 if (path == null) {
91 /* icon not found, using default */
92 System.out.println("Mappaint: Icon " + atts.getValue(count) + " not found, using default icon");
93 path = getClass().getResource("/styles/standard/icons/misc/no_icon.png");
94 curIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(path));
95 } else {
96 curIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(path));
97 }
98 }
99 catch (Exception e){
100 URL path = getClass().getResource("/styles/standard/icons/amenity.png");
101 curIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(path));
102 }
103 }
104 } else if (atts.getQName(count).equals("annotate")) {
105 curIconAnnotate = Boolean.parseBoolean (atts.getValue(count));
106 }
107 }
108 }
109 else if (qName.equals("area"))
110 {
111 inArea = true;
112 for (int count=0; count<atts.getLength(); count++)
113 {
114 if (atts.getQName(count).equals("colour"))
115 curAreaColour=ColorHelper.html2color(atts.getValue(count));
116 }
117 }
118 }
119 }
120
121 @Override public void endElement(String uri,String name, String qName)
122 {
123 if (inRule && qName.equals("rule")) {
124 ElemStyle newStyle;
125 inRule = false;
126 if (curLineWidth != -1) {
127 newStyle = new LineElemStyle(curLineWidth, curLineRealWidth, curLineColour,
128 curLineDashed, curScaleMax, curScaleMin);
129 MapPaintStyles.add(curKey, curValue, newStyle);
130 curLineWidth = -1;
131 curLineRealWidth= 0;
132 curLineDashed = false;
133 curLineColour = null;
134 }
135
136 if (curIcon != null) {
137 newStyle = new IconElemStyle(curIcon, curIconAnnotate, curScaleMax, curScaleMin);
138 MapPaintStyles.add(curKey, curValue, newStyle);
139 curIcon = null;
140 curIconAnnotate = true;
141 }
142 if (curAreaColour != null) {
143 newStyle = new AreaElemStyle (curAreaColour, curScaleMax, curScaleMin);
144 MapPaintStyles.add(curKey, curValue, newStyle);
145 curAreaColour = null;
146 }
147 curScaleMax = 1000000000;
148 curScaleMin = 0;
149
150 }
151 else if (inCondition && qName.equals("condition"))
152 inCondition = false;
153 else if (inLine && qName.equals("line"))
154 inLine = false;
155 else if (inIcon && qName.equals("icon"))
156 inIcon = false;
157 else if (inArea && qName.equals("area"))
158 inArea = false;
159 else if (qName.equals("scale_max"))
160 inScaleMax = false;
161 else if (qName.equals("scale_min"))
162 inScaleMin = false;
163 }
164
165 @Override public void characters(char ch[], int start, int length) {
166 if (inScaleMax == true) {
167 String content = new String(ch, start, length);
168 curScaleMax = Long.parseLong(content);
169 }
170 if (inScaleMin == true) {
171 String content = new String(ch, start, length);
172 curScaleMin = Long.parseLong(content);
173 }
174 }
175}
176
177
178
179
Note: See TracBrowser for help on using the repository browser.