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