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

Last change on this file since 12660 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

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