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

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

close #2196. Patch by xeen

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