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

Last change on this file since 3775 was 3530, checked in by stoecker, 14 years ago

fix array preferences

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