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.
18 package org.altusmetrum.altosuilib_2;
21 import javax.imageio.ImageIO;
22 import java.awt.image.*;
26 import java.net.URLConnection;
29 class AltosCacheImage {
37 public void load_image() throws IOException {
38 BufferedImage bimg = ImageIO.read(file);
39 Graphics2D g = image.createGraphics();
40 g.drawImage(bimg, 0, 0, null);
44 public Image validate() {
48 returnCode = image.validate(component.getGraphicsConfiguration());
50 returnCode = VolatileImage.IMAGE_INCOMPATIBLE;
51 if (returnCode == VolatileImage.IMAGE_RESTORED) {
54 } catch (IOException e) {
57 } else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) {
58 image = component.createVolatileImage(width, height);
61 } catch (IOException e) {
72 public AltosCacheImage(Component component, File file, int w, int h) throws IOException {
73 this.component = component;
77 image = component.createVolatileImage(w, h);
82 public class AltosSiteMapCache extends JLabel {
83 static final long google_maps_ratelimit_ms = 1200;
84 // Google limits static map queries to 50 per minute per IP, so
85 // each query should take at least 1.2 seconds.
87 public static boolean fetchMap(File file, String url) {
92 long startTime = System.nanoTime();
96 } catch (java.net.MalformedURLException e) {
102 URLConnection uc = u.openConnection();
103 int contentLength = uc.getContentLength();
104 InputStream in = new BufferedInputStream(uc.getInputStream());
107 data = new byte[contentLength];
108 while (offset < contentLength) {
109 bytesRead = in.read(data, offset, data.length - offset);
116 if (offset != contentLength) {
119 } catch (IOException e) {
124 FileOutputStream out = new FileOutputStream(file);
128 } catch (FileNotFoundException e) {
130 } catch (IOException e) {
137 long duration_ms = (System.nanoTime() - startTime) / 1000000;
138 if (duration_ms < google_maps_ratelimit_ms) {
140 Thread.sleep(google_maps_ratelimit_ms - duration_ms);
141 } catch (InterruptedException e) {
142 Thread.currentThread().interrupt();
149 static int cache_size = 9;
151 static AltosCacheImage[] images;
155 public static void set_cache_size(int cache_size) {
156 AltosSiteMapCache.cache_size = cache_size;
160 public static Image get_image(Component component, File file, int width, int height) {
163 AltosCacheImage image;
165 images = new AltosCacheImage[cache_size];
166 for (int i = 0; i < cache_size; i++) {
173 if (image.component == component && file.equals(image.file)) {
175 return image.validate();
177 if (image.used < age) {
184 image = new AltosCacheImage(component, file, width, height);
186 if (images[oldest] != null)
187 images[oldest].flush();
188 images[oldest] = image;
189 return image.validate();
190 } catch (IOException e) {