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