1 | // License: GPL. See LICENSE file for details.
|
---|
2 |
|
---|
3 | package org.openstreetmap.josm.gui;
|
---|
4 |
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
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;
|
---|
13 | import java.util.regex.Matcher;
|
---|
14 | import java.util.regex.Pattern;
|
---|
15 |
|
---|
16 | import java.awt.BorderLayout;
|
---|
17 | import java.awt.EventQueue;
|
---|
18 |
|
---|
19 | import javax.swing.JScrollPane;
|
---|
20 | import javax.swing.JEditorPane;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 | import javax.swing.event.HyperlinkEvent;
|
---|
23 | import javax.swing.event.HyperlinkListener;
|
---|
24 | import javax.swing.border.EmptyBorder;
|
---|
25 |
|
---|
26 | import org.openstreetmap.josm.Main;
|
---|
27 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
28 | import org.openstreetmap.josm.tools.WikiReader;
|
---|
29 | import org.openstreetmap.josm.actions.AboutAction;
|
---|
30 |
|
---|
31 | public 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 | content = "<html>\n"+
|
---|
184 | styles +
|
---|
185 | "<h1>JOSM - " + tr("Java OpenStreetMap Editor") + "</h1>\n"+
|
---|
186 | content+"\n"+
|
---|
187 | "</html>";
|
---|
188 | }
|
---|
189 |
|
---|
190 | public GettingStarted() {
|
---|
191 | super(new BorderLayout());
|
---|
192 | final LinkGeneral lg = new LinkGeneral(
|
---|
193 | "<html>" +
|
---|
194 | styles +
|
---|
195 | "<h1>" +
|
---|
196 | "JOSM - " +
|
---|
197 | tr("Java OpenStreetMap Editor") +
|
---|
198 | "</h1><h2 align=\"center\">" +
|
---|
199 | tr("Downloading \"Message of the day\"") +
|
---|
200 | "</h2>");
|
---|
201 | JScrollPane scroller = new JScrollPane(lg);
|
---|
202 | scroller.setViewportBorder(new EmptyBorder(10,100,10,100));
|
---|
203 | add(scroller, BorderLayout.CENTER);
|
---|
204 |
|
---|
205 | // Asynchronously get MOTD to speed-up JOSM startup
|
---|
206 | Thread t = new Thread(new Runnable() {
|
---|
207 | public void run() {
|
---|
208 | assignContent();
|
---|
209 | EventQueue.invokeLater(new Runnable() {
|
---|
210 | public void run() {
|
---|
211 | lg.setText(content);
|
---|
212 | //lg.moveCaretPosition(0);
|
---|
213 | }
|
---|
214 | });
|
---|
215 | }
|
---|
216 | }, "MOTD-Loader");
|
---|
217 | t.setDaemon(true);
|
---|
218 | t.start();
|
---|
219 |
|
---|
220 | new FileDrop(scroller);
|
---|
221 | }
|
---|
222 | }
|
---|