Changeset 32114 in osm for applications/editors/josm/plugins/wikipedia
- Timestamp:
- 2016-03-22T22:54:50+01:00 (8 years ago)
- Location:
- applications/editors/josm/plugins/wikipedia
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java
r32056 r32114 272 272 } 273 273 274 static LatLon getCoordinateForArticle(String wikipediaLang, String article) { 275 try { 276 final String url = "https://" + wikipediaLang + ".wikipedia.org/w/api.php" + 277 "?action=query" + 278 "&prop=coordinates" + 279 "&titles=" + Utils.encodeUrl(article) + 280 "&format=xml"; 281 try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) { 282 final Document xml = DOCUMENT_BUILDER.parse(in); 283 final Node node = (Node) X_PATH.compile("//coordinates/co").evaluate(xml, XPathConstants.NODE); 284 if (node == null) { 285 return null; 286 } else { 287 final double lat = Double.parseDouble(node.getAttributes().getNamedItem("lat").getTextContent()); 288 final double lon = Double.parseDouble(node.getAttributes().getNamedItem("lon").getTextContent()); 289 return new LatLon(lat, lon); 290 } 291 } 292 } catch (Exception ex) { 293 throw new RuntimeException(ex); 294 } 295 } 296 274 297 static class WikipediaLangArticle { 275 298 -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaToggleDialog.java
r32058 r32114 20 20 import javax.swing.JList; 21 21 import javax.swing.JOptionPane; 22 import javax.swing.JPopupMenu; 22 23 import javax.swing.SwingWorker; 23 24 … … 53 54 new SideButton(new PasteWikipediaArticlesAction()), 54 55 new SideButton(new AddWikipediaTagAction()), 55 new SideButton(new OpenWikipediaArticleAction()),56 56 new SideButton(new WikipediaSettingsAction(), false))); 57 57 updateTitle(); … … 100 100 } 101 101 }); 102 103 final JPopupMenu popupMenu = new JPopupMenu(); 104 popupMenu.add(new OpenWikipediaArticleAction()); 105 popupMenu.add(new ZoomToWikipediaArticleAction()); 106 setComponentPopupMenu(popupMenu); 102 107 } 103 108 }; … … 282 287 } 283 288 289 class ZoomToWikipediaArticleAction extends AbstractAction { 290 291 ZoomToWikipediaArticleAction() { 292 super(tr("Zoom to selection"), ImageProvider.get("dialogs/autoscale", "selection")); 293 putValue(SHORT_DESCRIPTION, tr("Zoom to selection")); 294 } 295 296 @Override 297 public void actionPerformed(ActionEvent e) { 298 final WikipediaEntry entry = list.getSelectedValue(); 299 if (entry == null) { 300 return; 301 } 302 final LatLon latLon = WikipediaApp.getCoordinateForArticle(entry.wikipediaLang, entry.wikipediaArticle); 303 if (latLon == null) { 304 return; 305 } 306 Main.map.mapView.zoomTo(latLon); 307 } 308 } 309 284 310 protected void updateWikipediaArticles() { 285 311 articles.clear(); -
applications/editors/josm/plugins/wikipedia/test/unit/org/wikipedia/WikipediaAppTest.java
r32003 r32114 125 125 126 126 @Test 127 public void testGetCoordinates() throws Exception { 128 assertThat(WikipediaApp.getCoordinateForArticle("de", "Marchreisenspitze"), is(new LatLon(47.1725, 11.30833333))); 129 assertThat(WikipediaApp.getCoordinateForArticle("en", "Austria"), is(new LatLon(47.33333333, 13.33333333))); 130 assertThat(WikipediaApp.getCoordinateForArticle("en", "Foobar2000"), nullValue()); 131 } 132 133 @Test 127 134 public void testGetBrowserUrl() { 128 135 final WikipediaEntry entry = new WikipediaEntry("Sternheim & Emanuel", "de", "Sternheim & Emanuel");
Note:
See TracChangeset
for help on using the changeset viewer.