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