altoslib: Set time in state for KML output correctly
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_12;
20
21 import java.io.*;
22 import java.util.*;
23
24 public class AltosMapTile implements AltosFontListener, AltosMapStoreListener {
25         LinkedList<AltosMapTileListener>        listeners = new LinkedList<AltosMapTileListener>();
26         public AltosLatLon      upper_left, center;
27         public int              px_size;
28         int             zoom;
29         int             maptype;
30         int             scale;
31         private AltosMapCache   cache;
32         public AltosMapStore    store;
33         public int      status;
34
35         static public final int loaded = 0;     /* loaded from file */
36         static public final int fetched = 1;    /* downloaded to file */
37         static public final int fetching = 2;   /* downloading from net */
38         static public final int failed = 3;     /* loading from file failed */
39         static public final int bad_request = 4;/* downloading failed */
40         static public final int forbidden = 5;  /* downloading failed */
41
42         static public String status_name(int status) {
43                 switch (status) {
44                 case loaded:
45                         return "loaded";
46                 case fetched:
47                         return "fetched";
48                 case fetching:
49                         return "fetching";
50                 case failed:
51                         return "failed";
52                 case bad_request:
53                         return "bad_request";
54                 case forbidden:
55                         return "forbidden";
56                 default:
57                         return "unknown";
58                 }
59         }
60
61         public void font_size_changed(int font_size) {
62         }
63
64         private synchronized void notify_listeners(int status) {
65                 this.status = status;
66                 for (AltosMapTileListener listener : listeners)
67                         listener.notify_tile(this, status);
68         }
69
70         public void notify_store(AltosMapStore store, int status) {
71                 notify_listeners(status);
72         }
73
74         public void notify_image(AltosImage image) {
75                 if (image == null)
76                         status = failed;
77                 else
78                         status = loaded;
79                 notify_listeners(status);
80         }
81
82         public void paint(AltosMapTransform t) {
83         }
84
85         public AltosImage get_image() {
86                 if (cache == null)
87                         return null;
88                 return cache.get(this);
89         }
90
91         public synchronized void add_listener(AltosMapTileListener listener) {
92                 if (!listeners.contains(listener))
93                         listeners.add(listener);
94                 listener.notify_tile(this, status);
95         }
96
97         public synchronized void remove_listener(AltosMapTileListener listener) {
98                 listeners.remove(listener);
99         }
100
101         public AltosMapTile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
102                 this.cache = cache;
103                 this.upper_left = upper_left;
104
105                 while (center.lon < -180.0)
106                         center.lon += 360.0;
107                 while (center.lon > 180.0)
108                         center.lon -= 360.0;
109
110                 this.center = center;
111                 this.zoom = zoom;
112                 this.maptype = maptype;
113                 this.px_size = px_size;
114                 this.scale = scale;
115
116                 store = AltosMapStore.get(center, zoom, maptype, px_size, scale);
117                 store.add_listener(this);
118         }
119 }