source: josm/trunk/src/org/openstreetmap/josm/gui/GettingStarted.java@ 1350

Last change on this file since 1350 was 1350, checked in by stoecker, 15 years ago

speedup start, fix #2106, patch by xeen and Michel Marti

  • Property svn:eol-style set to native
File size: 8.2 KB
RevLine 
[608]1// License: GPL. See LICENSE file for details.
2
3package org.openstreetmap.josm.gui;
4
[582]5import static org.openstreetmap.josm.tools.I18n.tr;
6
[623]7import java.io.IOException;
[1290]8import java.util.ArrayList;
[1350]9import java.util.concurrent.Executors;
10import java.util.concurrent.ExecutorService;
11import java.util.concurrent.Callable;
12import java.util.concurrent.Future;
[623]13import java.util.regex.Matcher;
14import java.util.regex.Pattern;
[582]15
[623]16import java.awt.BorderLayout;
[1231]17import java.awt.Component;
[1350]18import java.awt.EventQueue;
[623]19
20import javax.swing.JScrollPane;
[582]21import javax.swing.JEditorPane;
22import javax.swing.JPanel;
23import javax.swing.event.HyperlinkEvent;
24import javax.swing.event.HyperlinkListener;
[623]25import javax.swing.border.EmptyBorder;
[582]26
27import org.openstreetmap.josm.Main;
28import org.openstreetmap.josm.tools.OpenBrowser;
[623]29import org.openstreetmap.josm.tools.WikiReader;
30import org.openstreetmap.josm.actions.AboutAction;
[608]31
[623]32public class GettingStarted extends JPanel {
[608]33
[1169]34 static private String content = "";
[608]35
[652]36 public class LinkGeneral extends JEditorPane implements HyperlinkListener {
37 public LinkGeneral(String text) {
38 setContentType("text/html");
39 setText(text);
40 setEditable(false);
41 setOpaque(false);
42 addHyperlinkListener(this);
[608]43 }
[652]44 public void hyperlinkUpdate(HyperlinkEvent e) {
45 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
46 OpenBrowser.displayUrl(e.getDescription());
47 }
[608]48 }
49 }
[1350]50
51 public class readMOTD implements Callable<String> {
52 private boolean isLocalized;
53 private boolean isHelp;
54 private String urlLoc;
55 private String urlIntl;
56 private String urlBase;
57
58 readMOTD(boolean isLocalized, String urlBase, String urlLoc, String urlIntl, boolean isHelp) {
59 this.isLocalized = isLocalized;
60 this.urlBase = urlBase;
61 this.urlLoc = urlLoc;
62 this.urlIntl = urlIntl;
63 this.isHelp = isHelp;
64 }
[608]65
[1350]66 public String call() {
67 WikiReader wr = new WikiReader(urlBase);
68 String content = "";
69 try {
70 // If we hit a non-localized link here, we already know there's no translated version available
71 String message = isLocalized ? wr.read(urlLoc) : "";
72 // Look for non-localized version
73 if (message.equals(""))
74 message = wr.read(urlIntl);
75
76 if (!message.equals(""))
77 if(isHelp)
78 content += message;
79 else
80 content += "<ul><li>"+ message.substring(8)+"</li></ul>";
81 } catch (IOException ioe) {
82 try {
83 if(isHelp)
84 content += wr.read(urlIntl);
85 else
86 content += "<ul><li>"+wr.read(urlIntl).substring(8)+"</li></ul>";
87 } catch (IOException ioe2) {
88 }
89 }
90
91 return content;
92 }
93 }
94
[652]95 private void assignContent() {
[1290]96 if (content.length() > 0 && Main.pref.getBoolean("help.displaymotd", true)) return;
[574]97
[1290]98 String baseurl = Main.pref.get("help.baseurl", "http://josm.openstreetmap.de");
99 WikiReader wr = new WikiReader(baseurl);
100 String motdcontent = "";
101 try {
102 motdcontent = wr.read(baseurl + "/wiki/MessageOfTheDay?format=txt");
103 } catch (IOException ioe) {
104 motdcontent = "<html><body>\n<h1>" +
105 "JOSM - " + tr("Java OpenStreetMap Editor") +
106 "</h1>\n<h2 align=\"center\">(" +
107 tr ("Message of the day not available") +
108 ")</h2>";
109 }
[574]110
[1290]111 int myVersion = AboutAction.getVersionNumber();
112 String languageCode = Main.getLanguageCodeU();
[608]113
[1290]114 // Finds wiki links like (underscores inserted for readability): [wiki:LANGCODE:messageoftheday_CONDITON_REVISION LANGCODE]
115 // Langcode usually consists of two letters describing the language and may be omitted
116 // Condition may be one of the following: > < <= =>
117 // Revision is the JOSM version
118 Pattern versionPattern = Pattern.compile("\\[wiki:(?:[A-Z]+:)?MessageOfTheDay(\\>\\=|\\<\\=|\\<|\\>)([0-9]+)\\s*([A-Z]*)\\]", Pattern.CASE_INSENSITIVE);
119 // 1=condition, 2=targetVersion, 3=lang
120 Matcher matcher = versionPattern.matcher(motdcontent);
121 matcher.reset();
122
[1350]123 ArrayList<String[]> links = new ArrayList<String[]>();
[1290]124 String linksList="";
125 while (matcher.find()) {
126 // Discards all but the selected locale and non-localized links
127 if(!(matcher.group(3)+":").equals(languageCode) && !matcher.group(3).equals(""))
128 continue;
129
130 links.add(new String[] {matcher.group(1), matcher.group(2), matcher.group(3)});
131 linksList += matcher.group(1)+matcher.group(2)+matcher.group(3)+": ";
132 }
[1350]133
134 // We cannot use Main.worker here because it's single-threaded and
135 // setting it to multi-threading will cause problems elsewhere
136 ExecutorService slave = Executors.newCachedThreadPool();
137
138 ArrayList<Future<String>> linkContent = new ArrayList<Future<String>>();
[1290]139 for(int i=0; i < links.size(); i++) {
140 String[] obj = (String[])links.get(i);
141 int targetVersion = Integer.parseInt(obj[1]);
142 String condition = obj[0];
143 Boolean isLocalized = !obj[2].equals("");
144
145 // Prefer localized over non-localized links, if they're otherwise the same
[1350]146 if(!isLocalized && linksList.indexOf(condition + obj[1] + languageCode + " ") >= 0)
[1290]147 continue;
148
149 boolean included = false;
150
151 if(myVersion == 0)
152 included = true;
153 else if(condition.equals(">="))
154 included=myVersion >= targetVersion;
155 else if(condition.equals(">"))
156 included = myVersion > targetVersion;
157 else if(condition.equals("<"))
158 included=myVersion < targetVersion;
159 else
160 included = myVersion <= targetVersion;
161
162 if(!included) continue;
[1350]163
164 boolean isHelp = targetVersion == 1;
[1290]165 String urlStart = baseurl + "/wiki/";
166 String urlEnd = "MessageOfTheDay" + condition + targetVersion + (isHelp ? "" : "?format=txt");
[1350]167 String urlLoc = urlStart + languageCode + urlEnd;
168 String urlIntl = urlStart + urlEnd;
169
170 // This adds all links to the worker which will download them concurrently
171 linkContent.add(slave.submit(new readMOTD(isLocalized, baseurl, urlLoc, urlIntl, isHelp)));
172 }
173
174 for(int i=0; i < linkContent.size(); i++) {
[1290]175 try {
[1350]176 content += linkContent.get(i).get();
177 } catch (Exception e) {}
[652]178 }
[1350]179
[1290]180 content = "<html>\n"+
181 "<style type=\"text/css\">\n"+
182 "body { font-family: sans-serif; font-weight: bold; }\n"+
183 "h1 {text-align: center;}\n"+
184 "</style>\n"+
185 "<h1>JOSM - " + tr("Java OpenStreetMap Editor") + "</h1>\n"+
186 content+"\n"+
187 "</html>";
[608]188 }
[1169]189
[652]190 public GettingStarted() {
191 super(new BorderLayout());
[1350]192 final LinkGeneral lg = new LinkGeneral(tr("Download \"Message of the day\""));
193 JScrollPane scroller = new JScrollPane(lg);
[652]194 // panel.add(GBC.glue(0,1), GBC.eol());
195 //panel.setMinimumSize(new Dimension(400, 600));
[1231]196 Component linkGeneral = new LinkGeneral(content);
[800]197 scroller.setViewportBorder(new EmptyBorder(10,100,10,100));
[652]198 add(scroller, BorderLayout.CENTER);
[1231]199
[1350]200 // Asynchronously get MOTD to speed-up JOSM startup
201 Thread t = new Thread(new Runnable() {
202 public void run() {
203 assignContent();
204 EventQueue.invokeLater(new Runnable() {
205 public void run() {
206 lg.setText(content);
207 lg.moveCaretPosition(0);
208 }
209 });
210 }
211 }, "MOTD-Loader");
212 t.setDaemon(true);
213 t.start();
214
[1231]215 new FileDrop(linkGeneral);
[652]216 }
[608]217}
Note: See TracBrowser for help on using the repository browser.