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

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

Robustness to allow to run more unit tests in headless mode

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