altosui: Display full map preload area in view.
[fw/altos] / altosui / AltosSiteMapCache.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 AltosSiteMapCache extends JLabel {
34         public static boolean fetchMap(File file, String url) {
35                 URL u;
36
37                 try {
38                         u = new URL(url);
39                 } catch (java.net.MalformedURLException e) {
40                         System.out.printf("Malformed URL '%s'\n", url);
41                         return false;
42                 }
43
44                 byte[] data;
45                 try {
46                         URLConnection uc = u.openConnection();
47                         int contentLength = uc.getContentLength();
48                         InputStream in = new BufferedInputStream(uc.getInputStream());
49                         int bytesRead = 0;
50                         int offset = 0;
51                         data = new byte[contentLength];
52                         while (offset < contentLength) {
53                                 bytesRead = in.read(data, offset, data.length - offset);
54                                 if (bytesRead == -1)
55                                         break;
56                                 offset += bytesRead;
57                         }
58                         in.close();
59
60                         if (offset != contentLength) {
61                                 System.out.printf("Bad length %d != %d\n",
62                                                   offset, contentLength);
63                                 return false;
64                         }
65                 } catch (IOException e) {
66                         System.out.printf("IO exception reading URL\n");
67                         return false;
68                 }
69
70                 try {
71                         FileOutputStream out = new FileOutputStream(file);
72                         out.write(data);
73                         out.flush();
74                         out.close();
75                 } catch (FileNotFoundException e) {
76                         System.out.printf("Can't create file\n");
77                         return false;
78                 } catch (IOException e) {
79                         if (file.exists()) {
80                                 file.delete();
81                         }
82                         System.out.printf("IO exception writing file\n");
83                         return false;
84                 }
85                 return true;
86         }
87
88         public static ImageIcon fetchAndLoadMap(File pngfile, String url) {
89                 if (!pngfile.exists()) {
90                         if (!fetchMap(pngfile, url)) {
91                                 return null;
92                         }
93                 }
94                 return loadMap(pngfile, url);
95         }
96
97         public static ImageIcon loadMap(File pngfile, String url) {
98                 if (!pngfile.exists()) {
99                         return null;
100                 }
101
102                 try {
103                         return new ImageIcon(ImageIO.read(pngfile));
104                 } catch (IOException e) {
105                         System.out.printf("# IO error trying to load %s\n", pngfile);
106                         return null;
107                 }
108         }
109 }