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

Last change on this file since 17318 was 17299, checked in by simon04, 3 years ago

see #19819 - Obtain link color using UIManager (default to JOSM blue)

  • Property svn:eol-style set to native
File size: 8.8 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.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.Timer;
18import javax.swing.border.EmptyBorder;
19import javax.swing.event.HyperlinkEvent;
20import javax.swing.event.HyperlinkListener;
21
22import org.openstreetmap.josm.actions.DownloadPrimitiveAction;
23import org.openstreetmap.josm.actions.HistoryInfoAction;
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.NetworkManager;
33import org.openstreetmap.josm.io.OnlineResource;
34import org.openstreetmap.josm.spi.preferences.Config;
35import org.openstreetmap.josm.tools.ImageProvider;
36import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
37import org.openstreetmap.josm.tools.LanguageInfo;
38import org.openstreetmap.josm.tools.Logging;
39import org.openstreetmap.josm.tools.OpenBrowser;
40import org.openstreetmap.josm.tools.Utils;
41import 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 */
49public 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() + "; }\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}
Note: See TracBrowser for help on using the repository browser.