altos: product defines are always in ao_product.h
[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                         return false;
41                 }
42
43                 byte[] data;
44                 try {
45                         URLConnection uc = u.openConnection();
46                         int contentLength = uc.getContentLength();
47                         InputStream in = new BufferedInputStream(uc.getInputStream());
48                         int bytesRead = 0;
49                         int offset = 0;
50                         data = new byte[contentLength];
51                         while (offset < contentLength) {
52                                 bytesRead = in.read(data, offset, data.length - offset);
53                                 if (bytesRead == -1)
54                                         break;
55                                 offset += bytesRead;
56                         }
57                         in.close();
58
59                         if (offset != contentLength) {
60                                 return false;
61                         }
62                 } catch (IOException e) {
63                         return false;
64                 }
65
66                 try {
67                         FileOutputStream out = new FileOutputStream(file);
68                         out.write(data);
69                         out.flush();
70                         out.close();
71                 } catch (FileNotFoundException e) {
72                         return false;
73                 } catch (IOException e) {
74                         if (file.exists()) {
75                                 file.delete();
76                         }
77                         return false;
78                 }
79                 return true;
80         }
81
82         public static ImageIcon fetchAndLoadMap(File pngfile, String url) {
83                 if (!pngfile.exists()) {
84                         if (!fetchMap(pngfile, url)) {
85                                 return null;
86                         }
87                 }
88                 return loadMap(pngfile, url);
89         }
90
91         public static ImageIcon loadMap(File pngfile, String url) {
92                 if (!pngfile.exists()) {
93                         return null;
94                 }
95
96                 try {
97                         return new ImageIcon(ImageIO.read(pngfile));
98                 } catch (IOException e) {
99                         System.out.printf("# IO error trying to load %s\n", pngfile);
100                         return null;
101                 }
102         }
103 }