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

Last change on this file since 4865 was 4865, checked in by bastiK, 12 years ago

fixed #7209 - do not replace unknown images by dummy image; quote properly

  • Property svn:eol-style set to native
File size: 5.9 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.UnsupportedEncodingException;
12import java.net.URL;
13import java.util.regex.Matcher;
14import java.util.regex.Pattern;
15
16import javax.swing.JComponent;
17import javax.swing.JEditorPane;
18import javax.swing.JPanel;
19import javax.swing.JScrollPane;
20import javax.swing.KeyStroke;
21import javax.swing.border.EmptyBorder;
22import javax.swing.event.HyperlinkEvent;
23import javax.swing.event.HyperlinkListener;
24
25import org.openstreetmap.josm.Main;
26import org.openstreetmap.josm.data.Version;
27import org.openstreetmap.josm.io.CacheCustomContent;
28import org.openstreetmap.josm.tools.LanguageInfo;
29import org.openstreetmap.josm.tools.OpenBrowser;
30import org.openstreetmap.josm.tools.WikiReader;
31
32public class GettingStarted extends JPanel {
33 private String content = "";
34 private static final String STYLE = "<style type=\"text/css\">\n"
35 + "body {font-family: sans-serif; font-weight: bold; }\n"
36 + "h1 {text-align: center; }\n"
37 + ".icon {font-size: 0; }\n"
38 + "</style>\n";
39
40 public static class LinkGeneral extends JEditorPane implements HyperlinkListener {
41 public LinkGeneral(String text) {
42 setContentType("text/html");
43 setText(text);
44 setEditable(false);
45 setOpaque(false);
46 addHyperlinkListener(this);
47 }
48
49 public void hyperlinkUpdate(HyperlinkEvent e) {
50 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
51 OpenBrowser.displayUrl(e.getDescription());
52 }
53 }
54 }
55
56 /**
57 * Grabs current MOTD from cache or webpage and parses it.
58 */
59 private static class MotdContent extends CacheCustomContent<RuntimeException> {
60 public MotdContent() {
61 super("motd.html", CacheCustomContent.INTERVAL_DAILY);
62 }
63
64 final private int myVersion = Version.getInstance().getVersion();
65 final private String myLang = LanguageInfo.getWikiLanguagePrefix();
66
67 /**
68 * This function gets executed whenever the cached files need updating
69 * @see org.openstreetmap.josm.io.CacheCustomContent#updateData()
70 */
71 @Override
72 protected byte[] updateData() {
73 String motd = new WikiReader().readLang("StartupPage");
74 if (motd.length() == 0) {
75 motd = "<html>" + STYLE + "<h1>" + "JOSM - " + tr("Java OpenStreetMap Editor")
76 + "</h1>\n<h2 align=\"center\">(" + tr("Message of the day not available") + ")</h2></html>";
77 }
78 // Save this to prefs in case JOSM is updated so MOTD can be refreshed
79 Main.pref.putInteger("cache.motd.html.version", myVersion);
80 Main.pref.put("cache.motd.html.lang", myLang);
81 try {
82 return motd.getBytes("utf-8");
83 } catch(UnsupportedEncodingException e){
84 e.printStackTrace();
85 return new byte[0];
86 }
87 }
88
89 /**
90 * Additionally check if JOSM has been updated and refresh MOTD
91 */
92 @Override
93 protected boolean isCacheValid() {
94 // We assume a default of myVersion because it only kicks in in two cases:
95 // 1. Not yet written - but so isn't the interval variable, so it gets updated anyway
96 // 2. Cannot be written (e.g. while developing). Obviously we don't want to update
97 // everytime because of something we can't read.
98 return (Main.pref.getInteger("cache.motd.html.version", -999) == myVersion)
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 public void run() {
121 if (content.length() == 0 && Main.pref.getBoolean("help.displaymotd", true)) {
122 content = new MotdContent().updateIfRequiredString();
123 }
124
125 EventQueue.invokeLater(new Runnable() {
126 public void run() {
127 lg.setText(fixImageLinks(content));
128 // lg.moveCaretPosition(0);
129 }
130 });
131 }
132 }, "MOTD-Loader");
133 t.setDaemon(true);
134 t.start();
135
136 new FileDrop(scroller);
137 }
138
139 private String fixImageLinks(String s) {
140 Matcher m = Pattern.compile("src=\"http://josm.openstreetmap.de/browser/trunk(/images/.*?\\.png)\\?format=raw\"").matcher(s);
141 StringBuffer sb = new StringBuffer();
142 while (m.find()) {
143 String im = m.group(1);
144 URL u = getClass().getResource(im);
145 if (u != null) {
146 m.appendReplacement(sb, Matcher.quoteReplacement("src=\"" + u.toString() + "\""));
147 }
148 }
149 m.appendTail(sb);
150 return sb.toString();
151 }
152}
Note: See TracBrowser for help on using the repository browser.