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