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

Last change on this file since 12620 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 7.6 KB
RevLine 
[8378]1// License: GPL. For details, see LICENSE file.
[608]2package org.openstreetmap.josm.gui;
3
[582]4import static org.openstreetmap.josm.tools.I18n.tr;
5
[623]6import java.awt.BorderLayout;
[1350]7import java.awt.EventQueue;
[5061]8import java.io.IOException;
[4863]9import java.net.URL;
[7082]10import java.nio.charset.StandardCharsets;
[4863]11import java.util.regex.Matcher;
12import java.util.regex.Pattern;
[623]13
[3171]14import javax.swing.JComponent;
[582]15import javax.swing.JPanel;
[1879]16import javax.swing.JScrollPane;
17import javax.swing.border.EmptyBorder;
[582]18import javax.swing.event.HyperlinkEvent;
19import javax.swing.event.HyperlinkListener;
20
21import org.openstreetmap.josm.Main;
[11031]22import org.openstreetmap.josm.actions.DownloadPrimitiveAction;
[2358]23import org.openstreetmap.josm.data.Version;
[10881]24import org.openstreetmap.josm.gui.datatransfer.OpenTransferHandler;
[11031]25import org.openstreetmap.josm.gui.dialogs.MenuItemSearchDialog;
[6525]26import org.openstreetmap.josm.gui.preferences.server.ProxyPreference;
27import org.openstreetmap.josm.gui.preferences.server.ProxyPreferenceListener;
[5886]28import org.openstreetmap.josm.gui.widgets.JosmEditorPane;
[1450]29import org.openstreetmap.josm.io.CacheCustomContent;
[7434]30import org.openstreetmap.josm.io.OnlineResource;
[1755]31import org.openstreetmap.josm.tools.LanguageInfo;
[12620]32import org.openstreetmap.josm.tools.Logging;
[582]33import org.openstreetmap.josm.tools.OpenBrowser;
[623]34import org.openstreetmap.josm.tools.WikiReader;
[608]35
[12259]36/**
37 * Panel that fills the main part of the program window when JOSM has just started.
[12620]38 *
[12259]39 * It downloads and displays the so called <em>message of the day</em>, which
40 * contains news about recent major changes, warning in case of outdated versions, etc.
41 */
[6525]42public final class GettingStarted extends JPanel implements ProxyPreferenceListener {
43
44 private final LinkGeneral lg;
[1450]45 private String content = "";
[8840]46 private boolean contentInitialized;
[6525]47
[4364]48 private static final String STYLE = "<style type=\"text/css\">\n"
49 + "body {font-family: sans-serif; font-weight: bold; }\n"
50 + "h1 {text-align: center; }\n"
51 + ".icon {font-size: 0; }\n"
52 + "</style>\n";
[608]53
[5886]54 public static class LinkGeneral extends JosmEditorPane implements HyperlinkListener {
[6070]55
[5886]56 /**
57 * Constructs a new {@code LinkGeneral} with the given HTML text
58 * @param text The text to display
59 */
[652]60 public LinkGeneral(String text) {
61 setContentType("text/html");
62 setText(text);
63 setEditable(false);
64 setOpaque(false);
65 addHyperlinkListener(this);
[6935]66 adaptForNimbus(this);
[608]67 }
[1879]68
[6084]69 @Override
[652]70 public void hyperlinkUpdate(HyperlinkEvent e) {
71 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
72 OpenBrowser.displayUrl(e.getDescription());
73 }
[608]74 }
75 }
[1359]76
[1450]77 /**
78 * Grabs current MOTD from cache or webpage and parses it.
79 */
[11171]80 static class MotdContent extends CacheCustomContent<IOException> {
[8836]81 MotdContent() {
[1450]82 super("motd.html", CacheCustomContent.INTERVAL_DAILY);
[1290]83 }
[574]84
[6889]85 private final int myVersion = Version.getInstance().getVersion();
86 private final String myJava = System.getProperty("java.version");
87 private final String myLang = LanguageInfo.getWikiLanguagePrefix();
[608]88
[1450]89 /**
90 * This function gets executed whenever the cached files need updating
91 * @see org.openstreetmap.josm.io.CacheCustomContent#updateData()
92 */
[1879]93 @Override
[5061]94 protected byte[] updateData() throws IOException {
[1512]95 String motd = new WikiReader().readLang("StartupPage");
[1450]96 // Save this to prefs in case JOSM is updated so MOTD can be refreshed
97 Main.pref.putInteger("cache.motd.html.version", myVersion);
[5883]98 Main.pref.put("cache.motd.html.java", myJava);
[1562]99 Main.pref.put("cache.motd.html.lang", myLang);
[7082]100 return motd.getBytes(StandardCharsets.UTF_8);
[1350]101 }
[1359]102
[7434]103 @Override
104 protected void checkOfflineAccess() {
105 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(new WikiReader().getBaseUrlWiki(), Main.getJOSMWebsite());
106 }
107
[1450]108 /**
109 * Additionally check if JOSM has been updated and refresh MOTD
110 */
111 @Override
112 protected boolean isCacheValid() {
113 // We assume a default of myVersion because it only kicks in in two cases:
114 // 1. Not yet written - but so isn't the interval variable, so it gets updated anyway
115 // 2. Cannot be written (e.g. while developing). Obviously we don't want to update
[1879]116 // everytime because of something we can't read.
[1746]117 return (Main.pref.getInteger("cache.motd.html.version", -999) == myVersion)
[5882]118 && Main.pref.get("cache.motd.html.java").equals(myJava)
[1647]119 && Main.pref.get("cache.motd.html.lang").equals(myLang);
[1450]120 }
[608]121 }
[1169]122
[1450]123 /**
[1879]124 * Initializes getting the MOTD as well as enabling the FileDrop Listener. Displays a message
125 * while the MOTD is downloading.
[1450]126 */
[652]127 public GettingStarted() {
128 super(new BorderLayout());
[6525]129 lg = new LinkGeneral("<html>" + STYLE + "<h1>" + "JOSM - " + tr("Java OpenStreetMap Editor")
[4364]130 + "</h1><h2 align=\"center\">" + tr("Downloading \"Message of the day\"") + "</h2></html>");
[11031]131 // clear the build-in command ctrl+shift+O, ctrl+space because it is used as shortcut in JOSM
132 lg.getInputMap(JComponent.WHEN_FOCUSED).put(DownloadPrimitiveAction.SHORTCUT.getKeyStroke(), "none");
133 lg.getInputMap(JComponent.WHEN_FOCUSED).put(MenuItemSearchDialog.Action.SHORTCUT.getKeyStroke(), "none");
[10620]134 lg.setTransferHandler(null);
[3530]135
[1350]136 JScrollPane scroller = new JScrollPane(lg);
[1879]137 scroller.setViewportBorder(new EmptyBorder(10, 100, 10, 100));
[652]138 add(scroller, BorderLayout.CENTER);
[1231]139
[6525]140 getMOTD();
141
[10881]142 setTransferHandler(new OpenTransferHandler());
[6525]143 }
144
145 private void getMOTD() {
[1350]146 // Asynchronously get MOTD to speed-up JOSM startup
[10611]147 Thread t = new Thread((Runnable) () -> {
148 if (!contentInitialized && Main.pref.getBoolean("help.displaymotd", true)) {
149 try {
150 content = new MotdContent().updateIfRequiredString();
151 contentInitialized = true;
[10634]152 ProxyPreference.removeProxyPreferenceListener(this);
[10611]153 } catch (IOException ex) {
[12620]154 Logging.log(Logging.LEVEL_WARN, tr("Failed to read MOTD. Exception was: {0}", ex.toString()), ex);
[10611]155 content = "<html>" + STYLE + "<h1>" + "JOSM - " + tr("Java OpenStreetMap Editor")
156 + "</h1>\n<h2 align=\"center\">(" + tr("Message of the day not available") + ")</h2></html>";
157 // In case of MOTD not loaded because of proxy error, listen to preference changes to retry after update
[10634]158 ProxyPreference.addProxyPreferenceListener(this);
[1879]159 }
[10611]160 }
[1450]161
[10611]162 if (content != null) {
163 EventQueue.invokeLater(() -> lg.setText(fixImageLinks(content)));
[1350]164 }
165 }, "MOTD-Loader");
166 t.setDaemon(true);
167 t.start();
[652]168 }
[4863]169
[11171]170 static String fixImageLinks(String s) {
171 Matcher m = Pattern.compile("src=\"/browser/trunk(/images/.*?\\.png)\\?format=raw\"").matcher(s);
[4863]172 StringBuffer sb = new StringBuffer();
173 while (m.find()) {
174 String im = m.group(1);
[11171]175 URL u = GettingStarted.class.getResource(im);
[4865]176 if (u != null) {
[8846]177 m.appendReplacement(sb, Matcher.quoteReplacement("src=\"" + u + '\"'));
[4863]178 }
179 }
180 m.appendTail(sb);
181 return sb.toString();
182 }
[6525]183
184 @Override
185 public void proxyPreferenceChanged() {
186 getMOTD();
187 }
[608]188}
Note: See TracBrowser for help on using the repository browser.