36b32c85337c6d8a190a152ee1f4d0208f8b5ca0
[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_13;
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_13.*;
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         JComboBox<AltosLaunchSite>      site_list;
143
144         JToggleButton   load_button;
145         JButton         close_button;
146
147         JCheckBox[]     maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1];
148
149         JComboBox<Integer>      min_zoom;
150         JComboBox<Integer>      max_zoom;
151         JLabel                  radius_label;
152         JComboBox<Double>       radius;
153         int scale = 1;
154
155         Integer[]               zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
156
157         Double[]        radius_mi = { 1.0, 2.0, 5.0, 10.0, 20.0 };
158         Double          radius_def_mi = 5.0;
159         Double[]        radius_km = { 2.0, 5.0, 10.0, 20.0, 30.0 };
160         Double          radius_def_km = 10.0;
161
162         AltosMapLoader  loader;
163
164         static final String[]   lat_hemi_names = { "N", "S" };
165         static final String[]   lon_hemi_names = { "E", "W" };
166
167         double  latitude, longitude;
168
169         long    loader_notify_time;
170
171         /* AltosMapLoaderListener interfaces */
172         public void loader_start(final int max) {
173                 loader_notify_time = System.currentTimeMillis();
174
175                 SwingUtilities.invokeLater(new Runnable() {
176                                 public void run() {
177                                         pbar.setMaximum(max);
178                                         pbar.setValue(0);
179                                         pbar.setString("");
180                                 }
181                         });
182         }
183
184         public void loader_notify(final int cur, final int max, final String name) {
185                 long    now = System.currentTimeMillis();
186
187                 if (now - loader_notify_time < 100)
188                         return;
189
190                 loader_notify_time = now;
191
192                 SwingUtilities.invokeLater(new Runnable() {
193                                 public void run() {
194                                         pbar.setValue(cur);
195                                         pbar.setString(name);
196                                 }
197                         });
198         }
199
200         public void loader_done(int max) {
201                 loader = null;
202                 SwingUtilities.invokeLater(new Runnable() {
203                                 public void run() {
204                                         pbar.setValue(0);
205                                         pbar.setString("");
206                                         load_button.setSelected(false);
207                                 }
208                         });
209         }
210
211         public void debug(String format, Object ... arguments) {
212                 if (AltosSerial.debug)
213                         System.out.printf(format, arguments);
214         }
215
216
217         private int all_types() {
218                 int all_types = 0;
219                 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
220                         if (maptypes[t].isSelected())
221                                 all_types |= (1 << t);
222                 return all_types;
223         }
224
225         void center_map(double latitude, double longitude) {
226                 map.map.centre(new AltosLatLon(latitude, longitude));
227                 map.clear_marks();
228                 map.add_mark(latitude, longitude, AltosLib.ao_flight_boost);
229         }
230
231         void center_map() {
232                 try {
233                         center_map(lat.get_value(), lon.get_value());
234                 } catch (ParseException pe) {
235                 }
236         }
237
238         public void itemStateChanged(ItemEvent e) {
239                 int             state = e.getStateChange();
240
241                 if (state == ItemEvent.SELECTED) {
242                         Object  o = e.getItem();
243                         if (o instanceof AltosLaunchSite) {
244                                 AltosLaunchSite site = (AltosLaunchSite) o;
245                                 lat.set_value(site.latitude);
246                                 lon.set_value(site.longitude);
247                                 center_map(site.latitude, site.longitude);
248                         }
249                 }
250         }
251
252         public void actionPerformed(ActionEvent e) {
253                 String  cmd = e.getActionCommand();
254
255                 if (cmd.equals("close")) {
256                         if (loader != null)
257                                 loader.abort();
258                         setVisible(false);
259                 }
260
261                 if (cmd.equals("load")) {
262                         if (loader == null) {
263                                 try {
264                                         latitude = lat.get_value();
265                                         longitude = lon.get_value();
266                                         int min_z = (Integer) min_zoom.getSelectedItem();
267                                         int max_z = (Integer) max_zoom.getSelectedItem();
268                                         if (max_z < min_z)
269                                                 max_z = min_z;
270                                         Double r = (Double) radius.getSelectedItem();
271
272                                         if (AltosPreferences.imperial_units())
273                                                 r = AltosConvert.miles_to_meters(r);
274                                         else
275                                                 r = r * 1000;
276
277                                         center_map(latitude, longitude);
278
279                                         loader = new AltosMapLoader(this,
280                                                                     latitude, longitude,
281                                                                     min_z, max_z, r,
282                                                                     all_types(), scale);
283
284                                 } catch (ParseException pe) {
285                                         load_button.setSelected(false);
286                                 }
287                         }
288                 }
289         }
290
291         public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
292                 SwingUtilities.invokeLater(new Runnable() {
293                                 public void run() {
294                                         int     i = 1;
295                                         for (AltosLaunchSite site : sites) {
296                                                 site_list.insertItemAt(site, i);
297                                                 i++;
298                                         }
299                                 }
300                         });
301         }
302
303         private void set_radius_values() {
304                 radius_label.setText(String.format("Map Radius (%s)",
305                                                    AltosPreferences.imperial_units() ? "mi" : "km"));
306
307                 Double[]        radii;
308
309                 if (AltosPreferences.imperial_units())
310                         radii = radius_mi;
311                 else
312                         radii = radius_km;
313
314                 radius.removeAllItems();
315                 for (Double r : radii) {
316                         radius.addItem(r);
317                 }
318                 radius.setSelectedItem(radii[2]);
319                 radius.setMaximumRowCount(radii.length);
320         }
321
322         public void units_changed(boolean imperial_units) {
323                 map.units_changed(imperial_units);
324                 set_radius_values();
325         }
326
327         public void font_size_changed(int font_size) {
328                 map.font_size_changed(font_size);
329         }
330
331         public AltosUIMapPreload(AltosUIFrame in_owner) {
332                 owner = in_owner;
333
334                 Container               pane = getContentPane();
335                 GridBagConstraints      c = new GridBagConstraints();
336                 Insets                  i = new Insets(4,4,4,4);
337
338                 setTitle("AltOS Load Maps");
339
340                 pane.setLayout(new GridBagLayout());
341
342                 addWindowListener(new WindowAdapter() {
343                                 @Override
344                                 public void windowClosing(WindowEvent e) {
345                                         AltosUIPreferences.unregister_font_listener(AltosUIMapPreload.this);
346                                         AltosPreferences.unregister_units_listener(AltosUIMapPreload.this);
347                                 }
348                         });
349
350
351                 AltosPreferences.register_units_listener(this);
352                 AltosUIPreferences.register_font_listener(this);
353
354                 map = new AltosUIMap();
355
356                 c.fill = GridBagConstraints.BOTH;
357                 c.anchor = GridBagConstraints.CENTER;
358                 c.insets = i;
359                 c.weightx = 1;
360                 c.weighty = 1;
361
362                 c.gridx = 0;
363                 c.gridy = 0;
364                 c.gridwidth = 10;
365                 c.anchor = GridBagConstraints.CENTER;
366
367                 pane.add(map, c);
368
369                 pbar = new JProgressBar();
370                 pbar.setMinimum(0);
371                 pbar.setMaximum(1);
372                 pbar.setValue(0);
373                 pbar.setString("");
374                 pbar.setStringPainted(true);
375
376                 c.fill = GridBagConstraints.HORIZONTAL;
377                 c.anchor = GridBagConstraints.CENTER;
378                 c.insets = i;
379                 c.weightx = 1;
380                 c.weighty = 0;
381
382                 c.gridx = 0;
383                 c.gridy = 1;
384                 c.gridwidth = 10;
385
386                 pane.add(pbar, c);
387
388                 site_list_label = new JLabel ("Known Launch Sites:");
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 = 2;
398                 c.gridwidth = 1;
399
400                 pane.add(site_list_label, c);
401
402                 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
403                 site_list.addItemListener(this);
404
405                 new AltosLaunchSites(this);
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 = 1;
414                 c.gridy = 2;
415                 c.gridwidth = 1;
416
417                 pane.add(site_list, c);
418
419                 lat = new AltosUIMapPos(owner, this,
420                                         "Latitude:",
421                                         lat_hemi_names,
422                                         37.167833333);
423                 c.fill = GridBagConstraints.NONE;
424                 c.anchor = GridBagConstraints.CENTER;
425                 c.insets = i;
426                 c.weightx = 0;
427                 c.weighty = 0;
428
429                 c.gridx = 0;
430                 c.gridy = 3;
431                 c.gridwidth = 1;
432                 c.anchor = GridBagConstraints.CENTER;
433
434                 pane.add(lat, c);
435
436                 lon = new AltosUIMapPos(owner, this,
437                                         "Longitude:",
438                                         lon_hemi_names,
439                                         -97.73975);
440
441                 c.fill = GridBagConstraints.NONE;
442                 c.anchor = GridBagConstraints.CENTER;
443                 c.insets = i;
444                 c.weightx = 0;
445                 c.weighty = 0;
446
447                 c.gridx = 1;
448                 c.gridy = 3;
449                 c.gridwidth = 1;
450                 c.anchor = GridBagConstraints.CENTER;
451
452                 pane.add(lon, c);
453
454                 load_button = new JToggleButton("Load Map");
455                 load_button.addActionListener(this);
456                 load_button.setActionCommand("load");
457
458                 c.fill = GridBagConstraints.NONE;
459                 c.anchor = GridBagConstraints.CENTER;
460                 c.insets = i;
461                 c.weightx = 1;
462                 c.weighty = 0;
463
464                 c.gridx = 0;
465                 c.gridy = 4;
466                 c.gridwidth = 1;
467                 c.anchor = GridBagConstraints.CENTER;
468
469                 pane.add(load_button, c);
470
471                 close_button = new JButton("Close");
472                 close_button.addActionListener(this);
473                 close_button.setActionCommand("close");
474
475                 c.fill = GridBagConstraints.NONE;
476                 c.anchor = GridBagConstraints.CENTER;
477                 c.insets = i;
478                 c.weightx = 1;
479                 c.weighty = 0;
480
481                 c.gridx = 1;
482                 c.gridy = 4;
483                 c.gridwidth = 1;
484                 c.anchor = GridBagConstraints.CENTER;
485
486                 pane.add(close_button, c);
487
488                 JLabel  types_label = new JLabel("Map Types");
489                 c.gridx = 2;
490                 c.gridwidth = 2;
491                 c.gridy = 2;
492                 pane.add(types_label, c);
493
494                 c.gridwidth = 1;
495
496                 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
497                         maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
498                                                        type == AltosMap.maptype_hybrid);
499                         c.gridx = 2 + (type >> 1);
500                         c.fill = GridBagConstraints.HORIZONTAL;
501                         c.gridy = (type & 1) + 3;
502                         pane.add(maptypes[type], c);
503                 }
504
505                 JLabel  min_zoom_label = new JLabel("Minimum Zoom");
506                 c.gridx = 4;
507                 c.gridy = 2;
508                 pane.add(min_zoom_label, c);
509
510                 min_zoom = new JComboBox<Integer>(zooms);
511                 min_zoom.setSelectedItem(zooms[10]);
512                 min_zoom.setEditable(false);
513                 c.gridx = 5;
514                 c.gridy = 2;
515                 pane.add(min_zoom, c);
516
517                 JLabel  max_zoom_label = new JLabel("Maximum Zoom");
518                 c.gridx = 4;
519                 c.gridy = 3;
520                 pane.add(max_zoom_label, c);
521
522                 max_zoom = new JComboBox<Integer>(zooms);
523                 max_zoom.setSelectedItem(zooms[14]);
524                 max_zoom.setEditable(false);
525                 c.gridx = 5;
526                 c.gridy = 3;
527                 pane.add(max_zoom, c);
528
529                 radius_label = new JLabel();
530
531                 c.gridx = 4;
532                 c.gridy = 4;
533                 pane.add(radius_label, c);
534
535                 radius = new JComboBox<Double>();
536                 radius.setEditable(true);
537                 c.gridx = 5;
538                 c.gridy = 4;
539                 pane.add(radius, c);
540
541                 set_radius_values();
542
543                 pack();
544                 setLocationRelativeTo(owner);
545                 setVisible(true);
546         }
547 }