1 | // License: GPL. For details, see Readme.txt file.
|
---|
2 | package org.openstreetmap.gui.jmapviewer;
|
---|
3 |
|
---|
4 | import org.openstreetmap.gui.jmapviewer.interfaces.TileCache;
|
---|
5 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
6 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
|
---|
7 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
8 |
|
---|
9 | public class TileController {
|
---|
10 | protected TileLoader tileLoader;
|
---|
11 | protected TileCache tileCache;
|
---|
12 | protected TileSource tileSource;
|
---|
13 |
|
---|
14 | public TileController(TileSource source, TileCache tileCache, TileLoaderListener listener) {
|
---|
15 | this.tileSource = source;
|
---|
16 | this.tileLoader = new OsmTileLoader(listener);
|
---|
17 | this.tileCache = tileCache;
|
---|
18 | }
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * retrieves a tile from the cache. If the tile is not present in the cache
|
---|
22 | * a load job is added to the working queue of {@link JobThread}.
|
---|
23 | *
|
---|
24 | * @param tilex the X position of the tile
|
---|
25 | * @param tiley the Y position of the tile
|
---|
26 | * @param zoom the zoom level of the tile
|
---|
27 | * @return specified tile from the cache or <code>null</code> if the tile
|
---|
28 | * was not found in the cache.
|
---|
29 | */
|
---|
30 | public Tile getTile(int tilex, int tiley, int zoom) {
|
---|
31 | int max = 1 << zoom;
|
---|
32 | if (tilex < 0 || tilex >= max || tiley < 0 || tiley >= max)
|
---|
33 | return null;
|
---|
34 | Tile tile = tileCache.getTile(tileSource, tilex, tiley, zoom);
|
---|
35 | if (tile == null) {
|
---|
36 | tile = new Tile(tileSource, tilex, tiley, zoom);
|
---|
37 | tileCache.addTile(tile);
|
---|
38 | tile.loadPlaceholderFromCache(tileCache);
|
---|
39 | }
|
---|
40 | if (tile.error) {
|
---|
41 | tile.loadPlaceholderFromCache(tileCache);
|
---|
42 | }
|
---|
43 | if (!tile.isLoaded()) {
|
---|
44 | tileLoader.createTileLoaderJob(tile).submit();
|
---|
45 | }
|
---|
46 | return tile;
|
---|
47 | }
|
---|
48 |
|
---|
49 | public TileCache getTileCache() {
|
---|
50 | return tileCache;
|
---|
51 | }
|
---|
52 |
|
---|
53 | public void setTileCache(TileCache tileCache) {
|
---|
54 | this.tileCache = tileCache;
|
---|
55 | }
|
---|
56 |
|
---|
57 | public TileLoader getTileLoader() {
|
---|
58 | return tileLoader;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void setTileLoader(TileLoader tileLoader) {
|
---|
62 | this.tileLoader = tileLoader;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public TileSource getTileLayerSource() {
|
---|
66 | return tileSource;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public TileSource getTileSource() {
|
---|
70 | return tileSource;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public void setTileSource(TileSource tileSource) {
|
---|
74 | this.tileSource = tileSource;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Removes all jobs from the queue that are currently not being processed.
|
---|
79 | *
|
---|
80 | */
|
---|
81 | public void cancelOutstandingJobs() {
|
---|
82 | tileLoader.cancelOutstandingTasks();
|
---|
83 | }
|
---|
84 | }
|
---|