altoslib: Preload maps based on distance rather than number of tiles
[fw/altos] / altosuilib / AltosUIMapPreloadNew.java
index 7d3c2a0b847e0df54a1211f2266cf2a7a20f19d1..ba559534e3f8226826a8475a72f8f545a1bbf2e3 100644 (file)
@@ -118,251 +118,87 @@ class AltosUIMapPos extends Box {
        }
 }
 
-class AltosUISite {
-       String  name;
-       double  latitude;
-       double  longitude;
-
-       public String toString() {
-               return name;
-       }
-
-       public AltosUISite(String in_name, double in_latitude, double in_longitude) {
-               name = in_name;
-               latitude = in_latitude;
-               longitude = in_longitude;
-       }
-
-       public AltosUISite(String line) throws ParseException {
-               String[]        elements = line.split(":");
-
-               if (elements.length < 3)
-                       throw new ParseException(String.format("Invalid site line %s", line), 0);
-
-               name = elements[0];
-
-               try {
-                       latitude = AltosParse.parse_double_net(elements[1]);
-                       longitude = AltosParse.parse_double_net(elements[2]);
-               } catch (ParseException pe) {
-                       throw new ParseException(String.format("Invalid site line %s", line), 0);
-               }
-       }
-}
-
-class AltosUISites extends Thread {
-       AltosUIMapPreloadNew    preload;
-       URL                     url;
-       LinkedList<AltosUISite> sites;
-
-       void notify_complete() {
-               SwingUtilities.invokeLater(new Runnable() {
-                               public void run() {
-                                       preload.set_sites();
-                               }
-                       });
-       }
-
-       void add(AltosUISite site) {
-               sites.add(site);
-       }
-
-       void add(String line) {
-               try {
-                       add(new AltosUISite(line));
-               } catch (ParseException pe) {
-                       System.out.printf("parse exception %s\n", pe.toString());
-               }
-       }
-
-       public void run() {
-               try {
-                       URLConnection uc = url.openConnection();
-                       //int length = uc.getContentLength();
-
-                       InputStreamReader in_stream = new InputStreamReader(uc.getInputStream(), AltosLib.unicode_set);
-                       BufferedReader in = new BufferedReader(in_stream);
-
-                       for (;;) {
-                               String line = in.readLine();
-                               if (line == null)
-                                       break;
-                               add(line);
-                       }
-               } catch (IOException e) {
-               } finally {
-                       notify_complete();
-               }
-       }
-
-       public AltosUISites(AltosUIMapPreloadNew in_preload) {
-               sites = new LinkedList<AltosUISite>();
-               preload = in_preload;
-               try {
-                       url = new URL(AltosLib.launch_sites_url);
-               } catch (java.net.MalformedURLException e) {
-                       notify_complete();
-               }
-               start();
-       }
-}
-
-public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener, ItemListener, AltosMapTileListener  {
+public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener, ItemListener, AltosLaunchSiteListener, AltosMapLoaderListener  {
        AltosUIFrame    owner;
        AltosUIMapNew   map;
-       AltosMapCache   cache;
 
        AltosUIMapPos   lat;
        AltosUIMapPos   lon;
 
        JProgressBar    pbar;
-       int             pbar_max;
-       int             pbar_cur;
 
-       AltosUISites    sites;
+       AltosMapLoader  loader;
+
        JLabel          site_list_label;
-       JComboBox<AltosUISite>  site_list;
+       JComboBox<AltosLaunchSite>      site_list;
 
        JToggleButton   load_button;
        boolean         loading;
        JButton         close_button;
 
-       JCheckBox[]     maptypes = new JCheckBox[AltosUIMap.maptype_terrain - AltosUIMap.maptype_hybrid + 1];
+       JCheckBox[]     maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1];
 
        JComboBox<Integer>      min_zoom;
        JComboBox<Integer>      max_zoom;
-       JComboBox<Integer>      radius;
+       JComboBox<Double>       radius;
 
        Integer[]               zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
-       Integer[]               radii = { 1, 2, 3, 4, 5 };
-
-       static final String[]   lat_hemi_names = { "N", "S" };
-       static final String[]   lon_hemi_names = { "E", "W" };
-
-       class updatePbar implements Runnable {
-               String          s;
 
-               public updatePbar(String in_s) {
-                       s = in_s;
-               }
+       Double[]        radius_mi = { 1.0, 2.0, 5.0, 10.0, 20.0 };
+       Double          radius_def_mi = 5.0;
+       Double[]        radius_km = { 2.0, 5.0, 10.0, 20.0, 30.0 };
+       Double          radius_def_km = 10.0;
 
-               public void run() {
-                       int     n = ++pbar_cur;
 
-                       pbar.setMaximum(pbar_max);
-                       pbar.setValue(n);
-                       pbar.setString(s);
-               }
-       }
+       static final String[]   lat_hemi_names = { "N", "S" };
+       static final String[]   lon_hemi_names = { "E", "W" };
 
        double  latitude, longitude;
-       int     min_z;
-       int     max_z;
-       int     cur_z;
-       int     all_types;
-       int     cur_type;
-       int     r;
-
-       int     tiles_per_layer;
-       int     tiles_loaded;
-       int     layers_total;
-       int     layers_loaded;
-
-
-       private void do_load() {
-               tiles_loaded = 0;
-               map.set_zoom(cur_z + AltosUIMapView.default_zoom);
-               map.set_maptype(cur_type);
-               map.set_load_params(latitude, longitude, r, this);
-       }
 
-       private int next_type(int start) {
-               int next_type;
-               for (next_type = start;
-                    next_type <= AltosUIMap.maptype_terrain && (all_types & (1 << next_type)) == 0;
-                    next_type++)
-                       ;
-               return next_type;
+       /* AltosMapLoaderListener interfaces */
+       public void loader_start(final int max) {
+               SwingUtilities.invokeLater(new Runnable() {
+                               public void run() {
+                                       pbar.setMaximum(max);
+                                       pbar.setValue(0);
+                                       pbar.setString("");
+                                       map.clear_marks();
+                                       map.add_mark(latitude, longitude, AltosLib.ao_flight_boost);
+                               }
+                       });
        }
 
-       private void next_load() {
-               int next_type = next_type(cur_type + 1);
-
-               if (next_type > AltosUIMap.maptype_terrain) {
-                       if (cur_z == max_z) {
-                               return;
-                       } else {
-                               cur_z++;
-                       }
-                       next_type = next_type(0);
-               }
-               cur_type = next_type;
-               do_load();
+       public void loader_notify(final int cur, final int max, final String name) {
+               SwingUtilities.invokeLater(new Runnable() {
+                               public void run() {
+                                       pbar.setValue(cur);
+                                       pbar.setString(name);
+                               }
+                       });
        }
 
-       private void start_load() {
-               cur_z = min_z;
-               int ntype = 0;
-               all_types = 0;
-               for (int t = AltosUIMap.maptype_hybrid; t <= AltosUIMap.maptype_terrain; t++)
-                       if (maptypes[t].isSelected()) {
-                               all_types |= (1 << t);
-                               ntype++;
-                       }
-               if (ntype == 0) {
-                       all_types |= (1 << AltosUIMap.maptype_hybrid);
-                       ntype = 1;
-               }
-
-               cur_type = next_type(0);
-               tiles_per_layer = (r * 2 + 1) * (r * 2 + 1);
-               layers_total = (max_z - min_z + 1) * ntype;
-               layers_loaded = 0;
-               pbar_max = layers_total * tiles_per_layer;
-               pbar_cur = 0;
-
-               map.clear_marks();
-               map.add_mark(latitude,longitude, AltosLib.ao_flight_boost);
-               do_load();
+       public void loader_done(int max) {
+               SwingUtilities.invokeLater(new Runnable() {
+                               public void run() {
+                                       pbar.setValue(0);
+                                       pbar.setString("");
+                                       load_button.setSelected(false);
+                                       loading = false;
+                               }
+                       });
        }
 
-       /* AltosMapTileListener methods */
-
-       public synchronized void notify_tile(AltosMapTile tile, int status) {
-               if (status == AltosMapTile.loading)
-                       return;
-
-               SwingUtilities.invokeLater(new updatePbar(tile.store.file.toString()));
-               ++tiles_loaded;
-               if (tiles_loaded == tiles_per_layer) {
-                       ++layers_loaded;
-                       if (layers_loaded == layers_total) {
-                               SwingUtilities.invokeLater(new Runnable() {
-                                               public void run() {
-                                                       pbar.setValue(0);
-                                                       pbar.setString("");
-                                                       load_button.setSelected(false);
-                                                       loading = false;
-                                               }
-                                       });
-                       } else {
-                               SwingUtilities.invokeLater(new Runnable() {
-                                               public void run() {
-                                                       next_load();
-                                               }
-                                       });
-                       }
-               }
+       public void debug(String format, Object ... arguments) {
+               System.out.printf(format, arguments);
        }
 
-       public AltosMapCache cache() { return cache; }
 
-       public void set_sites() {
-               int     i = 1;
-               for (AltosUISite site : sites.sites) {
-                       site_list.insertItemAt(site, i);
-                       i++;
-               }
+       private int all_types() {
+               int all_types = 0;
+               for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
+                       if (maptypes[t].isSelected())
+                               all_types |= (1 << t);
+               return all_types;
        }
 
        public void itemStateChanged(ItemEvent e) {
@@ -370,8 +206,8 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener
 
                if (state == ItemEvent.SELECTED) {
                        Object  o = e.getItem();
-                       if (o instanceof AltosUISite) {
-                               AltosUISite     site = (AltosUISite) o;
+                       if (o instanceof AltosLaunchSite) {
+                               AltosLaunchSite site = (AltosLaunchSite) o;
                                lat.set_value(site.latitude);
                                lon.set_value(site.longitude);
                        }
@@ -389,20 +225,38 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener
                                try {
                                        latitude = lat.get_value();
                                        longitude = lon.get_value();
-                                       min_z = (Integer) min_zoom.getSelectedItem();
-                                       max_z = (Integer) max_zoom.getSelectedItem();
+                                       int min_z = (Integer) min_zoom.getSelectedItem();
+                                       int max_z = (Integer) max_zoom.getSelectedItem();
                                        if (max_z < min_z)
                                                max_z = min_z;
-                                       r = (Integer) radius.getSelectedItem();
+                                       Double r = (Double) radius.getSelectedItem();
+
+                                       if (AltosPreferences.imperial_units())
+                                               r = AltosConvert.distance.inverse(r);
+                                       else
+                                               r = r * 1000;
                                        loading = true;
+
+                                       loader.load(latitude, longitude, min_z, max_z, r, all_types());
                                } catch (ParseException pe) {
                                        load_button.setSelected(false);
                                }
-                               start_load();
                        }
                }
        }
 
+       public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
+               SwingUtilities.invokeLater(new Runnable() {
+                               public void run() {
+                                       int     i = 1;
+                                       for (AltosLaunchSite site : sites) {
+                                               site_list.insertItemAt(site, i);
+                                               i++;
+                                       }
+                               }
+                       });
+       }
+
        public AltosUIMapPreloadNew(AltosUIFrame in_owner) {
                owner = in_owner;
 
@@ -415,7 +269,8 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener
                pane.setLayout(new GridBagLayout());
 
                map = new AltosUIMapNew();
-               cache = new AltosMapCache(map);
+
+               loader = new AltosMapLoader(map.map, this);
 
                c.fill = GridBagConstraints.BOTH;
                c.anchor = GridBagConstraints.CENTER;
@@ -463,10 +318,10 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener
 
                pane.add(site_list_label, c);
 
-               site_list = new JComboBox<AltosUISite>(new AltosUISite[] { new AltosUISite("Site List", 0, 0) });
+               site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
                site_list.addItemListener(this);
 
-               sites = new AltosUISites(this);
+               new AltosLaunchSites(this);
 
                c.fill = GridBagConstraints.HORIZONTAL;
                c.anchor = GridBagConstraints.CENTER;
@@ -557,9 +412,9 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener
 
                c.gridwidth = 1;
 
-               for (int type = AltosUIMap.maptype_hybrid; type <= AltosUIMap.maptype_terrain; type++) {
-                       maptypes[type] = new JCheckBox(AltosUIMap.maptype_labels[type],
-                                                      type == AltosUIMap.maptype_hybrid);
+               for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
+                       maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
+                                                      type == AltosMap.maptype_hybrid);
                        c.gridx = 2 + (type >> 1);
                        c.fill = GridBagConstraints.HORIZONTAL;
                        c.gridy = (type & 1) + 3;
@@ -590,13 +445,21 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener
                c.gridy = 3;
                pane.add(max_zoom, c);
 
-               JLabel radius_label = new JLabel("Tile Radius");
+               JLabel radius_label = new JLabel(String.format("Map Radius (%s)",
+                                                              AltosPreferences.imperial_units() ? "miles" : "km"));
                c.gridx = 4;
                c.gridy = 4;
                pane.add(radius_label, c);
 
-               radius = new JComboBox<Integer>(radii);
-               radius.setSelectedItem(radii[4]);
+               Double[]        radii;
+               Double          radius_default;
+
+               if (AltosPreferences.imperial_units())
+                       radii = radius_mi;
+               else
+                       radii = radius_km;
+               radius = new JComboBox<Double>(radii);
+               radius.setSelectedItem(radii[2]);
                radius.setEditable(true);
                c.gridx = 5;
                c.gridy = 4;