Changeset 32610 in osm for applications/editors
- Timestamp:
- 2016-07-08T19:58:21+02:00 (8 years ago)
- Location:
- applications/editors/josm/plugins/wikipedia/src/org/wikipedia
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikidataTagCellRenderer.java
r32424 r32610 92 92 return null; 93 93 } 94 texts.add(Utils.escapeReservedCharactersHTML(id) 95 + " <span color='gray'>" + Utils.escapeReservedCharactersHTML(label) + "</span>"); 94 texts.add(WikipediaApp.WikidataEntry.getLabelText(id, label)); 96 95 } 97 96 component.setText("<html>" + Utils.join("; ", texts)); -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java
r32608 r32610 37 37 import org.openstreetmap.josm.tools.HttpClient; 38 38 import org.openstreetmap.josm.tools.Predicate; 39 import org.openstreetmap.josm.tools.Predicates; 39 40 import org.openstreetmap.josm.tools.Utils; 40 41 import org.openstreetmap.josm.tools.Utils.Function; … … 60 61 } 61 62 63 static String getSiteUrl(String wikipediaLang) { 64 if ("wikidata".equals(wikipediaLang)) { 65 return "https://www.wikidata.org"; 66 } else { 67 return "https://" + wikipediaLang + ".wikipedia.org"; 68 } 69 } 70 62 71 static List<WikipediaEntry> getEntriesFromCoordinates(String wikipediaLang, LatLon min, LatLon max) { 63 72 try { 64 73 // construct url 65 final String url = "https://" + wikipediaLang + ".wikipedia.org/w/api.php"74 final String url = getSiteUrl(wikipediaLang) + "/w/api.php" 66 75 + "?action=query" 67 76 + "&list=geosearch" … … 82 91 final Node node = nodes.item(i); 83 92 final String name = xpathName.evaluate(node); 84 entries.add(new WikipediaEntry(name, wikipediaLang, name,new LatLon((93 final LatLon latLon = new LatLon(( 85 94 (double) xpathLat.evaluate(node, XPathConstants.NUMBER)), 86 (double) xpathLon.evaluate(node, XPathConstants.NUMBER)) 87 )); 95 (double) xpathLon.evaluate(node, XPathConstants.NUMBER)); 96 if ("wikidata".equals(wikipediaLang)) { 97 entries.add(new WikidataEntry(name, latLon, getLabelForWikidata(name, Locale.getDefault()))); 98 } else { 99 entries.add(new WikipediaEntry(name, wikipediaLang, name, latLon 100 )); 101 } 88 102 } 89 103 return entries; … … 166 180 167 181 static Collection<String> getWikipediaArticles(final String wikipediaLang, OsmPrimitive p) { 182 if ("wikidata".equals(wikipediaLang)) { 183 return Utils.filter(Collections.singleton(p.get("wikidata")), Predicates.not(Predicates.isNull())); 184 } 168 185 final Map<String, String> tags = p.getKeys(); 169 186 return Utils.transform(Utils.filter( … … 266 283 try { 267 284 Collection<WikipediaLangArticle> r = new ArrayList<>(); 268 final String url = "https://" + wikipediaLang + ".wikipedia.org/w/api.php" +285 final String url = getSiteUrl(wikipediaLang) + "/w/api.php" + 269 286 "?action=query" + 270 287 "&prop=langlinks" + … … 289 306 static LatLon getCoordinateForArticle(String wikipediaLang, String article) { 290 307 try { 291 final String url = "https://" + wikipediaLang + ".wikipedia.org/w/api.php" +308 final String url = getSiteUrl(wikipediaLang) + "/w/api.php" + 292 309 "?action=query" + 293 310 "&prop=coordinates" + … … 395 412 } 396 413 397 protected finalTag createWikipediaTag() {414 protected Tag createWikipediaTag() { 398 415 return new Tag("wikipedia", wikipediaLang + ":" + wikipediaArticle); 399 416 } … … 408 425 409 426 public String getBrowserUrl() { 410 return "https://" + wikipediaLang + ".wikipedia.org/wiki/" 411 + Utils.encodeUrl(wikipediaArticle.replace(" ", "_")); 427 return getSiteUrl(wikipediaLang) + "/wiki/" + Utils.encodeUrl(wikipediaArticle.replace(" ", "_")); 428 } 429 430 public String getLabelText() { 431 return name; 412 432 } 413 433 … … 420 440 public int compareTo(WikipediaEntry o) { 421 441 return AlphanumComparator.getInstance().compare(name, o.name); 442 } 443 } 444 445 static class WikidataEntry extends WikipediaEntry { 446 447 WikidataEntry(String id, LatLon coordinate, String label) { 448 super(label, "wikidata", id, coordinate); 449 ensureValidWikidataId(id); 450 } 451 452 @Override 453 protected Tag createWikipediaTag() { 454 return new Tag("wikidata", wikipediaArticle); 455 } 456 457 @Override 458 public String getLabelText() { 459 return getLabelText(name, wikipediaArticle); 460 } 461 462 static String getLabelText(String bold, String gray) { 463 return Utils.escapeReservedCharactersHTML(bold) + " <span color='gray'>" + Utils.escapeReservedCharactersHTML(gray) + "</span>"; 422 464 } 423 465 } -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaToggleDialog.java
r32607 r32610 36 36 import org.openstreetmap.josm.data.preferences.IntegerProperty; 37 37 import org.openstreetmap.josm.data.preferences.StringProperty; 38 import org.openstreetmap.josm.gui.MapView;39 38 import org.openstreetmap.josm.gui.SideButton; 40 39 import org.openstreetmap.josm.gui.dialogs.ToggleDialog; 41 40 import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent; 42 41 import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener; 43 import org.openstreetmap.josm.gui.layer.OsmDataLayer;44 42 import org.openstreetmap.josm.tools.ImageProvider; 45 43 import org.openstreetmap.josm.tools.LanguageInfo; … … 52 50 super(tr("Wikipedia"), "wikipedia", tr("Fetch Wikipedia articles with coordinates"), null, 150); 53 51 createLayout(list, true, Arrays.asList( 54 new SideButton(new WikipediaLoadCoordinatesAction()), 52 new SideButton(new WikipediaLoadCoordinatesAction(false)), 53 new SideButton(new WikipediaLoadCoordinatesAction(true)), 55 54 new SideButton(new WikipediaLoadCategoryAction()), 56 55 new SideButton(new PasteWikipediaArticlesAction()), … … 88 87 @Override 89 88 public JLabel getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 90 JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);91 89 final WikipediaEntry entry = (WikipediaEntry) value; 90 final String labelText = "<html>" + entry.getLabelText(); 91 final JLabel label = (JLabel) super.getListCellRendererComponent(list, labelText, index, isSelected, cellHasFocus); 92 92 if (entry.getWiwosmStatus() != null && entry.getWiwosmStatus()) { 93 93 label.setIcon(ImageProvider.getIfAvailable("misc", "grey_check")); … … 111 111 112 112 private void updateTitle() { 113 final String lang = getLanguageOfFirstItem(); 114 final String host = WikipediaApp.getSiteUrl(lang).split("/+")[1]; 113 115 if (titleContext == null) { 114 setTitle( /* I18n: [language].Wikipedia.org */ tr("{0}.Wikipedia.org", wikipediaLang.get()));116 setTitle(host); 115 117 } else { 116 setTitle(/* I18n: [language].Wikipedia.org: [context] */ tr("{0}.Wikipedia.org: {1}", wikipediaLang.get(), titleContext)); 118 setTitle(tr("{0}: {1}", host, titleContext)); 119 } 120 } 121 122 private String getLanguageOfFirstItem() { 123 try { 124 return list.getModel().getElementAt(0).wikipediaLang; 125 } catch (ArrayIndexOutOfBoundsException ignore) { 126 return wikipediaLang.get(); 117 127 } 118 128 } … … 120 130 class WikipediaLoadCoordinatesAction extends AbstractAction { 121 131 122 public WikipediaLoadCoordinatesAction() { 123 super(tr("Coordinates")); 124 new ImageProvider("dialogs", "wikipedia").getResource().attachImageIcon(this, true); 125 putValue(SHORT_DESCRIPTION, tr("Fetches all coordinates from Wikipedia in the current view")); 132 private final boolean wikidata; 133 134 public WikipediaLoadCoordinatesAction(boolean wikidata) { 135 super(wikidata ? tr("Wikidata") : tr("Coordinates")); 136 this.wikidata = wikidata; 137 new ImageProvider("dialogs", wikidata ? "wikidata" : "wikipedia").getResource().attachImageIcon(this, true); 138 putValue(SHORT_DESCRIPTION, wikidata 139 ? tr("Fetches all coordinates from Wikidata in the current view") 140 : tr("Fetches all coordinates from Wikipedia in the current view")); 126 141 } 127 142 … … 140 155 List<WikipediaEntry> getEntries() { 141 156 return WikipediaApp.getEntriesFromCoordinates( 142 wiki pediaLang.get(), min, max);157 wikidata ? "wikidata" : wikipediaLang.get(), min, max); 143 158 } 144 159 }.execute(); … … 161 176 publish(entries.toArray(new WikipediaEntry[entries.size()])); 162 177 for (List<WikipediaEntry> chunk : WikipediaApp.partitionList(entries, wikipediaStatusUpdateChunkSize.get())) { 163 WikipediaApp.updateWIWOSMStatus( wikipediaLang.get(), chunk);178 WikipediaApp.updateWIWOSMStatus(chunk.get(0).wikipediaLang, chunk); 164 179 } 165 180 return null; … … 172 187 model.addElement(i); 173 188 } 189 updateTitle(); 190 updateWikipediaArticles(); 174 191 } 175 192 … … 309 326 return; 310 327 } 311 final LatLon latLon = WikipediaApp.getCoordinateForArticle(entry.wikipediaLang, entry.wikipediaArticle); 328 final LatLon latLon = entry.coordinate != null 329 ? entry.coordinate 330 : WikipediaApp.getCoordinateForArticle(entry.wikipediaLang, entry.wikipediaArticle); 312 331 if (latLon == null) { 313 332 return; … … 318 337 319 338 protected void updateWikipediaArticles() { 339 final String language = getLanguageOfFirstItem(); 320 340 articles.clear(); 321 341 if (Main.main != null && Main.getLayerManager().getEditDataSet() != null) { 322 342 for (final OsmPrimitive p : Main.getLayerManager().getEditDataSet().allPrimitives()) { 323 articles.addAll(WikipediaApp.getWikipediaArticles( wikipediaLang.get(), p));343 articles.addAll(WikipediaApp.getWikipediaArticles(language, p)); 324 344 } 325 345 }
Note:
See TracChangeset
for help on using the changeset viewer.