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