AltosSiteMapTile: draw boost circle as well as landed
[fw/altos] / ao-tools / altosui / AltosSiteMapLabel.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 altosui;
19
20 import java.awt.*;
21 import java.awt.image.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import javax.imageio.ImageIO;
25 import javax.swing.table.*;
26 import java.io.*;
27 import java.util.*;
28 import java.text.*;
29 import java.util.prefs.*;
30 import java.net.URL;
31 import java.net.URLConnection;
32
33 public class AltosSiteMapLabel extends JLabel {
34     public static boolean fetchMap(File file, String url) {
35         URL u;
36         try {
37             u = new URL(url);
38         } catch (java.net.MalformedURLException e) {
39             return false;
40         }
41
42         byte[] data;
43         try {
44             URLConnection uc = u.openConnection();
45             int contentLength = uc.getContentLength();
46             InputStream in = new BufferedInputStream(uc.getInputStream());
47             int bytesRead = 0;
48             int offset = 0;
49             data = new byte[contentLength];
50             while (offset < contentLength) {
51                 bytesRead = in.read(data, offset, data.length - offset);
52                 if (bytesRead == -1)
53                     break;
54                 offset += bytesRead;
55             }
56             in.close();
57
58             if (offset != contentLength) {
59                 return false;
60             }
61         } catch (IOException e) {
62             return false;
63         }
64    
65         try {
66             FileOutputStream out = new FileOutputStream(file);
67             out.write(data);
68             out.flush();
69             out.close();
70         } catch (FileNotFoundException e) {
71             return false;
72         } catch (IOException e) {
73             if (file.exists()) {
74                 file.delete();
75             }
76             return false;
77         }
78         return true;
79     }
80
81     public void fetchAndLoadMap(final File pngfile, final String url) {
82         System.out.printf("# Trying to fetch %s...\n", pngfile);
83
84         Thread thread = new Thread() {
85             public void run() {
86                 try {
87                     if (fetchMap(pngfile, url)) {
88                         setIcon(new ImageIcon(ImageIO.read(pngfile)));
89                     }
90                 } catch (Exception e) {
91                     System.out.printf("# Failed to fetch file %s\n", pngfile);
92                     System.out.printf(" wget -O '%s' ''\n", pngfile, url);
93                 }
94             }
95         };
96         thread.start();
97     }
98
99     public void fetchMap(double lat, double lng, int zoom, int px_size) {
100         File pngfile = MapFile(lat, lng, zoom);
101         String url = MapURL(lat, lng, zoom, px_size);
102
103         if (!pngfile.exists()) {
104             fetchMap(pngfile, url);
105         }
106     }
107
108     public void loadMap(double lat, double lng, int zoom, int px_size) {
109         File pngfile = MapFile(lat, lng, zoom);
110         String url = MapURL(lat, lng, zoom, px_size);
111         
112         if (!pngfile.exists()) {
113             fetchAndLoadMap(pngfile, url);
114             return;
115         }
116
117         try {
118             setIcon(new ImageIcon(ImageIO.read(pngfile)));
119             return;
120         } catch (IOException e) { 
121             System.out.printf("# IO error trying to load %s\n", pngfile);
122             return;
123         }
124     }
125
126     private static File MapFile(double lat, double lng, int zoom) {
127         char chlat = lat < 0 ? 'S' : 'N';
128         char chlng = lng < 0 ? 'E' : 'W';
129         if (lat < 0) lat = -lat;
130         if (lng < 0) lng = -lng;
131         return new File(AltosPreferences.logdir(), 
132                 String.format("map-%c%.6f,%c%.6f-%d.png",
133                     chlat, lat, chlng, lng, zoom));
134     }
135
136     private static String MapURL(double lat, double lng, 
137             int zoom, int px_size) 
138     {
139         return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&sensor=false&maptype=hybrid&format=png32", lat, lng, zoom, px_size, px_size);
140     }
141 }
142