X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altosui%2FAltosSiteMapCache.java;h=03dc3cf5d67ebc8a3170d863d8557c75258452c1;hp=6e7e423b3c2a1a0cbebcb1394e5e72f65f68a189;hb=677fcafd3816b6d8d86b2dc41b840d97b5ccbf07;hpb=708e7937cba52982b91244cf89bfbff46d346135 diff --git a/altosui/AltosSiteMapCache.java b/altosui/AltosSiteMapCache.java index 6e7e423b..03dc3cf5 100644 --- a/altosui/AltosSiteMapCache.java +++ b/altosui/AltosSiteMapCache.java @@ -15,25 +15,23 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -package AltosUI; +package altosui; -import java.awt.*; -import java.awt.image.*; -import java.awt.event.*; import javax.swing.*; import javax.imageio.ImageIO; -import javax.swing.table.*; +import java.awt.image.*; import java.io.*; -import java.util.*; -import java.text.*; -import java.util.prefs.*; import java.net.URL; import java.net.URLConnection; -import org.altusmetrum.AltosLib.*; public class AltosSiteMapCache extends JLabel { + static final long google_maps_ratelimit_ms = 1200; + // Google limits static map queries to 50 per minute per IP, so + // each query should take at least 1.2 seconds. + public static boolean fetchMap(File file, String url) { URL u; + long startTime = System.nanoTime(); try { u = new URL(url); @@ -77,6 +75,16 @@ public class AltosSiteMapCache extends JLabel { } return false; } + + long duration_ms = (System.nanoTime() - startTime) / 1000000; + if (duration_ms < google_maps_ratelimit_ms) { + try { + Thread.sleep(google_maps_ratelimit_ms - duration_ms); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + return true; } @@ -95,7 +103,14 @@ public class AltosSiteMapCache extends JLabel { } try { - return new ImageIcon(ImageIO.read(pngfile)); + BufferedImage img; + + img = ImageIO.read(pngfile); + if (img == null) { + System.out.printf("# Can't read pngfile %s\n", pngfile); + return null; + } + return new ImageIcon(img); } catch (IOException e) { System.out.printf("# IO error trying to load %s\n", pngfile); return null;