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