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

Last change on this file since 729 was 679, checked in by stoecker, 16 years ago

finished XML based translations of presets fixes #960
correct bounding box illegal access fixes #1044
do no longer modify objects when unnessesary (can result in 0 objects
modified in Undo :-)
correct typo fixes #730
some I18N corrections

  • Property svn:eol-style set to native
File size: 6.8 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 = "<html><body>\n<h1>" +
56 tr("JOSM, the Java OpenStreetMap editor") +
57 "</h1>\n<h2 align=\"center\">(" +
58 tr ("Message of the day not available") +
59 ")</h2>";
60 }
61
62 int myVersion;
63 try {
64 myVersion = Integer.parseInt(AboutAction.getVersion());
65 } catch (NumberFormatException e) {
66 myVersion = 0;
67 }
68
69 Pattern commentPattern = Pattern.compile("\\<p\\>\\s*\\/\\*[^\\*]*\\*\\/\\s*\\<\\/p\\>", Pattern.CASE_INSENSITIVE|Pattern.DOTALL|Pattern.MULTILINE);
70 Matcher matcherComment = commentPattern.matcher(motdcontent);
71 motdcontent = matcherComment.replaceAll("");
72
73 /* look for hrefs of the form wiki/MessageOfTheDay>123 where > can also be <,<=,>= and the number is the revision number */
74 int start = 0;
75 boolean nothingIncluded = true;
76 Pattern versionPattern = Pattern.compile("\\<a[^\\>]*href\\=\\\"([^\\\"]*\\/wiki\\/)(MessageOfTheDay(\\%3E%3D|%3C%3D|\\%3E|\\%3C)([0-9]+))\\\"[^\\>]*\\>[^\\<]*\\<\\/a\\>", Pattern.CASE_INSENSITIVE|Pattern.DOTALL|Pattern.MULTILINE);
77 Matcher matcher = versionPattern.matcher(motdcontent);
78 matcher.reset();
79 while (matcher.find()) {
80 int targetVersion = Integer.parseInt(matcher.group(4));
81 String condition = matcher.group(3);
82 boolean included = false;
83 if (condition.equals("%3E")) {
84 if ((myVersion == 0 || myVersion > targetVersion)
85 /* && ! Main.pref.getBoolean("motd.gt."+targetVersion) */) {
86 /* Main.pref.put("motd.gt."+targetVersion, true); */
87 included = true;
88 }
89 } else if (condition.equals("%3E%3D")) {
90 if ((myVersion == 0 || myVersion >= targetVersion)
91 /* && ! Main.pref.getBoolean("motd.ge."+targetVersion) */) {
92 /* Main.pref.put("motd.ge."+targetVersion, true); */
93 included = true;
94 }
95 } else if (condition.equals("%3C")) {
96 included = myVersion < targetVersion;
97 } else {
98 included = myVersion <= targetVersion;
99 }
100 if (matcher.start() > start) {
101 content += motdcontent.substring(start, matcher.start() - 1);
102 }
103 start = matcher.end();
104 if (included) {
105 // translators: set this to a suitable language code to
106 // be able to provide translations in the Wiki.
107 String languageCode = tr("En:");
108 String url = matcher.group(1) + languageCode + matcher.group(2);
109 try {
110 String message = wr.read(url);
111 // a return containing the article name indicates that the page didn't
112 // exist in the Wiki.
113 String emptyIndicator = "Describe \"" + languageCode + "MessageOfTheDay";
114 if (message.indexOf(emptyIndicator) >= 0) {
115 url = matcher.group(1) + matcher.group(2);
116 message = wr.read(url);
117 emptyIndicator = "Describe \"MessageOfTheDay";
118 }
119 if (message.indexOf(emptyIndicator) == -1) {
120 content += message.replace("<html>", "").replace("</html>", "").replace("<div id=\"searchable\">", "").replace("</div>", "");
121 nothingIncluded = false;
122 }
123 } catch (IOException ioe) {
124 url = matcher.group(1) + matcher.group(2);
125 try {
126 content += wr.read(url).replace("<html>", "").replace("</html>", "").replace("<div id=\"searchable\">", "").replace("</div>", "");
127 nothingIncluded = false;
128 } catch (IOException ioe2) {
129 }
130 }
131 }
132 }
133 content += motdcontent.substring(start);
134 content = content.replace("<html>", "<html><style>\nbody { font-family: sans-serif; font-weight: bold; }\n</style>");
135 content = content.replace("<h1", "<h1 align=\"center\"");
136 /* replace the wiki title */
137 content = content.replace("JOSM, the Java OpenStreetMap editor", tr("JOSM, the Java OpenStreetMap editor"));
138 }
139
140 }
141
142 public GettingStarted() {
143 super(new BorderLayout());
144 assignContent();
145
146 // panel.add(GBC.glue(0,1), GBC.eol());
147 //panel.setMinimumSize(new Dimension(400, 600));
148 JScrollPane scroller = new JScrollPane(new LinkGeneral(content));
149 scroller.setViewportBorder(new EmptyBorder(100,100,10,100));
150 add(scroller, BorderLayout.CENTER);
151 }
152}
Note: See TracBrowser for help on using the repository browser.