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

Last change on this file since 3128 was 3122, checked in by Gubaer, 14 years ago

fixed #4673: I18n: motd in russian not displayed

  • Property svn:eol-style set to native
File size: 4.8 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.io.UnsupportedEncodingException;
10
11import javax.swing.JEditorPane;
12import javax.swing.JPanel;
13import javax.swing.JScrollPane;
14import javax.swing.border.EmptyBorder;
15import javax.swing.event.HyperlinkEvent;
16import javax.swing.event.HyperlinkListener;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.data.Version;
20import org.openstreetmap.josm.io.CacheCustomContent;
21import org.openstreetmap.josm.tools.LanguageInfo;
22import org.openstreetmap.josm.tools.OpenBrowser;
23import org.openstreetmap.josm.tools.WikiReader;
24
25public class GettingStarted extends JPanel {
26 private String content = "";
27 static private String styles = "<style type=\"text/css\">\n"
28 + "body { font-family: sans-serif; font-weight: bold; }\n" + "h1 {text-align: center;}\n" + "</style>\n";
29
30 public static class LinkGeneral extends JEditorPane implements HyperlinkListener {
31 public LinkGeneral(String text) {
32 setContentType("text/html");
33 setText(text);
34 setEditable(false);
35 setOpaque(false);
36 addHyperlinkListener(this);
37 }
38
39 public void hyperlinkUpdate(HyperlinkEvent e) {
40 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
41 OpenBrowser.displayUrl(e.getDescription());
42 }
43 }
44 }
45
46 /**
47 * Grabs current MOTD from cache or webpage and parses it.
48 */
49 private static class MotdContent extends CacheCustomContent {
50 public MotdContent() {
51 super("motd.html", CacheCustomContent.INTERVAL_DAILY);
52 }
53
54 final private int myVersion = Version.getInstance().getVersion();
55 final private String myLang = LanguageInfo.getWikiLanguagePrefix();
56
57 /**
58 * This function gets executed whenever the cached files need updating
59 * @see org.openstreetmap.josm.io.CacheCustomContent#updateData()
60 */
61 @Override
62 protected byte[] updateData() {
63 String motd = new WikiReader().readLang("StartupPage");
64 if (motd.length() == 0) {
65 motd = "<html>" + styles + "<body><h1>" + "JOSM - " + tr("Java OpenStreetMap Editor")
66 + "</h1>\n<h2 align=\"center\">(" + tr("Message of the day not available") + ")</h2></html>";
67 }
68 // Save this to prefs in case JOSM is updated so MOTD can be refreshed
69 Main.pref.putInteger("cache.motd.html.version", myVersion);
70 Main.pref.put("cache.motd.html.lang", myLang);
71 try {
72 return motd.getBytes("utf-8");
73 } catch(UnsupportedEncodingException e){
74 e.printStackTrace();
75 return new byte[0];
76 }
77 }
78
79 /**
80 * Additionally check if JOSM has been updated and refresh MOTD
81 */
82 @Override
83 protected boolean isCacheValid() {
84 // We assume a default of myVersion because it only kicks in in two cases:
85 // 1. Not yet written - but so isn't the interval variable, so it gets updated anyway
86 // 2. Cannot be written (e.g. while developing). Obviously we don't want to update
87 // everytime because of something we can't read.
88 return (Main.pref.getInteger("cache.motd.html.version", -999) == myVersion)
89 && Main.pref.get("cache.motd.html.lang").equals(myLang);
90 }
91 }
92
93 /**
94 * Initializes getting the MOTD as well as enabling the FileDrop Listener. Displays a message
95 * while the MOTD is downloading.
96 */
97 public GettingStarted() {
98 super(new BorderLayout());
99 final LinkGeneral lg = new LinkGeneral("<html>" + styles + "<h1>" + "JOSM - " + tr("Java OpenStreetMap Editor")
100 + "</h1><h2 align=\"center\">" + tr("Downloading \"Message of the day\"") + "</h2>");
101 JScrollPane scroller = new JScrollPane(lg);
102 scroller.setViewportBorder(new EmptyBorder(10, 100, 10, 100));
103 add(scroller, BorderLayout.CENTER);
104
105 // Asynchronously get MOTD to speed-up JOSM startup
106 Thread t = new Thread(new Runnable() {
107 public void run() {
108 if (content.length() == 0 && Main.pref.getBoolean("help.displaymotd", true)) {
109 content = new MotdContent().updateIfRequiredString();
110 }
111
112 EventQueue.invokeLater(new Runnable() {
113 public void run() {
114 lg.setText(content);
115 // lg.moveCaretPosition(0);
116 }
117 });
118 }
119 }, "MOTD-Loader");
120 t.setDaemon(true);
121 t.start();
122
123 new FileDrop(scroller);
124 }
125}
Note: See TracBrowser for help on using the repository browser.