source: josm/trunk/src/org/openstreetmap/josm/gui/layer/geoimage/WikimediaCommonsLoader.java

Last change on this file was 18943, checked in by GerdP, 22 months ago

fix #23417: Load images from Wikimedia Commons can cause crash report

  • disable the action when last layer was closed

This requires special action in layerRemoving(), so default listeners in JosmAction are no longer needed

File size: 3.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.geoimage;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.io.IOException;
8import java.util.ArrayList;
9import java.util.List;
10
11import javax.xml.parsers.ParserConfigurationException;
12import javax.xml.xpath.XPathExpressionException;
13
14import org.openstreetmap.josm.actions.JosmAction;
15import org.openstreetmap.josm.data.Bounds;
16import org.openstreetmap.josm.gui.MainApplication;
17import org.openstreetmap.josm.gui.PleaseWaitRunnable;
18import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
19import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
20import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
21import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
22import org.openstreetmap.josm.io.OsmTransferException;
23import org.openstreetmap.josm.tools.Logging;
24import org.openstreetmap.josm.tools.Mediawiki;
25import org.xml.sax.SAXException;
26
27/**
28 * Loads geocoded images from <a href="https://commons.wikimedia.org/">Wikimedia Commons</a> for the given bounding box.
29 */
30public class WikimediaCommonsLoader extends PleaseWaitRunnable {
31 protected String apiUrl = "https://commons.wikimedia.org/w/api.php";
32 protected GeoImageLayer layer;
33 private final Bounds bounds;
34
35 /**
36 * Constructs a new {@code WikimediaCommonsLoader}
37 * @param bounds The bounds to load
38 */
39 public WikimediaCommonsLoader(Bounds bounds) {
40 super(tr("Load images from Wikimedia Commons"));
41 this.bounds = bounds;
42 }
43
44 @Override
45 protected void realRun() throws SAXException, IOException, OsmTransferException {
46 List<ImageEntry> imageEntries = new ArrayList<>();
47 try {
48 new Mediawiki(apiUrl).searchGeoImages(bounds, (title, latLon) -> imageEntries.add(new WikimediaCommonsEntry(title, latLon)));
49 } catch (ParserConfigurationException | XPathExpressionException e) {
50 throw new IllegalStateException(e);
51 }
52 Logging.info("Loaded {0} images from Wikimedia Commons", imageEntries.size());
53 layer = new WikimediaCommonsLayer(imageEntries);
54 }
55
56 @Override
57 protected void finish() {
58 if (layer != null) {
59 MainApplication.getLayerManager().addLayer(layer);
60 }
61 }
62
63 @Override
64 protected void cancel() {
65 // do nothing
66 }
67
68 /**
69 * Load images from Wikimedia Commons
70 * @since 18021
71 */
72 public static class WikimediaCommonsLoadImagesAction extends JosmAction implements LayerChangeListener {
73 /**
74 * Constructs a new {@code WikimediaCommonsLoadImagesAction}
75 */
76 public WikimediaCommonsLoadImagesAction() {
77 super(tr("Load images from Wikimedia Commons"), "wikimedia_commons", null, null, false, false);
78 MainApplication.getLayerManager().addLayerChangeListener(this);
79 initEnabledState();
80 }
81
82 @Override
83 public void actionPerformed(ActionEvent e) {
84 Bounds bounds = MainApplication.getMap().mapView.getRealBounds();
85 MainApplication.worker.execute(new WikimediaCommonsLoader(bounds));
86 }
87
88 @Override
89 protected void updateEnabledState() {
90 setEnabled(MainApplication.isDisplayingMapView());
91 }
92
93 @Override
94 public void layerAdded(LayerAddEvent e) {
95 updateEnabledState();
96 }
97
98 @Override
99 public void layerRemoving(LayerRemoveEvent e) {
100 if (e.isLastLayer())
101 setEnabled(false);
102 }
103
104 @Override
105 public void layerOrderChanged(LayerOrderChangeEvent e) {
106 // not used
107 }
108
109 @Override
110 public void destroy() {
111 MainApplication.getLayerManager().removeLayerChangeListener(this);
112 super.destroy();
113 }
114 }
115}
Note: See TracBrowser for help on using the repository browser.