Changeset 1879 in josm for trunk/src/org/openstreetmap/josm/tools
- Timestamp:
- 2009-08-02T14:36:40+02:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r1814 r1879 43 43 * @author imi 44 44 */ 45 public static enum OverlayPosition {NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST} 45 public static enum OverlayPosition { 46 NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST 47 } 46 48 47 49 /** … … 51 53 52 54 /** 53 * Add here all ClassLoader whose ressource should be searched. 54 * Plugin's class loaders are addedby main.55 * Add here all ClassLoader whose ressource should be searched. Plugin's class loaders are added 56 * by main. 55 57 */ 56 58 public static final List<ClassLoader> sources = new LinkedList<ClassLoader>(); … … 58 60 /** 59 61 * Return an image from the specified location. 60 * 61 * @param subdir 62 * @param name 62 * 63 * @param subdir The position of the directory, e.g. "layer" 64 * @param name The icons name (without the ending of ".png") 63 65 * @return The requested Image. 64 66 */ … … 67 69 if (icon == null) { 68 70 String ext = name.indexOf('.') != -1 ? "" : ".png"; 69 throw new NullPointerException("/images/" +subdir+"/"+name+ext+" not found");71 throw new NullPointerException("/images/" + subdir + "/" + name + ext + " not found"); 70 72 } 71 73 return icon; 72 74 } 73 75 74 public static ImageIcon getIfAvailable(String subdir, String name) 75 { 76 return getIfAvailable((Collection<String>)null, null, subdir, name); 77 } 78 public static final ImageIcon getIfAvailable(String[] dirs, String id, String subdir, String name) 79 { 76 public static ImageIcon getIfAvailable(String subdir, String name) { 77 return getIfAvailable((Collection<String>) null, null, subdir, name); 78 } 79 80 public static final ImageIcon getIfAvailable(String[] dirs, String id, String subdir, String name) { 80 81 return getIfAvailable(Arrays.asList(dirs), id, subdir, name); 81 82 } 82 83 83 84 /** 84 * Like {@link #get(String)}, but does not throw and return <code>null</code> 85 * in case of nothing is found. Use this, if the image to retrieve is optional. 86 */ 87 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name) 88 { 85 * Like {@link #get(String)}, but does not throw and return <code>null</code> in case of nothing 86 * is found. Use this, if the image to retrieve is optional. 87 */ 88 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name) { 89 89 if (name == null) 90 90 return null; 91 if (name.startsWith("http://")) 92 { 91 if (name.startsWith("http://")) { 93 92 Image img = cache.get(name); 94 if(img == null) 95 { 96 try 97 { 98 MirroredInputStream is = new MirroredInputStream(name, 99 new File(Main.pref.getPreferencesDir(), "images").toString()); 100 if(is != null) 101 { 93 if (img == null) { 94 try { 95 MirroredInputStream is = new MirroredInputStream(name, new File(Main.pref.getPreferencesDir(), 96 "images").toString()); 97 if (is != null) { 102 98 img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL()); 103 99 cache.put(name, img); 104 100 } 105 } 106 catch(IOException e) { 101 } catch (IOException e) { 107 102 } 108 103 } … … 115 110 } 116 111 String ext = name.indexOf('.') != -1 ? "" : ".png"; 117 String full_name = subdir +name+ext;112 String full_name = subdir + name + ext; 118 113 String cache_name = full_name; 119 114 /* cache separately */ 120 if (dirs != null && dirs.size() > 0) {121 cache_name = "id:" +id+":"+full_name;115 if (dirs != null && dirs.size() > 0) { 116 cache_name = "id:" + id + ":" + full_name; 122 117 } 123 118 … … 125 120 if (img == null) { 126 121 // getImageUrl() does a ton of "stat()" calls and gets expensive 127 // and redundant when you have a whole ton of objects. 122 // and redundant when you have a whole ton of objects. So, 128 123 // index the cache by the name of the icon we're looking for 129 124 // and don't bother to create a URL unless we're actually … … 139 134 } 140 135 141 private static URL getImageUrl(String path, String name) 142 { 143 if(path.startsWith("resource://")) 144 { 136 private static URL getImageUrl(String path, String name) { 137 if (path.startsWith("resource://")) { 145 138 String p = path.substring("resource://".length()); 146 for (ClassLoader source : sources) 147 { 139 for (ClassLoader source : sources) { 148 140 URL res; 149 if ((res = source.getResource(p +name)) != null)141 if ((res = source.getResource(p + name)) != null) 150 142 return res; 151 143 } 152 } 153 else 154 { 144 } else { 155 145 try { 156 146 File f = new File(path, name); 157 if (f.exists())147 if (f.exists()) 158 148 return f.toURI().toURL(); 159 } catch (MalformedURLException e) {} 149 } catch (MalformedURLException e) { 150 } 160 151 } 161 152 return null; 162 153 } 163 154 164 private static URL getImageUrl(String imageName, Collection<String> dirs) 165 {166 URL u; 155 private static URL getImageUrl(String imageName, Collection<String> dirs) { 156 URL u = null; 157 167 158 // Try passed directories first 168 if(dirs != null) 169 { 170 for (String name : dirs) 171 { 172 u = getImageUrl(name, imageName); 173 if(u != null) return u; 159 if (dirs != null) { 160 for (String name : dirs) { 161 try { 162 u = getImageUrl(name, imageName); 163 if (u != null) 164 return u; 165 } catch (SecurityException e) { 166 System.out.println(tr( 167 "Warning: failed to acccess directory ''{0}'' for security reasons. Exception was: {1}", 168 name, e.toString())); 169 } 170 174 171 } 175 172 } 176 173 // Try user-preference directory 177 u = getImageUrl(Main.pref.getPreferencesDir()+"images", imageName); 178 if(u != null) return u; 174 String dir = Main.pref.getPreferencesDir() + "images"; 175 try { 176 u = getImageUrl(dir, imageName); 177 if (u != null) 178 return u; 179 } catch (SecurityException e) { 180 System.out.println(tr( 181 "Warning: failed to acccess directory ''{0}'' for security reasons. Exception was: {1}", dir, e 182 .toString())); 183 } 179 184 180 185 // Try plugins and josm classloader 181 186 u = getImageUrl("resource://images/", imageName); 182 if(u != null) return u; 187 if (u != null) 188 return u; 183 189 184 190 // Try all other ressource directories 185 for (String location : Main.pref.getAllPossiblePreferenceDirs()) 186 {187 u = getImageUrl(location+"images", imageName);188 if(u != null)return u;191 for (String location : Main.pref.getAllPossiblePreferenceDirs()) { 192 u = getImageUrl(location + "images", imageName); 193 if (u != null) 194 return u; 189 195 u = getImageUrl(location, imageName); 190 if(u != null) return u; 191 } 196 if (u != null) 197 return u; 198 } 199 System.out 200 .println(tr( 201 "Fatal: failed to locate image ''{0}''. This is a serious configuration problem. JOSM will stop working.", 202 imageName)); 192 203 return null; 193 204 } … … 201 212 202 213 public static Cursor getCursor(String name, String overlay) { 203 ImageIcon img = get("cursor", name);214 ImageIcon img = get("cursor", name); 204 215 if (overlay != null) { 205 img = overlay(img, "cursor/modifier/" +overlay, OverlayPosition.SOUTHEAST);216 img = overlay(img, "cursor/modifier/" + overlay, OverlayPosition.SOUTHEAST); 206 217 } 207 218 Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(img.getImage(), 208 name.equals("crosshair") ? new Point(10, 10) : new Point(3,2), "Cursor");219 name.equals("crosshair") ? new Point(10, 10) : new Point(3, 2), "Cursor"); 209 220 return c; 210 221 } 211 222 212 223 /** 213 * @return an icon that represent the overlay of the two given icons. The 214 * second icon is layedon the first relative to the given position.224 * @return an icon that represent the overlay of the two given icons. The second icon is layed 225 * on the first relative to the given position. 215 226 */ 216 227 public static ImageIcon overlay(Icon ground, String overlayImage, OverlayPosition pos) { 217 GraphicsConfiguration conf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); 228 GraphicsConfiguration conf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() 229 .getDefaultConfiguration(); 218 230 int w = ground.getIconWidth(); 219 231 int h = ground.getIconHeight(); … … 221 233 int wo = overlay.getIconWidth(); 222 234 int ho = overlay.getIconHeight(); 223 BufferedImage img = conf.createCompatibleImage(w, h, Transparency.TRANSLUCENT);235 BufferedImage img = conf.createCompatibleImage(w, h, Transparency.TRANSLUCENT); 224 236 Graphics g = img.createGraphics(); 225 237 ground.paintIcon(null, g, 0, 0); … … 231 243 break; 232 244 case NORTHEAST: 233 x = w -wo;245 x = w - wo; 234 246 y = 0; 235 247 break; 236 248 case SOUTHWEST: 237 249 x = 0; 238 y = h -ho;250 y = h - ho; 239 251 break; 240 252 case SOUTHEAST: 241 x = w -wo;242 y = h -ho;253 x = w - wo; 254 y = h - ho; 243 255 break; 244 256 } … … 256 268 } 257 269 258 /* from: http://www.jidesoft.com/blog/2008/02/29/rotate-an-icon-in-java/ 259 * License: "feel free to use" 270 /* 271 * from: http://www.jidesoft.com/blog/2008/02/29/rotate-an-icon-in-java/ License: 272 * "feel free to use" 260 273 */ 261 274 final static double DEGREE_90 = 90.0 * Math.PI / 180.0; … … 263 276 /** 264 277 * Creates a rotated version of the input image. 265 * 266 * @param c The component to get properties useful for painting, e.g. the foreground267 * orbackground color.268 * @param icon 278 * 279 * @param c The component to get properties useful for painting, e.g. the foreground or 280 * background color. 281 * @param icon the image to be rotated. 269 282 * @param rotatedAngle the rotated angle, in degree, clockwise. It could be any double but we 270 * 271 * 283 * will mod it with 360 before using it. 284 * 272 285 * @return the image after rotating. 273 286 */ … … 295 308 w = (int) (iw * Math.sin(DEGREE_90 - radian) + ih * Math.sin(radian)); 296 309 h = (int) (iw * Math.sin(radian) + ih * Math.sin(DEGREE_90 - radian)); 297 } 298 else { 310 } else { 299 311 w = (int) (ih * Math.sin(DEGREE_90 - radian) + iw * Math.sin(radian)); 300 312 h = (int) (ih * Math.sin(radian) + iw * Math.sin(DEGREE_90 - radian)); … … 309 321 310 322 // move the graphics center point to the center of the icon. 311 g2d.translate(w /2, h/2);323 g2d.translate(w / 2, h / 2); 312 324 313 325 // rotate the graphics about the center point of the icon … … 329 341 if (type == null) 330 342 throw new IllegalArgumentException(tr("parameter ''{0}'' must not be null", "type")); 331 return get("data", type.getAPIName());343 return get("data", type.getAPIName()); 332 344 } 333 345 } -
trunk/src/org/openstreetmap/josm/tools/WikiReader.java
r1755 r1879 4 4 import java.io.BufferedReader; 5 5 import java.io.IOException; 6 import java.io.InputStream; 6 7 import java.io.InputStreamReader; 7 8 import java.net.URL; … … 10 11 import org.openstreetmap.josm.tools.LanguageInfo; 11 12 13 import static org.openstreetmap.josm.tools.I18n.tr; 14 12 15 /** 13 16 * Read a trac-wiki page. 14 * 17 * 15 18 * @author imi 16 19 */ … … 29 32 /** 30 33 * Read the page specified by the url and return the content. 31 * 32 * If the url is within the baseurl path, parse it as an trac wikipage and 33 * replace relativepathes etc..34 * 34 * 35 * If the url is within the baseurl path, parse it as an trac wikipage and replace relative 36 * pathes etc.. 37 * 35 38 * @return Either the string of the content of the wiki page. 36 39 * @throws IOException Throws, if the page could not be loaded. … … 45 48 public String readLang(String text) { 46 49 String languageCode = LanguageInfo.getLanguageCodeWiki(); 47 String url = baseurl + "/wiki/" +languageCode+text;50 String url = baseurl + "/wiki/" + languageCode + text; 48 51 String res = ""; 52 InputStream in = null; 49 53 try { 50 res = readFromTrac(new BufferedReader(new InputStreamReader(new URL(url).openStream(), "utf-8"))); 51 } catch (IOException ioe) {} 52 if(res.length() == 0 && languageCode.length() != 0) 53 { 54 url = baseurl + "/wiki/"+text; 54 in = new URL(url).openStream(); 55 res = readFromTrac(new BufferedReader(new InputStreamReader(in, "utf-8"))); 56 } catch (IOException ioe) { 57 System.out.println(tr("Warning: failed to read MOTD from ''{0}''. Exception was: {1}", url, ioe 58 .toString())); 59 } catch(SecurityException e) { 60 System.out.println(tr( 61 "Warning: failed to read MOTD from ''{0}'' for security reasons. Exception was: {1}", url, e 62 .toString())); 63 } finally { 64 if (in != null) { 65 try { 66 in.close(); 67 } catch (IOException e) { 68 } 69 } 70 } 71 if (res.length() == 0 && languageCode.length() != 0) { 72 url = baseurl + "/wiki/" + text; 55 73 try { 56 res = readFromTrac(new BufferedReader(new InputStreamReader(new URL(url).openStream(), "utf-8"))); 57 } catch (IOException ioe) {} 74 in = new URL(url).openStream(); 75 } catch (IOException e) { 76 System.out.println(tr("Warning: failed to read MOTD from ''{0}''. Exception was: {1}", url, e 77 .toString())); 78 return res; 79 } catch (SecurityException e) { 80 System.out.println(tr( 81 "Warning: failed to read MOTD from ''{0}'' for security reasons. Exception was: {1}", url, e 82 .toString())); 83 return res; 84 } 85 try { 86 res = readFromTrac(new BufferedReader(new InputStreamReader(in, "utf-8"))); 87 } catch (IOException ioe) { 88 System.out.println(tr("Warning: failed to read MOTD from ''{0}''. Exception was: {1}", url, ioe 89 .toString())); 90 return res; 91 } finally { 92 if (in != null) { 93 try { 94 in.close(); 95 } catch (IOException e) { 96 } 97 } 98 } 58 99 } 59 100 return res; … … 63 104 String b = ""; 64 105 for (String line = in.readLine(); line != null; line = in.readLine()) { 65 if (!line.contains("[[TranslatedPages]]"))106 if (!line.contains("[[TranslatedPages]]")) { 66 107 b += line.replaceAll(" />", ">") + "\n"; 108 } 67 109 } 68 110 return "<html>" + b + "</html>"; … … 74 116 String b = ""; 75 117 for (String line = in.readLine(); line != null; line = in.readLine()) { 76 if (line.contains("<div id=\"searchable\">")) 118 if (line.contains("<div id=\"searchable\">")) { 77 119 inside = true; 78 else if (line.contains("<div class=\"wiki-toc trac-nav\""))120 } else if (line.contains("<div class=\"wiki-toc trac-nav\"")) { 79 121 transl = true; 80 else if (line.contains("<div class=\"wikipage searchable\">"))122 } else if (line.contains("<div class=\"wikipage searchable\">")) { 81 123 inside = true; 82 else if (line.contains("<div class=\"buttons\">"))124 } else if (line.contains("<div class=\"buttons\">")) { 83 125 inside = false; 126 } 84 127 if (inside && !transl) { 85 b += line.replaceAll("<img src=\"/", "<img src=\""+baseurl+"/") 86 .replaceAll("href=\"/", "href=\""+baseurl+"/") 87 .replaceAll(" />", ">") + "\n"; 128 b += line.replaceAll("<img src=\"/", "<img src=\"" + baseurl + "/").replaceAll("href=\"/", 129 "href=\"" + baseurl + "/").replaceAll(" />", ">") 130 + "\n"; 131 } else if (transl && line.contains("</div>")) { 132 transl = false; 88 133 } 89 else if (transl && line.contains("</div>"))90 transl = false;91 134 } 92 if (b.indexOf(" Describe ") >= 0)135 if (b.indexOf(" Describe ") >= 0) 93 136 return ""; 94 137 return "<html>" + b + "</html>";
Note:
See TracChangeset
for help on using the changeset viewer.