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