1 | package org.openstreetmap.josm.gui.mappaint;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.io.InputStreamReader;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.LinkedList;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import javax.swing.ImageIcon;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.io.MirroredInputStream;
|
---|
18 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
19 | import org.openstreetmap.josm.tools.XmlObjectParser;
|
---|
20 | import org.xml.sax.SAXException;
|
---|
21 | import org.xml.sax.SAXParseException;
|
---|
22 |
|
---|
23 | public class MapPaintStyles {
|
---|
24 |
|
---|
25 | private static ElemStyles styles = new ElemStyles();
|
---|
26 | private static Collection<String> iconDirs;
|
---|
27 | private static File zipIcons;
|
---|
28 |
|
---|
29 | public static ElemStyles getStyles()
|
---|
30 | {
|
---|
31 | return styles;
|
---|
32 | }
|
---|
33 |
|
---|
34 | public static ImageIcon getIcon(String name, String styleName)
|
---|
35 | {
|
---|
36 | List<String> dirs = new LinkedList<String>();
|
---|
37 | for(String fileset : iconDirs)
|
---|
38 | {
|
---|
39 | String[] a;
|
---|
40 | if(fileset.indexOf("=") >= 0) {
|
---|
41 | a = fileset.split("=", 2);
|
---|
42 | } else {
|
---|
43 | a = new String[] {"", fileset};
|
---|
44 | }
|
---|
45 |
|
---|
46 | /* non-prefixed path is generic path, always take it */
|
---|
47 | if(a[0].length() == 0 || styleName.equals(a[0])) {
|
---|
48 | dirs.add(a[1]);
|
---|
49 | }
|
---|
50 | }
|
---|
51 | ImageIcon i = ImageProvider.getIfAvailable(dirs, "mappaint."+styleName, null, name, zipIcons);
|
---|
52 | if(i == null)
|
---|
53 | {
|
---|
54 | System.out.println("Mappaint style \""+styleName+"\" icon \"" + name + "\" not found.");
|
---|
55 | i = ImageProvider.getIfAvailable(dirs, "mappaint."+styleName, null, "misc/no_icon.png");
|
---|
56 | }
|
---|
57 | return i;
|
---|
58 | }
|
---|
59 |
|
---|
60 | @SuppressWarnings("null")
|
---|
61 | public static void readFromPreferences() {
|
---|
62 | iconDirs = Main.pref.getCollection("mappaint.icon.sources", Collections.<String>emptySet());
|
---|
63 | if(Main.pref.getBoolean("mappaint.icon.enable-defaults", true))
|
---|
64 | {
|
---|
65 | LinkedList<String> f = new LinkedList<String>(iconDirs);
|
---|
66 | /* don't prefix icon path, as it should be generic */
|
---|
67 | f.add("resource://images/styles/standard/");
|
---|
68 | f.add("resource://images/styles/");
|
---|
69 | iconDirs = f;
|
---|
70 | }
|
---|
71 |
|
---|
72 | Collection<String> files = Main.pref.getCollection("mappaint.style.sources", Collections.<String>emptySet());
|
---|
73 | if (Main.pref.getBoolean("mappaint.style.enable-defaults", true)) {
|
---|
74 | LinkedList<String> f = new LinkedList<String>();
|
---|
75 | f.add("resource://data/elemstyles.xml");
|
---|
76 | f.addAll(files);
|
---|
77 | files = f;
|
---|
78 | }
|
---|
79 |
|
---|
80 | for (String file : files) {
|
---|
81 | String[] a = null;
|
---|
82 | try {
|
---|
83 | if (file.indexOf("=") >= 0) {
|
---|
84 | a = file.split("=", 2);
|
---|
85 | } else {
|
---|
86 | a = new String[] { null, file };
|
---|
87 | }
|
---|
88 | XmlObjectParser parser = new XmlObjectParser(new ElemStyleHandler(a[0]));
|
---|
89 | MirroredInputStream in = new MirroredInputStream(a[1]);
|
---|
90 | InputStream zip = in.getZipEntry("xml","style");
|
---|
91 | InputStreamReader ins;
|
---|
92 | if(zip != null)
|
---|
93 | {
|
---|
94 | zipIcons = in.getFile();
|
---|
95 | ins = new InputStreamReader(zip);
|
---|
96 | } else {
|
---|
97 | ins = new InputStreamReader(in);
|
---|
98 | }
|
---|
99 | parser.startWithValidation(ins, "http://josm.openstreetmap.de/mappaint-style-1.0",
|
---|
100 | "resource://data/mappaint-style.xsd");
|
---|
101 | while(parser.hasNext()) {
|
---|
102 | }
|
---|
103 | } catch(IOException e) {
|
---|
104 | System.err.println(tr("Warning: failed to load Mappaint styles from ''{0}''. Exception was: {1}", a[1], e.toString()));
|
---|
105 | e.printStackTrace();
|
---|
106 | } catch(SAXParseException e) {
|
---|
107 | System.err.println(tr("Warning: failed to parse Mappaint styles from ''{0}''. Error was: [{1}:{2}] {3}", a[1], e.getLineNumber(), e.getColumnNumber(), e.getMessage()));
|
---|
108 | e.printStackTrace();
|
---|
109 | } catch(SAXException e) {
|
---|
110 | System.err.println(tr("Warning: failed to parse Mappaint styles from ''{0}''. Error was: {1}", a[1], e.getMessage()));
|
---|
111 | e.printStackTrace();
|
---|
112 | }
|
---|
113 | }
|
---|
114 | iconDirs = null;
|
---|
115 | zipIcons = null;
|
---|
116 | }
|
---|
117 | }
|
---|