Bump java lib versions to 13
[fw/altos] / altosuilib / AltosUIMapPreload.java
index d702dddfee70f59029a6a3356e019544d7444229..36b32c85337c6d8a190a152ee1f4d0208f8b5ca0 100644 (file)
@@ -3,7 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -15,7 +16,7 @@
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  */
 
-package org.altusmetrum.altosuilib_2;
+package org.altusmetrum.altosuilib_13;
 
 import java.awt.*;
 import java.awt.event.*;
@@ -26,9 +27,10 @@ import java.text.*;
 import java.lang.Math;
 import java.net.URL;
 import java.net.URLConnection;
-import org.altusmetrum.altoslib_4.*;
+import org.altusmetrum.altoslib_13.*;
 
-class AltosUIMapPos extends Box {
+class AltosUIMapPos extends Box implements ActionListener {
+       AltosUIMapPreload       preload;
        AltosUIFrame    owner;
        JLabel          label;
        JComboBox       hemi;
@@ -37,6 +39,11 @@ class AltosUIMapPos extends Box {
        JTextField      min;
        JLabel          min_label;
 
+       /* ActionListener interface */
+       public void actionPerformed(ActionEvent e) {
+               preload.center_map();
+       }
+
        public void set_value(double new_value) {
                double  d, m;
                int     h;
@@ -53,33 +60,33 @@ class AltosUIMapPos extends Box {
                hemi.setSelectedIndex(h);
        }
 
-       public double get_value() throws NumberFormatException {
+       public double get_value() throws ParseException {
                int     h = hemi.getSelectedIndex();
                String  d_t = deg.getText();
                String  m_t = min.getText();
                double  d, m, v;
                try {
-                       d = Double.parseDouble(d_t);
-               } catch (NumberFormatException ne) {
+                       d = AltosParse.parse_double_locale(d_t);
+               } catch (ParseException pe) {
                        JOptionPane.showMessageDialog(owner,
                                                      String.format("Invalid degrees \"%s\"",
                                                                    d_t),
                                                      "Invalid number",
                                                      JOptionPane.ERROR_MESSAGE);
-                       throw ne;
+                       throw pe;
                }
                try {
                        if (m_t.equals(""))
                                m = 0;
                        else
-                               m = Double.parseDouble(m_t);
-               } catch (NumberFormatException ne) {
+                               m = AltosParse.parse_double_locale(m_t);
+               } catch (ParseException pe) {
                        JOptionPane.showMessageDialog(owner,
                                                      String.format("Invalid minutes \"%s\"",
                                                                    m_t),
                                                      "Invalid number",
                                                      JOptionPane.ERROR_MESSAGE);
-                       throw ne;
+                       throw pe;
                }
                v = d + m/60.0;
                if (h == 1)
@@ -88,19 +95,23 @@ class AltosUIMapPos extends Box {
        }
 
        public AltosUIMapPos(AltosUIFrame in_owner,
-                          String label_value,
-                          String[] hemi_names,
-                          double default_value) {
+                            AltosUIMapPreload preload,
+                            String label_value,
+                            String[] hemi_names,
+                            double default_value) {
                super(BoxLayout.X_AXIS);
                owner = in_owner;
+               this.preload = preload;
                label = new JLabel(label_value);
                hemi = new JComboBox<String>(hemi_names);
                hemi.setEditable(false);
                deg = new JTextField(5);
+               deg.addActionListener(this);
                deg.setMinimumSize(deg.getPreferredSize());
                deg.setHorizontalAlignment(JTextField.RIGHT);
                deg_label = new JLabel("°");
                min = new JTextField(9);
+               min.addActionListener(this);
                min.setMinimumSize(min.getPreferredSize());
                min_label = new JLabel("'");
                set_value(default_value);
@@ -118,95 +129,7 @@ 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 = Double.parseDouble(elements[1]);
-                       longitude = Double.parseDouble(elements[2]);
-               } catch (NumberFormatException ne) {
-                       throw new ParseException(String.format("Invalid site line %s", line), 0);
-               }
-       }
-}
-
-class AltosUISites extends Thread {
-       AltosUIMapPreload       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) {
-               }
-       }
-
-       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(AltosUIMapPreload 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 AltosUIMapPreload extends AltosUIFrame implements ActionListener, ItemListener, AltosUIMapTileListener {
+public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, ItemListener, AltosLaunchSiteListener, AltosMapLoaderListener, AltosUnitsListener, AltosFontListener  {
        AltosUIFrame    owner;
        AltosUIMap      map;
 
@@ -214,150 +137,101 @@ public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, I
        AltosUIMapPos   lon;
 
        JProgressBar    pbar;
-       int             pbar_max;
-       int             pbar_cur;
 
-       AltosUISites    sites;
        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;
+       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 };
-       Integer[]               radii = { 1, 2, 3, 4, 5 };
+
+       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;
+
+       AltosMapLoader  loader;
 
        static final String[]   lat_hemi_names = { "N", "S" };
        static final String[]   lon_hemi_names = { "E", "W" };
 
-       class updatePbar implements Runnable {
-               String          s;
+       double  latitude, longitude;
 
-               public updatePbar(String in_s) {
-                       s = in_s;
-               }
+       long    loader_notify_time;
 
-               public void run() {
-                       int     n = ++pbar_cur;
+       /* AltosMapLoaderListener interfaces */
+       public void loader_start(final int max) {
+               loader_notify_time = System.currentTimeMillis();
 
-                       pbar.setMaximum(pbar_max);
-                       pbar.setValue(n);
-                       pbar.setString(s);
-               }
+               SwingUtilities.invokeLater(new Runnable() {
+                               public void run() {
+                                       pbar.setMaximum(max);
+                                       pbar.setValue(0);
+                                       pbar.setString("");
+                               }
+                       });
        }
 
-       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);
-       }
+       public void loader_notify(final int cur, final int max, final String name) {
+               long    now = System.currentTimeMillis();
 
-       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;
-       }
+               if (now - loader_notify_time < 100)
+                       return;
 
-       private void next_load() {
-               int next_type = next_type(cur_type + 1);
+               loader_notify_time = now;
 
-               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();
+               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;
+       public void loader_done(int max) {
+               loader = null;
+               SwingUtilities.invokeLater(new Runnable() {
+                               public void run() {
+                                       pbar.setValue(0);
+                                       pbar.setString("");
+                                       load_button.setSelected(false);
+                               }
+                       });
+       }
 
-               map.clear_marks();
-               map.add_mark(latitude,longitude, AltosLib.ao_flight_boost);
-               do_load();
+       public void debug(String format, Object ... arguments) {
+               if (AltosSerial.debug)
+                       System.out.printf(format, arguments);
        }
 
-       /* AltosUIMapTileListener methods */
 
-       public void notify_tile(AltosUIMapTile tile, int status) {
-               if (status == AltosUIMapStore.loading)
-                       return;
+       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;
+       }
 
-               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();
-                                               }
-                                       });
-                       }
-               }
+       void center_map(double latitude, double longitude) {
+               map.map.centre(new AltosLatLon(latitude, longitude));
+               map.clear_marks();
+               map.add_mark(latitude, longitude, AltosLib.ao_flight_boost);
        }
 
