altos/test: Adjust CRC error rate after FEC fix
[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         }
234
235         void reset_marks() {
236                 map.clear_marks();
237                 AltosLatLon centre = null;
238                 String centre_name = null;
239                 if (map != null && map.map != null)
240                         centre = map.map.centre;
241                 for (AltosLaunchSite site : sites) {
242                         if (centre != null && centre.lat == site.latitude && centre.lon == site.longitude)
243                                 centre_name = site.name;
244                         else
245                                 add_mark(site.latitude, site.longitude, AltosLib.ao_flight_main, site.name);
246                 }
247                 if (centre != null)
248                         add_mark(centre.lat, centre.lon, AltosLib.ao_flight_boost, centre_name);
249         }
250
251         void center_map(double latitude, double longitude) {
252                 map.map.centre(new AltosLatLon(latitude, longitude));
253                 reset_marks();
254         }
255
256         void center_map() {
257                 try {
258                         center_map(lat.get_value(), lon.get_value());
259                 } catch (ParseException pe) {
260                 }
261         }
262
263         public void itemStateChanged(ItemEvent e) {
264                 int             state = e.getStateChange();
265
266                 if (state == ItemEvent.SELECTED) {
267                         Object  o = e.getItem();
268                         if (o instanceof AltosLaunchSite) {
269                                 AltosLaunchSite site = (AltosLaunchSite) o;
270                                 lat.set_value(site.latitude);
271                                 lon.set_value(site.longitude);
272                                 center_map(site.latitude, site.longitude);
273                         }
274                 }
275         }
276
277         public void actionPerformed(ActionEvent e) {
278                 String  cmd = e.getActionCommand();
279
280                 if (cmd.equals("close")) {
281                         if (loader != null)
282                                 loader.abort();
283                         setVisible(false);
284                 }
285
286                 if (cmd.equals("load")) {
287                         if (loader == null) {
288                                 try {
289                                         latitude = lat.get_value();
290                                         longitude = lon.get_value();
291                                         int min_z = (Integer) min_zoom.getSelectedItem();
292                                         int max_z = (Integer) max_zoom.getSelectedItem();
293                                         if (max_z < min_z)
294                                                 max_z = min_z;
295                                         Double r = (Double) radius.getSelectedItem();
296
297                                         if (AltosPreferences.imperial_units())
298                                                 r = AltosConvert.miles_to_meters(r);
299                                         else
300                                                 r = r * 1000;
301
302                                         center_map(latitude, longitude);
303
304                                         loader = new AltosMapLoader(this,
305                                                                     latitude, longitude,
306                                                                     min_z, max_z, r,
307                                                                     all_types(), scale);
308
309                                 } catch (ParseException pe) {
310                                         load_button.setSelected(false);
311                                 }
312                         }
313                 }
314         }
315
316         public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
317                 this.sites = sites;
318                 SwingUtilities.invokeLater(new Runnable() {
319                                 public void run() {
320                                         int     i = 1;
321                                         for (AltosLaunchSite site : sites) {
322                                                 site_list.insertItemAt(site, i);
323                                                 i++;
324                                         }
325                                         reset_marks();
326                                 }
327                         });
328         }
329
330         private void set_radius_values() {
331                 radius_label.setText(String.format("Map Radius (%s)",
332                                                    AltosPreferences.imperial_units() ? "mi" : "km"));
333
334                 Double[]        radii;
335
336                 if (AltosPreferences.imperial_units())
337                         radii = radius_mi;
338                 else
339                         radii = radius_km;
340
341                 radius.removeAllItems();
342                 for (Double r : radii) {
343                         radius.addItem(r);
344                 }
345                 radius.setSelectedItem(radii[2]);
346                 radius.setMaximumRowCount(radii.length);
347         }
348
349         public void units_changed(boolean imperial_units) {
350                 map.units_changed(imperial_units);
351                 set_radius_values();
352         }
353
354         public void font_size_changed(int font_size) {
355                 map.font_size_changed(font_size);
356         }
357
358         public AltosUIMapPreload(AltosUIFrame in_owner) {
359                 owner = in_owner;
360
361                 Container               pane = getScrollablePane();
362                 GridBagConstraints      c = new GridBagConstraints();
363                 Insets                  i = new Insets(4,4,4,4);
364
365                 setTitle("AltOS Load Maps");
366
367                 pane.setLayout(new GridBagLayout());
368
369                 addWindowListener(new WindowAdapter() {
370                                 @Override
371                                 public void windowClosing(WindowEvent e) {
372                                         AltosUIPreferences.unregister_font_listener(AltosUIMapPreload.this);
373                                         AltosPreferences.unregister_units_listener(AltosUIMapPreload.this);
374                                 }
375                         });
376
377
378                 AltosPreferences.register_units_listener(this);
379                 AltosUIPreferences.register_font_listener(this);
380
381                 map = new AltosUIMap();
382
383                 c.fill = GridBagConstraints.BOTH;
384                 c.anchor = GridBagConstraints.CENTER;
385                 c.insets = i;
386                 c.weightx = 1;
387                 c.weighty = 1;
388
389                 c.gridx = 0;
390                 c.gridy = 0;
391                 c.gridwidth = 10;
392                 c.anchor = GridBagConstraints.CENTER;
393
394                 pane.add(map, c);
395
396                 pbar = new JProgressBar();
397                 pbar.setMinimum(0);
398                 pbar.setMaximum(1);
399                 pbar.setValue(0);
400                 pbar.setString("");
401                 pbar.setStringPainted(true);
402
403                 c.fill = GridBagConstraints.HORIZONTAL;
404                 c.anchor = GridBagConstraints.CENTER;
405                 c.insets = i;
406                 c.weightx = 1;
407                 c.weighty = 0;
408
409                 c.gridx = 0;
410                 c.gridy = 1;
411                 c.gridwidth = 10;
412
413                 pane.add(pbar, c);
414
415                 site_list_label = new JLabel ("Known Launch Sites:");
416
417                 c.fill = GridBagConstraints.NONE;
418                 c.anchor = GridBagConstraints.CENTER;
419                 c.insets = i;
420                 c.weightx = 1;
421                 c.weighty = 0;
422
423                 c.gridx = 0;
424                 c.gridy = 2;
425                 c.gridwidth = 1;
426
427                 pane.add(site_list_label, c);
428
429                 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
430                 site_list.addItemListener(this);
431
432                 new AltosLaunchSites(this);
433
434                 c.fill = GridBagConstraints.HORIZONTAL;
435                 c.anchor = GridBagConstraints.CENTER;
436                 c.insets = i;
437                 c.weightx = 1;
438                 c.weighty = 0;
439
440                 c.gridx = 1;
441                 c.gridy = 2;
442                 c.gridwidth = 1;
443
444                 pane.add(site_list, c);
445
446                 lat = new AltosUIMapPos(owner, this,
447                                         "Latitude:",
448                                         lat_hemi_names,
449                                         37.167833333);
450                 c.fill = GridBagConstraints.NONE;
451                 c.anchor = GridBagConstraints.CENTER;
452                 c.insets = i;
453                 c.weightx = 0;
454                 c.weighty = 0;
455
456                 c.gridx = 0;
457                 c.gridy = 3;
458                 c.gridwidth = 1;
459                 c.anchor = GridBagConstraints.CENTER;
460
461                 pane.add(lat, c);
462
463                 lon = new AltosUIMapPos(owner, this,
464                                         "Longitude:",
465                                         lon_hemi_names,
466                                         -97.73975);
467
468                 c.fill = GridBagConstraints.NONE;
469                 c.anchor = GridBagConstraints.CENTER;
470                 c.insets = i;
471                 c.weightx = 0;
472                 c.weighty = 0;
473
474                 c.gridx = 1;
475                 c.gridy = 3;
476                 c.gridwidth = 1;
477                 c.anchor = GridBagConstraints.CENTER;
478
479                 pane.add(lon, c);
480
481                 load_button = new JToggleButton("Load Map");
482                 load_button.addActionListener(this);
483                 load_button.setActionCommand("load");
484
485                 c.fill = GridBagConstraints.NONE;
486                 c.anchor = GridBagConstraints.CENTER;
487                 c.insets = i;
488                 c.weightx = 1;
489                 c.weighty = 0;
490
491                 c.gridx = 0;
492                 c.gridy = 4;
493                 c.gridwidth = 1;
494                 c.anchor = GridBagConstraints.CENTER;
495
496                 pane.add(load_button, c);
497
498                 close_button = new JButton("Close");
499                 close_button.addActionListener(this);
500                 close_button.setActionCommand("close");
501
502                 c.fill = GridBagConstraints.NONE;
503                 c.anchor = GridBagConstraints.CENTER;
504                 c.insets = i;
505                 c.weightx = 1;
506                 c.weighty = 0;
507
508                 c.gridx = 1;
509                 c.gridy = 4;
510                 c.gridwidth = 1;
511                 c.anchor = GridBagConstraints.CENTER;
512
513                 pane.add(close_button, c);
514
515 /*
516                 JLabel  types_label = new JLabel("Map Types");
517                 c.gridx = 2;
518                 c.gridwidth = 2;
519                 c.gridy = 2;
520                 pane.add(types_label, c);
521
522                 c.gridwidth = 1;
523
524                 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
525                         maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
526                                                        type == AltosMap.maptype_hybrid);
527                         c.gridx = 2 + (type >> 1);
528                         c.fill = GridBagConstraints.HORIZONTAL;
529                         c.gridy = (type & 1) + 3;
530                         pane.add(maptypes[type], c);
531                 }
532 */
533
534                 JLabel  min_zoom_label = new JLabel("Minimum Zoom");
535                 c.gridx = 4;
536                 c.gridy = 2;
537                 pane.add(min_zoom_label, c);
538
539                 min_zoom = new JComboBox<Integer>(zooms);
540                 min_zoom.setSelectedItem(zooms[10]);
541                 min_zoom.setEditable(false);
542                 c.gridx = 5;
543                 c.gridy = 2;
544                 pane.add(min_zoom, c);
545
546                 JLabel  max_zoom_label = new JLabel("Maximum Zoom");
547                 c.gridx = 4;
548                 c.gridy = 3;
549                 pane.add(max_zoom_label, c);
550
551                 max_zoom = new JComboBox<Integer>(zooms);
552                 max_zoom.setSelectedItem(zooms[14]);
553                 max_zoom.setEditable(false);
554                 c.gridx = 5;
555                 c.gridy = 3;
556                 pane.add(max_zoom, c);
557
558                 radius_label = new JLabel();
559
560                 c.gridx = 4;
561                 c.gridy = 4;
562                 pane.add(radius_label, c);
563
564                 radius = new JComboBox<Double>();
565                 radius.setEditable(true);
566                 c.gridx = 5;
567                 c.gridy = 4;
568                 pane.add(radius, c);
569
570                 set_radius_values();
571
572                 pack();
573                 setLocationRelativeTo(owner);
574                 setVisible(true);
575         }
576 }