Changeset 32687 in osm for applications/editors/josm/plugins/wikipedia
- Timestamp:
- 2016-07-21T12:20:41+02:00 (8 years ago)
- Location:
- applications/editors/josm/plugins/wikipedia
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/Debouncer.java
r32625 r32687 19 19 */ 20 20 public void debounce(final Object key, final Runnable runnable, long delay, TimeUnit unit) { 21 final Future<?> prev = delayedMap.put(key, scheduler.schedule(new Runnable() { 22 @Override 23 public void run() { 24 try { 25 runnable.run(); 26 } finally { 27 delayedMap.remove(key); 28 } 21 final Future<?> prev = delayedMap.put(key, scheduler.schedule(() -> { 22 try { 23 runnable.run(); 24 } finally { 25 delayedMap.remove(key); 29 26 } 30 27 }, delay, unit)); -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/GuiUtils.java
r32058 r32687 14 14 import java.util.SortedSet; 15 15 import java.util.TreeSet; 16 import java.util. concurrent.Callable;16 import java.util.stream.Collectors; 17 17 18 18 import static org.openstreetmap.josm.tools.I18n.tr; 19 19 import static org.openstreetmap.josm.tools.I18n.trn; 20 20 21 publicclass GuiUtils {21 class GuiUtils { 22 22 23 23 static final String PREF_OVERWRITE = "wikipedia.overwrite-tag"; 24 24 25 protected static boolean confirmOverwrite(final String key, final String newValue, final Collection<OsmPrimitive> primitives) { 26 final SortedSet<String> existingValues = new TreeSet<>(AlphanumComparator.getInstance()); 27 existingValues.addAll(Utils.transform(primitives, new Utils.Function<OsmPrimitive, String>() { 28 @Override 29 public String apply(OsmPrimitive x) { 30 return x.get(key); 31 } 32 })); 33 existingValues.remove(newValue); 34 existingValues.remove(null); 25 static boolean confirmOverwrite(final String key, final String newValue, final Collection<OsmPrimitive> primitives) { 26 final SortedSet<String> existingValues = primitives.stream() 27 .map(x -> x.get(key)) 28 .filter(x -> x != null && !newValue.equals(x)) 29 .collect(Collectors.toCollection(() -> new TreeSet<>(AlphanumComparator.getInstance()))); 35 30 36 31 if (existingValues.isEmpty()) { 37 32 return true; 38 33 } 39 final Boolean r = GuiHelper.runInEDTAndWaitAndReturn(new Callable<Boolean>() { 40 @Override 41 public Boolean call() throws Exception { 42 return ConditionalOptionPaneUtil.showConfirmationDialog(PREF_OVERWRITE, Main.parent, 34 final Boolean r = GuiHelper.runInEDTAndWaitAndReturn(() -> 35 ConditionalOptionPaneUtil.showConfirmationDialog(PREF_OVERWRITE, Main.parent, 43 36 trn( 44 37 "Overwrite ''{0}'' tag {1} from {2} with new value ''{3}''?", … … 49 42 JOptionPane.YES_NO_OPTION, 50 43 JOptionPane.QUESTION_MESSAGE, 51 JOptionPane.YES_OPTION); 52 } 53 }); 44 JOptionPane.YES_OPTION)); 54 45 return Boolean.TRUE.equals(r); 55 46 } -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikidataItemSearchDialog.java
r32670 r32687 8 8 import java.awt.GridBagLayout; 9 9 import java.awt.event.ActionEvent; 10 import java.awt.event.ActionListener;11 10 import java.util.Collection; 12 11 import java.util.Collections; … … 30 29 import org.openstreetmap.josm.gui.util.GuiHelper; 31 30 import org.openstreetmap.josm.tools.GBC; 32 import org.openstreetmap.josm.tools.Predicate;33 import org.openstreetmap.josm.tools.Utils;34 31 35 32 public final class WikidataItemSearchDialog extends ExtendedDialog { … … 42 39 super(Main.parent, tr("Search Wikidata items"), new String[]{tr("Add Tag"), tr("Cancel")}); 43 40 this.selector = new Selector(); 44 this.selector.setDblClickListener(new ActionListener() { 45 @Override 46 public void actionPerformed(ActionEvent e) { 47 buttonAction(0, null); 48 } 49 }); 41 this.selector.setDblClickListener(e -> buttonAction(0, null)); 50 42 this.targetKey = new AutoCompletingComboBox(); 51 43 this.targetKey.setEditable(true); … … 93 85 keys.add(new AutoCompletionListItem("subject:wikidata")); 94 86 keys.add(new AutoCompletionListItem("name:etymology:wikidata")); 95 keys.addAll(Utils.filter(editDataSet.getAutoCompletionManager().getKeys(), new Predicate<AutoCompletionListItem>() { 96 @Override 97 public boolean evaluate(AutoCompletionListItem object) { 98 return object.getValue().contains("wikidata"); 99 } 100 })); 87 editDataSet.getAutoCompletionManager().getKeys().stream() 88 .filter(v -> v.getValue().contains("wikidata")) 89 .forEach(keys::add); 101 90 targetKey.setPossibleACItems(keys); 102 91 } … … 134 123 protected void filterItems() { 135 124 final String query = edSearchText.getText(); 136 debouncer.debounce(Void.class, new Runnable() { 137 @Override 138 public void run() { 139 final List<WikipediaApp.WikidataEntry> entries = query == null || query.isEmpty() 140 ? Collections.<WikipediaApp.WikidataEntry>emptyList() 141 : WikipediaApp.getWikidataEntriesForQuery(WikipediaToggleDialog.wikipediaLang.get(), query, Locale.getDefault()); 142 GuiHelper.runInEDT(new Runnable() { 143 @Override 144 public void run() { 145 lsResultModel.setItems(entries); 146 } 147 }); 148 } 125 debouncer.debounce(getClass(), () -> { 126 final List<WikipediaApp.WikidataEntry> entries = query == null || query.isEmpty() 127 ? Collections.emptyList() 128 : WikipediaApp.getWikidataEntriesForQuery(WikipediaToggleDialog.wikipediaLang.get(), query, Locale.getDefault()); 129 GuiHelper.runInEDT(() -> lsResultModel.setItems(entries)); 149 130 }, 200, TimeUnit.MILLISECONDS); 150 131 } -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaAddNamesAction.java
r32424 r32687 20 20 21 21 public class WikipediaAddNamesAction extends JosmAction { 22 23 final StringProperty wikipediaLang = new StringProperty("wikipedia.lang", LanguageInfo.getJOSMLocaleCode().substring(0, 2));24 22 25 23 public WikipediaAddNamesAction() { -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java
r32671 r32687 20 20 import java.util.regex.Matcher; 21 21 import java.util.regex.Pattern; 22 import java.util.stream.Collectors; 23 import java.util.stream.Stream; 22 24 23 25 import javax.xml.parsers.DocumentBuilder; … … 37 39 import org.openstreetmap.josm.tools.CheckParameterUtil; 38 40 import org.openstreetmap.josm.tools.HttpClient; 39 import org.openstreetmap.josm.tools.Predicate;40 import org.openstreetmap.josm.tools.Predicates;41 41 import org.openstreetmap.josm.tools.Utils; 42 import org.openstreetmap.josm.tools.Utils.Function;43 42 import org.w3c.dom.Document; 44 43 import org.w3c.dom.Node; … … 103 102 if ("wikidata".equals(wikipediaLang)) { 104 103 final List<WikidataEntry> withLabel = getLabelForWikidata(entries, Locale.getDefault()); 105 return new ArrayList< WikipediaEntry>(withLabel);104 return new ArrayList<>(withLabel); 106 105 } else { 107 106 return entries; … … 162 161 163 162 static List<WikipediaEntry> getEntriesFromClipboard(final String wikipediaLang) { 164 final List<String> clipboardLines = Arrays.asList(Utils.getClipboardContent().split("[\\n\\r]+")); 165 return new ArrayList<>(Utils.transform(clipboardLines, new Function<String, WikipediaEntry>() { 166 167 @Override 168 public WikipediaEntry apply(String x) { 169 return new WikipediaEntry(wikipediaLang, x); 170 } 171 })); 163 return Arrays.stream(Utils.getClipboardContent().split("[\\n\\r]+")) 164 .map(x -> new WikipediaEntry(wikipediaLang, x)) 165 .collect(Collectors.toList()); 172 166 } 173 167 … … 210 204 } 211 205 212 static Collection<String> getWikipediaArticles(final String wikipediaLang, OsmPrimitive p) {206 static Stream<String> getWikipediaArticles(final String wikipediaLang, OsmPrimitive p) { 213 207 if ("wikidata".equals(wikipediaLang)) { 214 return Utils.filter(Collections.singleton(p.get("wikidata")), Predicates.not(Predicates.isNull()));208 return Stream.of(p.get("wikidata")).filter(Objects::nonNull); 215 209 } 216 210 final Map<String, String> tags = p.getKeys(); 217 return Utils.transform(Utils.filter(218 Arrays.asList(211 return Stream 212 .of( 219 213 WikipediaLangArticle.parseTag("wikipedia", tags.get("wikipedia")), 220 214 WikipediaLangArticle.parseTag("wikipedia:" + wikipediaLang, tags.get("wikipedia:" + wikipediaLang)) 221 ), new Predicate<WikipediaLangArticle>() { 222 @Override 223 public boolean evaluate(WikipediaLangArticle wp) { 224 return wp != null && wikipediaLang.equals(wp.lang); 225 } 226 }), new Function<WikipediaLangArticle, String>() { 227 @Override 228 public String apply(WikipediaLangArticle wp) { 229 return wp.article; 230 } 231 }); 215 ).filter(Objects::nonNull) 216 .filter(wikipediaLang::equals) 217 .map(wp -> wp.article); 232 218 } 233 219 … … 250 236 "&sitefilter=" + wikipediaLang + "wiki" + 251 237 "&format=xml" + 252 "&titles=" + Utils.join("|", Utils.transform(articles, new Function<String, String>() { 253 @Override 254 public String apply(String x) { 255 return Utils.encodeUrl(x); 256 } 257 })); 238 "&titles=" + articles.stream().map(Utils::encodeUrl).collect(Collectors.joining("|")); 258 239 final Map<String, String> r = new TreeMap<>(); 259 240 try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) { … … 315 296 return entriesWithLabel; 316 297 } 317 final Collection<String> wikidataIds = Utils.transform(entries, new Function<WikipediaEntry, String>() {318 @Override319 public String apply(WikipediaEntry x) {320 return x.wikipediaArticle;321 }322 });323 298 try { 324 299 final String url = "https://www.wikidata.org/w/api.php" + 325 300 "?action=wbgetentities" + 326 301 "&props=labels|descriptions" + 327 "&ids=" + Utils.join("|", wikidataIds) +302 "&ids=" + entries.stream().map(x -> x.wikipediaArticle).collect(Collectors.joining("|")) + 328 303 "&format=xml"; 329 304 final Collection<String> languages = new ArrayList<>(); -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaCategorySearchDialog.java
r32670 r32687 23 23 super(Main.parent, tr("Search Wikipedia category"), new String[]{tr("Load category"), tr("Cancel")}); 24 24 this.selector = new Selector(); 25 this.selector.setDblClickListener(new ActionListener() { 26 @Override 27 public void actionPerformed(ActionEvent e) { 28 buttonAction(0, null); 29 } 30 }); 25 this.selector.setDblClickListener(e -> buttonAction(0, null)); 31 26 32 27 setContent(selector, false); … … 66 61 protected void filterItems() { 67 62 final String query = edSearchText.getText(); 68 debouncer.debounce(Void.class, new Runnable() { 69 @Override 70 public void run() { 71 final List<String> entries = query == null || query.isEmpty() 72 ? Collections.<String>emptyList() 73 : WikipediaApp.getCategoriesForPrefix(WikipediaToggleDialog.wikipediaLang.get(), query); 74 GuiHelper.runInEDT(new Runnable() { 75 @Override 76 public void run() { 77 lsResultModel.setItems(entries); 78 } 79 }); 80 } 63 debouncer.debounce(getClass(), () -> { 64 final List<String> entries = query == null || query.isEmpty() 65 ? Collections.emptyList() 66 : WikipediaApp.getCategoriesForPrefix(WikipediaToggleDialog.wikipediaLang.get(), query); 67 GuiHelper.runInEDT(() -> lsResultModel.setItems(entries)); 81 68 }, 200, TimeUnit.MILLISECONDS); 82 69 } -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaToggleDialog.java
r32670 r32687 12 12 import java.util.HashSet; 13 13 import java.util.List; 14 import java.util.Optional; 14 15 import java.util.Set; 15 16 … … 43 44 import org.openstreetmap.josm.tools.LanguageInfo; 44 45 import org.openstreetmap.josm.tools.OpenBrowser; 45 import org.openstreetmap.josm.tools.Utils;46 46 import org.wikipedia.WikipediaApp.WikipediaEntry; 47 47 … … 73 73 public void mouseClicked(MouseEvent e) { 74 74 if (e.getClickCount() == 2 && getSelectedValue() != null) { 75 final WikipediaEntry entry = (WikipediaEntry)getSelectedValue();75 final WikipediaEntry entry = getSelectedValue(); 76 76 if (entry.coordinate != null) { 77 77 BoundingXYVisitor bbox = new BoundingXYVisitor(); … … 79 79 Main.map.mapView.zoomTo(bbox); 80 80 } 81 final String search = Utils.firstNonNull(entry.label,entry.wikipediaArticle).replaceAll("\\(.*\\)", "");81 final String search = Optional.ofNullable(entry.label).orElse(entry.wikipediaArticle).replaceAll("\\(.*\\)", ""); 82 82 SearchAction.search(search, SearchAction.SearchMode.replace); 83 83 } … … 261 261 public void actionPerformed(ActionEvent e) { 262 262 if (list.getSelectedValue() != null) { 263 final String url = ((WikipediaEntry) list.getSelectedValue()).getBrowserUrl();263 final String url = list.getSelectedValue().getBrowserUrl(); 264 264 Main.info("Wikipedia: opening " + url); 265 265 OpenBrowser.displayUrl(url); … … 357 357 articles.clear(); 358 358 if (Main.main != null && Main.getLayerManager().getEditDataSet() != null) { 359 for (final OsmPrimitive p : Main.getLayerManager().getEditDataSet().allPrimitives()) {360 articles.addAll(WikipediaApp.getWikipediaArticles(language, p));361 }359 Main.getLayerManager().getEditDataSet().allPrimitives().stream() 360 .flatMap(p -> WikipediaApp.getWikipediaArticles(language, p)) 361 .forEach(articles::add); 362 362 } 363 363 } -
applications/editors/josm/plugins/wikipedia/test/unit/org/wikipedia/WikipediaAppTest.java
r32670 r32687 5 5 import org.openstreetmap.josm.Main; 6 6 import org.openstreetmap.josm.data.coor.LatLon; 7 import org.openstreetmap.josm.tools.Predicate;8 import org.openstreetmap.josm.tools.Predicates;9 import org.openstreetmap.josm.tools.Utils;10 7 import org.wikipedia.WikipediaApp.WikipediaEntry; 11 8 import org.wikipedia.WikipediaApp.WikipediaLangArticle; … … 20 17 import static org.hamcrest.CoreMatchers.is; 21 18 import static org.hamcrest.CoreMatchers.nullValue; 19 import static org.junit.Assert.assertEquals; 22 20 import static org.junit.Assert.assertThat; 23 21 import static org.junit.Assert.assertTrue; … … 142 140 final List<WikipediaEntry> entries = WikipediaApp.getEntriesFromCoordinates("de", 143 141 new LatLon(52.5179786, 13.3753321), new LatLon(52.5192215, 13.3768705)); 144 assertTrue(Utils.exists(entries, new Predicate<WikipediaEntry>() { 145 @Override 146 public boolean evaluate(WikipediaEntry entry) { 147 return "Reichstagsgebäude".equals(entry.wikipediaArticle) && "de".equals(entry.wikipediaLang); 148 } 149 })); 142 final long c = entries.stream() 143 .filter(entry -> "Reichstagsgebäude".equals(entry.wikipediaArticle) && "de".equals(entry.wikipediaLang)) 144 .count(); 145 assertEquals(1, c); 150 146 } 151 147 … … 166 162 final List<WikipediaEntry> entries = WikipediaApp.getEntriesFromCoordinates("wikidata", 167 163 new LatLon(47.20, 11.30), new LatLon(47.22, 11.32)); 168 assertTrue(Utils.exists(entries, new Predicate<WikipediaEntry>() { 169 @Override 170 public boolean evaluate(WikipediaEntry entry) { 171 return "Q865406".equals(entry.wikipediaArticle) && "wikidata".equals(entry.wikipediaLang) && "Birgitzer Alm".equals(entry.label); 172 } 173 })); 164 final long c = entries.stream() 165 .filter(entry -> "Q865406".equals(entry.wikipediaArticle) && "wikidata".equals(entry.wikipediaLang) && "Birgitzer Alm".equals(entry.label)) 166 .count(); 167 assertEquals(1, c); 174 168 } 175 169 … … 219 213 public void testCategoriesForPrefix() throws Exception { 220 214 final List<String> categories = WikipediaApp.getCategoriesForPrefix("de", "Gemeinde in Öster"); 221 assertTrue( Utils.exists(categories, Predicates.equalTo("Gemeinde in Österreich")));215 assertTrue(categories.contains("Gemeinde in Österreich")); 222 216 } 223 217 }
Note:
See TracChangeset
for help on using the changeset viewer.