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