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