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