| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package relcontext.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.event.ActionEvent;
|
|---|
| 7 | import java.net.HttpURLConnection;
|
|---|
| 8 | import java.net.URI;
|
|---|
| 9 | import java.net.URLEncoder;
|
|---|
| 10 | import java.util.ArrayList;
|
|---|
| 11 | import java.util.List;
|
|---|
| 12 |
|
|---|
| 13 | import javax.swing.AbstractAction;
|
|---|
| 14 |
|
|---|
| 15 | import org.openstreetmap.josm.Main;
|
|---|
| 16 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 17 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 18 | import org.openstreetmap.josm.tools.LanguageInfo;
|
|---|
| 19 | import org.openstreetmap.josm.tools.OpenBrowser;
|
|---|
| 20 |
|
|---|
| 21 | import relcontext.ChosenRelation;
|
|---|
| 22 | import relcontext.ChosenRelationListener;
|
|---|
| 23 |
|
|---|
| 24 | public 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 | }
|
|---|