altosuilib: Use altoslib site list loader
[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_7;
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_7.*;
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, AltosMapTileListener, AltosLaunchSiteListener  {
122         AltosUIFrame    owner;
123         AltosUIMapNew   map;
124         AltosMapCache   cache;
125
126         AltosUIMapPos   lat;
127         AltosUIMapPos   lon;
128
129         JProgressBar    pbar;
130         int             pbar_max;
131         int             pbar_cur;
132
133         JLabel          site_list_label;
134         JComboBox<AltosLaunchSite>      site_list;
135
136         JToggleButton   load_button;
137         boolean         loading;
138         JButton         close_button;
139
140         JCheckBox[]     maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1];
141
142         JComboBox<Integer>      min_zoom;
143         JComboBox<Integer>      max_zoom;
144         JComboBox<Integer>      radius;
145
146         Integer[]               zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
147         Integer[]               radii = { 1, 2, 3, 4, 5 };
148
149         static final String[]   lat_hemi_names = { "N", "S" };
150         static final String[]   lon_hemi_names = { "E", "W" };
151
152         class updatePbar implements Runnable {
153                 String          s;
154
155                 public updatePbar(String in_s) {
156                         s = in_s;
157                 }
158
159                 public void run() {
160                         int     n = ++pbar_cur;
161
162                         pbar.setMaximum(pbar_max);
163                         pbar.setValue(n);
164                         pbar.setString(s);
165                 }
166         }
167
168         double  latitude, longitude;
169         int     min_z;
170         int     max_z;
171         int     cur_z;
172         int     all_types;
173         int     cur_type;
174         int     r;
175
176         int     tiles_per_layer;
177         int     tiles_loaded;
178         int     layers_total;
179         int     layers_loaded;
180
181
182         private void do_load() {
183                 tiles_loaded = 0;
184                 map.set_zoom(cur_z + AltosMap.default_zoom);
185                 map.set_maptype(cur_type);
186                 map.set_load_params(latitude, longitude, r, this);
187         }
188
189         private int next_type(int start) {
190                 int next_type;
191                 for (next_type = start;
192                      next_type <= AltosMap.maptype_terrain && (all_types & (1 << next_type)) == 0;
193                      next_type++)
194                         ;
195                 return next_type;
196         }
197
198         private void next_load() {
199                 int next_type = next_type(cur_type + 1);
200
201                 if (next_type > AltosMap.maptype_terrain) {
202                         if (cur_z == max_z) {
203                                 return;
204                         } else {
205                                 cur_z++;
206                         }
207                         next_type = next_type(0);
208                 }
209                 cur_type = next_type;
210                 do_load();
211         }
212
213         private void start_load() {
214                 cur_z = min_z;
215                 int ntype = 0;
216                 all_types = 0;
217                 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
218                         if (maptypes[t].isSelected()) {
219                                 all_types |= (1 << t);
220                                 ntype++;
221                         }
222                 if (ntype == 0) {
223                         all_types |= (1 << AltosMap.maptype_hybrid);
224                         ntype = 1;
225                 }
226
227                 cur_type = next_type(0);
228                 tiles_per_layer = (r * 2 + 1) * (r * 2 + 1);
229                 layers_total = (max_z - min_z + 1) * ntype;
230                 layers_loaded = 0;
231                 pbar_max = layers_total * tiles_per_layer;
232                 pbar_cur = 0;
233
234                 map.clear_marks();
235                 map.add_mark(latitude,longitude, AltosLib.ao_flight_boost);
236                 do_load();
237         }
238
239         /* AltosMapTileListener methods */
240
241         public synchronized void notify_tile(AltosMapTile tile, int status) {
242                 if (status == AltosMapTile.loading)
243                         return;
244
245                 SwingUtilities.invokeLater(new updatePbar(tile.store.file.toString()));
246                 ++tiles_loaded;
247                 if (tiles_loaded == tiles_per_layer) {
248                         ++layers_loaded;
249                         if (layers_loaded == layers_total) {
250                                 SwingUtilities.invokeLater(new Runnable() {
251                                                 public void run() {
252                                                         pbar.setValue(0);
253                                                         pbar.setString("");
254                                                         load_button.setSelected(false);
255                                                         loading = false;
256                                                 }
257                                         });
258                         } else {
259                                 SwingUtilities.invokeLater(new Runnable() {
260                                                 public void run() {
261                                                         next_load();
262                                                 }
263                                         });
264                         }
265                 }
266         }
267
268         public AltosMapCache cache() { return cache; }
269
270         public void itemStateChanged(ItemEvent e) {
271                 int             state = e.getStateChange();
272
273                 if (state == ItemEvent.SELECTED) {
274                         Object  o = e.getItem();
275                         if (o instanceof AltosLaunchSite) {
276                                 AltosLaunchSite site = (AltosLaunchSite) o;
277                                 lat.set_value(site.latitude);
278                                 lon.set_value(site.longitude);
279                         }
280                 }
281         }
282
283         public void actionPerformed(ActionEvent e) {
284                 String  cmd = e.getActionCommand();
285
286                 if (cmd.equals("close"))
287                         setVisible(false);
288
289                 if (cmd.equals("load")) {
290                         if (!loading) {
291                                 try {
292                                         latitude = lat.get_value();
293                                         longitude = lon.get_value();
294                                         min_z = (Integer) min_zoom.getSelectedItem();
295                                         max_z = (Integer) max_zoom.getSelectedItem();
296                                         if (max_z < min_z)
297                                                 max_z = min_z;
298                                         r = (Integer) radius.getSelectedItem();
299                                         loading = true;
300                                 } catch (ParseException pe) {
301                                         load_button.setSelected(false);
302                                 }
303                                 start_load();
304                         }
305                 }
306         }
307
308         public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
309                 SwingUtilities.invokeLater(new Runnable() {
310                                 public void run() {
311                                         int     i = 1;
312                                         for (AltosLaunchSite site : sites) {
313                                                 site_list.insertItemAt(site, i);
314                                                 i++;
315                                         }
316                                 }
317                         });
318         }
319
320         public AltosUIMapPreloadNew(AltosUIFrame in_owner) {
321                 owner = in_owner;
322
323                 Container               pane = getContentPane();
324                 GridBagConstraints      c = new GridBagConstraints();
325                 Insets                  i = new Insets(4,4,4,4);
326
327                 setTitle("AltOS Load Maps");
328
329                 pane.setLayout(new GridBagLayout());
330
331                 map = new AltosUIMapNew();
332                 cache = new AltosMapCache(map);
333
334                 c.fill = GridBagConstraints.BOTH;
335                 c.anchor = GridBagConstraints.CENTER;
336                 c.insets = i;
337                 c.weightx = 1;
338                 c.weighty = 1;
339
340                 c.gridx = 0;
341                 c.gridy = 0;
342                 c.gridwidth = 10;
343                 c.anchor = GridBagConstraints.CENTER;
344
345                 pane.add(map, c);
346
347                 pbar = new JProgressBar();
348                 pbar.setMinimum(0);
349                 pbar.setMaximum(1);
350                 pbar.setValue(0);
351                 pbar.setString("");
352                 pbar.setStringPainted(true);
353
354                 c.fill = GridBagConstraints.HORIZONTAL;
355                 c.anchor = GridBagConstraints.CENTER;
356                 c.insets = i;
357                 c.weightx = 1;
358                 c.weighty = 0;
359
360                 c.gridx = 0;
361                 c.gridy = 1;
362                 c.gridwidth = 10;
363
364                 pane.add(pbar, c);
365
366                 site_list_label = new JLabel ("Known Launch Sites:");
367
368                 c.fill = GridBagConstraints.NONE;
369                 c.anchor = GridBagConstraints.CENTER;
370                 c.insets = i;
371                 c.weightx = 1;
372                 c.weighty = 0;
373
374                 c.gridx = 0;
375                 c.gridy = 2;
376                 c.gridwidth = 1;
377
378                 pane.add(site_list_label, c);
379
380                 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
381                 site_list.addItemListener(this);
382
383                 new AltosLaunchSites(this);
384
385                 c.fill = GridBagConstraints.HORIZONTAL;
386                 c.anchor = GridBagConstraints.CENTER;
387                 c.insets = i;
388                 c.weightx = 1;
389                 c.weighty = 0;
390
391                 c.gridx = 1;
392                 c.gridy = 2;
393                 c.gridwidth = 1;
394
395                 pane.add(site_list, c);
396
397                 lat = new AltosUIMapPos(owner,
398                                         "Latitude:",
399                                         lat_hemi_names,
400                                         37.167833333);
401                 c.fill = GridBagConstraints.NONE;
402                 c.anchor = GridBagConstraints.CENTER;
403                 c.insets = i;
404                 c.weightx = 0;
405                 c.weighty = 0;
406
407                 c.gridx = 0;
408                 c.gridy = 3;
409                 c.gridwidth = 1;
410                 c.anchor = GridBagConstraints.CENTER;
411
412                 pane.add(lat, c);
413
414                 lon = new AltosUIMapPos(owner,
415                                         "Longitude:",
416                                         lon_hemi_names,
417                                         -97.73975);
418
419                 c.fill = GridBagConstraints.NONE;
420                 c.anchor = GridBagConstraints.CENTER;
421                 c.insets = i;
422                 c.weightx = 0;
423                 c.weighty = 0;
424
425                 c.gridx = 1;
426                 c.gridy = 3;
427                 c.gridwidth = 1;
428                 c.anchor = GridBagConstraints.CENTER;
429
430                 pane.add(lon, c);
431
432                 load_button = new JToggleButton("Load Map");
433                 load_button.addActionListener(this);
434                 load_button.setActionCommand("load");
435
436                 c.fill = GridBagConstraints.NONE;
437                 c.anchor = GridBagConstraints.CENTER;
438                 c.insets = i;
439                 c.weightx = 1;
440                 c.weighty = 0;
441
442                 c.gridx = 0;
443                 c.gridy = 4;
444                 c.gridwidth = 1;
445                 c.anchor = GridBagConstraints.CENTER;
446
447                 pane.add(load_button, c);
448
449                 close_button = new JButton("Close");
450                 close_button.addActionListener(this);
451                 close_button.setActionCommand("close");
452
453                 c.fill = GridBagConstraints.NONE;
454                 c.anchor = GridBagConstraints.CENTER;
455                 c.insets = i;
456                 c.weightx = 1;
457                 c.weighty = 0;
458
459                 c.gridx = 1;
460                 c.gridy = 4;
461                 c.gridwidth = 1;
462                 c.anchor = GridBagConstraints.CENTER;
463
464                 pane.add(close_button, c);
465
466                 JLabel  types_label = new JLabel("Map Types");
467                 c.gridx = 2;
468                 c.gridwidth = 2;
469                 c.gridy = 2;
470                 pane.add(types_label, c);
471
472                 c.gridwidth = 1;
473
474                 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
475                         maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
476                                                        type == AltosMap.maptype_hybrid);
477                         c.gridx = 2 + (type >> 1);
478                         c.fill = GridBagConstraints.HORIZONTAL;
479                         c.gridy = (type & 1) + 3;
480                         pane.add(maptypes[type], c);
481                 }
482
483                 JLabel  min_zoom_label = new JLabel("Minimum Zoom");
484                 c.gridx = 4;
485                 c.gridy = 2;
486                 pane.add(min_zoom_label, c);
487
488                 min_zoom = new JComboBox<Integer>(zooms);
489                 min_zoom.setSelectedItem(zooms[10]);
490                 min_zoom.setEditable(false);
491                 c.gridx = 5;
492                 c.gridy = 2;
493                 pane.add(min_zoom, c);
494
495                 JLabel  max_zoom_label = new JLabel("Maximum Zoom");
496                 c.gridx = 4;
497                 c.gridy = 3;
498                 pane.add(max_zoom_label, c);
499
500                 max_zoom = new JComboBox<Integer>(zooms);
501                 max_zoom.setSelectedItem(zooms[14]);
502                 max_zoom.setEditable(false);
503                 c.gridx = 5;
504                 c.gridy = 3;
505                 pane.add(max_zoom, c);
506
507                 JLabel radius_label = new JLabel("Tile Radius");
508                 c.gridx = 4;
509                 c.gridy = 4;
510                 pane.add(radius_label, c);
511
512                 radius = new JComboBox<Integer>(radii);
513                 radius.setSelectedItem(radii[4]);
514                 radius.setEditable(true);
515                 c.gridx = 5;
516                 c.gridy = 4;
517                 pane.add(radius, c);
518
519                 pack();
520                 setLocationRelativeTo(owner);
521                 setVisible(true);
522         }
523 }