Changeset 991 in josm for trunk/src/org/openstreetmap/josm/tools
- Timestamp:
- 2008-09-19T08:33:02+02:00 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.