56066d70b25764a3ad0fb661112172123c82caa9
[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_2;
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_4.*;
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 NumberFormatException {
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 = Double.parseDouble(d_t);
63                 } catch (NumberFormatException ne) {
64                         JOptionPane.showMessageDialog(owner,
65                                                       String.format("Invalid degrees \"%s\"",
66                                                                     d_t),
67                                                       "Invalid number",
68                                                       JOptionPane.ERROR_MESSAGE);
69                         throw ne;
70                 }
71                 try {
72                         if (m_t.equals(""))
73                                 m = 0;
74                         else
75                                 m = Double.parseDouble(m_t);
76                 } catch (NumberFormatException ne) {
77                         JOptionPane.showMessageDialog(owner,
78                                                       String.format("Invalid minutes \"%s\"",
79                                                                     m_t),
80                                                       "Invalid number",
81                                                       JOptionPane.ERROR_MESSAGE);
82                         throw ne;
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 class AltosUISite {
122         String  name;
123         double  latitude;
124         double  longitude;
125
126         public String toString() {
127                 return name;
128         }
129
130         public AltosUISite(String in_name, double in_latitude, double in_longitude) {
131                 name = in_name;
132                 latitude = in_latitude;
133                 longitude = in_longitude;
134         }
135
136         public AltosUISite(String line) throws ParseException {
137                 String[]        elements = line.split(":");
138
139                 if (elements.length < 3)
140                         throw new ParseException(String.format("Invalid site line %s", line), 0);
141
142                 name = elements[0];
143
144                 try {
145                         latitude = Double.parseDouble(elements[1]);
146                         longitude = Double.parseDouble(elements[2]);
147                 } catch (NumberFormatException ne) {
148                         throw new ParseException(String.format("Invalid site line %s", line), 0);
149                 }
150         }
151 }
152
153 class AltosUISites extends Thread {
154         AltosUIMapPreload       preload;
155         URL                     url;
156         LinkedList<AltosUISite> sites;
157
158         void notify_complete() {
159                 SwingUtilities.invokeLater(new Runnable() {
160                                 public void run() {
161                                         preload.set_sites();
162                                 }
163                         });
164         }
165
166         void add(AltosUISite site) {
167                 sites.add(site);
168         }
169
170         void add(String line) {
171                 try {
172                         add(new AltosUISite(line));
173                 } catch (ParseException pe) {
174                 }
175         }
176
177         public void run() {
178                 try {
179                         URLConnection uc = url.openConnection();
180                         //int length = uc.getContentLength();
181
182                         InputStreamReader in_stream = new InputStreamReader(uc.getInputStream(), AltosLib.unicode_set);
183                         BufferedReader in = new BufferedReader(in_stream);
184
185                         for (;;) {
186                                 String line = in.readLine();
187                                 if (line == null)
188                                         break;
189                                 add(line);
190                         }
191                 } catch (IOException e) {
192                 } finally {
193                         notify_complete();
194                 }
195         }
196
197         public AltosUISites(AltosUIMapPreload in_preload) {
198                 sites = new LinkedList<AltosUISite>();
199                 preload = in_preload;
200                 try {
201                         url = new URL(AltosLib.launch_sites_url);
202                 } catch (java.net.MalformedURLException e) {
203                         notify_complete();
204                 }
205                 start();
206         }
207 }
208
209 public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, ItemListener, AltosUIMapTileListener {
210         AltosUIFrame    owner;
211         AltosUIMap      map;
212         AltosUIMapCache cache = new AltosUIMapCache();
213
214         AltosUIMapPos   lat;
215         AltosUIMapPos   lon;
216
217         JProgressBar    pbar;
218         int             pbar_max;
219         int             pbar_cur;
220
221         AltosUISites    sites;
222         JLabel          site_list_label;
223         JComboBox<AltosUISite>  site_list;
224
225         JToggleButton   load_button;
226         boolean         loading;
227         JButton         close_button;
228
229         JCheckBox[]     maptypes = new JCheckBox[AltosUIMap.maptype_terrain - AltosUIMap.maptype_hybrid + 1];
230
231         JComboBox<Integer>      min_zoom;
232         JComboBox<Integer>      max_zoom;
233         JComboBox<Integer>      radius;
234
235         Integer[]               zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
236         Integer[]               radii = { 1, 2, 3, 4, 5 };
237
238         static final String[]   lat_hemi_names = { "N", "S" };
239         static final String[]   lon_hemi_names = { "E", "W" };
240
241         class updatePbar implements Runnable {
242                 String          s;
243
244                 public updatePbar(String in_s) {
245                         s = in_s;
246                 }
247
248                 public void run() {
249                         int     n = ++pbar_cur;
250
251                         pbar.setMaximum(pbar_max);
252                         pbar.setValue(n);
253                         pbar.setString(s);
254                 }
255         }
256
257         double  latitude, longitude;
258         int     min_z;
259         int     max_z;
260         int     cur_z;
261         int     all_types;
262         int     cur_type;
263         int     r;
264
265         int     tiles_per_layer;
266         int     tiles_loaded;
267         int     layers_total;
268         int     layers_loaded;
269
270
271         private void do_load() {
272                 tiles_loaded = 0;
273                 map.set_zoom(cur_z + AltosUIMapView.default_zoom);
274                 map.set_maptype(cur_type);
275                 map.set_load_params(latitude, longitude, r, this);
276         }
277
278         private int next_type(int start) {
279                 int next_type;
280                 for (next_type = start;
281                      next_type <= AltosUIMap.maptype_terrain && (all_types & (1 << next_type)) == 0;
282                      next_type++)
283                         ;
284                 return next_type;
285         }
286
287         private void next_load() {
288                 int next_type = next_type(cur_type + 1);
289
290                 if (next_type > AltosUIMap.maptype_terrain) {
291                         if (cur_z == max_z) {
292                                 return;
293                         } else {
294                                 cur_z++;
295                         }
296                         next_type = next_type(0);
297                 }
298                 cur_type = next_type;
299                 do_load();
300         }
301
302         private void start_load() {
303                 cur_z = min_z;
304                 int ntype = 0;
305                 all_types = 0;
306                 for (int t = AltosUIMap.maptype_hybrid; t <= AltosUIMap.maptype_terrain; t++)
307                         if (maptypes[t].isSelected()) {
308                                 all_types |= (1 << t);
309                                 ntype++;
310                         }
311                 if (ntype == 0) {
312                         all_types |= (1 << AltosUIMap.maptype_hybrid);
313                         ntype = 1;
314                 }
315
316                 cur_type = next_type(0);
317                 tiles_per_layer = (r * 2 + 1) * (r * 2 + 1);
318                 layers_total = (max_z - min_z + 1) * ntype;
319                 layers_loaded = 0;
320                 pbar_max = layers_total * tiles_per_layer;
321                 pbar_cur = 0;
322
323                 map.clear_marks();
324                 map.add_mark(latitude,longitude, AltosLib.ao_flight_boost);
325                 do_load();
326         }
327
328         /* AltosUIMapTileListener methods */
329
330         public synchronized void notify_tile(AltosUIMapTile tile, int status) {
331                 if (status == AltosUIMapStore.loading)
332                         return;
333
334                 SwingUtilities.invokeLater(new updatePbar(tile.store.file.toString()));
335                 ++tiles_loaded;
336                 if (tiles_loaded == tiles_per_layer) {
337                         ++layers_loaded;
338                         if (layers_loaded == layers_total) {
339                                 SwingUtilities.invokeLater(new Runnable() {
340                                                 public void run() {
341                                                         pbar.setValue(0);
342                                                         pbar.setString("");
343                                                         load_button.setSelected(false);
344                                                         loading = false;
345                                                 }
346                                         });
347                         } else {
348                                 SwingUtilities.invokeLater(new Runnable() {
349                                                 public void run() {
350                                                         next_load();
351                                                 }
352                                         });
353                         }
354                 }
355         }
356
357         public AltosUIMapCache cache() { return cache; }
358
359         public void set_sites() {
360                 int     i = 1;
361                 for (AltosUISite site : sites.sites) {
362                         site_list.insertItemAt(site, i);
363                         i++;
364                 }
365         }
366
367         public void itemStateChanged(ItemEvent e) {
368                 int             state = e.getStateChange();
369
370                 if (state == ItemEvent.SELECTED) {
371                         Object  o = e.getItem();
372                         if (o instanceof AltosUISite) {
373                                 AltosUISite     site = (AltosUISite) o;
374                                 lat.set_value(site.latitude);
375                                 lon.set_value(site.longitude);
376                         }
377                 }
378         }
379
380         public void actionPerformed(ActionEvent e) {
381                 String  cmd = e.getActionCommand();
382
383                 if (cmd.equals("close"))
384                         setVisible(false);
385
386                 if (cmd.equals("load")) {
387                         if (!loading) {
388                                 try {
389                                         latitude = lat.get_value();
390                                         longitude = lon.get_value();
391                                         min_z = (Integer) min_zoom.getSelectedItem();
392                                         max_z = (Integer) max_zoom.getSelectedItem();
393                                         if (max_z < min_z)
394                                                 max_z = min_z;
395                                         r = (Integer) radius.getSelectedItem();
396                                         loading = true;
397                                 } catch (NumberFormatException ne) {
398                                         load_button.setSelected(false);
399                                 }
400                                 start_load();
401                         }
402                 }
403         }
404
405         public AltosUIMapPreload(AltosUIFrame in_owner) {
406                 owner = in_owner;
407
408                 Container               pane = getContentPane();
409                 GridBagConstraints      c = new GridBagConstraints();
410                 Insets                  i = new Insets(4,4,4,4);
411
412                 setTitle("AltOS Load Maps");
413
414                 pane.setLayout(new GridBagLayout());
415
416                 map = new AltosUIMap();
417
418                 c.fill = GridBagConstraints.BOTH;
419                 c.anchor = GridBagConstraints.CENTER;
420                 c.insets = i;
421                 c.weightx = 1;
422                 c.weighty = 1;
423
424                 c.gridx = 0;
425                 c.gridy = 0;
426                 c.gridwidth = 10;
427                 c.anchor = GridBagConstraints.CENTER;
428
429                 pane.add(map, c);
430
431                 pbar = new JProgressBar();
432                 pbar.setMinimum(0);
433                 pbar.setMaximum(1);
434                 pbar.setValue(0);
435                 pbar.setString("");
436                 pbar.setStringPainted(true);
437
438                 c.fill = GridBagConstraints.HORIZONTAL;
439                 c.anchor = GridBagConstraints.CENTER;
440                 c.insets = i;
441                 c.weightx = 1;
442                 c.weighty = 0;
443
444                 c.gridx = 0;
445                 c.gridy = 1;
446                 c.gridwidth = 10;
447
448                 pane.add(pbar, c);
449
450                 site_list_label = new JLabel ("Known Launch Sites:");
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 = 0;
459                 c.gridy = 2;
460                 c.gridwidth = 1;
461
462                 pane.add(site_list_label, c);
463
464                 site_list = new JComboBox<AltosUISite>(new AltosUISite[] { new AltosUISite("Site List", 0, 0) });
465                 site_list.addItemListener(this);
466
467                 sites = new AltosUISites(this);
468
469                 c.fill = GridBagConstraints.HORIZONTAL;
470                 c.anchor = GridBagConstraints.CENTER;
471                 c.insets = i;
472                 c.weightx = 1;
473                 c.weighty = 0;
474
475                 c.gridx = 1;
476                 c.gridy = 2;
477                 c.gridwidth = 1;
478
479                 pane.add(site_list, c);
480
481                 lat = new AltosUIMapPos(owner,
482                                         "Latitude:",
483                                         lat_hemi_names,
484                                         37.167833333);
485                 c.fill = GridBagConstraints.NONE;
486                 c.anchor = GridBagConstraints.CENTER;
487                 c.insets = i;
488                 c.weightx = 0;
489                 c.weighty = 0;
490
491                 c.gridx = 0;
492                 c.gridy = 3;
493                 c.gridwidth = 1;
494                 c.anchor = GridBagConstraints.CENTER;
495
496                 pane.add(lat, c);
497
498                 lon = new AltosUIMapPos(owner,
499                                         "Longitude:",
500                                         lon_hemi_names,
501                                         -97.73975);
502
503                 c.fill = GridBagConstraints.NONE;
504                 c.anchor = GridBagConstraints.CENTER;
505                 c.insets = i;
506                 c.weightx = 0;
507                 c.weighty = 0;
508
509                 c.gridx = 1;
510                 c.gridy = 3;
511                 c.gridwidth = 1;
512                 c.anchor = GridBagConstraints.CENTER;
513
514                 pane.add(lon, c);
515
516                 load_button = new JToggleButton("Load Map");
517                 load_button.addActionListener(this);
518                 load_button.setActionCommand("load");
519
520                 c.fill = GridBagConstraints.NONE;
521                 c.anchor = GridBagConstraints.CENTER;
522                 c.insets = i;
523                 c.weightx = 1;
524                 c.weighty = 0;
525
526                 c.gridx = 0;
527                 c.gridy = 4;
528                 c.gridwidth = 1;
529                 c.anchor = GridBagConstraints.CENTER;
530
531                 pane.add(load_button, c);
532
533                 close_button = new JButton("Close");
534                 close_button.addActionListener(this);
535                 close_button.setActionCommand("close");
536
537                 c.fill = GridBagConstraints.NONE;
538                 c.anchor = GridBagConstraints.CENTER;
539                 c.insets = i;
540                 c.weightx = 1;
541                 c.weighty = 0;
542
543                 c.gridx = 1;
544                 c.gridy = 4;
545                 c.gridwidth = 1;
546                 c.anchor = GridBagConstraints.CENTER;
547
548                 pane.add(close_button, c);
549
550                 JLabel  types_label = new JLabel("Map Types");
551                 c.gridx = 2;
552                 c.gridwidth = 2;
553                 c.gridy = 2;
554                 pane.add(types_label, c);
555
556                 c.gridwidth = 1;
557
558                 for (int type = AltosUIMap.maptype_hybrid; type <= AltosUIMap.maptype_terrain; type++) {
559                         maptypes[type] = new JCheckBox(AltosUIMap.maptype_labels[type],
560                                                        type == AltosUIMap.maptype_hybrid);
561                         c.gridx = 2 + (type >> 1);
562                         c.fill = GridBagConstraints.HORIZONTAL;
563                         c.gridy = (type & 1) + 3;
564                         pane.add(maptypes[type], c);
565                 }
566
567                 JLabel  min_zoom_label = new JLabel("Minimum Zoom");
568                 c.gridx = 4;
569                 c.gridy = 2;
570                 pane.add(min_zoom_label, c);
571
572                 min_zoom = new JComboBox<Integer>(zooms);
573                 min_zoom.setSelectedItem(zooms[10]);
574                 min_zoom.setEditable(false);
575                 c.gridx = 5;
576                 c.gridy = 2;
577                 pane.add(min_zoom, c);
578
579                 JLabel  max_zoom_label = new JLabel("Maximum Zoom");
580                 c.gridx = 4;
581                 c.gridy = 3;
582                 pane.add(max_zoom_label, c);
583
584                 max_zoom = new JComboBox<Integer>(zooms);
585                 max_zoom.setSelectedItem(zooms[14]);
586                 max_zoom.setEditable(false);
587                 c.gridx = 5;
588                 c.gridy = 3;
589                 pane.add(max_zoom, c);
590
591                 JLabel radius_label = new JLabel("Tile Radius");
592                 c.gridx = 4;
593                 c.gridy = 4;
594                 pane.add(radius_label, c);
595
596                 radius = new JComboBox<Integer>(radii);
597                 radius.setSelectedItem(radii[4]);
598                 radius.setEditable(true);
599                 c.gridx = 5;
600                 c.gridy = 4;
601                 pane.add(radius, c);
602
603                 pack();
604                 setLocationRelativeTo(owner);
605                 setVisible(true);
606         }
607 }