Ticket #2106: MakeMOTDconcurrent.patch
File MakeMOTDconcurrent.patch, 6.1 KB (added by , 15 years ago) |
---|
-
src/org/openstreetmap/josm/gui/GettingStarted.java
6 6 7 7 import java.io.IOException; 8 8 import java.util.ArrayList; 9 import java.util.concurrent.Executors; 10 import java.util.concurrent.ExecutorService; 11 import java.util.concurrent.Callable; 12 import java.util.concurrent.Future; 9 13 import java.util.regex.Matcher; 10 14 import java.util.regex.Pattern; 11 15 … … 42 46 } 43 47 } 44 48 } 49 50 public class readMOTD implements Callable<String> { 51 private boolean isLocalized; 52 private boolean isHelp; 53 private String urlLoc; 54 private String urlIntl; 55 private String urlBase; 56 57 readMOTD(boolean isLocalized, String urlBase, String urlLoc, String urlIntl, boolean isHelp) { 58 this.isLocalized = isLocalized; 59 this.urlBase = urlBase; 60 this.urlLoc = urlLoc; 61 this.urlIntl = urlIntl; 62 this.isHelp = isHelp; 63 } 45 64 65 public String call() { 66 WikiReader wr = new WikiReader(urlBase); 67 String content = ""; 68 try { 69 // If we hit a non-localized link here, we already know there's no translated version available 70 String message = isLocalized ? wr.read(urlLoc) : ""; 71 // Look for non-localized version 72 if (message.equals("")) 73 message = wr.read(urlIntl); 74 75 if (!message.equals("")) 76 if(isHelp) 77 content += message; 78 else 79 content += "<ul><li>"+ message.substring(8)+"</li></ul>"; 80 } catch (IOException ioe) { 81 try { 82 if(isHelp) 83 content += wr.read(urlIntl); 84 else 85 content += "<ul><li>"+wr.read(urlIntl).substring(8)+"</li></ul>"; 86 } catch (IOException ioe2) { 87 } 88 } 89 90 return content; 91 } 92 } 93 46 94 private void assignContent() { 47 95 if (content.length() > 0 && Main.pref.getBoolean("help.displaymotd", true)) return; 48 96 … … 71 119 Matcher matcher = versionPattern.matcher(motdcontent); 72 120 matcher.reset(); 73 121 74 ArrayList< Object> links = new ArrayList<Object>();122 ArrayList<String[]> links = new ArrayList<String[]>(); 75 123 String linksList=""; 76 124 while (matcher.find()) { 77 125 // Discards all but the selected locale and non-localized links … … 81 129 links.add(new String[] {matcher.group(1), matcher.group(2), matcher.group(3)}); 82 130 linksList += matcher.group(1)+matcher.group(2)+matcher.group(3)+": "; 83 131 } 84 132 133 // We cannot use Main.worker here because it's single-threaded and 134 // setting it to multi-threading will cause problems elsewhere 135 ExecutorService slave = Executors.newCachedThreadPool(); 136 137 ArrayList<Future<String>> linkContent = new ArrayList<Future<String>>(); 85 138 for(int i=0; i < links.size(); i++) { 86 139 String[] obj = (String[])links.get(i); 87 140 int targetVersion = Integer.parseInt(obj[1]); … … 89 142 Boolean isLocalized = !obj[2].equals(""); 90 143 91 144 // Prefer localized over non-localized links, if they're otherwise the same 92 if(!isLocalized && linksList.indexOf(condition +obj[1]+languageCode+" ") >= 0)145 if(!isLocalized && linksList.indexOf(condition + obj[1] + languageCode + " ") >= 0) 93 146 continue; 94 147 95 148 boolean included = false; … … 106 159 included = myVersion <= targetVersion; 107 160 108 161 if(!included) continue; 109 110 Boolean isHelp = targetVersion == 1; 111 162 163 boolean isHelp = targetVersion == 1; 112 164 String urlStart = baseurl + "/wiki/"; 113 165 String urlEnd = "MessageOfTheDay" + condition + targetVersion + (isHelp ? "" : "?format=txt"); 114 166 String urlLoc = urlStart + languageCode + urlEnd; 167 String urlIntl = urlStart + urlEnd; 168 169 // This adds all links to the worker which will download them concurrently 170 linkContent.add(slave.submit(new readMOTD(isLocalized, baseurl, urlLoc, urlIntl, isHelp))); 171 } 172 173 for(int i=0; i < linkContent.size(); i++) { 115 174 try { 116 // If we hit a non-localized link here, we already know there's no translated version available 117 String message = isLocalized ? wr.read(urlStart + languageCode + urlEnd) : ""; 118 // Look for non-localized version 119 if (message.equals("")) 120 message = wr.read(urlStart + urlEnd); 121 122 if (!message.equals("")) 123 if(isHelp) 124 content += message; 125 else 126 content += "<ul><li>"+ message.substring(8)+"</li></ul>"; 127 } catch (IOException ioe) { 128 try { 129 if(isHelp) 130 content += wr.read(urlStart + urlEnd); 131 else 132 content += "<ul><li>"+wr.read(urlStart + urlEnd).substring(8)+"</li></ul>"; 133 } catch (IOException ioe2) { 134 } 135 } 175 content += linkContent.get(i).get(); 176 } catch (Exception e) {} 136 177 } 137 178 138 179 content = "<html>\n"+ 139 180 "<style type=\"text/css\">\n"+ 140 181 "body { font-family: sans-serif; font-weight: bold; }\n"+ … … 149 190 super(new BorderLayout()); 150 191 assignContent(); 151 192 152 // panel.add(GBC.glue(0,1), GBC.eol());153 //panel.setMinimumSize(new Dimension(400, 600));154 193 Component linkGeneral = new LinkGeneral(content); 155 194 JScrollPane scroller = new JScrollPane(linkGeneral); 156 195 scroller.setViewportBorder(new EmptyBorder(10,100,10,100));