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

Last change on this file since 652 was 652, checked in by framm, 16 years ago
  • always show all "message of the day" messages, not only unread.
  • make "message of the day" wiki mechanism i18nable. for details see josm-dev list
  • Property svn:eol-style set to native
File size: 6.2 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.io.IOException;
8import java.util.regex.Matcher;
9import java.util.regex.Pattern;
10
11import java.awt.BorderLayout;
12
13import javax.swing.JScrollPane;
14import javax.swing.JEditorPane;
15import javax.swing.JPanel;
16import javax.swing.event.HyperlinkEvent;
17import javax.swing.event.HyperlinkListener;
18import javax.swing.border.EmptyBorder;
19
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.tools.ImageProvider;
22import org.openstreetmap.josm.tools.OpenBrowser;
23import org.openstreetmap.josm.tools.WikiReader;
24import org.openstreetmap.josm.actions.AboutAction;
25
26public class GettingStarted extends JPanel {
27
28 private JPanel panel;
29 static private String content = "";
30
31 public class LinkGeneral extends JEditorPane implements HyperlinkListener {
32 private String action;
33 public LinkGeneral(String text) {
34 setContentType("text/html");
35 setText(text);
36 setEditable(false);
37 setOpaque(false);
38 addHyperlinkListener(this);
39 }
40 public void hyperlinkUpdate(HyperlinkEvent e) {
41 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
42 OpenBrowser.displayUrl(e.getDescription());
43 }
44 }
45 }
46
47 private void assignContent() {
48 if (content.length() == 0) {
49 String baseurl = Main.pref.get("help.baseurl", "http://josm.openstreetmap.de");
50 WikiReader wr = new WikiReader(baseurl);
51 String motdcontent = "";
52 try {
53 motdcontent = wr.read(baseurl + "/wiki/MessageOfTheDay");
54 } catch (IOException ioe) {
55 motdcontent = tr("<html>\n<h1>JOSM, the Java OpenStreetMap editor</h1>\n<h2>(Message of the day not available)</h2>");
56 }
57
58 int myVersion;
59 try {
60 myVersion = Integer.parseInt(AboutAction.getVersion());
61 } catch (NumberFormatException e) {
62 myVersion = 0;
63 }
64
65 Pattern commentPattern = Pattern.compile("\\<p\\>\\s*\\/\\*[^\\*]*\\*\\/\\s*\\<\\/p\\>", Pattern.CASE_INSENSITIVE|Pattern.DOTALL|Pattern.MULTILINE);
66 Matcher matcherComment = commentPattern.matcher(motdcontent);
67 motdcontent = matcherComment.replaceAll("");
68
69 /* look for hrefs of the form wiki/MessageOfTheDay>123 where > can also be <,<=,>= and the number is the revision number */
70 int start = 0;
71 boolean nothingIncluded = true;
72 Pattern versionPattern = Pattern.compile("\\<a[^\\>]*href\\=\\\"([^\\\"]*\\/wiki\\/)(MessageOfTheDay(\\%3E%3D|%3C%3D|\\%3E|\\%3C)([0-9]+))\\\"[^\\>]*\\>[^\\<]*\\<\\/a\\>", 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 content += wr.read(url).replace("<html>", "").replace("</html>", "").replace("<div id=\"searchable\">", "").replace("</div>", "");
107 nothingIncluded = false;
108 } catch (IOException ioe) {
109 url = matcher.group(1) + matcher.group(2);
110 try {
111 content += wr.read(url).replace("<html>", "").replace("</html>", "").replace("<div id=\"searchable\">", "").replace("</div>", "");
112 nothingIncluded = false;
113 } catch (IOException ioe2) {
114 }
115 }
116 }
117 }
118 if (nothingIncluded) {
119 content += "<div align=\"center\">Watch this space for announcements</div>";
120 content += "<div align=\"center\" style=\"font-weight: normal\">(remove the \"motd\" entries in Advanced Preferences to see any available announcements next time)</div>";
121 }
122 content += motdcontent.substring(start);
123 content = content.replace("<html>", "<html><style>\nbody { font-family: sans-serif; font-weight: bold; }\n</style>");
124 content = content.replace("<h1", "<h1 align=\"center\"");
125 }
126
127 }
128
129 public GettingStarted() {
130 super(new BorderLayout());
131 assignContent();
132
133 // panel.add(GBC.glue(0,1), GBC.eol());
134 //panel.setMinimumSize(new Dimension(400, 600));
135 JScrollPane scroller = new JScrollPane(new LinkGeneral(content));
136 scroller.setViewportBorder(new EmptyBorder(100,100,10,100));
137 add(scroller, BorderLayout.CENTER);
138 }
139}
Note: See TracBrowser for help on using the repository browser.