19332de191385254591155f755f0c85fabebdaea
[fw/altos] / altoslib / AltosMapTile.java
1 /*
2  * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altoslib_10;
19
20 import java.io.*;
21 import java.util.*;
22
23 public class AltosMapTile implements AltosFontListener, AltosMapStoreListener {
24         LinkedList<AltosMapTileListener>        listeners = new LinkedList<AltosMapTileListener>();
25         public AltosLatLon      upper_left, center;
26         public int              px_size;
27         int             zoom;
28         int             maptype;
29         int             scale;
30         private AltosMapCache   cache;
31         public AltosMapStore    store;
32         public int      status;
33
34         static public final int loaded = 0;     /* loaded from file */
35         static public final int fetched = 1;    /* downloaded to file */
36         static public final int fetching = 2;   /* downloading from net */
37         static public final int failed = 3;     /* loading from file failed */
38         static public final int bad_request = 4;/* downloading failed */
39         static public final int forbidden = 5;  /* downloading failed */
40
41         public void font_size_changed(int font_size) {
42         }
43
44         private synchronized void notify_listeners(int status) {
45                 this.status = status;
46                 for (AltosMapTileListener listener : listeners)
47                         listener.notify_tile(this, status);
48         }
49
50         public void notify_store(AltosMapStore store, int status) {
51                 notify_listeners(status);
52         }
53
54         public void notify_image(AltosImage image) {
55                 if (image == null)
56                         status = failed;
57                 else
58                         status = loaded;
59                 notify_listeners(status);
60         }
61
62         public void paint(AltosMapTransform t) {
63         }
64
65         public AltosImage get_image() {
66                 if (cache == null)
67                         return null;
68                 return cache.get(this);
69         }
70
71         public synchronized void add_listener(AltosMapTileListener listener) {
72                 if (!listeners.contains(listener))
73                         listeners.add(listener);
74                 listener.notify_tile(this, status);
75         }
76
77         public synchronized void remove_listener(AltosMapTileListener listener) {
78                 listeners.remove(listener);
79         }
80
81         public AltosMapTile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
82                 this.cache = cache;
83                 this.upper_left = upper_left;
84
85                 while (center.lon < -180.0)
86                         center.lon += 360.0;
87                 while (center.lon > 180.0)
88                         center.lon -= 360.0;
89
90                 this.center = center;
91                 this.zoom = zoom;
92                 this.maptype = maptype;
93                 this.px_size = px_size;
94                 this.scale = scale;
95
96                 store = AltosMapStore.get(center, zoom, maptype, px_size, scale);
97                 store.add_listener(this);
98         }
99
100         public AltosMapTile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
101                 this(cache, upper_left, center, zoom, maptype, px_size, 1);
102         }
103 }