source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java@ 3803

Last change on this file since 3803 was 3803, checked in by bastiK, 13 years ago

Don't merge all mappaitn style rules into one StyleSet, but keep them as separate StyleSources. This allows switching styles on and off at runtime.

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