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