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

Last change on this file since 12496 was 12259, checked in by bastiK, 7 years ago

see #14794 - javadoc

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