telegps: Shuffle menu entries around
[fw/altos] / altosuilib / AltosUIMapTile.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.altosuilib_2;
19
20 import java.awt.*;
21 import java.awt.image.*;
22 import javax.swing.*;
23 import javax.imageio.*;
24 import java.awt.geom.*;
25 import java.io.*;
26 import java.util.*;
27 import java.awt.RenderingHints.*;
28 import org.altusmetrum.altoslib_4.*;
29
30 public class AltosUIMapTile {
31         AltosUIMapTileListener  listener;
32         AltosUILatLon   upper_left, center;
33         int             px_size;
34         int             zoom;
35         int             maptype;
36         AltosUIMapStore store;
37         int             status;
38
39         private File map_file() {
40                 double lat = center.lat;
41                 double lon = center.lon;
42                 char chlat = lat < 0 ? 'S' : 'N';
43                 char chlon = lon < 0 ? 'W' : 'E';
44
45                 if (lat < 0) lat = -lat;
46                 if (lon < 0) lon = -lon;
47                 String maptype_string = String.format("%s-", AltosUIMap.maptype_names[maptype]);
48                 String format_string;
49                 if (maptype == AltosUIMap.maptype_hybrid || maptype == AltosUIMap.maptype_satellite || maptype == AltosUIMap.maptype_terrain)
50                         format_string = "jpg";
51                 else
52                         format_string = "png";
53                 return new File(AltosUIPreferences.mapdir(),
54                                 String.format("map-%c%.6f,%c%.6f-%s%d.%s",
55                                               chlat, lat, chlon, lon, maptype_string, zoom, format_string));
56         }
57
58         private String map_url() {
59                 String format_string;
60                 if (maptype == AltosUIMap.maptype_hybrid || maptype == AltosUIMap.maptype_satellite || maptype == AltosUIMap.maptype_terrain)
61                         format_string = "jpg";
62                 else
63                         format_string = "png32";
64
65                 if (AltosUIVersion.has_google_maps_api_key())
66                         return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&sensor=false&maptype=%s&format=%s&key=%s",
67                                              center.lat, center.lon, zoom, px_size, px_size, AltosUIMap.maptype_names[maptype], format_string, AltosUIVersion.google_maps_api_key);
68                 else
69                         return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&sensor=false&maptype=%s&format=%s",
70                                              center.lat, center.lon, zoom, px_size, px_size, AltosUIMap.maptype_names[maptype], format_string);
71         }
72         private Font    font = null;
73
74         public void set_font(Font font) {
75                 this.font = font;
76         }
77
78         int     painting_serial;
79         int     painted_serial;
80
81         Image   image;
82
83         public void paint_graphics(Graphics2D g2d, AltosUIMapTransform t, int serial) {
84                 if (serial < painted_serial)
85                         return;
86
87                 Point2D.Double  point_double = t.screen(upper_left);
88                 Point           point = new Point((int) (point_double.x + 0.5),
89                                                   (int) (point_double.y + 0.5));
90
91                 painted_serial = serial;
92
93                 if (!g2d.hitClip(point.x, point.y, px_size, px_size))
94                         return;
95
96                 if (image != null) {
97                         g2d.drawImage(image, point.x, point.y, null);
98                         image = null;
99                 } else {
100                         g2d.setColor(Color.GRAY);
101                         g2d.fillRect(point.x, point.y, px_size, px_size);
102
103                         if (t.has_location()) {
104                                 String  message = null;
105                                 switch (status) {
106                                 case AltosUIMapCache.loading:
107                                         message = "Loading...";
108                                         break;
109                                 case AltosUIMapCache.bad_request:
110                                         message = "Internal error";
111                                         break;
112                                 case AltosUIMapCache.failed:
113                                         message = "Network error, check connection";
114                                         break;
115                                 case AltosUIMapCache.forbidden:
116                                         message = "Too many requests, try later";
117                                         break;
118                                 }
119                                 if (message != null && font != null) {
120                                         g2d.setFont(font);
121                                         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
122                                         Rectangle2D bounds = font.getStringBounds(message, g2d.getFontRenderContext());
123
124                                         float x = px_size / 2.0f;
125                                         float y = px_size / 2.0f;
126                                         x = x - (float) bounds.getWidth() / 2.0f;
127                                         y = y + (float) bounds.getHeight() / 2.0f;
128                                         g2d.setColor(Color.BLACK);
129                                         g2d.drawString(message, (float) point_double.x + x, (float) point_double.y + y);
130                                 }
131                         }
132                 }
133         }
134
135         public void set_status(int status) {
136                 this.status = status;
137                 listener.notify_tile(this, status);
138         }
139
140         public void notify_image(Image image) {
141                 listener.notify_tile(this, status);
142         }
143
144         public void paint(Graphics g, AltosUIMapTransform t) {
145                 Graphics2D              g2d = (Graphics2D) g;
146                 boolean                 queued = false;
147
148                 Point2D.Double  point = t.screen(upper_left);
149
150                 if (!g.hitClip((int) (point.x + 0.5), (int) (point.y + 0.5), px_size, px_size))
151                         return;
152
153                 ++painting_serial;
154
155                 if (image == null && t.has_location())
156                         image = AltosUIMapCache.get(this, store, px_size, px_size);
157
158                 paint_graphics(g2d, t, painting_serial);
159         }
160
161         public int store_status() {
162                 return store.status();
163         }
164
165         public void add_store_listener(AltosUIMapStoreListener listener) {
166                 store.add_listener(listener);
167         }
168
169         public void remove_store_listener(AltosUIMapStoreListener listener) {
170                 store.remove_listener(listener);
171         }
172
173         public AltosUIMapTile(AltosUIMapTileListener listener, AltosUILatLon upper_left, AltosUILatLon center, int zoom, int maptype, int px_size, Font font) {
174                 this.listener = listener;
175                 this.upper_left = upper_left;
176
177                 while (center.lon < -180.0)
178                         center.lon += 360.0;
179                 while (center.lon > 180.0)
180                         center.lon -= 360.0;
181
182                 this.center = center;
183                 this.zoom = zoom;
184                 this.maptype = maptype;
185                 this.px_size = px_size;
186                 this.font = font;
187                 status = AltosUIMapCache.loading;
188                 store = AltosUIMapStore.get(map_url(), map_file());
189         }
190 }