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

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

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