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

Last change on this file since 4609 was 4364, checked in by simon04, 13 years ago

MOTD: get rid of unnecessary space in front of external links

  • Property svn:eol-style set to native
File size: 5.2 KB
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.UnsupportedEncodingException;
12
13import javax.swing.JComponent;
14import javax.swing.JEditorPane;
15import javax.swing.JPanel;
16import javax.swing.JScrollPane;
17import javax.swing.KeyStroke;
18import javax.swing.border.EmptyBorder;
19import javax.swing.event.HyperlinkEvent;
20import javax.swing.event.HyperlinkListener;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.data.Version;
24import org.openstreetmap.josm.io.CacheCustomContent;
25import org.openstreetmap.josm.tools.LanguageInfo;
26import org.openstreetmap.josm.tools.OpenBrowser;
27import org.openstreetmap.josm.tools.WikiReader;
28
29public class GettingStarted extends JPanel {
30 private String content = "";
31 private static final String STYLE = "<style type=\"text/css\">\n"
32 + "body {font-family: sans-serif; font-weight: bold; }\n"
33 + "h1 {text-align: center; }\n"
34 + ".icon {font-size: 0; }\n"
35 + "</style>\n";
36
37 public static class LinkGeneral extends JEditorPane implements HyperlinkListener {
38 public LinkGeneral(String text) {
39 setContentType("text/html");
40 setText(text);
41 setEditable(false);
42 setOpaque(false);
43 addHyperlinkListener(this);
44 }
45
46 public void hyperlinkUpdate(HyperlinkEvent e) {
47 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
48 OpenBrowser.displayUrl(e.getDescription());
49 }
50 }
51 }
52
53 /**
54 * Grabs current MOTD from cache or webpage and parses it.
55 */
56 private static class MotdContent extends CacheCustomContent {
57 public MotdContent() {
58 super("motd.html", CacheCustomContent.INTERVAL_DAILY);
59 }
60
61 final private int myVersion = Version.getInstance().getVersion();
62 final private String myLang = LanguageInfo.getWikiLanguagePrefix();
63
64 /**
65 * This function gets executed whenever the cached files need updating
66 * @see org.openstreetmap.josm.io.CacheCustomContent#updateData()
67 */
68 @Override
69 protected byte[] updateData() {
70 String motd = new WikiReader().readLang("StartupPage");
71 if (motd.length() == 0) {
72 motd = "<html>" + STYLE + "<h1>" + "JOSM - " + tr("Java OpenStreetMap Editor")
73 + "</h1>\n<h2 align=\"center\">(" + tr("Message of the day not available") + ")</h2></html>";
74 }
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 public void run() {
118 if (content.length() == 0 && Main.pref.getBoolean("help.displaymotd", true)) {
119 content = new MotdContent().updateIfRequiredString();
120 }
121
122 EventQueue.invokeLater(new Runnable() {
123 public void run() {
124 lg.setText(content);
125 // lg.moveCaretPosition(0);
126 }
127 });
128 }
129 }, "MOTD-Loader");
130 t.setDaemon(true);
131 t.start();
132
133 new FileDrop(scroller);
134 }
135}
Note: See TracBrowser for help on using the repository browser.