source: josm/trunk/src/org/openstreetmap/josm/gui/GettingStarted.java @ 5241

Revision 5113, 6.1 KB checked in by simon04, 2 months ago (diff)

fix typo, add code comment

  • Property svn:eol-style set to native
Line 
1// License: GPL. See LICENSE file for details.
2
3package org.openstreetmap.josm.gui;
4
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.BorderLayout;
8import java.awt.EventQueue;
9import java.awt.event.InputEvent;
10import java.awt.event.KeyEvent;
11import java.io.IOException;
12import java.io.UnsupportedEncodingException;
13import java.net.URL;
14import java.util.regex.Matcher;
15import java.util.regex.Pattern;
16
17import javax.swing.JComponent;
18import javax.swing.JEditorPane;
19import javax.swing.JPanel;
20import javax.swing.JScrollPane;
21import javax.swing.KeyStroke;
22import javax.swing.border.EmptyBorder;
23import javax.swing.event.HyperlinkEvent;
24import javax.swing.event.HyperlinkListener;
25
26import org.openstreetmap.josm.Main;
27import org.openstreetmap.josm.data.Version;
28import org.openstreetmap.josm.io.CacheCustomContent;
29import org.openstreetmap.josm.tools.LanguageInfo;
30import org.openstreetmap.josm.tools.OpenBrowser;
31import org.openstreetmap.josm.tools.WikiReader;
32
33public class GettingStarted extends JPanel {
34    private String content = "";
35    private static final String STYLE = "<style type=\"text/css\">\n"
36            + "body {font-family: sans-serif; font-weight: bold; }\n"
37            + "h1 {text-align: center; }\n"
38            + ".icon {font-size: 0; }\n"
39            + "</style>\n";
40
41    public static class LinkGeneral extends JEditorPane implements HyperlinkListener {
42        public LinkGeneral(String text) {
43            setContentType("text/html");
44            setText(text);
45            setEditable(false);
46            setOpaque(false);
47            addHyperlinkListener(this);
48        }
49
50        public void hyperlinkUpdate(HyperlinkEvent e) {
51            if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
52                OpenBrowser.displayUrl(e.getDescription());
53            }
54        }
55    }
56
57    /**
58     * Grabs current MOTD from cache or webpage and parses it.
59     */
60    private static class MotdContent extends CacheCustomContent<IOException> {
61        public MotdContent() {
62            super("motd.html", CacheCustomContent.INTERVAL_DAILY);
63        }
64
65        final private int myVersion = Version.getInstance().getVersion();
66        final private String myLang = LanguageInfo.getWikiLanguagePrefix();
67
68        /**
69         * This function gets executed whenever the cached files need updating
70         * @see org.openstreetmap.josm.io.CacheCustomContent#updateData()
71         */
72        @Override
73        protected byte[] updateData() throws IOException {
74            String motd = new WikiReader().readLang("StartupPage");
75            // Save this to prefs in case JOSM is updated so MOTD can be refreshed
76            Main.pref.putInteger("cache.motd.html.version", myVersion);
77            Main.pref.put("cache.motd.html.lang", myLang);
78            try {
79                return motd.getBytes("utf-8");
80            } catch(UnsupportedEncodingException e){
81                e.printStackTrace();
82                return new byte[0];
83            }
84        }
85
86        /**
87         * Additionally check if JOSM has been updated and refresh MOTD
88         */
89        @Override
90        protected boolean isCacheValid() {
91            // We assume a default of myVersion because it only kicks in in two cases:
92            // 1. Not yet written - but so isn't the interval variable, so it gets updated anyway
93            // 2. Cannot be written (e.g. while developing). Obviously we don't want to update
94            // everytime because of something we can't read.
95            return (Main.pref.getInteger("cache.motd.html.version", -999) == myVersion)
96            && Main.pref.get("cache.motd.html.lang").equals(myLang);
97        }
98    }
99
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}
Note: See TracBrowser for help on using the repository browser.