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