Add version numbers to java libraries
[fw/altos] / altosui / AltosSiteMap.java
index 7306813806e0e6034719c7d202dd5748d3005892..f111e62d45066d0d4dd6e6295a4fb44df955a0c8 100644 (file)
 package altosui;
 
 import java.awt.*;
-import java.awt.image.*;
-import java.awt.event.*;
 import javax.swing.*;
-import javax.swing.event.MouseInputAdapter;
-import javax.imageio.ImageIO;
-import javax.swing.table.*;
 import java.io.*;
-import java.util.*;
-import java.text.*;
-import java.util.prefs.*;
 import java.lang.Math;
 import java.awt.geom.Point2D;
-import java.awt.geom.Line2D;
+import java.util.concurrent.*;
+import org.altusmetrum.altoslib_1.*;
+import org.altusmetrum.altosuilib_1.*;
 
 public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
        // preferred vertical step in a tile in naut. miles
@@ -59,11 +53,13 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
        // based on google js
        //  http://maps.gstatic.com/intl/en_us/mapfiles/api-3/2/10/main.js
        // search for fromLatLngToPoint and fromPointToLatLng
+       /*
        private static Point2D.Double pt(LatLng latlng, int zoom) {
                double scale_x = 256/360.0 * Math.pow(2, zoom);
                double scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
                return pt(latlng, scale_x, scale_y);
        }
+       */
 
        private static Point2D.Double pt(LatLng latlng,
                                         double scale_x, double scale_y)
@@ -97,6 +93,8 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
        int zoom;
        double scale_x, scale_y;
 
+       int radius;     /* half width/height of tiles to load */
+
        private Point2D.Double pt(double lat, double lng) {
                return pt(new LatLng(lat, lng), scale_x, scale_y);
        }
@@ -104,11 +102,13 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
        private LatLng latlng(double x, double y) {
                return latlng(new Point2D.Double(x,y), scale_x, scale_y);
        }
+       /*
        private LatLng latlng(Point2D.Double pt) {
                return latlng(pt, scale_x, scale_y);
        }
+       */
 
-       HashMap<Point,AltosSiteMapTile> mapTiles = new HashMap<Point,AltosSiteMapTile>();
+       ConcurrentHashMap<Point,AltosSiteMapTile> mapTiles = new ConcurrentHashMap<Point,AltosSiteMapTile>();
        Point2D.Double centre;
 
        private Point2D.Double tileCoordOffset(Point p) {
@@ -144,37 +144,35 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
                // nothing
        }
 
-       private void bgLoadMap(final AltosSiteMapTile tile,
-                              final File pngfile, final String pngurl)
+       public void set_font() {
+               // nothing
+       }
+
+       private void loadMap(final AltosSiteMapTile tile,
+                            File pngfile, String pngurl)
        {
-               //System.out.printf("Loading/fetching map %s\n", pngfile);
-               Thread thread = new Thread() {
-                       public void run() {
-                               final ImageIcon res = AltosSiteMapCache.fetchAndLoadMap(pngfile, pngurl);
-                               if (res != null) {
-                                       SwingUtilities.invokeLater(new Runnable() {
-                                                       public void run() {
-                                                               tile.loadMap(res);
-                                                       }
-                                               });
-                               } else {
-                                       System.out.printf("# Failed to fetch file %s\n", pngfile);
-                                       System.out.printf(" wget -O '%s' ''\n", pngfile, pngurl);
-                               }
-                       }
-               };
-               thread.start();
+               final ImageIcon res = AltosSiteMapCache.fetchAndLoadMap(pngfile, pngurl);
+               if (res != null) {
+                       SwingUtilities.invokeLater(new Runnable() {
+                                       public void run() {
+                                               tile.loadMap(res);
+                                       }
+                               });
+               } else {
+                       System.out.printf("# Failed to fetch file %s\n", pngfile);
+                       System.out.printf(" wget -O '%s' '%s'\n", pngfile, pngurl);
+               }
        }
 
