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