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

Last change on this file since 7447 was 7434, checked in by Don-vip, 10 years ago

fix #8885 (see #4614) - add offline mode with new command line argument --offline which can take one of several of these values (comma separated):

  • josm_website: to disable all accesses to JOSM website (when not cached, disables Getting Started page, help, plugin list, styles, imagery, presets, rules)
  • osm_api: to disable all accesses to OSM API (disables download, upload, changeset queries, history, user message notification)
  • all: alias to disable all values. Currently equivalent to "josm_website,osm_api"

Plus improved javadoc, fixed EDT violations, and fixed a bug with HTTP redirection sent without "Location" header

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