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.regex.Matcher;
|
---|
9 | import java.util.regex.Pattern;
|
---|
10 |
|
---|
11 | import java.awt.BorderLayout;
|
---|
12 |
|
---|
13 | import javax.swing.JScrollPane;
|
---|
14 | import javax.swing.JEditorPane;
|
---|
15 | import javax.swing.JPanel;
|
---|
16 | import javax.swing.event.HyperlinkEvent;
|
---|
17 | import javax.swing.event.HyperlinkListener;
|
---|
18 | import javax.swing.border.EmptyBorder;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
22 | import org.openstreetmap.josm.tools.WikiReader;
|
---|
23 | import org.openstreetmap.josm.actions.AboutAction;
|
---|
24 |
|
---|
25 | public class GettingStarted extends JPanel {
|
---|
26 |
|
---|
27 | static private String content = "";
|
---|
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 | public void hyperlinkUpdate(HyperlinkEvent e) {
|
---|
38 | if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
|
---|
39 | OpenBrowser.displayUrl(e.getDescription());
|
---|
40 | }
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | private void assignContent() {
|
---|
45 | if (content.length() == 0) {
|
---|
46 | String baseurl = Main.pref.get("help.baseurl", "http://josm.openstreetmap.de");
|
---|
47 | WikiReader wr = new WikiReader(baseurl);
|
---|
48 | String motdcontent = "";
|
---|
49 | try {
|
---|
50 | motdcontent = wr.read(baseurl + "/wiki/MessageOfTheDay");
|
---|
51 | } catch (IOException ioe) {
|
---|
52 | motdcontent = "<html><body>\n<h1>" +
|
---|
53 | tr("JOSM, the Java OpenStreetMap editor") +
|
---|
54 | "</h1>\n<h2 align=\"center\">(" +
|
---|
55 | tr ("Message of the day not available") +
|
---|
56 | ")</h2>";
|
---|
57 | }
|
---|
58 |
|
---|
59 | int myVersion;
|
---|
60 | try {
|
---|
61 | myVersion = Integer.parseInt(AboutAction.getVersion());
|
---|
62 | } catch (NumberFormatException e) {
|
---|
63 | myVersion = 0;
|
---|
64 | }
|
---|
65 |
|
---|
66 | Pattern commentPattern = Pattern.compile("\\<p\\>\\s*\\/\\*[^\\*]*\\*\\/\\s*\\<\\/p\\>", Pattern.CASE_INSENSITIVE|Pattern.DOTALL|Pattern.MULTILINE);
|
---|
67 | Matcher matcherComment = commentPattern.matcher(motdcontent);
|
---|
68 | motdcontent = matcherComment.replaceAll("");
|
---|
69 |
|
---|
70 | /* look for hrefs of the form wiki/MessageOfTheDay>123 where > can also be <,<=,>= and the number is the revision number */
|
---|
71 | int start = 0;
|
---|
72 | Pattern versionPattern = Pattern.compile("(?:\\<p\\>\\s*)?\\<a[^\\>]*href\\=\\\"([^\\\"]*\\/wiki\\/)(MessageOfTheDay(\\%3E%3D|%3C%3D|\\%3E|\\%3C)([0-9]+))\\\"[^\\>]*\\>[^\\<]*\\<\\/a\\>(?:\\s*\\</p\\>)?", Pattern.CASE_INSENSITIVE|Pattern.DOTALL|Pattern.MULTILINE);
|
---|
73 | Matcher matcher = versionPattern.matcher(motdcontent);
|
---|
74 | matcher.reset();
|
---|
75 | while (matcher.find()) {
|
---|
76 | int targetVersion = Integer.parseInt(matcher.group(4));
|
---|
77 | String condition = matcher.group(3);
|
---|
78 | boolean included = false;
|
---|
79 | if (condition.equals("%3E")) {
|
---|
80 | if ((myVersion == 0 || myVersion > targetVersion)
|
---|
81 | /* && ! Main.pref.getBoolean("motd.gt."+targetVersion) */) {
|
---|
82 | /* Main.pref.put("motd.gt."+targetVersion, true); */
|
---|
83 | included = true;
|
---|
84 | }
|
---|
85 | } else if (condition.equals("%3E%3D")) {
|
---|
86 | if ((myVersion == 0 || myVersion >= targetVersion)
|
---|
87 | /* && ! Main.pref.getBoolean("motd.ge."+targetVersion) */) {
|
---|
88 | /* Main.pref.put("motd.ge."+targetVersion, true); */
|
---|
89 | included = true;
|
---|
90 | }
|
---|
91 | } else if (condition.equals("%3C")) {
|
---|
92 | included = myVersion < targetVersion;
|
---|
93 | } else {
|
---|
94 | included = myVersion <= targetVersion;
|
---|
95 | }
|
---|
96 | if (matcher.start() > start) {
|
---|
97 | content += motdcontent.substring(start, matcher.start() - 1);
|
---|
98 | }
|
---|
99 | start = matcher.end();
|
---|
100 | if (included) {
|
---|
101 | // translators: set this to a suitable language code to
|
---|
102 | // be able to provide translations in the Wiki.
|
---|
103 | String languageCode = tr("En:");
|
---|
104 | String url = matcher.group(1) + languageCode + matcher.group(2);
|
---|
105 | try {
|
---|
106 | String message = wr.read(url);
|
---|
107 | // a return containing the article name indicates that the page didn't
|
---|
108 | // exist in the Wiki.
|
---|
109 | String emptyIndicator = "Describe \"" + languageCode + "MessageOfTheDay";
|
---|
110 | if (message.indexOf(emptyIndicator) >= 0) {
|
---|
111 | url = matcher.group(1) + matcher.group(2);
|
---|
112 | message = wr.read(url);
|
---|
113 | emptyIndicator = "Describe \"MessageOfTheDay";
|
---|
114 | }
|
---|
115 | if (message.indexOf(emptyIndicator) == -1) {
|
---|
116 | content += message.replace("<html>", "").replace("</html>", "").replace("<div id=\"searchable\">", "").replace("</div>", "");
|
---|
117 | }
|
---|
118 | } catch (IOException ioe) {
|
---|
119 | url = matcher.group(1) + matcher.group(2);
|
---|
120 | try {
|
---|
121 | content += wr.read(url).replace("<html>", "").replace("</html>", "").replace("<div id=\"searchable\">", "").replace("</div>", "");
|
---|
122 | } catch (IOException ioe2) {
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | content += motdcontent.substring(start);
|
---|
128 | content = content.replace("<html>", "<html><style type=\"text/css\">\nbody { font-family: sans-serif; font-weight: bold; }\nh1 {text-align: center;}</style>");
|
---|
129 | /* replace the wiki title */
|
---|
130 | content = content.replace("JOSM, the Java OpenStreetMap editor", tr("JOSM, the Java OpenStreetMap editor"));
|
---|
131 | }
|
---|
132 |
|
---|
133 | }
|
---|
134 |
|
---|
135 | public GettingStarted() {
|
---|
136 | super(new BorderLayout());
|
---|
137 | assignContent();
|
---|
138 |
|
---|
139 | // panel.add(GBC.glue(0,1), GBC.eol());
|
---|
140 | //panel.setMinimumSize(new Dimension(400, 600));
|
---|
141 | JScrollPane scroller = new JScrollPane(new LinkGeneral(content));
|
---|
142 | scroller.setViewportBorder(new EmptyBorder(10,100,10,100));
|
---|
143 | add(scroller, BorderLayout.CENTER);
|
---|
144 | }
|
---|
145 | }
|
---|