use multimaint-merge to make Debian changelogs less ugly
[fw/altos] / altosui / AltosSiteMapPreload.java
index f939e9d6f71c3e0396b397a2beab46eb1c435e7d..aa07bebc03a0b205e370b3b001354ff72b8050d9 100644 (file)
@@ -31,8 +31,11 @@ import java.util.prefs.*;
 import java.lang.Math;
 import java.awt.geom.Point2D;
 import java.awt.geom.Line2D;
+import java.net.URL;
+import java.net.URLConnection;
 
 class AltosMapPos extends Box {
+       AltosUI         owner;
        JLabel          label;
        JComboBox       hemi;
        JTextField      deg;
@@ -58,25 +61,54 @@ class AltosMapPos extends Box {
 
        public double get_value() throws NumberFormatException {
                int     h = hemi.getSelectedIndex();
-               double d = Double.parseDouble(deg.getText());
-               double m = Double.parseDouble(min.getText());
-               double v = d + m/60.0;
+               String  d_t = deg.getText();
+               String  m_t = min.getText();
+               double  d, m, v;
+               try {
+                       d = Double.parseDouble(d_t);
+               } catch (NumberFormatException ne) {
+                       JOptionPane.showMessageDialog(owner,
+                                                     String.format("Invalid degrees \"%s\"",
+                                                                   d_t),
+                                                     "Invalid number",
+                                                     JOptionPane.ERROR_MESSAGE);
+                       throw ne;
+               }
+               try {
+                       if (m_t.equals(""))
+                               m = 0;
+                       else
+                               m = Double.parseDouble(m_t);
+               } catch (NumberFormatException ne) {
+                       JOptionPane.showMessageDialog(owner,
+                                                     String.format("Invalid minutes \"%s\"",
+                                                                   m_t),
+                                                     "Invalid number",
+                                                     JOptionPane.ERROR_MESSAGE);
+                       throw ne;
+               }
+               v = d + m/60.0;
                if (h == 1)
                        v = -v;
                return v;
        }
 
-       public AltosMapPos(String label_value,
+       public AltosMapPos(AltosUI in_owner,
+                          String label_value,
                           String[] hemi_names,
                           double default_value) {
                super(BoxLayout.X_AXIS);
+               owner = in_owner;
                label = new JLabel(label_value);
                hemi = new JComboBox(hemi_names);
                hemi.setEditable(false);
-               deg = new JTextField("000");
-               deg_label = new JLabel("degrees");
-               min = new JTextField("00.0000");
-               min_label = new JLabel("minutes");
+               deg = new JTextField(5);
+               deg.setMinimumSize(deg.getPreferredSize());
+               deg.setHorizontalAlignment(JTextField.RIGHT);
+               deg_label = new JLabel("°");
+               min = new JTextField(9);
+               min.setMinimumSize(min.getPreferredSize());
+               min_label = new JLabel("'");
                set_value(default_value);
                add(label);
                add(Box.createRigidArea(new Dimension(5, 0)));
@@ -92,19 +124,111 @@ class AltosMapPos extends Box {
        }
 }
 
-public class AltosSiteMapPreload extends JDialog implements ActionListener {
+class AltosSite {
+       String  name;
+       double  latitude;
+       double  longitude;
+
+       public String toString() {
+               return name;
+       }
+
+       public AltosSite(String in_name, double in_latitude, double in_longitude) {
+               name = in_name;
+               latitude = in_latitude;
+               longitude = in_longitude;
+       }
+
+       public AltosSite(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 AltosSites extends Thread {
+       AltosSiteMapPreload     preload;
+       URL                     url;
+       LinkedList<AltosSite>   sites;
+
+       void notify_complete() {
+               SwingUtilities.invokeLater(new Runnable() {
+                               public void run() {
+                                       preload.set_sites();
+                               }
+                       });
+       }
+
+       void add(AltosSite site) {
+               sites.add(site);
+       }
+
+       void add(String line) {
+               try {
+                       add(new AltosSite(line));
+               } catch (ParseException pe) {
+               }
+       }
+
+       public void run() {
+               try {
+                       URLConnection uc = url.openConnection();
+                       int length = uc.getContentLength();
+                       
+                       InputStreamReader in_stream = new InputStreamReader(uc.getInputStream(), Altos.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 AltosSites(AltosSiteMapPreload in_preload) {
+               sites = new LinkedList<AltosSite>();
+               preload = in_preload;
+               try {
+                       url = new URL(Altos.launch_sites_url);
+               } catch (java.net.MalformedURLException e) {
+                       notify_complete();
+               }
+               start();
+       }
+}
+
+public class AltosSiteMapPreload extends JDialog implements ActionListener, ItemListener {
        AltosUI         owner;
        AltosSiteMap    map;
 
        AltosMapPos     lat;
        AltosMapPos     lon;
 
-       JProgressBar    pbar;
+       final static int        radius = 4;
+       final static int        width = (radius * 2 + 1);
+       final static int        height = (radius * 2 + 1);
 
-       final static int        width = 9;
-       final static int        height = 9;
-       final static int        tiles = width * height;
+       JProgressBar    pbar;
 
+       AltosSites      sites;
+       JLabel          site_list_label;
+       JComboBox       site_list;
+               
        JToggleButton   load_button;
        boolean         loading;
        JButton         close_button;
@@ -116,15 +240,20 @@ public class AltosSiteMapPreload extends JDialog implements ActionListener {
                int             n;
                String          s;
 
-               public updatePbar(int in_n, String in_s) {
-                       n = in_n;
+               public updatePbar(int x, int y, String in_s) {
+                       n = (x + radius) + (y + radius) * width + 1;
                        s = in_s;
                }
 
                public void run() {
                        pbar.setValue(n);
                        pbar.setString(s);
-                       if (n == width * height) {
+                       if (n < width * height) {
+                               pbar.setValue(n);
+                               pbar.setString(s);
+                       } else {
+                               pbar.setValue(0);
+                               pbar.setString("");
                                load_button.setSelected(false);
                                loading = false;
                        }
@@ -140,16 +269,37 @@ public class AltosSiteMapPreload extends JDialog implements ActionListener {
                }
 
                public void run() {
-                       for (int y = 0; y < height; y++) {
-                               for (int x = 0; x < width; x++) {
-                                       map.prefetchMap(y - height/2, x - width/2);
-                                       SwingUtilities.invokeLater(new updatePbar(y * height + x + 1,
-                                                                                 map.pngfile.toString()));
+                       for (int y = -map.radius; y <= map.radius; y++) {
+                               for (int x = -map.radius; x <= map.radius; x++) {
+                                       String  pngfile;
+                                       pngfile = map.initMap(new Point(x,y));
+                                       SwingUtilities.invokeLater(new updatePbar(x, y, pngfile));
                                }
                        }
                }
        }
 
+       public void set_sites() {
+               int     i = 1;
+               for (AltosSite site : sites.sites) {
+                       site_list.insertItemAt(site, i);
+                       i++;
+               }
+       }
+
+       public void itemStateChanged(ItemEvent e) {
+               int             state = e.getStateChange();
+
+               if (state == ItemEvent.SELECTED) {
+                       Object  o = e.getItem();
+                       if (o instanceof AltosSite) {
+                               AltosSite       site = (AltosSite) o;
+                               lat.set_value(site.latitude);
+                               lon.set_value(site.longitude);
+                       }
+               }
+       }
+
        public void actionPerformed(ActionEvent e) {
                String  cmd = e.getActionCommand();
 
@@ -158,12 +308,17 @@ public class AltosSiteMapPreload extends JDialog implements ActionListener {
 
                if (cmd.equals("load")) {
                        if (!loading) {
-                               loading = true;
-                               final double    latitude = lat.get_value();
-                               final double    longitude = lon.get_value();
-                               map.show(latitude,longitude);
-                               bgLoad thread = new bgLoad(map);
-                               thread.start();
+                               try {
+                                       final double    latitude = lat.get_value();
+                                       final double    longitude = lon.get_value();
+                                       map.setBaseLocation(latitude,longitude);
+                                       map.draw_circle(latitude,longitude);
+                                       loading = true;
+                                       bgLoad thread = new bgLoad(map);
+                                       thread.start();
+                               } catch (NumberFormatException ne) {
+                                       load_button.setSelected(false);
+                               }
                        }
                }
        }
@@ -177,7 +332,7 @@ public class AltosSiteMapPreload extends JDialog implements ActionListener {
 
                pane.setLayout(new GridBagLayout());
 
-               map = new AltosSiteMap();
+               map = new AltosSiteMap(4);
 
                c.fill = GridBagConstraints.BOTH;
                c.anchor = GridBagConstraints.CENTER;
@@ -211,7 +366,39 @@ public class AltosSiteMapPreload extends JDialog implements ActionListener {
 
                pane.add(pbar, c);
 
-               lat = new AltosMapPos("Latitude:",
+               site_list_label = new JLabel ("Known Launch Sites:");
+
+               c.fill = GridBagConstraints.NONE;
+               c.anchor = GridBagConstraints.CENTER;
+               c.insets = i;
+               c.weightx = 1;
+               c.weighty = 0;
+
+               c.gridx = 0;
+               c.gridy = 2;
+               c.gridwidth = 1;
+
+               pane.add(site_list_label, c);
+               
+               site_list = new JComboBox(new String[] { "Site List" });
+               site_list.addItemListener(this);
+
+               sites = new AltosSites(this);
+
+               c.fill = GridBagConstraints.HORIZONTAL;
+               c.anchor = GridBagConstraints.CENTER;
+               c.insets = i;
+               c.weightx = 1;
+               c.weighty = 0;
+
+               c.gridx = 1;
+               c.gridy = 2;
+               c.gridwidth = 1;
+
+               pane.add(site_list, c);
+               
+               lat = new AltosMapPos(owner,
+                                     "Latitude:",
                                      lat_hemi_names,
                                      37.167833333);
                c.fill = GridBagConstraints.NONE;
@@ -221,13 +408,14 @@ public class AltosSiteMapPreload extends JDialog implements ActionListener {
                c.weighty = 0;
 
                c.gridx = 0;
-               c.gridy = 2;
+               c.gridy = 3;
                c.gridwidth = 1;
                c.anchor = GridBagConstraints.CENTER;
 
                pane.add(lat, c);
                
-               lon = new AltosMapPos("Longitude:",
+               lon = new AltosMapPos(owner,
+                                     "Longitude:",
                                      lon_hemi_names,
                                      -97.73975);
 
@@ -238,7 +426,7 @@ public class AltosSiteMapPreload extends JDialog implements ActionListener {
                c.weighty = 0;
 
                c.gridx = 1;
-               c.gridy = 2;
+               c.gridy = 3;
                c.gridwidth = 1;
                c.anchor = GridBagConstraints.CENTER;
 
@@ -255,7 +443,7 @@ public class AltosSiteMapPreload extends JDialog implements ActionListener {
                c.weighty = 0;
 
                c.gridx = 0;
-               c.gridy = 3;
+               c.gridy = 4;
                c.gridwidth = 1;
                c.anchor = GridBagConstraints.CENTER;
 
@@ -272,7 +460,7 @@ public class AltosSiteMapPreload extends JDialog implements ActionListener {
                c.weighty = 0;
 
                c.gridx = 1;
-               c.gridy = 3;
+               c.gridy = 4;
                c.gridwidth = 1;
                c.anchor = GridBagConstraints.CENTER;