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

Last change on this file since 13060 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
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.EventQueue;
8import java.io.IOException;
9import java.net.URL;
10import java.nio.charset.StandardCharsets;
11import java.util.regex.Matcher;
12import java.util.regex.Pattern;
13
14import javax.swing.JComponent;
15import javax.swing.JPanel;
16import javax.swing.JScrollPane;
17import javax.swing.border.EmptyBorder;
18import javax.swing.event.HyperlinkEvent;
19import javax.swing.event.HyperlinkListener;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.actions.DownloadPrimitiveAction;
23import org.openstreetmap.josm.data.Version;
24import org.openstreetmap.josm.gui.datatransfer.OpenTransferHandler;
25import org.openstreetmap.josm.gui.dialogs.MenuItemSearchDialog;
26import org.openstreetmap.josm.gui.preferences.server.ProxyPreference;
27import org.openstreetmap.josm.gui.preferences.server.ProxyPreferenceListener;
28import org.openstreetmap.josm.gui.widgets.JosmEditorPane;
29import org.openstreetmap.josm.io.CacheCustomContent;
30import org.openstreetmap.josm.io.OnlineResource;
31import org.openstreetmap.josm.spi.preferences.Config;
32import org.openstreetmap.josm.tools.LanguageInfo;
33import org.openstreetmap.josm.tools.Logging;
34import org.openstreetmap.josm.tools.OpenBrowser;
35import org.openstreetmap.josm.tools.WikiReader;
36
37/**
38 * Panel that fills the main part of the program window when JOSM has just started.
39 *
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 */
43public final class GettingStarted extends JPanel implements ProxyPreferenceListener {
44
45 private final LinkGeneral lg;
46 private String content = "";
47 private boolean contentInitialized;
48
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";
54
55 public static class LinkGeneral extends JosmEditorPane implements HyperlinkListener {
56
57 /**
58 * Constructs a new {@code LinkGeneral} with the given HTML text
59 * @param text The text to display
60 */
61 public LinkGeneral(String text) {
62 setContentType("text/html");
63 setText(text);
64 setEditable(false);
65 setOpaque(false);
66 addHyperlinkListener(this);
67 adaptForNimbus(this);
68 }
69
70 @Override
71 public void hyperlinkUpdate(HyperlinkEvent e) {
72 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
73 OpenBrowser.displayUrl(e.getDescription());
74 }
75 }
76 }
77
78 /**
79 * Grabs current MOTD from cache or webpage and parses it.
80 */
81 static class MotdContent extends CacheCustomContent<IOException> {
82 MotdContent() {
83 super("motd.html", CacheCustomContent.INTERVAL_DAILY);
84 }
85
86 private final int myVersion = Version.getInstance().getVersion();
87 private final String myJava = System.getProperty("java.version");
88 private final String myLang = LanguageInfo.getWikiLanguagePrefix();
89
90 /**
91 * This function gets executed whenever the cached files need updating
92 * @see org.openstreetmap.josm.io.CacheCustomContent#updateData()
93 */
94 @Override
95 protected byte[] updateData() throws IOException {
96 String motd = new WikiReader().readLang("StartupPage");
97 // Save this to prefs in case JOSM is updated so MOTD can be refreshed
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);
101 return motd.getBytes(StandardCharsets.UTF_8);
102 }
103
104 @Override
105 protected void checkOfflineAccess() {
106 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(new WikiReader().getBaseUrlWiki(), Main.getJOSMWebsite());
107 }
108
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
117 // everytime because of something we can't read.
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);
121 }
122 }
123
124 /**
125 * Initializes getting the MOTD as well as enabling the FileDrop Listener. Displays a message
126 * while the MOTD is downloading.
127 */
128 public GettingStarted() {
129 super(new BorderLayout());
130 lg = new LinkGeneral("<html>" + STYLE + "<h1>" + "JOSM - " + tr("Java OpenStreetMap Editor")
131 + "</h1><h2 align=\"center\">" + tr("Downloading \"Message of the day\"") + "</h2></html>");
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");
135 lg.setTransferHandler(null);
136
137 JScrollPane scroller = new JScrollPane(lg);
138 scroller.setViewportBorder(new EmptyBorder(10, 100, 10, 100));
139 add(scroller, BorderLayout.CENTER);
140
141 getMOTD();
142
143 setTransferHandler(new OpenTransferHandler());
144 }
145
146 private void getMOTD() {
147 // Asynchronously get MOTD to speed-up JOSM startup
148 Thread t = new Thread((Runnable) () -> {
149 if (!contentInitialized && Config.getPref().getBoolean("help.displaymotd", true)) {
150 try {
151 content = new MotdContent().updateIfRequiredString();
152 contentInitialized = true;
153 ProxyPreference.removeProxyPreferenceListener(this);
154 } catch (IOException ex) {
155 Logging.log(Logging.LEVEL_WARN, tr("Failed to read MOTD. Exception was: {0}", ex.toString()), ex);
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
159 ProxyPreference.addProxyPreferenceListener(this);
160 }
161 }
162
163 if (content != null) {
164 EventQueue.invokeLater(() -> lg.setText(fixImageLinks(content)));
165 }
166 }, "MOTD-Loader");
167 t.setDaemon(true);
168 t.start();
169 }
170
171 static String fixImageLinks(String s) {
172 Matcher m = Pattern.compile("src=\"/browser/trunk(/images/.*?\\.png)\\?format=raw\"").matcher(s);
173 StringBuffer sb = new StringBuffer();
174 while (m.find()) {
175 String im = m.group(1);
176 URL u = GettingStarted.class.getResource(im);
177 if (u != null) {
178 m.appendReplacement(sb, Matcher.quoteReplacement("src=\"" + u + '\"'));
179 }
180 }
181 m.appendTail(sb);
182 return sb.toString();
183 }
184
185 @Override
186 public void proxyPreferenceChanged() {
187 getMOTD();
188 }
189}
Note: See TracBrowser for help on using the repository browser.