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

Last change on this file since 10694 was 10634, checked in by Don-vip, 8 years ago

sonar - pmd:UselessQualifiedThis - Useless qualified this usage in the same class

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