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

Last change on this file since 2657 was 2626, checked in by jttt, 14 years ago

Fixed some of the warnings found by FindBugs

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