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

Last change on this file since 1922 was 1879, checked in by Gubaer, 15 years ago

towards a fix for #3142: JOSM applet class no longer functional

  • Property svn:eol-style set to native
File size: 4.7 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.actions.AboutAction;
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 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 class MotdContent extends CacheCustomContent {
49 public MotdContent() {
50 super("motd.html", CacheCustomContent.INTERVAL_DAILY);
51 }
52
53 final private int myVersion = AboutAction.getVersionNumber();
54 final private String myLang = LanguageInfo.getLanguageCodeWiki();
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 } else {
67 motd = motd.replace("<!-- VERSION -->", tr("- running version is {0}", AboutAction.getVersionString()));
68 }
69 // Save this to prefs in case JOSM is updated so MOTD can be refreshed
70 Main.pref.putInteger("cache.motd.html.version", myVersion);
71 Main.pref.put("cache.motd.html.lang", myLang);
72
73 return motd.getBytes();
74 }
75
76 /**
77 * Additionally check if JOSM has been updated and refresh MOTD
78 */
79 @Override
80 protected boolean isCacheValid() {
81 // We assume a default of myVersion because it only kicks in in two cases:
82 // 1. Not yet written - but so isn't the interval variable, so it gets updated anyway
83 // 2. Cannot be written (e.g. while developing). Obviously we don't want to update
84 // everytime because of something we can't read.
85 return (Main.pref.getInteger("cache.motd.html.version", -999) == myVersion)
86 && Main.pref.get("cache.motd.html.lang").equals(myLang);
87 }
88 }
89
90 /**
91 * Initializes getting the MOTD as well as enabling the FileDrop Listener. Displays a message
92 * while the MOTD is downloading.
93 */
94 public GettingStarted() {
95 super(new BorderLayout());
96 final LinkGeneral lg = new LinkGeneral("<html>" + styles + "<h1>" + "JOSM - " + tr("Java OpenStreetMap Editor")
97 + "</h1><h2 align=\"center\">" + tr("Downloading \"Message of the day\"") + "</h2>");
98 JScrollPane scroller = new JScrollPane(lg);
99 scroller.setViewportBorder(new EmptyBorder(10, 100, 10, 100));
100 add(scroller, BorderLayout.CENTER);
101
102 // Asynchronously get MOTD to speed-up JOSM startup
103 Thread t = new Thread(new Runnable() {
104 public void run() {
105 if (content.length() == 0 && Main.pref.getBoolean("help.displaymotd", true)) {
106 content = new MotdContent().updateIfRequiredString();
107 }
108
109 EventQueue.invokeLater(new Runnable() {
110 public void run() {
111 lg.setText(content);
112 // lg.moveCaretPosition(0);
113 }
114 });
115 }
116 }, "MOTD-Loader");
117 t.setDaemon(true);
118 t.start();
119
120 new FileDrop(scroller);
121 }
122}
Note: See TracBrowser for help on using the repository browser.