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