001// License: GPL. For details, see Readme.txt file.
002package org.openstreetmap.gui.jmapviewer;
003
004import org.openstreetmap.gui.jmapviewer.JobDispatcher.JobThread;
005import org.openstreetmap.gui.jmapviewer.interfaces.TileCache;
006import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
007import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
008import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
009import org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource;
010
011public class TileController {
012    protected TileLoader tileLoader;
013    protected TileCache tileCache;
014    protected TileSource tileSource;
015
016    JobDispatcher jobDispatcher;
017
018    public TileController(TileSource source, TileCache tileCache, TileLoaderListener listener) {
019        tileSource = new OsmTileSource.Mapnik();
020        tileLoader = new OsmTileLoader(listener);
021        this.tileCache = tileCache;
022        jobDispatcher = JobDispatcher.getInstance();
023    }
024
025    /**
026     * retrieves a tile from the cache. If the tile is not present in the cache
027     * a load job is added to the working queue of {@link JobThread}.
028     *
029     * @param tilex the X position of the tile
030     * @param tiley the Y position of the tile
031     * @param zoom the zoom level of the tile
032     * @return specified tile from the cache or <code>null</code> if the tile
033     *         was not found in the cache.
034     */
035    public Tile getTile(int tilex, int tiley, int zoom) {
036        int max = (1 << zoom);
037        if (tilex < 0 || tilex >= max || tiley < 0 || tiley >= max)
038            return null;
039        Tile tile = tileCache.getTile(tileSource, tilex, tiley, zoom);
040        if (tile == null) {
041            tile = new Tile(tileSource, tilex, tiley, zoom);
042            tileCache.addTile(tile);
043            tile.loadPlaceholderFromCache(tileCache);
044        }
045        if (tile.error) {
046            tile.loadPlaceholderFromCache(tileCache);
047        }
048        if (!tile.isLoaded()) {
049            jobDispatcher.addJob(tileLoader.createTileLoaderJob(tile));
050        }
051        return tile;
052    }
053
054    public TileCache getTileCache() {
055        return tileCache;
056    }
057
058    public void setTileCache(TileCache tileCache) {
059        this.tileCache = tileCache;
060    }
061
062    public TileLoader getTileLoader() {
063        return tileLoader;
064    }
065
066    public void setTileLoader(TileLoader tileLoader) {
067        this.tileLoader = tileLoader;
068    }
069
070    public TileSource getTileLayerSource() {
071        return tileSource;
072    }
073
074    public TileSource getTileSource() {
075        return tileSource;
076    }
077
078    public void setTileSource(TileSource tileSource) {
079        this.tileSource = tileSource;
080    }
081
082    /**
083     *
084     */
085    public void cancelOutstandingJobs() {
086        jobDispatcher.cancelOutstandingJobs();
087    }
088}