Ticket #1848: Getting Started.patch
| File Getting Started.patch, 8.5 KB (added by , 17 years ago) |
|---|
-
src/org/openstreetmap/josm/gui/GettingStarted.java
4 4 5 5 import static org.openstreetmap.josm.tools.I18n.tr; 6 6 7 import java.io.BufferedReader; 8 import java.io.BufferedWriter; 9 import java.io.File; 10 import java.io.FileReader; 11 import java.io.FileWriter; 7 12 import java.io.IOException; 13 import java.io.Writer; 8 14 import java.util.ArrayList; 15 import java.util.Date; 9 16 import java.util.concurrent.Executors; 10 17 import java.util.concurrent.ExecutorService; 11 18 import java.util.concurrent.Callable; … … 50 57 } 51 58 } 52 59 } 53 60 61 /** 62 * This class encapsulates the "reading URL" task and can be executed in background and in 63 * parallel. Since the MOTD is many separate pages this speeds things up quite a lot. If no 64 * localized version is available, it automatically falls back to the international one. 65 */ 54 66 public class readMOTD implements Callable<String> { 55 67 private boolean isLocalized; 56 68 private boolean isHelp; … … 58 70 private String urlIntl; 59 71 private String urlBase; 60 72 61 readMOTD(boolean isLocalized, String urlBase, String urlLoc, String urlIntl, boolean isHelp) { 62 this.isLocalized = isLocalized; 63 this.urlBase = urlBase; 64 this.urlLoc = urlLoc; 65 this.urlIntl = urlIntl; 66 this.isHelp = isHelp; 73 /** 74 * Read a MOTD page 75 * @param isLocalized If true, tries to get localized version as defined in urlLoc 76 * @param urlBase Base URL (i.e. http://www.openstreetmap.de/wiki/) 77 * @param urlLoc Part to append to base URL to receive localized version 78 * @param urlIntl Part to append to base URL to receive international version 79 * @param makeList If true, the URL's contents will be wrapped in a list (<ul><li>) 80 */ 81 readMOTD(boolean isLocalized, String urlBase, String urlLoc, String urlIntl, boolean makeList) { 82 this.isLocalized = isLocalized; 83 this.urlBase = urlBase; 84 this.urlLoc = urlLoc; 85 this.urlIntl = urlIntl; 86 this.isHelp = makeList; 67 87 } 68 88 89 /* 90 * Does the actual work 91 * @see java.util.concurrent.Callable#call() 92 */ 69 93 public String call() { 70 94 WikiReader wr = new WikiReader(urlBase); 71 95 String content = ""; … … 80 104 if(isHelp) 81 105 content += message; 82 106 else 83 content += "<ul><li>"+ message.substring(8).replaceAll("\n *\\* +","</li><li>")+"</li></ul>"; 107 content += "<ul><li>"+ message.substring(8) 108 .replaceAll("\n *\\* +","</li><li>")+"</li></ul>"; 84 109 } catch (IOException ioe) { 85 110 try { 86 111 if(isHelp) 87 112 content += wr.read(urlIntl); 88 113 else 89 content += "<ul><li>"+wr.read(urlIntl).substring(8).replaceAll("\n *\\* +","</li><li>")+"</li></ul>"; 114 content += "<ul><li>"+wr.read(urlIntl).substring(8) 115 .replaceAll("\n *\\* +","</li><li>")+"</li></ul>"; 90 116 } catch (IOException ioe2) { 91 117 } 92 118 } … … 94 120 return content; 95 121 } 96 122 } 97 123 124 /** 125 * This is where the MOTD will be cached 126 */ 127 final static private File cacheDir = new File(Main.pref.getPreferencesDir(), "motd.html"); 128 129 /** 130 * Grabs current MOTD from cache or webpage and parses, then caches it. 131 */ 98 132 private void assignContent() { 99 if (content.length() > 0 && Main.pref.getBoolean("help.displaymotd", true)) return; 133 if (content.length() > 0 || !Main.pref.getBoolean("motd.display", true)) return; 134 135 int myVersion = AboutAction.getVersionNumber(); 136 String languageCode = Main.getLanguageCodeU(); 137 138 // Try loading it from cache and quit if successful 139 if(loadFromDisk(myVersion)) 140 return; 100 141 101 142 String baseurl = Main.pref.get("help.baseurl", "http://josm.openstreetmap.de"); 102 143 WikiReader wr = new WikiReader(baseurl); … … 111 152 ")</h2>"; 112 153 } 113 154 114 int myVersion = AboutAction.getVersionNumber();115 String languageCode = Main.getLanguageCodeU();116 117 155 // Finds wiki links like (underscores inserted for readability): [wiki:LANGCODE:messageoftheday_CONDITON_REVISION LANGCODE] 118 156 // Langcode usually consists of two letters describing the language and may be omitted 119 157 // Condition may be one of the following: > < <= => … … 174 212 linkContent.add(slave.submit(new readMOTD(isLocalized, baseurl, urlLoc, urlIntl, isHelp))); 175 213 } 176 214 177 for(int i=0; i < linkContent.size(); i++) { 215 linkContent.add(slave.submit(new readMOTD(false, baseurl, "", baseurl + "/latest?format=txt", true))); 216 217 for(int i=0; i < linkContent.size()-1; i++) { 178 218 try { 179 219 content += linkContent.get(i).get(); 180 220 } catch (Exception e) {} 181 221 } 182 222 223 String version = "<div style=\"text-align:right;font-weight:normal;font-size:small\">"; 224 try { 225 String v = linkContent.get(linkContent.size()-1).get().trim().substring(6); 226 version += tr("(Current version: {0}. Your version: {1})", v, myVersion); 227 } catch(Exception e) {} 228 version += "</div>"; 229 183 230 linkContent.clear(); 184 231 try { 185 232 slave.shutdown(); 186 233 } catch(SecurityException x) {} 187 234 188 189 235 content = "<html>\n"+ 190 236 styles + 191 "<h1>JOSM - " + tr("Java OpenStreetMap Editor") + "</h1>\n"+ 192 content+"\n"+ 237 "<h1>JOSM - " + tr("Java OpenStreetMap Editor") + "</h1>"+ 238 content.replace("</html>", "")+ 239 version+ 193 240 "</html>"; 241 242 saveToDisk(myVersion); 194 243 } 244 245 /** 246 * Tries to load cached MOTD from disk if the JOSM version is still the same and the last update 247 * is younger than motd.updateinterval milliseconds (default: 24 hours). 248 * 249 * @param myVersion Currently running version 250 * @return false if update is required, true if cache was valid. 251 */ 252 private boolean loadFromDisk(int myVersion) { 253 if(Main.pref.getInteger("motd.lastversion", 0) != myVersion) 254 return false; 255 if(Long.parseLong(Main.pref.get("motd.lastupdate", "0")) 256 + Main.pref.getInteger("motd.updateinterval", 24*60*60*1000) 257 < new Date().getTime()) 258 return false; 195 259 260 StringBuilder c = new StringBuilder(); 261 try { 262 BufferedReader input = new BufferedReader(new FileReader(cacheDir)); 263 try { 264 String line = null; 265 while (( line = input.readLine()) != null){ 266 c.append(line); 267 c.append("\n"); 268 } 269 } finally { 270 input.close(); 271 } 272 } 273 catch (IOException ex){ 274 ex.printStackTrace(); 275 } 276 277 content = c.toString(); 278 return content.length() > 0; 279 } 280 281 /** 282 * Saves current MOTD to disk and updates "last update" variables 283 * @param myVersion currently running version 284 */ 285 private void saveToDisk(int myVersion) { 286 try { 287 Writer output = new BufferedWriter(new FileWriter( 288 new File(Main.pref.getPreferencesDir(), "motd.html"))); 289 try { 290 output.write(content); 291 Main.pref.put("motd.lastupdate", Long.toString(new Date().getTime())); 292 Main.pref.putInteger("motd.lastversion", myVersion); 293 } finally { 294 output.close(); 295 } 296 } catch(Exception e) { 297 Main.pref.putInteger("motd.lastupdate", 0); 298 } 299 } 300 301 /** 302 * Initializes getting the MOTD as well as enabling the FileDrop Listener. 303 * Displays a message while the MOTD is downloading. 304 */ 196 305 public GettingStarted() { 197 306 super(new BorderLayout()); 198 307 final LinkGeneral lg = new LinkGeneral(
