    001// License: GPL. For details, see LICENSE file.
    002package org.openstreetmap.josm.plugins.streetside.cubemap;
    003
    004import java.awt.image.BufferedImage;
    005import java.util.HashMap;
    006import java.util.List;
    007import java.util.Objects;
    008import java.util.concurrent.Callable;
    009import java.util.concurrent.CopyOnWriteArrayList;
    010
    011import javax.imageio.ImageIO;
    012
    013import org.openstreetmap.josm.plugins.streetside.cache.StreetsideCache;
    014import org.openstreetmap.josm.plugins.streetside.utils.StreetsideURL;
    015import org.openstreetmap.josm.tools.I18n;
    016import org.openstreetmap.josm.tools.Logging;
    017
    018import us.monoid.web.Resty;
    019
    020public class TileDownloadingTask implements Callable<String> {
    021
    022        private String tileId;
    023        private final long startTime = System.currentTimeMillis();
    024        private StreetsideCache cache;
    025        protected CubemapBuilder cb;
    026
    027        /**
    028           * Listeners of the class.
    029           */
    030    private final List<ITileDownloadingTaskListener> listeners = new CopyOnWriteArrayList<>();
    031
    032        boolean cancelled = false;
    033
    034        public TileDownloadingTask(String id) {
    035                tileId = id;
    036                cb = CubemapBuilder.getInstance();
    037                addListener(CubemapBuilder.getInstance());
    038        }
    039
    040        /**
    041           * Adds a new listener.
    042           *
    043           * @param lis Listener to be added.
    044           */
    045        public final void addListener(final ITileDownloadingTaskListener lis) {
    046            listeners.add(lis);
    047        }
    048
    049        /**
    050         * @return the tileId
    051         */
    052        public String getId() {
    053                return tileId;
    054        }
    055
    056        /**
    057         * @param id the tileId to set
    058         */
    059        public void setId(String id) {
    060                tileId = id;
    061        }
    062
    063        /**
    064         * @return the cache
    065         */
    066        public StreetsideCache getCache() {
    067                return cache;
    068        }
    069
    070        /**
    071         * @param cache the cache to set
    072         */
    073        public void setCache(StreetsideCache cache) {
    074                this.cache = cache;
    075        }
    076
    077        /**
    078         * @return the cb
    079         */
    080        public CubemapBuilder getCb() {
    081                return cb;
    082        }
    083
    084        /**
    085         * @param cb the cb to set
    086         */
    087        public void setCb(CubemapBuilder cb) {
    088                this.cb = cb;
    089        }
    090
    091        /**
    092         * @param cancelled the cancelled to set
    093         */
    094        public void setCancelled(boolean cancelled) {
    095                this.cancelled = cancelled;
    096        }
    097
    098        @Override
    099        public String call() throws Exception {
    100
    101                BufferedImage img = ImageIO.read(new Resty().bytes(
    102                                StreetsideURL.VirtualEarth.streetsideTile(tileId, false).toExternalForm())
    103                                .stream());
    104
    105                if (img == null) {
    106                        Logging.error(I18n.tr("Download of BufferedImage {0} is null!", tileId));
    107                }
    108
    109                //String faceId = CubemapUtils.getFaceIdFromTileId(tileId);
    110
    111                /*Map<String, Map<String, BufferedImage>> faces2TilesMap = CubemapBuilder
    112                                .getInstance().getCubemap().getFace2TilesMap();*/
    113
    114                /*if(CubemapBuilder.getInstance().getTileImages().get(tileId)==null) {
    115                  CubemapBuilder.getInstance().getTileImages().put(tileId, new HashMap<String,BufferedImage>());
    116                }*/
    117                CubemapBuilder.getInstance().getTileImages().put(tileId, img);
    118
    119                fireTileAdded(tileId);
    120
    121                long endTime = System.currentTimeMillis();
    122                long runTime = (endTime-startTime)/1000;
    123                Logging.debug("Loaded image for tile {0} in {1} seconds", tileId, runTime);
    124
    125                return tileId;
    126        }
    127
    128        private void fireTileAdded(String id) {
    129            listeners.stream().filter(Objects::nonNull).forEach(lis -> lis.tileAdded(id));
    130        }
    131}