[608] | 1 | // License: GPL. See LICENSE file for details.
|
---|
| 2 |
|
---|
| 3 | package org.openstreetmap.josm.gui;
|
---|
| 4 |
|
---|
[582] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
[623] | 7 | import java.awt.BorderLayout;
|
---|
[1350] | 8 | import java.awt.EventQueue;
|
---|
[3171] | 9 | import java.awt.event.InputEvent;
|
---|
| 10 | import java.awt.event.KeyEvent;
|
---|
[3122] | 11 | import java.io.UnsupportedEncodingException;
|
---|
[623] | 12 |
|
---|
[3171] | 13 | import javax.swing.JComponent;
|
---|
[582] | 14 | import javax.swing.JEditorPane;
|
---|
| 15 | import javax.swing.JPanel;
|
---|
[1879] | 16 | import javax.swing.JScrollPane;
|
---|
[3171] | 17 | import javax.swing.KeyStroke;
|
---|
[1879] | 18 | import javax.swing.border.EmptyBorder;
|
---|
[582] | 19 | import javax.swing.event.HyperlinkEvent;
|
---|
| 20 | import javax.swing.event.HyperlinkListener;
|
---|
| 21 |
|
---|
| 22 | import org.openstreetmap.josm.Main;
|
---|
[2358] | 23 | import org.openstreetmap.josm.data.Version;
|
---|
[1450] | 24 | import org.openstreetmap.josm.io.CacheCustomContent;
|
---|
[1755] | 25 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
[582] | 26 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
[623] | 27 | import org.openstreetmap.josm.tools.WikiReader;
|
---|
[608] | 28 |
|
---|
[623] | 29 | public class GettingStarted extends JPanel {
|
---|
[1450] | 30 | private String content = "";
|
---|
[4364] | 31 | private static final String STYLE = "<style type=\"text/css\">\n"
|
---|
| 32 | + "body {font-family: sans-serif; font-weight: bold; }\n"
|
---|
| 33 | + "h1 {text-align: center; }\n"
|
---|
| 34 | + ".icon {font-size: 0; }\n"
|
---|
| 35 | + "</style>\n";
|
---|
[608] | 36 |
|
---|
[2626] | 37 | public static class LinkGeneral extends JEditorPane implements HyperlinkListener {
|
---|
[652] | 38 | public LinkGeneral(String text) {
|
---|
| 39 | setContentType("text/html");
|
---|
| 40 | setText(text);
|
---|
| 41 | setEditable(false);
|
---|
| 42 | setOpaque(false);
|
---|
| 43 | addHyperlinkListener(this);
|
---|
[608] | 44 | }
|
---|
[1879] | 45 |
|
---|
[652] | 46 | public void hyperlinkUpdate(HyperlinkEvent e) {
|
---|
| 47 | if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
|
---|
| 48 | OpenBrowser.displayUrl(e.getDescription());
|
---|
| 49 | }
|
---|
[608] | 50 | }
|
---|
| 51 | }
|
---|
[1359] | 52 |
|
---|
[1450] | 53 | /**
|
---|
| 54 | * Grabs current MOTD from cache or webpage and parses it.
|
---|
| 55 | */
|
---|
[4709] | 56 | private static class MotdContent extends CacheCustomContent<RuntimeException> {
|
---|
[1879] | 57 | public MotdContent() {
|
---|
[1450] | 58 | super("motd.html", CacheCustomContent.INTERVAL_DAILY);
|
---|
[1290] | 59 | }
|
---|
[574] | 60 |
|
---|
[2358] | 61 | final private int myVersion = Version.getInstance().getVersion();
|
---|
[2308] | 62 | final private String myLang = LanguageInfo.getWikiLanguagePrefix();
|
---|
[608] | 63 |
|
---|
[1450] | 64 | /**
|
---|
| 65 | * This function gets executed whenever the cached files need updating
|
---|
| 66 | * @see org.openstreetmap.josm.io.CacheCustomContent#updateData()
|
---|
| 67 | */
|
---|
[1879] | 68 | @Override
|
---|
[1450] | 69 | protected byte[] updateData() {
|
---|
[1512] | 70 | String motd = new WikiReader().readLang("StartupPage");
|
---|
[1879] | 71 | if (motd.length() == 0) {
|
---|
[4364] | 72 | motd = "<html>" + STYLE + "<h1>" + "JOSM - " + tr("Java OpenStreetMap Editor")
|
---|
[1879] | 73 | + "</h1>\n<h2 align=\"center\">(" + tr("Message of the day not available") + ")</h2></html>";
|
---|
[1450] | 74 | }
|
---|
| 75 | // Save this to prefs in case JOSM is updated so MOTD can be refreshed
|
---|
| 76 | Main.pref.putInteger("cache.motd.html.version", myVersion);
|
---|
[1562] | 77 | Main.pref.put("cache.motd.html.lang", myLang);
|
---|
[3122] | 78 | try {
|
---|
| 79 | return motd.getBytes("utf-8");
|
---|
| 80 | } catch(UnsupportedEncodingException e){
|
---|
| 81 | e.printStackTrace();
|
---|
| 82 | return new byte[0];
|
---|
| 83 | }
|
---|
[1350] | 84 | }
|
---|
[1359] | 85 |
|
---|
[1450] | 86 | /**
|
---|
| 87 | * Additionally check if JOSM has been updated and refresh MOTD
|
---|
| 88 | */
|
---|
| 89 | @Override
|
---|
| 90 | protected boolean isCacheValid() {
|
---|
| 91 | // We assume a default of myVersion because it only kicks in in two cases:
|
---|
| 92 | // 1. Not yet written - but so isn't the interval variable, so it gets updated anyway
|
---|
| 93 | // 2. Cannot be written (e.g. while developing). Obviously we don't want to update
|
---|
[1879] | 94 | // everytime because of something we can't read.
|
---|
[1746] | 95 | return (Main.pref.getInteger("cache.motd.html.version", -999) == myVersion)
|
---|
[1647] | 96 | && Main.pref.get("cache.motd.html.lang").equals(myLang);
|
---|
[1450] | 97 | }
|
---|
[608] | 98 | }
|
---|
[1169] | 99 |
|
---|
[1450] | 100 | /**
|
---|
[1879] | 101 | * Initializes getting the MOTD as well as enabling the FileDrop Listener. Displays a message
|
---|
| 102 | * while the MOTD is downloading.
|
---|
[1450] | 103 | */
|
---|
[652] | 104 | public GettingStarted() {
|
---|
| 105 | super(new BorderLayout());
|
---|
[4364] | 106 | final LinkGeneral lg = new LinkGeneral("<html>" + STYLE + "<h1>" + "JOSM - " + tr("Java OpenStreetMap Editor")
|
---|
| 107 | + "</h1><h2 align=\"center\">" + tr("Downloading \"Message of the day\"") + "</h2></html>");
|
---|
[3171] | 108 | // clear the build-in command ctrl+shift+O, because it is used as shortcut in JOSM
|
---|
| 109 | lg.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.SHIFT_MASK | InputEvent.CTRL_MASK), "none");
|
---|
[3530] | 110 |
|
---|
[1350] | 111 | JScrollPane scroller = new JScrollPane(lg);
|
---|
[1879] | 112 | scroller.setViewportBorder(new EmptyBorder(10, 100, 10, 100));
|
---|
[652] | 113 | add(scroller, BorderLayout.CENTER);
|
---|
[1231] | 114 |
|
---|
[1350] | 115 | // Asynchronously get MOTD to speed-up JOSM startup
|
---|
| 116 | Thread t = new Thread(new Runnable() {
|
---|
| 117 | public void run() {
|
---|
[1879] | 118 | if (content.length() == 0 && Main.pref.getBoolean("help.displaymotd", true)) {
|
---|
| 119 | content = new MotdContent().updateIfRequiredString();
|
---|
| 120 | }
|
---|
[1450] | 121 |
|
---|
[1350] | 122 | EventQueue.invokeLater(new Runnable() {
|
---|
| 123 | public void run() {
|
---|
[1879] | 124 | lg.setText(content);
|
---|
| 125 | // lg.moveCaretPosition(0);
|
---|
[1350] | 126 | }
|
---|
| 127 | });
|
---|
| 128 | }
|
---|
| 129 | }, "MOTD-Loader");
|
---|
| 130 | t.setDaemon(true);
|
---|
| 131 | t.start();
|
---|
| 132 |
|
---|
[1370] | 133 | new FileDrop(scroller);
|
---|
[652] | 134 | }
|
---|
[608] | 135 | }
|
---|