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

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

It's Christmas time! Animate our "Getting Started" page with shining stars.

Adapted from Icedtea-Web, original code by Jiri Vanek (Red Hat).
See http://icedtea.classpath.org/hg/icedtea-web/rev/87d3081ab573

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