altoslib: Add all known launch sites and names to map preload
[fw/altos] / altosuilib / AltosUIMapPreload.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altosuilib_14;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import java.io.*;
25 import java.util.*;
26 import java.text.*;
27 import java.lang.Math;
28 import java.net.URL;
29 import java.net.URLConnection;
30 import org.altusmetrum.altoslib_14.*;
31
32 class AltosUIMapPos extends Box implements ActionListener {
33         AltosUIMapPreload       preload;
34         AltosUIFrame    owner;
35         JLabel          label;
36         JComboBox       hemi;
37         JTextField      deg;
38         JLabel          deg_label;
39         JTextField      min;
40         JLabel          min_label;
41
42         /* ActionListener interface */
43         public void actionPerformed(ActionEvent e) {
44                 preload.center_map();
45         }
46
47         public void set_value(double new_value) {
48                 double  d, m;
49                 int     h;
50
51                 h = 0;
52                 if (new_value < 0) {
53                         h = 1;
54                         new_value = -new_value;
55                 }
56                 d = Math.floor(new_value);
57                 deg.setText(String.format("%3.0f", d));
58                 m = (new_value - d) * 60.0;
59                 min.setText(String.format("%7.4f", m));
60                 hemi.setSelectedIndex(h);
61         }
62
63         public double get_value() throws ParseException {
64                 int     h = hemi.getSelectedIndex();
65                 String  d_t = deg.getText();
66                 String  m_t = min.getText();
67                 double  d, m, v;
68                 try {
69                         d = AltosParse.parse_double_locale(d_t);
70                 } catch (ParseException pe) {
71                         JOptionPane.showMessageDialog(owner,
72                                                       String.format("Invalid degrees \"%s\"",
73                                                                     d_t),
74                                                       "Invalid number",
75                                                       JOptionPane.ERROR_MESSAGE);
76                         throw pe;
77                 }
78                 try {
79                         if (m_t.equals(""))
80                                 m = 0;
81                         else
82                                 m = AltosParse.parse_double_locale(m_t);
83                 } catch (ParseException pe) {
84                         JOptionPane.showMessageDialog(owner,
85                                                       String.format("Invalid minutes \"%s\"",
86                                                                     m_t),
87                                                       "Invalid number",
88                                                       JOptionPane.ERROR_MESSAGE);
89                         throw pe;
90                 }
91                 v = d + m/60.0;
92                 if (h == 1)
93                         v = -v;
94                 return v;
95         }
96
97         public AltosUIMapPos(AltosUIFrame in_owner,
98                              AltosUIMapPreload preload,
99                              String label_value,
100                              String[] hemi_names,
101                              double default_value) {
102                 super(BoxLayout.X_AXIS);
103                 owner = in_owner;
104                 this.preload = preload;
105                 label = new JLabel(label_value);
106                 hemi = new JComboBox<String>(hemi_names);
107                 hemi.setEditable(false);
108                 deg = new JTextField(5);
109                 deg.addActionListener(this);
110                 deg.setMinimumSize(deg.getPreferredSize());
111                 deg.setHorizontalAlignment(JTextField.RIGHT);
112                 deg_label = new JLabel("°");
113                 min = new JTextField(9);
114                 min.addActionListener(this);
115                 min.setMinimumSize(min.getPreferredSize());
116                 min_label = new JLabel("'");
117                 set_value(default_value);
118                 add(label);
119                 add(Box.createRigidArea(new Dimension(5, 0)));
120                 add(hemi);
121                 add(Box.createRigidArea(new Dimension(5, 0)));
122                 add(deg);
123                 add(Box.createRigidArea(new Dimension(5, 0)));
124                 add(deg_label);
125                 add(Box.createRigidArea(new Dimension(5, 0)));
126                 add(min);
127                 add(Box.createRigidArea(new Dimension(5, 0)));
128                 add(min_label);
129         }
130 }
131
132 public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, ItemListener, AltosLaunchSiteListener, AltosMapLoaderListener, AltosUnitsListener, AltosFontListener  {
133         AltosUIFrame    owner;
134         AltosUIMap      map;
135
136         AltosUIMapPos   lat;
137         AltosUIMapPos   lon;
138
139         JProgressBar    pbar;
140
141         JLabel          site_list_label;
142         java.util.List<AltosLaunchSite> sites;
143         JComboBox<AltosLaunchSite>      site_list;
144
145         JToggleButton   load_button;
146         JButton         close_button;
147
148 /*
149         JCheckBox[]     maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1];
150 */
151
152         JComboBox<Integer>      min_zoom;
153         JComboBox<Integer>      max_zoom;
154         JLabel                  radius_label;
155         JComboBox<Double>       radius;
156         int scale = 1;
157
158         Integer[]               zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
159
160         Double[]        radius_mi = { 1.0, 2.0, 5.0, 10.0, 20.0 };
161         Double          radius_def_mi = 5.0;
162         Double[]        radius_km = { 2.0, 5.0, 10.0, 20.0, 30.0 };
163         Double          radius_def_km = 10.0;
164
165         AltosMapLoader  loader;
166
167         static final String[]   lat_hemi_names = { "N", "S" };
168         static final String[]   lon_hemi_names = { "E", "W" };
169
170         double  latitude, longitude;
171
172         long    loader_notify_time;
173
174         /* AltosMapLoaderListener interfaces */
175         public void loader_start(final int max) {
176                 loader_notify_time = System.currentTimeMillis();
177
178                 SwingUtilities.invokeLater(new Runnable() {
179                                 public void run() {
180                                         pbar.setMaximum(max);
181                                         pbar.setValue(0);
182                                         pbar.setString("");
183                                 }
184                         });
185         }
186
187         public void loader_notify(final int cur, final int max, final String name) {
188                 long    now = System.currentTimeMillis();
189
190                 if (now - loader_notify_time < 100)
191                         return;
192
193                 loader_notify_time = now;
194
195                 SwingUtilities.invokeLater(new Runnable() {
196                                 public void run() {
197                                         pbar.setValue(cur);
198                                         pbar.setString(name);
199                                 }
200                         });
201         }
202
203         public void loader_done(int max) {
204                 loader = null;
205                 SwingUtilities.invokeLater(new Runnable() {
206                                 public void run() {
207                                         pbar.setValue(0);
208                                         pbar.setString("");
209                                         load_button.setSelected(false);
210                                 }
211                         });
212         }
213
214         public void debug(String format, Object ... arguments) {
215                 if (AltosSerial.debug)
216                         System.out.printf(format, arguments);
217         }
218
219
220         private int all_types() {
221 /*
222                 int all_types = 0;
223                 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
224                         if (maptypes[t].isSelected())
225                                 all_types |= (1 << t);
226                 return all_types;
227 */
228                 return 1 << AltosMap.maptype_hybrid;
229         }
230
231         void add_mark(double lat, double lon, int state, String label) {
232                 map.add_mark(lat, lon, state, label);
233                 if (lon <= 0)
234                         map.add_mark(lat, lon + 360, state, label);
235                 if (lon >= 0)
236                         map.add_mark(lat, lon - 360, state, label);
237         }
238
239         void reset_marks() {
240                 map.clear_marks();
241                 AltosLatLon centre = null;
242                 String centre_name = null;
243                 if (map != null && map.map != null)
244                         centre = map.map.centre;
245                 for (AltosLaunchSite site : sites) {
246                         if (centre != null && centre.lat == site.latitude && centre.lon == site.longitude)
247                                 centre_name = site.name;
248                         else
249                                 add_mark(site.latitude, site.longitude, AltosLib.ao_flight_main, site.name);
250                 }
251                 if (centre != null)
252                         add_mark(centre.lat, centre.lon, AltosLib.ao_flight_boost, centre_name);
253         }
254
255         void center_map(double latitude, double longitude) {
256                 map.map.centre(new AltosLatLon(latitude, longitude));
257                 reset_marks();
258         }
259
260         void center_map() {
261                 try {
262                         center_map(lat.get_value(), lon.get_value());
263                 } catch (ParseException pe) {
264                 }
265         }
266
267         public void itemStateChanged(ItemEvent e) {
268                 int             state = e.getStateChange();
269
270                 if (state == ItemEvent.SELECTED) {
271                         Object  o = e.getItem();
272                         if (o instanceof AltosLaunchSite) {
273                                 AltosLaunchSite site = (AltosLaunchSite) o;
274                                 lat.set_value(site.latitude);
275                                 lon.set_value(site.longitude);
276                                 center_map(site.latitude, site.longitude);
277                         }
278                 }
279         }
280
281         public void actionPerformed(ActionEvent e) {
282                 String  cmd = e.getActionCommand();
283
284                 if (cmd.equals("close")) {
285                         if (loader != null)
286                                 loader.abort();
287                         setVisible(false);
288                 }
289
290                 if (cmd.equals("load")) {
291                         if (loader == null) {
292                                 try {
293                                         latitude = lat.get_value();
294                                         longitude = lon.get_value();
295                                         int min_z = (Integer) min_zoom.getSelectedItem();
296                                         int max_z = (Integer) max_zoom.getSelectedItem();
297                                         if (max_z < min_z)
298                                                 max_z = min_z;
299                                         Double r = (Double) radius.getSelectedItem();
300
301                                         if (AltosPreferences.imperial_units())
302                                                 r = AltosConvert.miles_to_meters(r);
303                                         else
304                                                 r = r * 1000;
305
306                                         center_map(latitude, longitude);
307
308                                         loader = new AltosMapLoader(this,
309                                                                     latitude, longitude,
310                                                                     min_z, max_z, r,
311                                                                     all_types(), scale);
312
313                                 } catch (ParseException pe) {
314                                         load_button.setSelected(false);
315                                 }
316                         }
317                 }
318         }
319
320         public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
321                 this.sites = sites;
322                 SwingUtilities.invokeLater(new Runnable() {
323                                 public void run() {
324                                         int     i = 1;
325                                         for (AltosLaunchSite site : sites) {
326                                                 site_list.insertItemAt(site, i);
327                                                 i++;
328                                         }
329                                         reset_marks();
330                                 }
331                         });
332         }
333
334         private void set_radius_values() {
335                 radius_label.setText(String.format("Map Radius (%s)",
336                                                    AltosPreferences.imperial_units() ? "mi" : "km"));
337
338                 Double[]        radii;
339
340                 if (AltosPreferences.imperial_units())
341                         radii = radius_mi;
342                 else
343                         radii = radius_km;
344
345                 radius.removeAllItems();
346                 for (Double r : radii) {
347                         radius.addItem(r);
348                 }
349                 radius.setSelectedItem(radii[2]);
350                 radius.setMaximumRowCount(radii.length);
351         }
352
353         public void units_changed(boolean imperial_units) {
354                 map.units_changed(imperial_units);
355                 set_radius_values();
356         }
357
358         public void font_size_changed(int font_size) {
359                 map.font_size_changed(font_size);
360         }
361
362         public AltosUIMapPreload(AltosUIFrame in_owner) {
363                 owner = in_owner;
364
365                 Container               pane = getScrollablePane();
366                 GridBagConstraints      c = new GridBagConstraints();
367                 Insets                  i = new Insets(4,4,4,4);
368
369                 setTitle("AltOS Load Maps");
370
371                 pane.setLayout(new GridBagLayout());
372
373                 addWindowListener(new WindowAdapter() {
374                                 @Override
375                                 public void windowClosing(WindowEvent e) {
376                                         AltosUIPreferences.unregister_font_listener(AltosUIMapPreload.this);
377                                         AltosPreferences.unregister_units_listener(AltosUIMapPreload.this);
378                                 }
379                         });
380
381
382                 AltosPreferences.register_units_listener(this);
383                 AltosUIPreferences.register_font_listener(this);
384
385                 map = new AltosUIMap();
386
387                 c.fill = GridBagConstraints.BOTH;
388                 c.anchor = GridBagConstraints.CENTER;
389                 c.insets = i;
390                 c.weightx = 1;
391                 c.weighty = 1;
392
393                 c.gridx = 0;
394                 c.gridy = 0;
395                 c.gridwidth = 10;
396                 c.anchor = GridBagConstraints.CENTER;
397
398                 pane.add(map, c);
399
400                 pbar = new JProgressBar();
401                 pbar.setMinimum(0);
402                 pbar.setMaximum(1);
403                 pbar.setValue(0);
404                 pbar.setString("");
405                 pbar.setStringPainted(true);
406
407                 c.fill = GridBagConstraints.HORIZONTAL;
408                 c.anchor = GridBagConstraints.CENTER;
409                 c.insets = i;
410                 c.weightx = 1;
411                 c.weighty = 0;
412
413                 c.gridx = 0;
414                 c.gridy = 1;
415                 c.gridwidth = 10;
416
417                 pane.add(pbar, c);
418
419                 site_list_label = new JLabel ("Known Launch Sites:");
420
421                 c.fill = GridBagConstraints.NONE;
422                 c.anchor = GridBagConstraints.CENTER;
423                 c.insets = i;
424                 c.weightx = 1;
425                 c.weighty = 0;
426
427                 c.gridx = 0;
428                 c.gridy = 2;
429                 c.gridwidth = 1;
430
431                 pane.add(site_list_label, c);
432
433                 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
434                 site_list.addItemListener(this);
435
436                 new AltosLaunchSites(this);
437
438                 c.fill = GridBagConstraints.HORIZONTAL;
439                 c.anchor = GridBagConstraints.CENTER;
440                 c.insets = i;
441                 c.weightx = 1;
442                 c.weighty = 0;
443
444                 c.gridx = 1;
445                 c.gridy = 2;
446                 c.gridwidth = 1;
447
448                 pane.add(site_list, c);
449
450                 lat = new AltosUIMapPos(owner, this,
451                                         "Latitude:",
452                                         lat_hemi_names,
453                                         37.167833333);
454                 c.fill = GridBagConstraints.NONE;
455                 c.anchor = GridBagConstraints.CENTER;
456                 c.insets = i;
457                 c.weightx = 0;
458                 c.weighty = 0;
459
460                 c.gridx = 0;
461                 c.gridy = 3;
462                 c.gridwidth = 1;
463                 c.anchor = GridBagConstraints.CENTER;
464
465                 pane.add(lat, c);
466
467                 lon = new AltosUIMapPos(owner, this,
468                                         "Longitude:",
469                                         lon_hemi_names,
470                                         -97.73975);
471
472                 c.fill = GridBagConstraints.NONE;
473                 c.anchor = GridBagConstraints.CENTER;
474                 c.insets = i;
475                 c.weightx = 0;
476                 c.weighty = 0;
477
478                 c.gridx = 1;
479                 c.gridy = 3;
480                 c.gridwidth = 1;
481                 c.anchor = GridBagConstraints.CENTER;
482
483                 pane.add(lon, c);
484
485                 load_button = new JToggleButton("Load Map");
486                 load_button.addActionListener(this);
487                 load_button.setActionCommand("load");
488
489                 c.fill = GridBagConstraints.NONE;
490                 c.anchor = GridBagConstraints.CENTER;
491                 c.insets = i;
492                 c.weightx = 1;
493                 c.weighty = 0;
494
495                 c.gridx = 0;
496                 c.gridy = 4;
497                 c.gridwidth = 1;
498                 c.anchor = GridBagConstraints.CENTER;
499
500                 pane.add(load_button, c);
501
502                 close_button = new JButton("Close");
503                 close_button.addActionListener(this);
504                 close_button.setActionCommand("close");
505
506                 c.fill = GridBagConstraints.NONE;
507                 c.anchor = GridBagConstraints.CENTER;
508                 c.insets = i;
509                 c.weightx = 1;
510                 c.weighty = 0;
511
512                 c.gridx = 1;
513                 c.gridy = 4;
514                 c.gridwidth = 1;
515                 c.anchor = GridBagConstraints.CENTER;
516
517                 pane.add(close_button, c);
518
519 /*
520                 JLabel  types_label = new JLabel("Map Types");
521                 c.gridx = 2;
522                 c.gridwidth = 2;
523                 c.gridy = 2;
524                 pane.add(types_label, c);
525
526                 c.gridwidth = 1;
527
528                 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
529                         maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
530                                                        type == AltosMap.maptype_hybrid);
531                         c.gridx = 2 + (type >> 1);
532                         c.fill = GridBagConstraints.HORIZONTAL;
533                         c.gridy = (type & 1) + 3;
534                         pane.add(maptypes[type], c);
535                 }
536 */
537
538                 JLabel  min_zoom_label = new JLabel("Minimum Zoom");
539                 c.gridx = 4;
540                 c.gridy = 2;
541                 pane.add(min_zoom_label, c);
542
543                 min_zoom = new JComboBox<Integer>(zooms);
544                 min_zoom.setSelectedItem(zooms[10]);
545                 min_zoom.setEditable(false);
546                 c.gridx = 5;
547                 c.gridy = 2;
548                 pane.add(min_zoom, c);
549
550                 JLabel  max_zoom_label = new JLabel("Maximum Zoom");
551                 c.gridx = 4;
552                 c.gridy = 3;
553                 pane.add(max_zoom_label, c);
554
555                 max_zoom = new JComboBox<Integer>(zooms);
556                 max_zoom.setSelectedItem(zooms[14]);
557                 max_zoom.setEditable(false);
558                 c.gridx = 5;
559                 c.gridy = 3;
560                 pane.add(max_zoom, c);
561
562                 radius_label = new JLabel();
563
564                 c.gridx = 4;
565                 c.gridy = 4;
566                 pane.add(radius_label, c);
567
568                 radius = new JComboBox<Double>();
569                 radius.setEditable(true);
570                 c.gridx = 5;
571                 c.gridy = 4;
572                 pane.add(radius, c);
573
574                 set_radius_values();
575
576                 pack();
577                 setLocationRelativeTo(owner);
578                 setVisible(true);
579         }
580 }