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

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

fix #8585 - update MOTD when updating Java version (wrong copy/paste, sorry)

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