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