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

Last change on this file since 12927 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

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