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