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