001// License: GPL. See LICENSE file for details. 002 003package org.openstreetmap.josm.gui; 004 005import static org.openstreetmap.josm.tools.I18n.tr; 006 007import java.awt.BorderLayout; 008import java.awt.EventQueue; 009import java.awt.event.InputEvent; 010import java.awt.event.KeyEvent; 011import java.io.IOException; 012import java.io.UnsupportedEncodingException; 013import java.net.URL; 014import java.util.regex.Matcher; 015import java.util.regex.Pattern; 016 017import javax.swing.JComponent; 018import javax.swing.JPanel; 019import javax.swing.JScrollPane; 020import javax.swing.KeyStroke; 021import javax.swing.border.EmptyBorder; 022import javax.swing.event.HyperlinkEvent; 023import javax.swing.event.HyperlinkListener; 024 025import org.openstreetmap.josm.Main; 026import org.openstreetmap.josm.data.Version; 027import org.openstreetmap.josm.gui.widgets.JosmEditorPane; 028import org.openstreetmap.josm.io.CacheCustomContent; 029import org.openstreetmap.josm.tools.LanguageInfo; 030import org.openstreetmap.josm.tools.OpenBrowser; 031import org.openstreetmap.josm.tools.WikiReader; 032 033public final class GettingStarted extends JPanel { 034 private String content = ""; 035 private static final String STYLE = "<style type=\"text/css\">\n" 036 + "body {font-family: sans-serif; font-weight: bold; }\n" 037 + "h1 {text-align: center; }\n" 038 + ".icon {font-size: 0; }\n" 039 + "</style>\n"; 040 041 public static class LinkGeneral extends JosmEditorPane implements HyperlinkListener { 042 043 /** 044 * Constructs a new {@code LinkGeneral} with the given HTML text 045 * @param text The text to display 046 */ 047 public LinkGeneral(String text) { 048 setContentType("text/html"); 049 setText(text); 050 setEditable(false); 051 setOpaque(false); 052 addHyperlinkListener(this); 053 } 054 055 @Override 056 public void hyperlinkUpdate(HyperlinkEvent e) { 057 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 058 OpenBrowser.displayUrl(e.getDescription()); 059 } 060 } 061 } 062 063 /** 064 * Grabs current MOTD from cache or webpage and parses it. 065 */ 066 private static class MotdContent extends CacheCustomContent<IOException> { 067 public MotdContent() { 068 super("motd.html", CacheCustomContent.INTERVAL_DAILY); 069 } 070 071 final private int myVersion = Version.getInstance().getVersion(); 072 final private String myJava = System.getProperty("java.version"); 073 final private String myLang = LanguageInfo.getWikiLanguagePrefix(); 074 075 /** 076 * This function gets executed whenever the cached files need updating 077 * @see org.openstreetmap.josm.io.CacheCustomContent#updateData() 078 */ 079 @Override 080 protected byte[] updateData() throws IOException { 081 String motd = new WikiReader().readLang("StartupPage"); 082 // Save this to prefs in case JOSM is updated so MOTD can be refreshed 083 Main.pref.putInteger("cache.motd.html.version", myVersion); 084 Main.pref.put("cache.motd.html.java", myJava); 085 Main.pref.put("cache.motd.html.lang", myLang); 086 try { 087 return motd.getBytes("utf-8"); 088 } catch(UnsupportedEncodingException e){ 089 e.printStackTrace(); 090 return new byte[0]; 091 } 092 } 093 094 /** 095 * Additionally check if JOSM has been updated and refresh MOTD 096 */ 097 @Override 098 protected boolean isCacheValid() { 099 // 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 102 // everytime because of something we can't read. 103 return (Main.pref.getInteger("cache.motd.html.version", -999) == myVersion) 104 && Main.pref.get("cache.motd.html.java").equals(myJava) 105 && Main.pref.get("cache.motd.html.lang").equals(myLang); 106 } 107 } 108 109 /** 110 * Initializes getting the MOTD as well as enabling the FileDrop Listener. Displays a message 111 * while the MOTD is downloading. 112 */ 113 public GettingStarted() { 114 super(new BorderLayout()); 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>"); 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"); 119 120 JScrollPane scroller = new JScrollPane(lg); 121 scroller.setViewportBorder(new EmptyBorder(10, 100, 10, 100)); 122 add(scroller, BorderLayout.CENTER); 123 124 // Asynchronously get MOTD to speed-up JOSM startup 125 Thread t = new Thread(new Runnable() { 126 @Override 127 public void run() { 128 if (content.isEmpty() && Main.pref.getBoolean("help.displaymotd", true)) { 129 try { 130 content = new MotdContent().updateIfRequiredString(); 131 } catch (IOException ex) { 132 Main.warn(tr("Failed to read MOTD. Exception was: {0}", ex.toString())); 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 } 136 } 137 138 EventQueue.invokeLater(new Runnable() { 139 @Override 140 public void run() { 141 lg.setText(fixImageLinks(content)); 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=\""+Main.JOSM_WEBSITE+"/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}