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