001    // License: GPL. See LICENSE file for details.
002    
003    package org.openstreetmap.josm.gui;
004    
005    import static org.openstreetmap.josm.tools.I18n.tr;
006    
007    import java.awt.BorderLayout;
008    import java.awt.EventQueue;
009    import java.awt.event.InputEvent;
010    import java.awt.event.KeyEvent;
011    import java.io.IOException;
012    import java.io.UnsupportedEncodingException;
013    import java.net.URL;
014    import java.util.regex.Matcher;
015    import java.util.regex.Pattern;
016    
017    import javax.swing.JComponent;
018    import javax.swing.JEditorPane;
019    import javax.swing.JPanel;
020    import javax.swing.JScrollPane;
021    import javax.swing.KeyStroke;
022    import javax.swing.border.EmptyBorder;
023    import javax.swing.event.HyperlinkEvent;
024    import javax.swing.event.HyperlinkListener;
025    
026    import org.openstreetmap.josm.Main;
027    import org.openstreetmap.josm.data.Version;
028    import org.openstreetmap.josm.io.CacheCustomContent;
029    import org.openstreetmap.josm.tools.LanguageInfo;
030    import org.openstreetmap.josm.tools.OpenBrowser;
031    import org.openstreetmap.josm.tools.WikiReader;
032    
033    public class GettingStarted extends JPanel {
034        private String content = "";
035        private static final String STYLE = "<style type=\"text/css\">\n"
036                + "body {font-family: sans-serif; font-weight: bold; }\n"
037                + "h1 {text-align: center; }\n"
038                + ".icon {font-size: 0; }\n"
039                + "</style>\n";
040    
041        public static class LinkGeneral extends JEditorPane implements HyperlinkListener {
042            public LinkGeneral(String text) {
043                setContentType("text/html");
044                setText(text);
045                setEditable(false);
046                setOpaque(false);
047                addHyperlinkListener(this);
048            }
049    
050            public void hyperlinkUpdate(HyperlinkEvent e) {
051                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
052                    OpenBrowser.displayUrl(e.getDescription());
053                }
054            }
055        }
056    
057        /**
058         * Grabs current MOTD from cache or webpage and parses it.
059         */
060        private static class MotdContent extends CacheCustomContent<IOException> {
061            public MotdContent() {
062                super("motd.html", CacheCustomContent.INTERVAL_DAILY);
063            }
064    
065            final private int myVersion = Version.getInstance().getVersion();
066            final private String myLang = LanguageInfo.getWikiLanguagePrefix();
067    
068            /**
069             * This function gets executed whenever the cached files need updating
070             * @see org.openstreetmap.josm.io.CacheCustomContent#updateData()
071             */
072            @Override
073            protected byte[] updateData() throws IOException {
074                String motd = new WikiReader().readLang("StartupPage");
075                // Save this to prefs in case JOSM is updated so MOTD can be refreshed
076                Main.pref.putInteger("cache.motd.html.version", myVersion);
077                Main.pref.put("cache.motd.html.lang", myLang);
078                try {
079                    return motd.getBytes("utf-8");
080                } catch(UnsupportedEncodingException e){
081                    e.printStackTrace();
082                    return new byte[0];
083                }
084            }
085    
086            /**
087             * Additionally check if JOSM has been updated and refresh MOTD
088             */
089            @Override
090            protected boolean isCacheValid() {
091                // We assume a default of myVersion because it only kicks in in two cases:
092                // 1. Not yet written - but so isn't the interval variable, so it gets updated anyway
093                // 2. Cannot be written (e.g. while developing). Obviously we don't want to update
094                // everytime because of something we can't read.
095                return (Main.pref.getInteger("cache.motd.html.version", -999) == myVersion)
096                && Main.pref.get("cache.motd.html.lang").equals(myLang);
097            }
098        }
099    
100        /**
101         * Initializes getting the MOTD as well as enabling the FileDrop Listener. Displays a message
102         * while the MOTD is downloading.
103         */
104        public GettingStarted() {
105            super(new BorderLayout());
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>");
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");
110    
111            JScrollPane scroller = new JScrollPane(lg);
112            scroller.setViewportBorder(new EmptyBorder(10, 100, 10, 100));
113            add(scroller, BorderLayout.CENTER);
114    
115            // Asynchronously get MOTD to speed-up JOSM startup
116            Thread t = new Thread(new Runnable() {
117                @Override
118                public void run() {
119                    if (content.isEmpty() && Main.pref.getBoolean("help.displaymotd", true)) {
120                        try {
121                            content = new MotdContent().updateIfRequiredString();
122                        } catch (IOException ex) {
123                            System.out.println(tr("Warning: failed to read MOTD. Exception was: {0}", ex.toString()));
124                            content = "<html>" + STYLE + "<h1>" + "JOSM - " + tr("Java OpenStreetMap Editor")
125                                    + "</h1>\n<h2 align=\"center\">(" + tr("Message of the day not available") + ")</h2></html>";
126                        }
127                    }
128    
129                    EventQueue.invokeLater(new Runnable() {
130                        @Override
131                        public void run() {
132                            lg.setText(fixImageLinks(content));
133                            // lg.moveCaretPosition(0);
134                        }
135                    });
136                }
137            }, "MOTD-Loader");
138            t.setDaemon(true);
139            t.start();
140    
141            new FileDrop(scroller);
142        }
143    
144        private String fixImageLinks(String s) {
145            Matcher m = Pattern.compile("src=\"http://josm.openstreetmap.de/browser/trunk(/images/.*?\\.png)\\?format=raw\"").matcher(s);
146            StringBuffer sb = new StringBuffer();
147            while (m.find()) {
148                String im = m.group(1);
149                URL u = getClass().getResource(im);
150                if (u != null) {
151                    m.appendReplacement(sb, Matcher.quoteReplacement("src=\"" + u.toString() + "\""));
152                }
153            }
154            m.appendTail(sb);
155            return sb.toString();
156        }
157    }