2 * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
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.
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.
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.
21 import java.awt.image.*;
22 import java.awt.event.*;
24 import javax.imageio.ImageIO;
25 import javax.swing.table.*;
29 import java.util.prefs.*;
31 import java.net.URLConnection;
32 import org.altusmetrum.AltosLib.*;
34 public class AltosSiteMapCache extends JLabel {
35 public static boolean fetchMap(File file, String url) {
40 } catch (java.net.MalformedURLException e) {
46 URLConnection uc = u.openConnection();
47 int contentLength = uc.getContentLength();
48 InputStream in = new BufferedInputStream(uc.getInputStream());
51 data = new byte[contentLength];
52 while (offset < contentLength) {
53 bytesRead = in.read(data, offset, data.length - offset);
60 if (offset != contentLength) {
63 } catch (IOException e) {
68 FileOutputStream out = new FileOutputStream(file);
72 } catch (FileNotFoundException e) {
74 } catch (IOException e) {
83 public static ImageIcon fetchAndLoadMap(File pngfile, String url) {
84 if (!pngfile.exists()) {
85 if (!fetchMap(pngfile, url)) {
89 return loadMap(pngfile, url);
92 public static ImageIcon loadMap(File pngfile, String url) {
93 if (!pngfile.exists()) {
98 return new ImageIcon(ImageIO.read(pngfile));
99 } catch (IOException e) {
100 System.out.printf("# IO error trying to load %s\n", pngfile);