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;
33 public class AltosSiteMapCache extends JLabel {
34 public static boolean fetchMap(File file, String url) {
39 } catch (java.net.MalformedURLException e) {
45 URLConnection uc = u.openConnection();
46 int contentLength = uc.getContentLength();
47 InputStream in = new BufferedInputStream(uc.getInputStream());
50 data = new byte[contentLength];
51 while (offset < contentLength) {
52 bytesRead = in.read(data, offset, data.length - offset);
59 if (offset != contentLength) {
62 } catch (IOException e) {
67 FileOutputStream out = new FileOutputStream(file);
71 } catch (FileNotFoundException e) {
73 } catch (IOException e) {
82 public static ImageIcon fetchAndLoadMap(File pngfile, String url) {
83 if (!pngfile.exists()) {
84 if (!fetchMap(pngfile, url)) {
88 return loadMap(pngfile, url);
91 public static ImageIcon loadMap(File pngfile, String url) {
92 if (!pngfile.exists()) {
97 return new ImageIcon(ImageIO.read(pngfile));
98 } catch (IOException e) {
99 System.out.printf("# IO error trying to load %s\n", pngfile);