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

Last change on this file since 8148 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
RevLine 
[608]1// License: GPL. See LICENSE file for details.
2
3package org.openstreetmap.josm.gui;
4
[582]5import static org.openstreetmap.josm.tools.I18n.tr;
6
[623]7import java.awt.BorderLayout;
[1350]8import java.awt.EventQueue;
[7075]9import java.awt.GraphicsEnvironment;
[3171]10import java.awt.event.InputEvent;
11import java.awt.event.KeyEvent;
[5061]12import java.io.IOException;
[4863]13import java.net.URL;
[7082]14import java.nio.charset.StandardCharsets;
[4863]15import java.util.regex.Matcher;
16import java.util.regex.Pattern;
[623]17
[3171]18import javax.swing.JComponent;
[582]19import javax.swing.JPanel;
[1879]20import javax.swing.JScrollPane;
[3171]21import javax.swing.KeyStroke;
[1879]22import javax.swing.border.EmptyBorder;
[582]23import javax.swing.event.HyperlinkEvent;
24import javax.swing.event.HyperlinkListener;
25
26import org.openstreetmap.josm.Main;
[2358]27import org.openstreetmap.josm.data.Version;
[6525]28import org.openstreetmap.josm.gui.preferences.server.ProxyPreference;
29import org.openstreetmap.josm.gui.preferences.server.ProxyPreferenceListener;
[5886]30import org.openstreetmap.josm.gui.widgets.JosmEditorPane;
[1450]31import org.openstreetmap.josm.io.CacheCustomContent;
[7434]32import org.openstreetmap.josm.io.OnlineResource;
[1755]33import org.openstreetmap.josm.tools.LanguageInfo;
[582]34import org.openstreetmap.josm.tools.OpenBrowser;
[623]35import org.openstreetmap.josm.tools.WikiReader;
[608]36
[6525]37public final class GettingStarted extends JPanel implements ProxyPreferenceListener {
38
39 private final LinkGeneral lg;
[1450]40 private String content = "";
[6525]41 private boolean contentInitialized = false;
42
[4364]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";
[608]48
[5886]49 public static class LinkGeneral extends JosmEditorPane implements HyperlinkListener {
[6070]50
[5886]51 /**
52 * Constructs a new {@code LinkGeneral} with the given HTML text
53 * @param text The text to display
54 */
[652]55 public LinkGeneral(String text) {
56 setContentType("text/html");
57 setText(text);
58 setEditable(false);
59 setOpaque(false);
60 addHyperlinkListener(this);
[6935]61 adaptForNimbus(this);
[608]62 }
[1879]63
[6084]64 @Override
[652]65 public void hyperlinkUpdate(HyperlinkEvent e) {
66 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
67 OpenBrowser.displayUrl(e.getDescription());
68 }
[608]69 }
70 }
[1359]71
[1450]72 /**
73 * Grabs current MOTD from cache or webpage and parses it.
74 */
[5061]75 private static class MotdContent extends CacheCustomContent<IOException> {
[1879]76 public MotdContent() {
[1450]77 super("motd.html", CacheCustomContent.INTERVAL_DAILY);
[1290]78 }
[574]79
[6889]80 private final int myVersion = Version.getInstance().getVersion();
81 private final String myJava = System.getProperty("java.version");
82 private final String myLang = LanguageInfo.getWikiLanguagePrefix();
[608]83
[1450]84 /**
85 * This function gets executed whenever the cached files need updating
86 * @see org.openstreetmap.josm.io.CacheCustomContent#updateData()
87 */
[1879]88 @Override
[5061]89 protected byte[] updateData() throws IOException {
[1512]90 String motd = new WikiReader().readLang("StartupPage");
[1450]91 // Save this to prefs in case JOSM is updated so MOTD can be refreshed
92 Main.pref.putInteger("cache.motd.html.version", myVersion);
[5883]93 Main.pref.put("cache.motd.html.java", myJava);
[1562]94 Main.pref.put("cache.motd.html.lang", myLang);
[7082]95 return motd.getBytes(StandardCharsets.UTF_8);
[1350]96 }
[1359]97
[7434]98 @Override
99 protected void checkOfflineAccess() {
100 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(new WikiReader().getBaseUrlWiki(), Main.getJOSMWebsite());
101 }
102
[1450]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
[1879]111 // everytime because of something we can't read.
[1746]112 return (Main.pref.getInteger("cache.motd.html.version", -999) == myVersion)
[5882]113 && Main.pref.get("cache.motd.html.java").equals(myJava)
[1647]114 && Main.pref.get("cache.motd.html.lang").equals(myLang);
[1450]115 }
[608]116 }
[1169]117
[1450]118 /**
[1879]119 * Initializes getting the MOTD as well as enabling the FileDrop Listener. Displays a message
120 * while the MOTD is downloading.
[1450]121 */
[652]122 public GettingStarted() {
123 super(new BorderLayout());
[6525]124 lg = new LinkGeneral("<html>" + STYLE + "<h1>" + "JOSM - " + tr("Java OpenStreetMap Editor")
[4364]125 + "</h1><h2 align=\"center\">" + tr("Downloading \"Message of the day\"") + "</h2></html>");
[3171]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");
[3530]128
[1350]129 JScrollPane scroller = new JScrollPane(lg);
[1879]130 scroller.setViewportBorder(new EmptyBorder(10, 100, 10, 100));
[652]131 add(scroller, BorderLayout.CENTER);
[1231]132
[6525]133 getMOTD();
134
[7075]135 if (!GraphicsEnvironment.isHeadless()) {
136 new FileDrop(scroller);
137 }
[6525]138 }
139
140 private void getMOTD() {
[1350]141 // Asynchronously get MOTD to speed-up JOSM startup
142 Thread t = new Thread(new Runnable() {
[5061]143 @Override
[1350]144 public void run() {
[6525]145 if (!contentInitialized && Main.pref.getBoolean("help.displaymotd", true)) {
[5061]146 try {
147 content = new MotdContent().updateIfRequiredString();
[6525]148 contentInitialized = true;
149 ProxyPreference.removeProxyPreferenceListener(GettingStarted.this);
[5061]150 } catch (IOException ex) {
[6248]151 Main.warn(tr("Failed to read MOTD. Exception was: {0}", ex.toString()));
[5061]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>";
[6525]154 // In case of MOTD not loaded because of proxy error, listen to preference changes to retry after update
155 ProxyPreference.addProxyPreferenceListener(GettingStarted.this);
[5061]156 }
[1879]157 }
[1450]158
[7434]159 if (content != null) {
160 EventQueue.invokeLater(new Runnable() {
161 @Override
162 public void run() {
163 lg.setText(fixImageLinks(content));
164 }
165 });
166 }
[1350]167 }
168 }, "MOTD-Loader");
169 t.setDaemon(true);
170 t.start();
[652]171 }
[4863]172
173 private String fixImageLinks(String s) {
[6897]174 Matcher m = Pattern.compile("src=\""+Main.getJOSMWebsite()+"/browser/trunk(/images/.*?\\.png)\\?format=raw\"").matcher(s);
[4863]175 StringBuffer sb = new StringBuffer();
176 while (m.find()) {
177 String im = m.group(1);
178 URL u = getClass().getResource(im);
[4865]179 if (u != null) {
180 m.appendReplacement(sb, Matcher.quoteReplacement("src=\"" + u.toString() + "\""));
[4863]181 }
182 }
183 m.appendTail(sb);
184 return sb.toString();
185 }
[6525]186
187 @Override
188 public void proxyPreferenceChanged() {
189 getMOTD();
190 }
[608]191}
Note: See TracBrowser for help on using the repository browser.