-       File    pngfile;
-       String  pngurl;
+       File pngfile;
+       String pngurl;
 
        public int prefetchMap(int x, int y) {
                LatLng map_latlng = latlng(
                        -centre.x + x*px_size + px_size/2,
                        -centre.y + y*px_size + px_size/2);
-               pngfile = MapFile(map_latlng.lat, map_latlng.lng);
-               pngurl = MapURL(map_latlng.lat, map_latlng.lng);
+               pngfile = MapFile(map_latlng.lat, map_latlng.lng, zoom);
+               pngurl = MapURL(map_latlng.lat, map_latlng.lng, zoom);
                if (pngfile.exists()) {
                        return 1;
                } else if (AltosSiteMapCache.fetchMap(pngfile, pngurl)) {
@@ -188,8 +186,8 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
                AltosSiteMap asm = new AltosSiteMap(true);
                asm.centre = asm.getBaseLocation(lat, lng);
 
-               Point2D.Double p = new Point2D.Double();
-               Point2D.Double p2;
+               //Point2D.Double p = new Point2D.Double();
+               //Point2D.Double p2;
                int dx = -w/2, dy = -h/2;
                for (int y = dy; y < h+dy; y++) {
                        for (int x = dx; x < w+dx; x++) {
@@ -210,35 +208,51 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
                }
        }
 
-       private void initMap(AltosSiteMapTile tile, Point offset) {
+       public String initMap(Point offset) {
+               AltosSiteMapTile tile = mapTiles.get(offset);
                Point2D.Double coord = tileCoordOffset(offset);
 
                LatLng map_latlng = latlng(px_size/2-coord.x, px_size/2-coord.y);
 
-               File pngfile = MapFile(map_latlng.lat, map_latlng.lng);
-               String pngurl = MapURL(map_latlng.lat, map_latlng.lng);
-               bgLoadMap(tile, pngfile, pngurl);
+               File pngfile = MapFile(map_latlng.lat, map_latlng.lng, zoom);
+               String pngurl = MapURL(map_latlng.lat, map_latlng.lng, zoom);
+               loadMap(tile, pngfile, pngurl);
+               return pngfile.toString();
        }
 
-       private void initMaps(double lat, double lng) {
-               centre = getBaseLocation(lat, lng);
-
+       public void setBaseLocation(double lat, double lng) {
                for (Point k : mapTiles.keySet()) {
-                       initMap(mapTiles.get(k), k);
+                       AltosSiteMapTile tile = mapTiles.get(k);
+                       tile.clearMap();
                }
+                       
+               centre = getBaseLocation(lat, lng);
+               scrollRocketToVisible(pt(lat,lng));
        }
 
-       private File MapFile(double lat, double lng) {
+       private void initMaps(double lat, double lng) {
+               setBaseLocation(lat, lng);
+
+               Thread thread = new Thread() {
+                               public void run() {
+                                       for (Point k : mapTiles.keySet())
+                                               initMap(k);
+                               }
+                       };
+               thread.start();
+       }
+
+       private static File MapFile(double lat, double lng, int zoom) {
                char chlat = lat < 0 ? 'S' : 'N';
                char chlng = lng < 0 ? 'W' : 'E';
                if (lat < 0) lat = -lat;
                if (lng < 0) lng = -lng;
-               return new File(AltosPreferences.mapdir(),
+               return new File(AltosUIPreferences.mapdir(),
                                String.format("map-%c%.6f,%c%.6f-%d.png",
                                              chlat, lat, chlng, lng, zoom));
        }
 
-       private String MapURL(double lat, double lng) {
+       private static String MapURL(double lat, double lng, int zoom) {
                return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&sensor=false&maptype=hybrid&format=png32", lat, lng, zoom, px_size, px_size);
        }
 
@@ -248,7 +262,6 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
 
        public void show(double lat, double lon) {
                initMaps(lat, lon);
-               initialised = true;
                scrollRocketToVisible(pt(lat, lon));
        }
        public void show(final AltosState state, final int crc_errors) {
@@ -295,7 +308,7 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
 
                        AltosSiteMapTile tile = createTile(offset);
                        tile.show(state, crc_errors, lref, ref);
-                       initMap(tile, offset);
+                       initMap(offset);
                        finishTileLater(tile, offset);
                }
 
@@ -309,6 +322,16 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
                last_state = state.state;
        }
 
+       public void draw_circle(double lat, double lon) {
+               final Point2D.Double pt = pt(lat, lon);
+
+               for (Point offset : mapTiles.keySet()) {
+                       AltosSiteMapTile tile = mapTiles.get(offset);
+                       Point2D.Double ref = translatePoint(pt, tileCoordOffset(offset));
+                       tile.draw_circle(ref);
+               }
+       }
+
        private AltosSiteMapTile createTile(Point offset) {
                AltosSiteMapTile tile = new AltosSiteMapTile(px_size);
                mapTiles.put(offset, tile);
@@ -325,13 +348,13 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
        }
 
        private void ensureTilesAround(Point base_offset) {
-               for (int x = -1; x <= 1; x++) {
-                       for (int y = -1; y <= 1; y++) {
+               for (int x = -radius; x <= radius; x++) {
+                       for (int y = -radius; y <= radius; y++) {
                                Point offset = new Point(base_offset.x + x, base_offset.y + y);
                                if (mapTiles.containsKey(offset))
                                        continue;
                                AltosSiteMapTile tile = createTile(offset);
-                               initMap(tile, offset);
+                               initMap(offset);
                                finishTileLater(tile, offset);
                        }
                }
@@ -396,13 +419,15 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
        JComponent comp = new JComponent() { };
        private GridBagLayout layout = new GridBagLayout();
 
-       public AltosSiteMap() {
+       public AltosSiteMap(int in_radius) {
+               radius = in_radius;
+
                GrabNDrag scroller = new GrabNDrag(comp);
 
                comp.setLayout(layout);
 
-               for (int x = -1; x <= 1; x++) {
-                       for (int y = -1; y <= 1; y++) {
+               for (int x = -radius; x <= radius; x++) {
+                       for (int y = -radius; y <= radius; y++) {
                                Point offset = new Point(x, y);
                                AltosSiteMapTile t = createTile(offset);
                                addTileAt(t, offset);
@@ -411,4 +436,8 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
                setViewportView(comp);
                setPreferredSize(new Dimension(500,500));
        }
+
+       public AltosSiteMap() {
+               this(1);
+       }
 }