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

Last change on this file since 32395 was 32395, checked in by donvip, 10 years ago

checkstyle, update to JOSM 10279

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