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

Last change on this file since 6827 was 6552, checked in by simon04, 10 years ago

Refactoring: introduce Utils.UTF_8 charset to avoid handling of UnsupportedEncodingException

According to the Javadoc of Charset, every implementation of the Java
platform is required to support UTF-8.

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