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