001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.layer.geoimage;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.event.ActionEvent;
007import java.io.IOException;
008import java.util.ArrayList;
009import java.util.List;
010
011import javax.xml.parsers.ParserConfigurationException;
012import javax.xml.xpath.XPathExpressionException;
013
014import org.openstreetmap.josm.actions.JosmAction;
015import org.openstreetmap.josm.data.Bounds;
016import org.openstreetmap.josm.gui.MainApplication;
017import org.openstreetmap.josm.gui.PleaseWaitRunnable;
018import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
019import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
020import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
021import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
022import org.openstreetmap.josm.io.OsmTransferException;
023import org.openstreetmap.josm.tools.Logging;
024import org.openstreetmap.josm.tools.Mediawiki;
025import org.xml.sax.SAXException;
026
027/**
028 * Loads geocoded images from <a href="https://commons.wikimedia.org/">Wikimedia Commons</a> for the given bounding box.
029 */
030public class WikimediaCommonsLoader extends PleaseWaitRunnable {
031    protected String apiUrl = "https://commons.wikimedia.org/w/api.php";
032    protected GeoImageLayer layer;
033    private final Bounds bounds;
034
035    /**
036     * Constructs a new {@code WikimediaCommonsLoader}
037     * @param bounds The bounds to load
038     */
039    public WikimediaCommonsLoader(Bounds bounds) {
040        super(tr("Load images from Wikimedia Commons"));
041        this.bounds = bounds;
042    }
043
044    @Override
045    protected void realRun() throws SAXException, IOException, OsmTransferException {
046        List<ImageEntry> imageEntries = new ArrayList<>();
047        try {
048            new Mediawiki(apiUrl).searchGeoImages(bounds, (title, latLon) -> imageEntries.add(new WikimediaCommonsEntry(title, latLon)));
049        } catch (ParserConfigurationException | XPathExpressionException e) {
050            throw new IllegalStateException(e);
051        }
052        Logging.info("Loaded {0} images from Wikimedia Commons", imageEntries.size());
053        layer = new WikimediaCommonsLayer(imageEntries);
054    }
055
056    @Override
057    protected void finish() {
058        if (layer != null) {
059            MainApplication.getLayerManager().addLayer(layer);
060        }
061    }
062
063    @Override
064    protected void cancel() {
065        // do nothing
066    }
067
068    /**
069     * Load images from Wikimedia Commons
070     * @since 18021
071     */
072    public static class WikimediaCommonsLoadImagesAction extends JosmAction implements LayerChangeListener {
073        /**
074         * Constructs a new {@code WikimediaCommonsLoadImagesAction}
075         */
076        public WikimediaCommonsLoadImagesAction() {
077            super(tr("Load images from Wikimedia Commons"), "wikimedia_commons", null, null, false, false);
078            MainApplication.getLayerManager().addLayerChangeListener(this);
079            initEnabledState();
080        }
081
082        @Override
083        public void actionPerformed(ActionEvent e) {
084            Bounds bounds = MainApplication.getMap().mapView.getRealBounds();
085            MainApplication.worker.execute(new WikimediaCommonsLoader(bounds));
086        }
087
088        @Override
089        protected void updateEnabledState() {
090            setEnabled(MainApplication.isDisplayingMapView());
091        }
092
093        @Override
094        public void layerAdded(LayerAddEvent e) {
095            updateEnabledState();
096        }
097
098        @Override
099        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}