1 package tim.prune.gui.map;
4 import java.awt.image.ImageObserver;
5 import java.net.MalformedURLException;
8 import tim.prune.config.Config;
12 * Class responsible for managing the map tiles,
13 * including invoking the correct memory cacher(s) and/or disk cacher(s)
15 public class MapTileManager implements ImageObserver
17 /** Consumer object to inform when tiles received */
18 private TileConsumer _consumer = null;
19 /** Current map source */
20 private MapSource _mapSource = null;
21 /** Array of tile caches, one per layer */
22 private MemTileCacher[] _tempCaches = null;
23 /** Flag for whether to download any tiles or just pull from disk */
24 private boolean _downloadTiles = true;
25 /** Flag for whether to return incomplete images or just pass to tile cache until they're done */
26 private boolean _returnIncompleteImages = false;
27 /** Number of layers */
28 private int _numLayers = -1;
29 /** Current zoom level */
30 private int _zoom = 0;
31 /** Number of tiles in each direction for this zoom level */
32 private int _numTileIndices = 1;
37 * @param inConsumer consumer object to be notified
39 public MapTileManager(TileConsumer inConsumer)
41 _consumer = inConsumer;
46 * @param inZoom zoom level
47 * @param inTileX x coord of central tile
48 * @param inTileY y coord of central tile
50 public void centreMap(int inZoom, int inTileX, int inTileY)
53 // Pass params onto all memory cachers
54 if (_tempCaches != null) {
55 for (int i=0; i<_tempCaches.length; i++) {
56 _tempCaches[i].centreMap(inZoom, inTileX, inTileY);
61 /** @param inZoom zoom level to set */
62 public void setZoom(int inZoom)
65 // Calculate number of tiles = 2^^zoom
66 _numTileIndices = 1 << _zoom;
70 * @return true if zoom is too high for tiles
72 public boolean isOverzoomed()
74 // Ask current map source what maximum zoom is
75 int maxZoom = (_mapSource == null?0:_mapSource.getMaxZoomLevel());
76 return (_zoom > maxZoom);
80 * Enable or disable tile downloading
81 * @param inEnabled true to enable downloading, false to just get tiles from disk
83 public void enableTileDownloading(boolean inEnabled)
85 _downloadTiles = inEnabled;
88 /** Configure to return incomplete images instead of going via caches (and another call) */
89 public void setReturnIncompleteImages()
91 _returnIncompleteImages = true;
95 * Clear all the memory caches due to changed config / zoom
97 public void clearMemoryCaches()
99 int numLayers = _mapSource.getNumLayers();
100 if (_tempCaches == null || _tempCaches.length != numLayers)
102 // Cachers don't match, so need to create the right number of them
103 _tempCaches = new MemTileCacher[numLayers];
104 for (int i=0; i<numLayers; i++) {
105 _tempCaches[i] = new MemTileCacher();
109 // Cachers already there, just need to be cleared
110 for (int i=0; i<numLayers; i++) {
111 _tempCaches[i].clearAll();
117 * @param inSourceNum selected map source index
119 public void setMapSource(int inSourceNum)
121 setMapSource(MapSourceLibrary.getSource(inSourceNum));
125 * @param inMapSource selected map source
127 public void setMapSource(MapSource inMapSource)
129 _mapSource = inMapSource;
130 if (_mapSource == null) {_mapSource = MapSourceLibrary.getSource(0);}
132 _numLayers = _mapSource.getNumLayers();
136 * @return the number of layers in the map
138 public int getNumLayers()
144 * Get a tile from the currently selected map source
145 * @param inLayer layer number, starting from 0
146 * @param inX x index of tile
147 * @param inY y index of tile
148 * @param inDownloadIfNecessary true to download the file if it's not available
149 * @return selected tile if already loaded, or null otherwise
151 public Image getTile(int inLayer, int inX, int inY, boolean inDownloadIfNecessary)
153 if (inY < 0 || inY >= _numTileIndices) return null;
154 // Wrap tile indices which are too big or too small
155 inX = ((inX % _numTileIndices) + _numTileIndices) % _numTileIndices;
157 // Check first in memory cache for tile
159 MemTileCacher tempCache = null;
160 if (_tempCaches != null)
162 tempCache = _tempCaches[inLayer]; // Should probably guard array indexes here
163 tile = tempCache.getTile(inX, inY);
169 // Tile wasn't in memory, but maybe it's in disk cache (if there is one)
170 String diskCachePath = Config.getConfigString(Config.KEY_DISK_CACHE);
171 boolean useDisk = (diskCachePath != null);
172 boolean onlineMode = Config.getConfigBoolean(Config.KEY_ONLINE_MODE);
175 tile = DiskTileCacher.getTile(diskCachePath, _mapSource.makeFilePath(inLayer, _zoom, inX, inY), onlineMode);
178 if (_returnIncompleteImages) {return tile;}
179 // Pass tile to memory cache
180 if (tempCache != null) {
181 tempCache.setTile(tile, inX, inY, _zoom);
183 if (tile.getWidth(this) > 0) {return tile;}
186 // else System.out.println("DTC gave null tile for " + _zoom + ", " + inX + ", " + inY);
188 // Tile wasn't in memory or on disk, so if online let's get it
189 if (onlineMode && _downloadTiles && inDownloadIfNecessary)
193 URL tileUrl = new URL(_mapSource.makeURL(inLayer, _zoom, inX, inY));
194 // System.out.println("Going to fetch: " + tileUrl);
195 if (useDisk && DiskTileCacher.saveTile(tileUrl, diskCachePath,
196 _mapSource.makeFilePath(inLayer, _zoom, inX, inY), this))
198 // Image now copied directly from URL stream to disk cache
202 // Load image asynchronously, using observer
203 // In order to set the http user agent, need to use a TileDownloader instead
204 TileDownloader.triggerLoad(this, tileUrl, inLayer, inX, inY, _zoom);
207 catch (MalformedURLException urle) {} // ignore
213 * Method called by image loader to inform of updates to the tiles
214 * @param img the image
215 * @param infoflags flags describing how much of the image is known
218 * @param width ignored
219 * @param height ignored
220 * @return false to carry on loading, true to stop
222 public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
224 boolean loaded = (infoflags & ImageObserver.ALLBITS) > 0;
225 boolean error = (infoflags & ImageObserver.ERROR) > 0;
226 if (loaded || error) {
227 _consumer.tilesUpdated(loaded);
233 * Callback method from TileDownloader to let us know that an image has been loaded
234 * @param inTile Loaded Image object
235 * @param inLayer layer index from 0
236 * @param inX x coordinate of tile
237 * @param inY y coordinate of tile
238 * @param inZoom zoom level of loaded image
240 public void notifyImageLoaded(Image inTile, int inLayer, int inX, int inY, int inZoom)
242 if (inTile != null && _tempCaches != null)
244 MemTileCacher tempCache = _tempCaches[inLayer]; // Should probably guard against nulls and array indexes here
245 if (tempCache.getTile(inX, inY) == null)
247 // Check with cache that the zoom level is still valid
248 tempCache.setTile(inTile, inX, inY, inZoom);
249 inTile.getWidth(this); // trigger imageUpdate when image is ready
252 else if (inTile != null) {
253 inTile.getWidth(this);