d6c257e0750e9ddfd38f52aada93e09cc52b5739
[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         long    loader_notify_time;
157
158         /* AltosMapLoaderListener interfaces */
159         public void loader_start(final int max) {
160                 loader_notify_time = System.currentTimeMillis();
161
162                 SwingUtilities.invokeLater(new Runnable() {
163                                 public void run() {
164                                         pbar.setMaximum(max);
165                                         pbar.setValue(0);
166                                         pbar.setString("");
167                                         map.clear_marks();
168                                         map.add_mark(latitude, longitude, AltosLib.ao_flight_boost);
169                                 }
170                         });
171         }
172
173         public void loader_notify(final int cur, final int max, final String name) {
174                 long    now = System.currentTimeMillis();
175
176                 if (now - loader_notify_time < 100)
177                         return;
178
179                 loader_notify_time = now;
180
181                 SwingUtilities.invokeLater(new Runnable() {
182                                 public void run() {
183                                         pbar.setValue(cur);
184                                         pbar.setString(name);
185                                 }
186                         });
187         }
188
189         public void loader_done(int max) {
190                 SwingUtilities.invokeLater(new Runnable() {
191                                 public void run() {
192                                         pbar.setValue(0);
193                                         pbar.setString("");
194                                         load_button.setSelected(false);
195                                         loading = false;
196                                 }
197                         });
198         }
199
200         public void debug(String format, Object ... arguments) {
201                 if (AltosSerial.debug)
202                         System.out.printf(format, arguments);
203         }
204
205
206         private int all_types() {
207                 int all_types = 0;
208                 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
209                         if (maptypes[t].isSelected())
210                                 all_types |= (1 << t);
211                 return all_types;
212         }
213
214         public void itemStateChanged(ItemEvent e) {
215                 int             state = e.getStateChange();
216
217                 if (state == ItemEvent.SELECTED) {
218                         Object  o = e.getItem();
219                         if (o instanceof AltosLaunchSite) {
220                                 AltosLaunchSite site = (AltosLaunchSite) o;
221                                 lat.set_value(site.latitude);
222                                 lon.set_value(site.longitude);
223                         }
224                 }
225         }
226
227         public void actionPerformed(ActionEvent e) {
228                 String  cmd = e.getActionCommand();
229
230                 if (cmd.equals("close"))
231                         setVisible(false);
232
233                 if (cmd.equals("load")) {
234                         if (!loading) {
235                                 try {
236                                         latitude = lat.get_value();
237                                         longitude = lon.get_value();
238                                         int min_z = (Integer) min_zoom.getSelectedItem();
239                                         int max_z = (Integer) max_zoom.getSelectedItem();
240                                         if (max_z < min_z)
241                                                 max_z = min_z;
242                                         Double r = (Double) radius.getSelectedItem();
243
244                                         if (AltosPreferences.imperial_units())
245                                                 r = AltosConvert.miles_to_meters(r);
246                                         else
247                                                 r = r * 1000;
248                                         loading = true;
249
250                                         new AltosMapLoader(map.map, this,
251                                                            latitude, longitude,
252                                                            min_z, max_z, r, all_types());
253
254                                 } catch (ParseException pe) {
255                                         load_button.setSelected(false);
256                                 }
257                         }
258                 }
259         }
260
261         public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
262                 SwingUtilities.invokeLater(new Runnable() {
263                                 public void run() {
264                                         int     i = 1;
265                                         for (AltosLaunchSite site : sites) {
266                                                 site_list.insertItemAt(site, i);
267                                                 i++;
268                                         }
269                                 }
270                         });
271         }
272
273         public AltosUIMapPreloadNew(AltosUIFrame in_owner) {
274                 owner = in_owner;
275
276                 Container               pane = getContentPane();
277                 GridBagConstraints      c = new GridBagConstraints();
278                 Insets                  i = new Insets(4,4,4,4);
279
280                 setTitle("AltOS Load Maps");
281
282                 pane.setLayout(new GridBagLayout());
283
284                 map = new AltosUIMapNew();
285
286                 c.fill = GridBagConstraints.BOTH;
287                 c.anchor = GridBagConstraints.CENTER;
288                 c.insets = i;
289                 c.weightx = 1;
290                 c.weighty = 1;
291
292                 c.gridx = 0;
293                 c.gridy = 0;
294                 c.gridwidth = 10;
295                 c.anchor = GridBagConstraints.CENTER;
296
297                 pane.add(map, c);
298
299                 pbar = new JProgressBar();
300                 pbar.setMinimum(0);
301                 pbar.setMaximum(1);
302                 pbar.setValue(0);
303                 pbar.setString("");
304                 pbar.setStringPainted(true);
305
306                 c.fill = GridBagConstraints.HORIZONTAL;
307                 c.anchor = GridBagConstraints.CENTER;
308                 c.insets = i;
309                 c.weightx = 1;
310                 c.weighty = 0;
311
312                 c.gridx = 0;
313                 c.gridy = 1;
314                 c.gridwidth = 10;
315
316                 pane.add(pbar, c);
317
318                 site_list_label = new JLabel ("Known Launch Sites:");
319
320                 c.fill = GridBagConstraints.NONE;
321                 c.anchor = GridBagConstraints.CENTER;
322                 c.insets = i;
323                 c.weightx = 1;
324                 c.weighty = 0;
325
326                 c.gridx = 0;
327                 c.gridy = 2;
328                 c.gridwidth = 1;
329
330                 pane.add(site_list_label, c);
331
332                 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
333                 site_list.addItemListener(this);
334
335                 new AltosLaunchSites(this);
336
337                 c.fill = GridBagConstraints.HORIZONTAL;
338                 c.anchor = GridBagConstraints.CENTER;
339                 c.insets = i;
340                 c.weightx = 1;
341                 c.weighty = 0;
342
343                 c.gridx = 1;
344                 c.gridy = 2;
345                 c.gridwidth = 1;
346
347                 pane.add(site_list, c);
348
349                 lat = new AltosUIMapPos(owner,
350                                         "Latitude:",
351                                         lat_hemi_names,
352                                         37.167833333);
353                 c.fill = GridBagConstraints.NONE;
354                 c.anchor = GridBagConstraints.CENTER;
355                 c.insets = i;
356                 c.weightx = 0;
357                 c.weighty = 0;
358
359                 c.gridx = 0;
360                 c.gridy = 3;
361                 c.gridwidth = 1;
362                 c.anchor = GridBagConstraints.CENTER;
363
364                 pane.add(lat, c);
365
366                 lon = new AltosUIMapPos(owner,
367                                         "Longitude:",
368                                         lon_hemi_names,
369                                         -97.73975);
370
371                 c.fill = GridBagConstraints.NONE;
372                 c.anchor = GridBagConstraints.CENTER;
373                 c.insets = i;
374                 c.weightx = 0;
375                 c.weighty = 0;
376
377                 c.gridx = 1;
378                 c.gridy = 3;
379                 c.gridwidth = 1;
380                 c.anchor = GridBagConstraints.CENTER;
381
382                 pane.add(lon, c);
383
384                 load_button = new JToggleButton("Load Map");
385                 load_button.addActionListener(this);
386                 load_button.setActionCommand("load");
387
388                 c.fill = GridBagConstraints.NONE;
389                 c.anchor = GridBagConstraints.CENTER;
390                 c.insets = i;
391                 c.weightx = 1;
392                 c.weighty = 0;
393
394                 c.gridx = 0;
395                 c.gridy = 4;
396                 c.gridwidth = 1;
397                 c.anchor = GridBagConstraints.CENTER;
398
399                 pane.add(load_button, c);
400
401                 close_button = new JButton("Close");
402                 close_button.addActionListener(this);
403                 close_button.setActionCommand("close");
404
405                 c.fill = GridBagConstraints.NONE;
406                 c.anchor = GridBagConstraints.CENTER;
407                 c.insets = i;
408                 c.weightx = 1;
409                 c.weighty = 0;
410
411                 c.gridx = 1;
412                 c.gridy = 4;
413                 c.gridwidth = 1;
414                 c.anchor = GridBagConstraints.CENTER;
415
416                 pane.add(close_button, c);
417
418                 JLabel  types_label = new JLabel("Map Types");
419                 c.gridx = 2;
420                 c.gridwidth = 2;
421                 c.gridy = 2;
422                 pane.add(types_label, c);
423
424                 c.gridwidth = 1;
425
426                 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
427                         maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
428                                                        type == AltosMap.maptype_hybrid);
429                         c.gridx = 2 + (type >> 1);
430                         c.fill = GridBagConstraints.HORIZONTAL;
431                         c.gridy = (type & 1) + 3;
432                         pane.add(maptypes[type], c);
433                 }
434
435                 JLabel  min_zoom_label = new JLabel("Minimum Zoom");
436                 c.gridx = 4;
437                 c.gridy = 2;
438                 pane.add(min_zoom_label, c);
439
440                 min_zoom = new JComboBox<Integer>(zooms);
441                 min_zoom.setSelectedItem(zooms[10]);
442                 min_zoom.setEditable(false);
443                 c.gridx = 5;
444                 c.gridy = 2;
445                 pane.add(min_zoom, c);
446
447                 JLabel  max_zoom_label = new JLabel("Maximum Zoom");
448                 c.gridx = 4;
449                 c.gridy = 3;
450                 pane.add(max_zoom_label, c);
451
452                 max_zoom = new JComboBox<Integer>(zooms);
453                 max_zoom.setSelectedItem(zooms[14]);
454                 max_zoom.setEditable(false);
455                 c.gridx = 5;
456                 c.gridy = 3;
457                 pane.add(max_zoom, c);
458
459                 JLabel radius_label = new JLabel(String.format("Map Radius (%s)",
460                                                                AltosPreferences.imperial_units() ? "miles" : "km"));
461                 c.gridx = 4;
462                 c.gridy = 4;
463                 pane.add(radius_label, c);
464
465                 Double[]        radii;
466                 Double          radius_default;
467
468                 if (AltosPreferences.imperial_units())
469                         radii = radius_mi;
470                 else
471                         radii = radius_km;
472                 radius = new JComboBox<Double>(radii);
473                 radius.setSelectedItem(radii[2]);
474                 radius.setEditable(true);
475                 c.gridx = 5;
476                 c.gridy = 4;
477                 pane.add(radius, c);
478
479                 pack();
480                 setLocationRelativeTo(owner);
481                 setVisible(true);
482         }
483 }