altoslib: Allow map preloading to be aborted
[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         JButton         close_button;
135
136         JCheckBox[]     maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1];
137
138         JComboBox<Integer>      min_zoom;
139         JComboBox<Integer>      max_zoom;
140         JComboBox<Double>       radius;
141
142         Integer[]               zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
143
144         Double[]        radius_mi = { 1.0, 2.0, 5.0, 10.0, 20.0 };
145         Double          radius_def_mi = 5.0;
146         Double[]        radius_km = { 2.0, 5.0, 10.0, 20.0, 30.0 };
147         Double          radius_def_km = 10.0;
148
149         AltosMapLoader  loader;
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                 loader = null;
191                 SwingUtilities.invokeLater(new Runnable() {
192                                 public void run() {
193                                         pbar.setValue(0);
194                                         pbar.setString("");
195                                         load_button.setSelected(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                         if (loader != null)
232                                 loader.abort();
233                         setVisible(false);
234                 }
235
236                 if (cmd.equals("load")) {
237                         if (loader == null) {
238                                 try {
239                                         latitude = lat.get_value();
240                                         longitude = lon.get_value();
241                                         int min_z = (Integer) min_zoom.getSelectedItem();
242                                         int max_z = (Integer) max_zoom.getSelectedItem();
243                                         if (max_z < min_z)
244                                                 max_z = min_z;
245                                         Double r = (Double) radius.getSelectedItem();
246
247                                         if (AltosPreferences.imperial_units())
248                                                 r = AltosConvert.miles_to_meters(r);
249                                         else
250                                                 r = r * 1000;
251
252                                         loader = new AltosMapLoader(map.map, this,
253                                                                     latitude, longitude,
254                                                                     min_z, max_z, r, all_types());
255
256                                 } catch (ParseException pe) {
257                                         load_button.setSelected(false);
258                                 }
259                         }
260                 }
261         }
262
263         public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
264                 SwingUtilities.invokeLater(new Runnable() {
265                                 public void run() {
266                                         int     i = 1;
267                                         for (AltosLaunchSite site : sites) {
268                                                 site_list.insertItemAt(site, i);
269                                                 i++;
270                                         }
271                                 }
272                         });
273         }
274
275         public AltosUIMapPreloadNew(AltosUIFrame in_owner) {
276                 owner = in_owner;
277
278                 Container               pane = getContentPane();
279                 GridBagConstraints      c = new GridBagConstraints();
280                 Insets                  i = new Insets(4,4,4,4);
281
282                 setTitle("AltOS Load Maps");
283
284                 pane.setLayout(new GridBagLayout());
285
286                 map = new AltosUIMapNew();
287
288                 c.fill = GridBagConstraints.BOTH;
289                 c.anchor = GridBagConstraints.CENTER;
290                 c.insets = i;
291                 c.weightx = 1;
292                 c.weighty = 1;
293
294                 c.gridx = 0;
295                 c.gridy = 0;
296                 c.gridwidth = 10;
297                 c.anchor = GridBagConstraints.CENTER;
298
299                 pane.add(map, c);
300
301                 pbar = new JProgressBar();
302                 pbar.setMinimum(0);
303                 pbar.setMaximum(1);
304                 pbar.setValue(0);
305                 pbar.setString("");
306                 pbar.setStringPainted(true);
307
308                 c.fill = GridBagConstraints.HORIZONTAL;
309                 c.anchor = GridBagConstraints.CENTER;
310                 c.insets = i;
311                 c.weightx = 1;
312                 c.weighty = 0;
313
314                 c.gridx = 0;
315                 c.gridy = 1;
316                 c.gridwidth = 10;
317
318                 pane.add(pbar, c);
319
320                 site_list_label = new JLabel ("Known Launch Sites:");
321
322                 c.fill = GridBagConstraints.NONE;
323                 c.anchor = GridBagConstraints.CENTER;
324                 c.insets = i;
325                 c.weightx = 1;
326                 c.weighty = 0;
327
328                 c.gridx = 0;
329                 c.gridy = 2;
330                 c.gridwidth = 1;
331
332                 pane.add(site_list_label, c);
333
334                 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
335                 site_list.addItemListener(this);
336
337                 new AltosLaunchSites(this);
338
339                 c.fill = GridBagConstraints.HORIZONTAL;
340                 c.anchor = GridBagConstraints.CENTER;
341                 c.insets = i;
342                 c.weightx = 1;
343                 c.weighty = 0;
344
345                 c.gridx = 1;
346                 c.gridy = 2;
347                 c.gridwidth = 1;
348
349                 pane.add(site_list, c);
350
351                 lat = new AltosUIMapPos(owner,
352                                         "Latitude:",
353                                         lat_hemi_names,
354                                         37.167833333);
355                 c.fill = GridBagConstraints.NONE;
356                 c.anchor = GridBagConstraints.CENTER;
357                 c.insets = i;
358                 c.weightx = 0;
359                 c.weighty = 0;
360
361                 c.gridx = 0;
362                 c.gridy = 3;
363                 c.gridwidth = 1;
364                 c.anchor = GridBagConstraints.CENTER;
365
366                 pane.add(lat, c);
367
368                 lon = new AltosUIMapPos(owner,
369                                         "Longitude:",
370                                         lon_hemi_names,
371                                         -97.73975);
372
373                 c.fill = GridBagConstraints.NONE;
374                 c.anchor = GridBagConstraints.CENTER;
375                 c.insets = i;
376                 c.weightx = 0;
377                 c.weighty = 0;
378
379                 c.gridx = 1;
380                 c.gridy = 3;
381                 c.gridwidth = 1;
382                 c.anchor = GridBagConstraints.CENTER;
383
384                 pane.add(lon, c);
385
386                 load_button = new JToggleButton("Load Map");
387                 load_button.addActionListener(this);
388                 load_button.setActionCommand("load");
389
390                 c.fill = GridBagConstraints.NONE;
391                 c.anchor = GridBagConstraints.CENTER;
392                 c.insets = i;
393                 c.weightx = 1;
394                 c.weighty = 0;
395
396                 c.gridx = 0;
397                 c.gridy = 4;
398                 c.gridwidth = 1;
399                 c.anchor = GridBagConstraints.CENTER;
400
401                 pane.add(load_button, c);
402
403                 close_button = new JButton("Close");
404                 close_button.addActionListener(this);
405                 close_button.setActionCommand("close");
406
407                 c.fill = GridBagConstraints.NONE;
408                 c.anchor = GridBagConstraints.CENTER;
409                 c.insets = i;
410                 c.weightx = 1;
411                 c.weighty = 0;
412
413                 c.gridx = 1;
414                 c.gridy = 4;
415                 c.gridwidth = 1;
416                 c.anchor = GridBagConstraints.CENTER;
417
418                 pane.add(close_button, c);
419
420                 JLabel  types_label = new JLabel("Map Types");
421                 c.gridx = 2;
422                 c.gridwidth = 2;
423                 c.gridy = 2;
424                 pane.add(types_label, c);
425
426                 c.gridwidth = 1;
427
428                 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
429                         maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
430                                                        type == AltosMap.maptype_hybrid);
431                         c.gridx = 2 + (type >> 1);
432                         c.fill = GridBagConstraints.HORIZONTAL;
433                         c.gridy = (type & 1) + 3;
434                         pane.add(maptypes[type], c);
435                 }
436
437                 JLabel  min_zoom_label = new JLabel("Minimum Zoom");
438                 c.gridx = 4;
439                 c.gridy = 2;
440                 pane.add(min_zoom_label, c);
441
442                 min_zoom = new JComboBox<Integer>(zooms);
443                 min_zoom.setSelectedItem(zooms[10]);
444                 min_zoom.setEditable(false);
445                 c.gridx = 5;
446                 c.gridy = 2;
447                 pane.add(min_zoom, c);
448
449                 JLabel  max_zoom_label = new JLabel("Maximum Zoom");
450                 c.gridx = 4;
451                 c.gridy = 3;
452                 pane.add(max_zoom_label, c);
453
454                 max_zoom = new JComboBox<Integer>(zooms);
455                 max_zoom.setSelectedItem(zooms[14]);
456                 max_zoom.setEditable(false);
457                 c.gridx = 5;
458                 c.gridy = 3;
459                 pane.add(max_zoom, c);
460
461                 JLabel radius_label = new JLabel(String.format("Map Radius (%s)",
462                                                                AltosPreferences.imperial_units() ? "miles" : "km"));
463                 c.gridx = 4;
464                 c.gridy = 4;
465                 pane.add(radius_label, c);
466
467                 Double[]        radii;
468                 Double          radius_default;
469
470                 if (AltosPreferences.imperial_units())
471                         radii = radius_mi;
472                 else
473                         radii = radius_km;
474                 radius = new JComboBox<Double>(radii);
475                 radius.setSelectedItem(radii[2]);
476                 radius.setEditable(true);
477                 c.gridx = 5;
478                 c.gridy = 4;
479                 pane.add(radius, c);
480
481                 pack();
482                 setLocationRelativeTo(owner);
483                 setVisible(true);
484         }
485 }