Changeset 991 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2008-09-19T08:33:02+02:00 (16 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyleHandler.java
r990 r991 150 150 { 151 151 if (atts.getQName(count).equals("src")) 152 rule.icon.icon = MapPaintStyles.getIcon(atts.getValue(count) );152 rule.icon.icon = MapPaintStyles.getIcon(atts.getValue(count), styleName); 153 153 else if (atts.getQName(count).equals("annotate")) 154 154 rule.icon.annotate = Boolean.parseBoolean (atts.getValue(count)); -
trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java
r987 r991 6 6 import java.net.URL; 7 7 import java.util.HashMap; 8 import java.util.LinkedList; 9 import java.util.List; 8 10 import java.util.Iterator; 9 11 … … 13 15 import org.openstreetmap.josm.data.osm.OsmPrimitive; 14 16 import org.openstreetmap.josm.gui.mappaint.ElemStyles; 17 import org.openstreetmap.josm.io.MirroredInputStream; 18 import org.openstreetmap.josm.tools.ImageProvider; 15 19 import org.xml.sax.InputSource; 16 20 import org.xml.sax.XMLReader; … … 19 23 public class MapPaintStyles { 20 24 21 private static String styleDir;22 private static String imageDir;23 private static String internalImageDir;24 private static Boolean isInternal = false;25 25 private static ElemStyles styles = new ElemStyles(); 26 private static String iconDirs; 26 27 27 28 public static ElemStyles getStyles() … … 30 31 } 31 32 32 public static ImageIcon getIcon(String name )33 public static ImageIcon getIcon(String name, String styleName) 33 34 { 34 try { 35 if(isInternal) 36 { 37 String imageFile = imageDir+name; 38 File f = new File(imageFile); 39 if(f.exists()) 40 { 41 //open icon from user directory 42 return new ImageIcon(imageFile); 43 } 44 } 45 URL path = Main.class.getResource(internalImageDir+name); 46 if(path == null) 47 path = Main.class.getResource("/images/styles/"+name); 48 if(path == null) 49 { 50 System.out.println("Mappaint: Icon " + name + " not found, using default icon"); 51 path = Main.class.getResource(internalImageDir+"misc/no_icon.png"); 52 } 53 return new ImageIcon(Toolkit.getDefaultToolkit().createImage(path)); 35 List<String> dirs = new LinkedList<String>(); 36 for(String fileset : iconDirs.split(";")) 37 { 38 String[] a; 39 if(fileset.indexOf("=") >= 0) 40 a = fileset.split("=", 2); 41 else 42 a = new String[] {"", fileset}; 43 44 /* non-prefixed path is generic path, always take it */ 45 if(a[0].length() == 0 || styleName.equals(a[0])) 46 dirs.add(a[1]); 54 47 } 55 catch (Exception e) 48 ImageIcon i = ImageProvider.getIfAvailable(dirs, "mappaint."+styleName, null, name); 49 if(i == null) 56 50 { 57 URL path = Main.class.getResource(internalImageDir+"incomming/amenity.png");58 return new ImageIcon(Toolkit.getDefaultToolkit().createImage(path));51 System.out.println("Mappaint-Style \""+styleName+"\" icon \"" + name + "\" not found."); 52 i = ImageProvider.getIfAvailable(dirs, "mappaint."+styleName, null, "misc/no_icon.png"); 59 53 } 54 return i; 60 55 } 61 56 62 57 public static void readFromPreferences() { 63 String styleName = Main.pref.get("mappaint.style", "standard"); 64 // fallback to standard name for internal case, as we only have one internal style 65 String internalStyleName = "standard"; 66 styleDir = Main.pref.get("mappaint.styledir", Main.pref.getPreferencesDir()+"plugins/mappaint/"+styleName+"/"); 67 String elemStylesFile = styleDir+"elemstyles.xml"; 68 imageDir = styleDir+"icons/"; 69 internalImageDir = "/images/styles/"+internalStyleName+"/"; 58 /* don't prefix icon path, as it should be generic */ 59 String internalicon = "resource://images/styles/standard/;resource://images/styles/"; 60 String internalfile = "standard=resource://styles/standard/elemstyles.xml"; 70 61 71 // System.out.println("mappaint: Using style: " + styleName); 72 // System.out.println("mappaint: Using style dir: " + styleDir); 73 // System.out.println("mappaint: Using style file: " + elemStylesFile); 62 iconDirs = Main.pref.get("mappaint.iconpaths"); 63 iconDirs = iconDirs == null || iconDirs.length() == 0 ? internalicon : iconDirs + ";" + internalicon; 74 64 75 File f = new File(elemStylesFile); 76 if (f.exists()) 65 String file = Main.pref.get("mappaint.sources"); 66 file = file == null || file.length() == 0 ? internalfile : internalfile + ";" + file; 67 68 for(String fileset : file.split(";")) 77 69 { 78 try // reading file from file system70 try 79 71 { 80 // System.out.println("mappaint: Using style file: \"" + f + "\""); 72 String[] a; 73 if(fileset.indexOf("=") >= 0) 74 a = fileset.split("=", 2); 75 else 76 a = new String[] {"standard", fileset}; 81 77 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 82 ElemStyleHandler handler = new ElemStyleHandler( styleName);78 ElemStyleHandler handler = new ElemStyleHandler(a[0]); 83 79 xmlReader.setContentHandler(handler); 84 80 xmlReader.setErrorHandler(handler); 85 // temporary only! 86 xmlReader.parse(new InputSource(new FileReader(f))); 81 xmlReader.parse(new InputSource(new MirroredInputStream(a[1]))); 87 82 } 88 83 catch (Exception e) 89 84 { 90 throw new RuntimeException(e);85 System.out.println("Mappaint-Style problems: \"" + fileset + "\""); 91 86 } 92 87 } 93 else {// reading the builtin file from the plugin jar file 94 URL elemStylesPath = Main.class.getResource("/styles/"+internalStyleName+"/elemstyles.xml"); 95 96 // System.out.println("mappaint: Using jar's elemstyles.xml: \"" + elemStylesPath + "\""); 97 if (elemStylesPath != null) 98 { 99 isInternal = true; 100 try 101 { 102 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 103 ElemStyleHandler handler = new ElemStyleHandler(internalStyleName); 104 xmlReader.setContentHandler(handler); 105 xmlReader.setErrorHandler(handler); 106 // temporary only! 107 xmlReader.parse(new InputSource(elemStylesPath.openStream())); 108 } 109 catch (Exception e) 110 { 111 throw new RuntimeException(e); 112 } 113 } else { 114 System.out.println("mappaint: Couldn't find style: \"" + styleDir + "elemstyles.xml\""); 115 } 116 } 88 iconDirs = null; 117 89 } 118 90 -
trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java
r949 r991 405 405 */ 406 406 public void setIcon(String iconName) { 407 ImageIcon icon = ImageProvider.getIfAvailable(null, iconName); 407 String s = Main.pref.get("taggingpreset.iconpaths"); 408 ImageIcon icon = ImageProvider.getIfAvailable((s != null ? s.split(";") : null), "presets", null, iconName); 408 409 if (icon == null) 409 410 { -
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r627 r991 14 14 import java.net.MalformedURLException; 15 15 import java.net.URL; 16 import java.util.Arrays; 17 import java.util.Collection; 16 18 import java.util.HashMap; 17 19 import java.util.LinkedList; … … 63 65 } 64 66 67 public static ImageIcon getIfAvailable(String subdir, String name) 68 { 69 return getIfAvailable((Collection<String>)null, null, subdir, name); 70 } 71 public static final ImageIcon getIfAvailable(String[] dirs, String id, String subdir, String name) 72 { 73 return getIfAvailable(Arrays.asList(dirs), id, subdir, name); 74 } 75 65 76 /** 66 77 * Like {@link #get(String)}, but does not throw and return <code>null</code> 67 78 * in case of nothing is found. Use this, if the image to retrieve is optional. 68 79 */ 69 public static ImageIcon getIfAvailable(String subdir, String name) { 80 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name) 81 { 70 82 if (name == null) 71 83 return null; … … 76 88 String ext = name.indexOf('.') != -1 ? "" : ".png"; 77 89 String full_name = subdir+name+ext; 78 79 Image img = cache.get(full_name); 90 String cache_name = full_name; 91 /* cache separately */ 92 if(dirs != null && dirs.size() > 0) 93 cache_name = "id:"+id+":"+full_name; 94 95 Image img = cache.get(cache_name); 80 96 if (img == null) { 81 97 // getImageUrl() does a ton of "stat()" calls and gets expensive … … 84 100 // and don't bother to create a URL unless we're actually 85 101 // creating the image. 86 URL path = getImageUrl(full_name );102 URL path = getImageUrl(full_name, dirs); 87 103 if (path == null) 88 104 return null; 89 105 img = Toolkit.getDefaultToolkit().createImage(path); 90 cache.put( full_name, img);106 cache.put(cache_name, img); 91 107 } 92 108 … … 94 110 } 95 111 96 private static URL getImageUrl(String imageName) { 97 URL path = null; 98 // Try user-preference directory first 99 try { 100 if (new File(Main.pref.getPreferencesDir()+"images/"+imageName).exists()) 101 return new URL("file", "", Main.pref.getPreferencesDir()+"images/"+imageName); 102 } catch (MalformedURLException e) { 103 } 104 105 // Try plugins and josm classloader 106 for (ClassLoader source : sources) 107 if ((path = source.getResource("images/"+imageName)) != null) 108 return path; 109 110 // Try all other ressource directories 111 for (String location : Main.pref.getAllPossiblePreferenceDirs()) { 112 try { 113 if (new File(location+"images/"+imageName).exists()) 114 return new URL("file", "", location+"images/"+imageName); 115 } catch (MalformedURLException e) { 116 } 117 } 118 return null; 119 } 112 private static URL getImageUrl(String path, String name) 113 { 114 if(path.startsWith("resource://")) 115 { 116 String p = path.substring("resource://".length()); 117 for (ClassLoader source : sources) 118 { 119 URL res; 120 if ((res = source.getResource(p+name)) != null) 121 return res; 122 } 123 } 124 else 125 { 126 try { 127 File f = new File(path, name); 128 if(f.exists()) 129 return f.toURI().toURL(); 130 } catch (MalformedURLException e) {} 131 } 132 return null; 133 } 134 135 private static URL getImageUrl(String imageName, Collection<String> dirs) 136 { 137 URL u; 138 // Try passed directories first 139 if(dirs != null) 140 { 141 for (String name : dirs) 142 { 143 u = getImageUrl(name, imageName); 144 if(u != null) return u; 145 } 146 } 147 // Try user-preference directory 148 u = getImageUrl(Main.pref.getPreferencesDir()+"images", imageName); 149 if(u != null) return u; 150 151 // Try plugins and josm classloader 152 u = getImageUrl("resource://images/", imageName); 153 if(u != null) return u; 154 155 // Try all other ressource directories 156 for (String location : Main.pref.getAllPossiblePreferenceDirs()) 157 { 158 u = getImageUrl(location+"images", imageName); 159 if(u != null) return u; 160 u = getImageUrl(location, imageName); 161 if(u != null) return u; 162 } 163 return null; 164 } 120 165 121 166 /**
Note:
See TracChangeset
for help on using the changeset viewer.