source: osm/applications/editors/josm/plugins/reltoolbox/src/relcontext/actions/RelationHelpAction.java@ 34026

Last change on this file since 34026 was 33530, checked in by donvip, 9 years ago

update to JOSM 12663

File size: 4.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package relcontext.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.net.HttpURLConnection;
8import java.net.URI;
9import java.net.URLEncoder;
10import java.util.ArrayList;
11import java.util.List;
12
13import javax.swing.AbstractAction;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.osm.Relation;
17import org.openstreetmap.josm.gui.MainApplication;
18import org.openstreetmap.josm.tools.ImageProvider;
19import org.openstreetmap.josm.tools.LanguageInfo;
20import org.openstreetmap.josm.tools.OpenBrowser;
21
22import relcontext.ChosenRelation;
23import relcontext.ChosenRelationListener;
24
25public class RelationHelpAction extends AbstractAction implements ChosenRelationListener {
26 private ChosenRelation rel;
27
28 public RelationHelpAction(ChosenRelation rel) {
29 super();
30 putValue(NAME, tr("Open relation wiki page"));
31 putValue(SHORT_DESCRIPTION, tr("Launch browser with wiki help for selected object"));
32 putValue(SMALL_ICON, ImageProvider.get("dialogs", "search"));
33 this.rel = rel;
34 rel.addChosenRelationListener(this);
35 setEnabled(rel.get() != null);
36 }
37
38 @Override
39 public void chosenRelationChanged(Relation oldRelation, Relation newRelation) {
40 setEnabled(newRelation != null);
41 }
42
43 /**
44 * Copypasted from {@link org.openstreetmap.josm.gui.dialogs.properties.PropertiesDialog.HelpAction}.
45 */
46 @Override
47 public void actionPerformed(ActionEvent e) {
48 if (rel.get() == null)
49 return;
50 try {
51 String base = Main.pref.get("url.openstreetmap-wiki", "http://wiki.openstreetmap.org/wiki/");
52 String lang = LanguageInfo.getWikiLanguagePrefix();
53 final List<URI> uris = new ArrayList<>();
54 String type = URLEncoder.encode(rel.get().get("type"), "UTF-8");
55
56 if (type != null && !type.equals("")) {
57 uris.add(new URI(String.format("%s%sRelation:%s", base, lang, type)));
58 uris.add(new URI(String.format("%sRelation:%s", base, type)));
59 }
60
61 uris.add(new URI(String.format("%s%sRelations", base, lang)));
62 uris.add(new URI(String.format("%sRelations", base)));
63
64 MainApplication.worker.execute(() -> {
65 try {
66 // find a page that actually exists in the wiki
67 HttpURLConnection conn;
68 for (URI u : uris) {
69 conn = (HttpURLConnection) u.toURL().openConnection();
70 conn.setConnectTimeout(5000);
71
72 if (conn.getResponseCode() != 200) {
73 System.out.println("INFO: " + u + " does not exist");
74 conn.disconnect();
75 } else {
76 int osize = conn.getContentLength();
77 conn.disconnect();
78
79 conn = (HttpURLConnection) new URI(u.toString()
80 .replace("=", "%3D") /* do not URLencode whole string! */
81 .replaceFirst("/wiki/", "/w/index.php?redirect=no&title=")
82 ).toURL().openConnection();
83 conn.setConnectTimeout(5000);
84
85 /* redirect pages have different content length, but retrieving a "nonredirect"
86 * page using index.php and the direct-link method gives slightly different
87 * content lengths, so we have to be fuzzy.. (this is UGLY, recode if u know better)
88 */
89 if (Math.abs(conn.getContentLength() - osize) > 200) {
90 System.out.println("INFO: " + u + " is a mediawiki redirect");
91 conn.disconnect();
92 } else {
93 System.out.println("INFO: browsing to " + u);
94 conn.disconnect();
95
96 OpenBrowser.displayUrl(u.toString());
97 break;
98 }
99 }
100 }
101 } catch (Exception e1) {
102 e1.printStackTrace();
103 }
104 });
105 } catch (Exception e1) {
106 e1.printStackTrace();
107 }
108 }
109}
Note: See TracBrowser for help on using the repository browser.