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

Last change on this file since 16695 was 16502, checked in by simon04, 4 years ago

fix #19295 - Ctrl+H doesn't work when getting started screen is focused

  • Property svn:eol-style set to native
File size: 8.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.awt.Graphics;
9import java.io.IOException;
10import java.net.URL;
11import java.nio.charset.StandardCharsets;
12import java.util.regex.Matcher;
13import java.util.regex.Pattern;
14
15import javax.swing.JComponent;
16import javax.swing.JPanel;
17import javax.swing.JScrollPane;
18import javax.swing.Timer;
19import javax.swing.border.EmptyBorder;
20import javax.swing.event.HyperlinkEvent;
21import javax.swing.event.HyperlinkListener;
22
23import org.openstreetmap.josm.actions.DownloadPrimitiveAction;
24import org.openstreetmap.josm.actions.HistoryInfoAction;
25import org.openstreetmap.josm.data.Version;
26import org.openstreetmap.josm.gui.animation.AnimationExtensionManager;
27import org.openstreetmap.josm.gui.datatransfer.OpenTransferHandler;
28import org.openstreetmap.josm.gui.dialogs.MenuItemSearchDialog;
29import org.openstreetmap.josm.gui.preferences.server.ProxyPreference;
30import org.openstreetmap.josm.gui.preferences.server.ProxyPreferenceListener;
31import org.openstreetmap.josm.gui.widgets.JosmEditorPane;
32import org.openstreetmap.josm.io.CacheCustomContent;
33import org.openstreetmap.josm.io.NetworkManager;
34import org.openstreetmap.josm.io.OnlineResource;
35import org.openstreetmap.josm.spi.preferences.Config;
36import org.openstreetmap.josm.tools.LanguageInfo;
37import org.openstreetmap.josm.tools.Logging;
38import org.openstreetmap.josm.tools.OpenBrowser;
39import org.openstreetmap.josm.tools.Utils;
40import org.openstreetmap.josm.tools.WikiReader;
41
42/**
43 * Panel that fills the main part of the program window when JOSM has just started.
44 *
45 * It downloads and displays the so called <em>message of the day</em>, which
46 * contains news about recent major changes, warning in case of outdated versions, etc.
47 */
48public final class GettingStarted extends JPanel implements ProxyPreferenceListener {
49
50 private final LinkGeneral lg;
51 private String content = "";
52 private boolean contentInitialized;
53 private final Timer timer = new Timer(50, e -> repaint());
54
55 private static final String STYLE = "<style type=\"text/css\">\n"
56 + "body {font-family: sans-serif; font-weight: bold; }\n"
57 + "h1 {text-align: center; }\n"
58 + ".icon {font-size: 0; }\n"
59 + "</style>\n";
60
61 public static class LinkGeneral extends JosmEditorPane implements HyperlinkListener {
62
63 /**
64 * Constructs a new {@code LinkGeneral} with the given HTML text
65 * @param text The text to display
66 */
67 public LinkGeneral(String text) {
68 setContentType("text/html");
69 setText(text);
70 setEditable(false);
71 setOpaque(false);
72 addHyperlinkListener(this);
73 adaptForNimbus(this);
74 }
75
76 @Override
77 public void hyperlinkUpdate(HyperlinkEvent e) {
78 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
79 OpenBrowser.displayUrl(e.getDescription());
80 }
81 }
82 }
83
84 /**
85 * Grabs current MOTD from cache or webpage and parses it.
86 */
87 static class MotdContent extends CacheCustomContent<IOException> {
88 MotdContent() {
89 super("motd.html", CacheCustomContent.INTERVAL_DAILY);
90 }
91
92 private final int myVersion = Version.getInstance().getVersion();
93 private final String myJava = Utils.getSystemProperty("java.version");
94 private final String myLang = LanguageInfo.getWikiLanguagePrefix();
95
96 /**
97 * This function gets executed whenever the cached files need updating
98 * @see org.openstreetmap.josm.io.CacheCustomContent#updateData()
99 */
100 @Override
101 protected byte[] updateData() throws IOException {
102 String motd = new WikiReader().readLang("StartupPage");
103 // Save this to prefs in case JOSM is updated so MOTD can be refreshed
104 Config.getPref().putInt("cache.motd.html.version", myVersion);
105 Config.getPref().put("cache.motd.html.java", myJava);
106 Config.getPref().put("cache.motd.html.lang", myLang);
107 return motd.getBytes(StandardCharsets.UTF_8);
108 }
109
110 @Override
111 protected boolean isOffline() {
112 return NetworkManager.isOffline(OnlineResource.JOSM_WEBSITE);
113 }
114
115 /**
116 * Additionally check if JOSM has been updated and refresh MOTD
117 */
118 @Override
119 protected boolean isCacheValid() {
120 // We assume a default of myVersion because it only kicks in in two cases:
121 // 1. Not yet written - but so isn't the interval variable, so it gets updated anyway
122 // 2. Cannot be written (e.g. while developing). Obviously we don't want to update
123 // every time because of something we can't read.
124 return (Config.getPref().getInt("cache.motd.html.version", -999) == myVersion)
125 && Config.getPref().get("cache.motd.html.java").equals(myJava)
126 && Config.getPref().get("cache.motd.html.lang").equals(myLang);
127 }
128 }
129
130 /**
131 * Initializes getting the MOTD as well as enabling the FileDrop Listener. Displays a message
132 * while the MOTD is downloading.
133 */
134 public GettingStarted() {
135 super(new BorderLayout());
136 lg = new LinkGeneral("<html>" + STYLE + "<h1>" + "JOSM - " + tr("Java OpenStreetMap Editor")
137 + "</h1><h2 align=\"center\">" + tr("Downloading \"Message of the day\"") + "</h2></html>");
138 // clear the build-in command ctrl+shift+O, ctrl+space, ctrl+H because it is used as shortcut in JOSM
139 lg.getInputMap(JComponent.WHEN_FOCUSED).put(DownloadPrimitiveAction.SHORTCUT.getKeyStroke(), "none");
140 lg.getInputMap(JComponent.WHEN_FOCUSED).put(MenuItemSearchDialog.Action.SHORTCUT.getKeyStroke(), "none");
141 lg.getInputMap(JComponent.WHEN_FOCUSED).put(HistoryInfoAction.SHORTCUT.getKeyStroke(), "none");
142 lg.setTransferHandler(null);
143
144 JScrollPane scroller = new JScrollPane(lg);
145 scroller.setViewportBorder(new EmptyBorder(10, 100, 10, 100));
146 add(scroller, BorderLayout.CENTER);
147
148 getMOTD();
149
150 setTransferHandler(new OpenTransferHandler());
151 }
152
153 @Override
154 public void addNotify() {
155 timer.start();
156 super.addNotify();
157 }
158
159 @Override
160 public void removeNotify() {
161 timer.stop();
162 super.removeNotify();
163 }
164
165 @Override
166 public void paint(Graphics g) {
167 super.paint(g);
168 if (isShowing()) {
169 AnimationExtensionManager.getExtension().adjustForSize(getWidth(), getHeight());
170 AnimationExtensionManager.getExtension().animate();
171 AnimationExtensionManager.getExtension().paint(g);
172 }
173 }
174
175 private void getMOTD() {
176 // Asynchronously get MOTD to speed-up JOSM startup
177 Thread t = new Thread((Runnable) () -> {
178 if (!contentInitialized && Config.getPref().getBoolean("help.displaymotd", true)) {
179 try {
180 content = new MotdContent().updateIfRequiredString();
181 contentInitialized = true;
182 ProxyPreference.removeProxyPreferenceListener(this);
183 } catch (IOException ex) {
184 Logging.log(Logging.LEVEL_WARN, tr("Failed to read MOTD. Exception was: {0}", ex.toString()), ex);
185 content = "<html>" + STYLE + "<h1>" + "JOSM - " + tr("Java OpenStreetMap Editor")
186 + "</h1>\n<h2 align=\"center\">(" + tr("Message of the day not available") + ")</h2></html>";
187 // In case of MOTD not loaded because of proxy error, listen to preference changes to retry after update
188 ProxyPreference.addProxyPreferenceListener(this);
189 }
190 }
191
192 if (content != null) {
193 EventQueue.invokeLater(() -> lg.setText(fixImageLinks(content)));
194 }
195 }, "MOTD-Loader");
196 t.setDaemon(true);
197 t.start();
198 }
199
200 static String fixImageLinks(String s) {
201 Matcher m = Pattern.compile("src=\"/browser/trunk/resources(/images/.*?\\.png)\\?format=raw\"").matcher(s);
202 StringBuffer sb = new StringBuffer();
203 while (m.find()) {
204 String im = m.group(1);
205 URL u = GettingStarted.class.getResource(im);
206 if (u != null) {
207 try {
208 m.appendReplacement(sb, Matcher.quoteReplacement("src=\"" + Utils.betterJarUrl(u, u) + '\"'));
209 } catch (IOException e) {
210 Logging.error(e);
211 }
212 }
213 }
214 m.appendTail(sb);
215 return sb.toString();
216 }
217
218 @Override
219 public void proxyPreferenceChanged() {
220 getMOTD();
221 }
222}
Note: See TracBrowser for help on using the repository browser.