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