-       public void set_sites() {
-               int     i = 1;
-               for (AltosUISite site : sites.sites) {
-                       site_list.insertItemAt(site, i);
-                       i++;
+       void center_map() {
+               try {
+                       center_map(lat.get_value(), lon.get_value());
+               } catch (ParseException pe) {
                }
        }
 
@@ -366,10 +240,11 @@ public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, I
 
                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);
+                               center_map(site.latitude, site.longitude);
                        }
                }
        }
@@ -377,31 +252,83 @@ public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, I
        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();
-                                       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();
-                                       loading = true;
-                               } catch (NumberFormatException ne) {
+                                       Double r = (Double) radius.getSelectedItem();
+
+                                       if (AltosPreferences.imperial_units())
+                                               r = AltosConvert.miles_to_meters(r);
+                                       else
+                                               r = r * 1000;
+
+                                       center_map(latitude, longitude);
+
+                                       loader = new AltosMapLoader(this,
+                                                                   latitude, longitude,
+                                                                   min_z, max_z, r,
+                                                                   all_types(), scale);
+
+                               } catch (ParseException pe) {
                                        load_button.setSelected(false);
                                }
-                               start_load();
                        }
                }
        }
 
-       public AltosUIMapPreload(AltosUIFrame in_owner) {
-               System.out.printf("start creating preload ui\n");
+       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++;
+                                       }
+                               }
+                       });
+       }
+
+       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) {
+                       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 AltosUIMapPreload(AltosUIFrame in_owner) {
                owner = in_owner;
 
                Container               pane = getContentPane();
@@ -412,6 +339,18 @@ public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, I
 
                pane.setLayout(new GridBagLayout());
 
+               addWindowListener(new WindowAdapter() {
+                               @Override
+                               public void windowClosing(WindowEvent e) {
+                                       AltosUIPreferences.unregister_font_listener(AltosUIMapPreload.this);
+                                       AltosPreferences.unregister_units_listener(AltosUIMapPreload.this);
+                               }
+                       });
+
+
+               AltosPreferences.register_units_listener(this);
+               AltosUIPreferences.register_font_listener(this);
+
                map = new AltosUIMap();
 
                c.fill = GridBagConstraints.BOTH;
@@ -460,10 +399,10 @@ public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, I
 
                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;
@@ -477,7 +416,7 @@ public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, I
 
                pane.add(site_list, c);
 
-               lat = new AltosUIMapPos(owner,
+               lat = new AltosUIMapPos(owner, this,
                                        "Latitude:",
                                        lat_hemi_names,
                                        37.167833333);
@@ -494,7 +433,7 @@ public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, I
 
                pane.add(lat, c);
 
-               lon = new AltosUIMapPos(owner,
+               lon = new AltosUIMapPos(owner, this,
                                        "Longitude:",
                                        lon_hemi_names,
                                        -97.73975);
@@ -554,9 +493,9 @@ public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, I
 
                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;
@@ -587,22 +526,22 @@ public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, I
                c.gridy = 3;
                pane.add(max_zoom, c);
 
-               JLabel radius_label = new JLabel("Tile Radius");
+               radius_label = new JLabel();
+
                c.gridx = 4;
                c.gridy = 4;
                pane.add(radius_label, c);
 
-               radius = new JComboBox<Integer>(radii);
-               radius.setSelectedItem(radii[4]);
+               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);
-
-               System.out.printf("done creating preload ui\n");
        }
 }