altosui: reindent
[fw/altos] / ao-tools / 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                 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 static ImageIcon fetchAndLoadMap(File pngfile, String url) {
82                 if (!pngfile.exists()) {
83                         if (!fetchMap(pngfile, url)) {
84                                 return null;
85                         }
86                 }
87                 return loadMap(pngfile, url);
88         }
89
90         public static ImageIcon loadMap(File pngfile, String url) {
91                 if (!pngfile.exists()) {
92                         return null;
93                 }
94
95                 try {
96                         return new ImageIcon(ImageIO.read(pngfile));
97                 } catch (IOException e) {
98                         System.out.printf("# IO error trying to load %s\n", pngfile);
99                         return null;
100                 }
101         }
102 }
103