altosui: Don't open command window when --graph is provided
[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; 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 implements ActionListener {
32         AltosUIMapPreload       preload;
33         AltosUIFrame    owner;
34         JLabel          label;
35         JComboBox       hemi;
36         JTextField      deg;
37         JLabel          deg_label;
38         JTextField      min;
39         JLabel          min_label;
40
41         /* ActionListener interface */
42         public void actionPerformed(ActionEvent e) {
43                 preload.center_map();
44         }
45
46         public void set_value(double new_value) {
47                 double  d, m;
48                 int     h;
49
50                 h = 0;
51                 if (new_value < 0) {
52                         h = 1;
53                         new_value = -new_value;
54                 }
55                 d = Math.floor(new_value);
56                 deg.setText(String.format("%3.0f", d));
57                 m = (new_value - d) * 60.0;
58                 min.setText(String.format("%7.4f", m));
59                 hemi.setSelectedIndex(h);
60         }
61
62         public double get_value() throws ParseException {
63                 int     h = hemi.getSelectedIndex();
64                 String  d_t = deg.getText();
65                 String  m_t = min.getText();
66                 double  d, m, v;
67                 try {
68                         d = AltosParse.parse_double_locale(d_t);
69                 } catch (ParseException pe) {
70                         JOptionPane.showMessageDialog(owner,
71                                                       String.format("Invalid degrees \"%s\"",
72                                                                     d_t),
73                                                       "Invalid number",
74                                                       JOptionPane.ERROR_MESSAGE);
75                         throw pe;
76                 }
77                 try {
78                         if (m_t.equals(""))
79                                 m = 0;
80                         else
81                                 m = AltosParse.parse_double_locale(m_t);
82                 } catch (ParseException pe) {
83                         JOptionPane.showMessageDialog(owner,
84                                                       String.format("Invalid minutes \"%s\"",
85                                                                     m_t),
86                                                       "Invalid number",
87                                                       JOptionPane.ERROR_MESSAGE);
88                         throw pe;
89                 }
90                 v = d + m/60.0;
91                 if (h == 1)
92                         v = -v;
93                 return v;
94         }
95
96         public AltosUIMapPos(AltosUIFrame in_owner,
97                              AltosUIMapPreload preload,
98                              String label_value,
99                              String[] hemi_names,
100                              double default_value) {
101                 super(BoxLayout.X_AXIS);
102                 owner = in_owner;
103                 this.preload = preload;
104                 label = new JLabel(label_value);
105                 hemi = new JComboBox<String>(hemi_names);
106                 hemi.setEditable(false);
107                 deg = new JTextField(5);
108                 deg.addActionListener(this);
109                 deg.setMinimumSize(deg.getPreferredSize());
110                 deg.setHorizontalAlignment(JTextField.RIGHT);
111                 deg_label = new JLabel("°");
112                 min = new JTextField(9);
113                 min.addActionListener(this);
114                 min.setMinimumSize(min.getPreferredSize());
115                 min_label = new JLabel("'");
116                 set_value(default_value);
117                 add(label);
118                 add(Box.createRigidArea(new Dimension(5, 0)));
119                 add(hemi);
120                 add(Box.createRigidArea(new Dimension(5, 0)));
121                 add(deg);
122                 add(Box.createRigidArea(new Dimension(5, 0)));
123                 add(deg_label);
124                 add(Box.createRigidArea(new Dimension(5, 0)));
125                 add(min);
126                 add(Box.createRigidArea(new Dimension(5, 0)));
127                 add(min_label);
128         }
129 }
130
131 public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, ItemListener, AltosLaunchSiteListener, AltosMapLoaderListener, AltosUnitsListener, AltosFontListener  {
132         AltosUIFrame    owner;
133         AltosUIMap      map;
134
135         AltosUIMapPos   lat;
136         AltosUIMapPos   lon;
137
138         JProgressBar    pbar;
139
140         JLabel          site_list_label;
141         JComboBox<AltosLaunchSite>      site_list;
142
143         JToggleButton   load_button;
144         JButton         close_button;
145
146         JCheckBox[]     maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1];
147
148         JComboBox<Integer>      min_zoom;
149         JComboBox<Integer>      max_zoom;
150         JLabel                  radius_label;
151         JComboBox<Double>       radius;
152         int scale = 1;
153
154         Integer[]               zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
155
156         Double[]        radius_mi = { 1.0, 2.0, 5.0, 10.0, 20.0 };
157         Double          radius_def_mi = 5.0;
158         Double[]        radius_km = { 2.0, 5.0, 10.0, 20.0, 30.0 };
159         Double          radius_def_km = 10.0;
160
161         AltosMapLoader  loader;
162
163         static final String[]   lat_hemi_names = { "N", "S" };
164         static final String[]   lon_hemi_names = { "E", "W" };
165
166         double  latitude, longitude;
167
168         long    loader_notify_time;
169
170         /* AltosMapLoaderListener interfaces */
171         public void loader_start(final int max) {
172                 loader_notify_time = System.currentTimeMillis();
173
174                 SwingUtilities.invokeLater(new Runnable() {
175                                 public void run() {
176                                         pbar.setMaximum(max);
177                                         pbar.setValue(0);
178                                         pbar.setString("");
179                                 }
180                         });
181         }
182
183         public void loader_notify(final int cur, final int max, final String name) {
184                 long    now = System.currentTimeMillis();
185
186                 if (now - loader_notify_time < 100)
187                         return;
188
189                 loader_notify_time = now;
190
191                 SwingUtilities.invokeLater(new Runnable() {
192                                 public void run() {
193                                         pbar.setValue(cur);
194                                         pbar.setString(name);
195                                 }
196                         });
197         }
198
199         public void loader_done(int max) {
200                 loader = null;
201                 SwingUtilities.invokeLater(new Runnable() {
202                                 public void run() {
203                                         pbar.setValue(0);
204                                         pbar.setString("");
205                                         load_button.setSelected(false);
206                                 }
207                         });
208         }
209
210         public void debug(String format, Object ... arguments) {
211                 if (AltosSerial.debug)
212                         System.out.printf(format, arguments);
213         }
214
215
216         private int all_types() {
217                 int all_types = 0;
218                 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
219                         if (maptypes[t].isSelected())
220                                 all_types |= (1 << t);
221                 return all_types;
222         }
223
224         void center_map(double latitude, double longitude) {
225                 map.map.centre(new AltosLatLon(latitude, longitude));
226                 map.clear_marks();
227                 map.add_mark(latitude, longitude, AltosLib.ao_flight_boost);
228         }
229
230         void center_map() {
231                 try {
232                         center_map(lat.get_value(), lon.get_value());
233                 } catch (ParseException pe) {
234                 }
235         }
236
237         public void itemStateChanged(ItemEvent e) {
238                 int             state = e.getStateChange();
239
240                 if (state == ItemEvent.SELECTED) {
241                         Object  o = e.getItem();
242                         if (o instanceof AltosLaunchSite) {
243                                 AltosLaunchSite site = (AltosLaunchSite) o;
244                                 lat.set_value(site.latitude);
245                                 lon.set_value(site.longitude);
246                                 center_map(site.latitude, site.longitude);
247                         }
248                 }
249         }
250
251         public void actionPerformed(ActionEvent e) {
252                 String  cmd = e.getActionCommand();
253
254                 if (cmd.equals("close")) {
255                         if (loader != null)
256                                 loader.abort();
257                         setVisible(false);
258                 }
259
260                 if (cmd.equals("load")) {
261                         if (loader == null) {
262                                 try {
263                                         latitude = lat.get_value();
264                                         longitude = lon.get_value();
265                                         int min_z = (Integer) min_zoom.getSelectedItem();
266                                         int max_z = (Integer) max_zoom.getSelectedItem();
267                                         if (max_z < min_z)
268                                                 max_z = min_z;
269                                         Double r = (Double) radius.getSelectedItem();
270
271                                         if (AltosPreferences.imperial_units())
272                                                 r = AltosConvert.miles_to_meters(r);
273                                         else
274                                                 r = r * 1000;
275
276                                         center_map(latitude, longitude);
277
278                                         loader = new AltosMapLoader(this,
279                                                                     latitude, longitude,
280                                                                     min_z, max_z, r,
281                                                                     all_types(), scale);
282
283                                 } catch (ParseException pe) {
284                                         load_button.setSelected(false);
285                                 }
286                         }
287                 }
288         }
289
290         public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
291                 SwingUtilities.invokeLater(new Runnable() {
292                                 public void run() {
293                                         int     i = 1;
294                                         for (AltosLaunchSite site : sites) {
295                                                 site_list.insertItemAt(site, i);
296                                                 i++;
297                                         }
298                                 }
299                         });
300         }
301
302         private void set_radius_values() {
303                 radius_label.setText(String.format("Map Radius (%s)",
304                                                    AltosPreferences.imperial_units() ? "mi" : "km"));
305
306                 Double[]        radii;
307
308                 if (AltosPreferences.imperial_units())
309                         radii = radius_mi;
310                 else
311                         radii = radius_km;
312
313                 radius.removeAllItems();
314                 for (Double r : radii) {
315                         radius.addItem(r);
316                 }
317                 radius.setSelectedItem(radii[2]);
318                 radius.setMaximumRowCount(radii.length);
319         }
320
321         public void units_changed(boolean imperial_units) {
322                 map.units_changed(imperial_units);
323                 set_radius_values();
324         }
325
326         public void font_size_changed(int font_size) {
327                 map.font_size_changed(font_size);
328         }
329
330         public AltosUIMapPreload(AltosUIFrame in_owner) {
331                 owner = in_owner;
332
333                 Container               pane = getContentPane();
334                 GridBagConstraints      c = new GridBagConstraints();
335                 Insets                  i = new Insets(4,4,4,4);
336
337                 setTitle("AltOS Load Maps");
338
339                 pane.setLayout(new GridBagLayout());
340
341                 addWindowListener(new WindowAdapter() {
342                                 @Override
343                                 public void windowClosing(WindowEvent e) {
344                                         AltosUIPreferences.unregister_font_listener(AltosUIMapPreload.this);
345                                         AltosPreferences.unregister_units_listener(AltosUIMapPreload.this);
346                                 }
347                         });
348
349
350                 AltosPreferences.register_units_listener(this);
351                 AltosUIPreferences.register_font_listener(this);
352
353                 map = new AltosUIMap();
354
355                 c.fill = GridBagConstraints.BOTH;
356                 c.anchor = GridBagConstraints.CENTER;
357                 c.insets = i;
358                 c.weightx = 1;
359                 c.weighty = 1;
360
361                 c.gridx = 0;
362                 c.gridy = 0;
363                 c.gridwidth = 10;
364                 c.anchor = GridBagConstraints.CENTER;
365
366                 pane.add(map, c);
367
368                 pbar = new JProgressBar();
369                 pbar.setMinimum(0);
370                 pbar.setMaximum(1);
371                 pbar.setValue(0);
372                 pbar.setString("");
373                 pbar.setStringPainted(true);
374
375                 c.fill = GridBagConstraints.HORIZONTAL;
376                 c.anchor = GridBagConstraints.CENTER;
377                 c.insets = i;
378                 c.weightx = 1;
379                 c.weighty = 0;
380
381                 c.gridx = 0;
382                 c.gridy = 1;
383                 c.gridwidth = 10;
384
385                 pane.add(pbar, c);
386
387                 site_list_label = new JLabel ("Known Launch Sites:");
388
389                 c.fill = GridBagConstraints.NONE;
390                 c.anchor = GridBagConstraints.CENTER;
391                 c.insets = i;
392                 c.weightx = 1;
393                 c.weighty = 0;
394
395                 c.gridx = 0;
396                 c.gridy = 2;
397                 c.gridwidth = 1;
398
399                 pane.add(site_list_label, c);
400
401                 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
402                 site_list.addItemListener(this);
403
404                 new AltosLaunchSites(this);
405
406                 c.fill = GridBagConstraints.HORIZONTAL;
407                 c.anchor = GridBagConstraints.CENTER;
408                 c.insets = i;
409                 c.weightx = 1;
410                 c.weighty = 0;
411
412                 c.gridx = 1;
413                 c.gridy = 2;
414                 c.gridwidth = 1;
415
416                 pane.add(site_list, c);
417
418                 lat = new AltosUIMapPos(owner, this,
419                                         "Latitude:",
420                                         lat_hemi_names,
421                                         37.167833333);
422                 c.fill = GridBagConstraints.NONE;
423                 c.anchor = GridBagConstraints.CENTER;
424                 c.insets = i;
425                 c.weightx = 0;
426                 c.weighty = 0;
427
428                 c.gridx = 0;
429                 c.gridy = 3;
430                 c.gridwidth = 1;
431                 c.anchor = GridBagConstraints.CENTER;
432
433                 pane.add(lat, c);
434
435                 lon = new AltosUIMapPos(owner, this,
436                                         "Longitude:",
437                                         lon_hemi_names,
438                                         -97.73975);
439
440                 c.fill = GridBagConstraints.NONE;
441                 c.anchor = GridBagConstraints.CENTER;
442                 c.insets = i;
443                 c.weightx = 0;
444                 c.weighty = 0;
445
446                 c.gridx = 1;
447                 c.gridy = 3;
448                 c.gridwidth = 1;
449                 c.anchor = GridBagConstraints.CENTER;
450
451                 pane.add(lon, c);
452
453                 load_button = new JToggleButton("Load Map");
454                 load_button.addActionListener(this);
455                 load_button.setActionCommand("load");
456
457                 c.fill = GridBagConstraints.NONE;
458                 c.anchor = GridBagConstraints.CENTER;
459                 c.insets = i;
460                 c.weightx = 1;
461                 c.weighty = 0;
462
463                 c.gridx = 0;
464                 c.gridy = 4;
465                 c.gridwidth = 1;
466                 c.anchor = GridBagConstraints.CENTER;
467
468                 pane.add(load_button, c);
469
470                 close_button = new JButton("Close");
471                 close_button.addActionListener(this);
472                 close_button.setActionCommand("close");
473
474                 c.fill = GridBagConstraints.NONE;
475                 c.anchor = GridBagConstraints.CENTER;
476                 c.insets = i;
477                 c.weightx = 1;
478                 c.weighty = 0;
479
480                 c.gridx = 1;
481                 c.gridy = 4;
482                 c.gridwidth = 1;
483                 c.anchor = GridBagConstraints.CENTER;
484
485                 pane.add(close_button, c);
486
487                 JLabel  types_label = new JLabel("Map Types");
488                 c.gridx = 2;
489                 c.gridwidth = 2;
490                 c.gridy = 2;
491                 pane.add(types_label, c);
492
493                 c.gridwidth = 1;
494
495                 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
496                         maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
497                                                        type == AltosMap.maptype_hybrid);
498                         c.gridx = 2 + (type >> 1);
499                         c.fill = GridBagConstraints.HORIZONTAL;
500                         c.gridy = (type & 1) + 3;
501                         pane.add(maptypes[type], c);
502                 }
503
504                 JLabel  min_zoom_label = new JLabel("Minimum Zoom");
505                 c.gridx = 4;
506                 c.gridy = 2;
507                 pane.add(min_zoom_label, c);
508
509                 min_zoom = new JComboBox<Integer>(zooms);
510                 min_zoom.setSelectedItem(zooms[10]);
511                 min_zoom.setEditable(false);
512                 c.gridx = 5;
513                 c.gridy = 2;
514                 pane.add(min_zoom, c);
515
516                 JLabel  max_zoom_label = new JLabel("Maximum Zoom");
517                 c.gridx = 4;
518                 c.gridy = 3;
519                 pane.add(max_zoom_label, c);
520
521                 max_zoom = new JComboBox<Integer>(zooms);
522                 max_zoom.setSelectedItem(zooms[14]);
523                 max_zoom.setEditable(false);
524                 c.gridx = 5;
525                 c.gridy = 3;
526                 pane.add(max_zoom, c);
527
528                 radius_label = new JLabel();
529
530                 c.gridx = 4;
531                 c.gridy = 4;
532                 pane.add(radius_label, c);
533
534                 radius = new JComboBox<Double>();
535                 radius.setEditable(true);
536                 c.gridx = 5;
537                 c.gridy = 4;
538                 pane.add(radius, c);
539
540                 set_radius_values();
541
542                 pack();
543                 setLocationRelativeTo(owner);
544                 setVisible(true);
545         }
546 }