altosuilib: Have map preload respond to units and font size changes
[fw/altos] / altosuilib / AltosUIMapPreloadNew.java
index 2d4d45b2edd921bf80e49c46fe06a99c65b42759..d043e9e34bc7580c9685112762438f0558d2487a 100644 (file)
@@ -15,7 +15,7 @@
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  */
 
-package org.altusmetrum.altosuilib_8;
+package org.altusmetrum.altosuilib_11;
 
 import java.awt.*;
 import java.awt.event.*;
@@ -26,7 +26,7 @@ import java.text.*;
 import java.lang.Math;
 import java.net.URL;
 import java.net.URLConnection;
-import org.altusmetrum.altoslib_8.*;
+import org.altusmetrum.altoslib_11.*;
 
 class AltosUIMapPos extends Box {
        AltosUIFrame    owner;
@@ -118,7 +118,7 @@ class AltosUIMapPos extends Box {
        }
 }
 
-public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener, ItemListener, AltosLaunchSiteListener, AltosMapLoaderListener  {
+public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener, ItemListener, AltosLaunchSiteListener, AltosMapLoaderListener, AltosUnitsListener, AltosFontListener  {
        AltosUIFrame    owner;
        AltosUIMapNew   map;
 
@@ -127,20 +127,19 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener
 
        JProgressBar    pbar;
 
-       AltosMapLoader  loader;
-
        JLabel          site_list_label;
        JComboBox<AltosLaunchSite>      site_list;
 
        JToggleButton   load_button;
-       boolean         loading;
        JButton         close_button;
 
        JCheckBox[]     maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1];
 
        JComboBox<Integer>      min_zoom;
        JComboBox<Integer>      max_zoom;
+       JLabel                  radius_label;
        JComboBox<Double>       radius;
+       int scale = 1;
 
        Integer[]               zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
 
@@ -149,14 +148,19 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener
        Double[]        radius_km = { 2.0, 5.0, 10.0, 20.0, 30.0 };
        Double          radius_def_km = 10.0;
 
+       AltosMapLoader  loader;
 
        static final String[]   lat_hemi_names = { "N", "S" };
        static final String[]   lon_hemi_names = { "E", "W" };
 
        double  latitude, longitude;
 
+       long    loader_notify_time;
+
        /* AltosMapLoaderListener interfaces */
        public void loader_start(final int max) {
+               loader_notify_time = System.currentTimeMillis();
+
                SwingUtilities.invokeLater(new Runnable() {
                                public void run() {
                                        pbar.setMaximum(max);
@@ -169,6 +173,13 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener
        }
 
        public void loader_notify(final int cur, final int max, final String name) {
+               long    now = System.currentTimeMillis();
+
+               if (now - loader_notify_time < 100)
+                       return;
+
+               loader_notify_time = now;
+
                SwingUtilities.invokeLater(new Runnable() {
                                public void run() {
                                        pbar.setValue(cur);
@@ -178,18 +189,19 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener
        }
 
        public void loader_done(int max) {
+               loader = null;
                SwingUtilities.invokeLater(new Runnable() {
                                public void run() {
                                        pbar.setValue(0);
                                        pbar.setString("");
                                        load_button.setSelected(false);
-                                       loading = false;
                                }
                        });
        }
 
        public void debug(String format, Object ... arguments) {
-               System.out.printf(format, arguments);
+               if (AltosSerial.debug)
+                       System.out.printf(format, arguments);
        }
 
 
@@ -217,11 +229,14 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener
        public void actionPerformed(ActionEvent e) {
                String  cmd = e.getActionCommand();
 
-               if (cmd.equals("close"))
+               if (cmd.equals("close")) {
+                       if (loader != null)
+                               loader.abort();
                        setVisible(false);
+               }
 
                if (cmd.equals("load")) {
-                       if (!loading) {
+                       if (loader == null) {
                                try {
                                        latitude = lat.get_value();
                                        longitude = lon.get_value();
@@ -232,12 +247,17 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener
                                        Double r = (Double) radius.getSelectedItem();
 
                                        if (AltosPreferences.imperial_units())
-                                               r = AltosConvert.distance.inverse(r);
+                                               r = AltosConvert.miles_to_meters(r);
                                        else
                                                r = r * 1000;
-                                       loading = true;
 
-                                       loader.load(latitude, longitude, min_z, max_z, r, all_types());
+                                       map.map.centre(new AltosLatLon(latitude, longitude));
+
+                                       loader = new AltosMapLoader(this,
+                                                                   latitude, longitude,
+                                                                   min_z, max_z, r,
+                                                                   all_types(), scale);
+
                                } catch (ParseException pe) {
                                        load_button.setSelected(false);
                                }
@@ -257,6 +277,35 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener
                        });
        }
 
+       private void set_radius_values() {
+               radius_label.setText(String.format("Map Radius (%s)",
+                                                  AltosPreferences.imperial_units() ? "mi" : "km"));
+
+               Double[]        radii;
+
+               if (AltosPreferences.imperial_units())
+                       radii = radius_mi;
+               else
+                       radii = radius_km;
+
+               radius.removeAllItems();
+               for (Double r : radii) {
+                       System.out.printf("adding radius %f\n",r);
+                       radius.addItem(r);
+               }
+               radius.setSelectedItem(radii[2]);
+               radius.setMaximumRowCount(radii.length);
+       }
+
+       public void units_changed(boolean imperial_units) {
+               map.units_changed(imperial_units);
+               set_radius_values();
+       }
+
+       public void font_size_changed(int font_size) {
+               map.font_size_changed(font_size);
+       }
+
        public AltosUIMapPreloadNew(AltosUIFrame in_owner) {
                owner = in_owner;
 
@@ -268,9 +317,19 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener
 
                pane.setLayout(new GridBagLayout());
 
-               map = new AltosUIMapNew();
+               addWindowListener(new WindowAdapter() {
+                               @Override
+                               public void windowClosing(WindowEvent e) {
+                                       AltosUIPreferences.unregister_font_listener(AltosUIMapPreloadNew.this);
+                                       AltosPreferences.unregister_units_listener(AltosUIMapPreloadNew.this);
+                               }
+                       });
 
-               loader = new AltosMapLoader(map.map, this);
+
+               AltosPreferences.register_units_listener(this);
+               AltosUIPreferences.register_font_listener(this);
+
+               map = new AltosUIMapNew();
 
                c.fill = GridBagConstraints.BOTH;
                c.anchor = GridBagConstraints.CENTER;
@@ -445,26 +504,20 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener
                c.gridy = 3;
                pane.add(max_zoom, c);
 
-               JLabel radius_label = new JLabel(String.format("Map Radius (%s)",
-                                                              AltosPreferences.imperial_units() ? "miles" : "km"));
+               radius_label = new JLabel();
+
                c.gridx = 4;
                c.gridy = 4;
                pane.add(radius_label, c);
 
-               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 = new JComboBox<Double>();
                radius.setEditable(true);
                c.gridx = 5;
                c.gridy = 4;
                pane.add(radius, c);
 
+               set_radius_values();
+
                pack();
                setLocationRelativeTo(owner);
                setVisible(true);