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

Last change on this file since 2839 was 2839, checked in by framm, 14 years ago

fix messages

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1package org.openstreetmap.josm.gui.mappaint;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.io.IOException;
6import java.util.Collection;
7import java.util.Collections;
8import java.util.LinkedList;
9import java.util.List;
10
11import javax.swing.ImageIcon;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.io.MirroredInputStream;
15import org.openstreetmap.josm.tools.ImageProvider;
16import org.xml.sax.InputSource;
17import org.xml.sax.SAXException;
18import org.xml.sax.XMLReader;
19import org.xml.sax.helpers.XMLReaderFactory;
20
21public 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}
Note: See TracBrowser for help on using the repository browser.