1 | package org.openstreetmap.josm.gui.mappaint;
|
---|
2 |
|
---|
3 | import java.awt.Toolkit;
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileReader;
|
---|
6 | import java.net.URL;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.Iterator;
|
---|
9 |
|
---|
10 | import javax.swing.ImageIcon;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.gui.mappaint.ElemStyles;
|
---|
15 | import org.xml.sax.InputSource;
|
---|
16 | import org.xml.sax.XMLReader;
|
---|
17 | import org.xml.sax.helpers.XMLReaderFactory;
|
---|
18 |
|
---|
19 | public class MapPaintStyles {
|
---|
20 |
|
---|
21 | private static String styleDir;
|
---|
22 | private static String imageDir;
|
---|
23 | private static String internalImageDir;
|
---|
24 | private static Boolean isInternal = false;
|
---|
25 | private static ElemStyles styles = new ElemStyles();
|
---|
26 |
|
---|
27 | public static ElemStyles getStyles()
|
---|
28 | {
|
---|
29 | return styles;
|
---|
30 | }
|
---|
31 |
|
---|
32 | public static ImageIcon getIcon(String name)
|
---|
33 | {
|
---|
34 | try {
|
---|
35 | if(isInternal)
|
---|
36 | {
|
---|
37 | String imageFile = imageDir+name;
|
---|
38 | File f = new File(imageFile);
|
---|
39 | if(f.exists())
|
---|
40 | {
|
---|
41 | //open icon from user directory
|
---|
42 | return new ImageIcon(imageFile);
|
---|
43 | }
|
---|
44 | }
|
---|
45 | URL path = Main.class.getResource(internalImageDir+name);
|
---|
46 | if(path == null)
|
---|
47 | {
|
---|
48 | System.out.println("Mappaint: Icon " + name + " not found, using default icon");
|
---|
49 | path = Main.class.getResource(internalImageDir+"misc/no_icon.png");
|
---|
50 | }
|
---|
51 | return new ImageIcon(Toolkit.getDefaultToolkit().createImage(path));
|
---|
52 | }
|
---|
53 | catch (Exception e)
|
---|
54 | {
|
---|
55 | URL path = Main.class.getResource(internalImageDir+"incomming/amenity.png");
|
---|
56 | return new ImageIcon(Toolkit.getDefaultToolkit().createImage(path));
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | public static void readFromPreferences() {
|
---|
61 | String styleName = Main.pref.get("mappaint.style", "standard");
|
---|
62 | // fallback to standard name for internal case, as we only have one internal style
|
---|
63 | String internalStyleName = "standard";
|
---|
64 | styleDir = Main.pref.get("mappaint.styledir", Main.pref.getPreferencesDir()+"plugins/mappaint/"+styleName+"/");
|
---|
65 | String elemStylesFile = styleDir+"elemstyles.xml";
|
---|
66 | imageDir = styleDir+"icons/";
|
---|
67 | internalImageDir = "/images/styles/"+internalStyleName+"/";
|
---|
68 |
|
---|
69 | // System.out.println("mappaint: Using style: " + styleName);
|
---|
70 | // System.out.println("mappaint: Using style dir: " + styleDir);
|
---|
71 | // System.out.println("mappaint: Using style file: " + elemStylesFile);
|
---|
72 |
|
---|
73 | File f = new File(elemStylesFile);
|
---|
74 | if (f.exists())
|
---|
75 | {
|
---|
76 | try// reading file from file system
|
---|
77 | {
|
---|
78 | // System.out.println("mappaint: Using style file: \"" + f + "\"");
|
---|
79 | XMLReader xmlReader = XMLReaderFactory.createXMLReader();
|
---|
80 | ElemStyleHandler handler = new ElemStyleHandler();
|
---|
81 | xmlReader.setContentHandler(handler);
|
---|
82 | xmlReader.setErrorHandler(handler);
|
---|
83 | // temporary only!
|
---|
84 | xmlReader.parse(new InputSource(new FileReader(f)));
|
---|
85 | }
|
---|
86 | catch (Exception e)
|
---|
87 | {
|
---|
88 | throw new RuntimeException(e);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | else {// reading the builtin file from the plugin jar file
|
---|
92 | URL elemStylesPath = Main.class.getResource("/styles/"+internalStyleName+"/elemstyles.xml");
|
---|
93 |
|
---|
94 | // System.out.println("mappaint: Using jar's elemstyles.xml: \"" + elemStylesPath + "\"");
|
---|
95 | if (elemStylesPath != null)
|
---|
96 | {
|
---|
97 | isInternal = true;
|
---|
98 | try
|
---|
99 | {
|
---|
100 | XMLReader xmlReader = XMLReaderFactory.createXMLReader();
|
---|
101 | ElemStyleHandler handler = new ElemStyleHandler();
|
---|
102 | xmlReader.setContentHandler(handler);
|
---|
103 | xmlReader.setErrorHandler(handler);
|
---|
104 | // temporary only!
|
---|
105 | xmlReader.parse(new InputSource(elemStylesPath.openStream()));
|
---|
106 | }
|
---|
107 | catch (Exception e)
|
---|
108 | {
|
---|
109 | throw new RuntimeException(e);
|
---|
110 | }
|
---|
111 | } else {
|
---|
112 | System.out.println("mappaint: Couldn't find style: \"" + styleDir + "elemstyles.xml\"");
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | }
|
---|