001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.BorderLayout;
007import java.awt.EventQueue;
008import java.io.IOException;
009import java.net.URL;
010import java.nio.charset.StandardCharsets;
011import java.util.regex.Matcher;
012import java.util.regex.Pattern;
013
014import javax.swing.JComponent;
015import javax.swing.JPanel;
016import javax.swing.JScrollPane;
017import javax.swing.border.EmptyBorder;
018import javax.swing.event.HyperlinkEvent;
019import javax.swing.event.HyperlinkListener;
020
021import org.openstreetmap.josm.Main;
022import org.openstreetmap.josm.actions.DownloadPrimitiveAction;
023import org.openstreetmap.josm.data.Version;
024import org.openstreetmap.josm.gui.datatransfer.OpenTransferHandler;
025import org.openstreetmap.josm.gui.dialogs.MenuItemSearchDialog;
026import org.openstreetmap.josm.gui.preferences.server.ProxyPreference;
027import org.openstreetmap.josm.gui.preferences.server.ProxyPreferenceListener;
028import org.openstreetmap.josm.gui.widgets.JosmEditorPane;
029import org.openstreetmap.josm.io.CacheCustomContent;
030import org.openstreetmap.josm.io.OnlineResource;
031import org.openstreetmap.josm.spi.preferences.Config;
032import org.openstreetmap.josm.tools.LanguageInfo;
033import org.openstreetmap.josm.tools.Logging;
034import org.openstreetmap.josm.tools.OpenBrowser;
035import org.openstreetmap.josm.tools.WikiReader;
036
037/**
038 * Panel that fills the main part of the program window when JOSM has just started.
039 *
040 * It downloads and displays the so called <em>message of the day</em>, which
041 * contains news about recent major changes, warning in case of outdated versions, etc.
042 */
043public final class GettingStarted extends JPanel implements ProxyPreferenceListener {
044
045    private final LinkGeneral lg;
046    private String content = "";
047    private boolean contentInitialized;
048
049    private static final String STYLE = "<style type=\"text/css\">\n"
050            + "body {font-family: sans-serif; font-weight: bold; }\n"
051            + "h1 {text-align: center; }\n"
052            + ".icon {font-size: 0; }\n"
053            + "</style>\n";
054
055    public static class LinkGeneral extends JosmEditorPane implements HyperlinkListener {
056
057        /**
058         * Constructs a new {@code LinkGeneral} with the given HTML text
059         * @param text The text to display
060         */
061        public LinkGeneral(String text) {
062            setContentType("text/html");
063            setText(text);
064            setEditable(false);
065            setOpaque(false);
066            addHyperlinkListener(this);
067            adaptForNimbus(this);
068        }
069
070        @Override
071        public void hyperlinkUpdate(HyperlinkEvent e) {
072            if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
073                OpenBrowser.displayUrl(e.getDescription());
074            }
075        }
076    }
077
078    /**
079     * Grabs current MOTD from cache or webpage and parses it.
080     */
081    static class MotdContent extends CacheCustomContent<IOException> {
082        MotdContent() {
083            super("motd.html", CacheCustomContent.INTERVAL_DAILY);
084        }
085
086        private final int myVersion = Version.getInstance().getVersion();
087        private final String myJava = System.getProperty("java.version");
088        private final String myLang = LanguageInfo.getWikiLanguagePrefix();
089
090        /**
091         * This function gets executed whenever the cached files need updating
092         * @see org.openstreetmap.josm.io.CacheCustomContent#updateData()
093         */
094        @Override
095        protected byte[] updateData() throws IOException {
096            String motd = new WikiReader().readLang("StartupPage");
097            // Save this to prefs in case JOSM is updated so MOTD can be refreshed
098            Config.getPref().putInt("cache.motd.html.version", myVersion);
099            Config.getPref().put("cache.motd.html.java", myJava);
100            Config.getPref().put("cache.motd.html.lang", myLang);
101            return motd.getBytes(StandardCharsets.UTF_8);
102        }
103
104        @Override
105        protected void checkOfflineAccess() {
106            OnlineResource.JOSM_WEBSITE.checkOfflineAccess(new WikiReader().getBaseUrlWiki(), Main.getJOSMWebsite());
107        }
108
109        /**
110         * Additionally check if JOSM has been updated and refresh MOTD
111         */
112        @Override
113        protected boolean isCacheValid() {
114            // We assume a default of myVersion because it only kicks in in two cases:
115            // 1. Not yet written - but so isn't the interval variable, so it gets updated anyway
116            // 2. Cannot be written (e.g. while developing). Obviously we don't want to update
117            // everytime because of something we can't read.
118            return (Config.getPref().getInt("cache.motd.html.version", -999) == myVersion)
119            && Config.getPref().get("cache.motd.html.java").equals(myJava)
120            && Config.getPref().get("cache.motd.html.lang").equals(myLang);
121        }
122    }
123
124    /**
125     * Initializes getting the MOTD as well as enabling the FileDrop Listener. Displays a message
126     * while the MOTD is downloading.
127     */
128    public GettingStarted() {
129        super(new BorderLayout());
130        lg = new LinkGeneral("<html>" + STYLE + "<h1>" + "JOSM - " + tr("Java OpenStreetMap Editor")
131                + "</h1><h2 align=\"center\">" + tr("Downloading \"Message of the day\"") + "</h2></html>");
132        // clear the build-in command ctrl+shift+O, ctrl+space because it is used as shortcut in JOSM
133        lg.getInputMap(JComponent.WHEN_FOCUSED).put(DownloadPrimitiveAction.SHORTCUT.getKeyStroke(), "none");
134        lg.getInputMap(JComponent.WHEN_FOCUSED).put(MenuItemSearchDialog.Action.SHORTCUT.getKeyStroke(), "none");
135        lg.setTransferHandler(null);
136
137        JScrollPane scroller = new JScrollPane(lg);
138        scroller.setViewportBorder(new EmptyBorder(10, 100, 10, 100));
139        add(scroller, BorderLayout.CENTER);
140
141        getMOTD();
142
143        setTransferHandler(new OpenTransferHandler());
144    }
145
146    private void getMOTD() {
147        // Asynchronously get MOTD to speed-up JOSM startup
148        Thread t = new Thread((Runnable) () -> {
149            if (!contentInitialized && Config.getPref().getBoolean("help.displaymotd", true)) {
150                try {
151                    content = new MotdContent().updateIfRequiredString();
152                    contentInitialized = true;
153                    ProxyPreference.removeProxyPreferenceListener(this);
154                } catch (IOException ex) {
155                    Logging.log(Logging.LEVEL_WARN, tr("Failed to read MOTD. Exception was: {0}", ex.toString()), ex);
156                    content = "<html>" + STYLE + "<h1>" + "JOSM - " + tr("Java OpenStreetMap Editor")
157                            + "</h1>\n<h2 align=\"center\">(" + tr("Message of the day not available") + ")</h2></html>";
158                    // In case of MOTD not loaded because of proxy error, listen to preference changes to retry after update
159                    ProxyPreference.addProxyPreferenceListener(this);
160                }
161            }
162
163            if (content != null) {
164                EventQueue.invokeLater(() -> lg.setText(fixImageLinks(content)));
165            }
166        }, "MOTD-Loader");
167        t.setDaemon(true);
168        t.start();
169    }
170
171    static String fixImageLinks(String s) {
172        Matcher m = Pattern.compile("src=\"/browser/trunk(/images/.*?\\.png)\\?format=raw\"").matcher(s);
173        StringBuffer sb = new StringBuffer();
174        while (m.find()) {
175            String im = m.group(1);
176            URL u = GettingStarted.class.getResource(im);
177            if (u != null) {
178                m.appendReplacement(sb, Matcher.quoteReplacement("src=\"" + u + '\"'));
179            }
180        }
181        m.appendTail(sb);
182        return sb.toString();
183    }
184
185    @Override
186    public void proxyPreferenceChanged() {
187        getMOTD();
188    }
189}