Bump java library versions
[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_11;
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         static public String status_name(int status) {
42                 switch (status) {
43                 case loaded:
44                         return "loaded";
45                 case fetched:
46                         return "fetched";
47                 case fetching:
48                         return "fetching";
49                 case failed:
50                         return "failed";
51                 case bad_request:
52                         return "bad_request";
53                 case forbidden:
54                         return "forbidden";
55                 default:
56                         return "unknown";
57                 }
58         }
59
60         public void font_size_changed(int font_size) {
61         }
62
63         private synchronized void notify_listeners(int status) {
64                 this.status = status;
65                 for (AltosMapTileListener listener : listeners)
66                         listener.notify_tile(this, status);
67         }
68
69         public void notify_store(AltosMapStore store, int status) {
70                 notify_listeners(status);
71         }
72
73         public void notify_image(AltosImage image) {
74                 if (image == null)
75                         status = failed;
76                 else
77                         status = loaded;
78                 notify_listeners(status);
79         }
80
81         public void paint(AltosMapTransform t) {
82         }
83
84         public AltosImage get_image() {
85                 if (cache == null)
86                         return null;
87                 return cache.get(this);
88         }
89
90         public synchronized void add_listener(AltosMapTileListener listener) {
91                 if (!listeners.contains(listener))
92                         listeners.add(listener);
93                 listener.notify_tile(this, status);
94         }
95
96         public synchronized void remove_listener(AltosMapTileListener listener) {
97                 listeners.remove(listener);
98         }
99
100         public AltosMapTile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
101                 this.cache = cache;
102                 this.upper_left = upper_left;
103
104                 while (center.lon < -180.0)
105                         center.lon += 360.0;
106                 while (center.lon > 180.0)
107                         center.lon -= 360.0;
108
109                 this.center = center;
110                 this.zoom = zoom;
111                 this.maptype = maptype;
112                 this.px_size = px_size;
113                 this.scale = scale;
114
115                 store = AltosMapStore.get(center, zoom, maptype, px_size, scale);
116                 store.add_listener(this);
117         }
118 }