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

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

fix potential bugs detected by FindBugs

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