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