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