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