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

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

[josm_plugins] fix Java 7 / unused code warnings

File size: 4.5 KB
Line 
1package relcontext.actions;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4import java.awt.event.ActionEvent;
5import java.net.HttpURLConnection;
6import java.net.URI;
7import java.net.URLEncoder;
8import java.util.ArrayList;
9import java.util.List;
10import javax.swing.AbstractAction;
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.osm.Relation;
13import org.openstreetmap.josm.tools.ImageProvider;
14import org.openstreetmap.josm.tools.LanguageInfo;
15import org.openstreetmap.josm.tools.OpenBrowser;
16import relcontext.ChosenRelation;
17import relcontext.ChosenRelationListener;
18
19public 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<>();
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}
Note: See TracBrowser for help on using the repository browser.