6b8066b36c350f47f427cfa682a116dcd445a193
[fw/altos] / altosuilib / AltosSiteMapPreload.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 AltosMapPos 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 AltosMapPos(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 AltosSite {
122         String  name;
123         double  latitude;
124         double  longitude;
125
126         public String toString() {
127                 return name;
128         }
129
130         public AltosSite(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 AltosSite(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 AltosSites extends Thread {
154         AltosSiteMapPreload     preload;
155         URL                     url;
156         LinkedList<AltosSite>   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(AltosSite site) {
167                 sites.add(site);
168         }
169
170         void add(String line) {
171                 try {
172                         add(new AltosSite(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 AltosSites(AltosSiteMapPreload in_preload) {
198                 sites = new LinkedList<AltosSite>();
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 AltosSiteMapPreload extends AltosUIFrame implements ActionListener, ItemListener {
210         AltosUIFrame    owner;
211         AltosSiteMap    map;
212
213         AltosMapPos     lat;
214         AltosMapPos     lon;
215
216         JProgressBar    pbar;
217         int             pbar_max;
218
219         AltosSites      sites;
220         JLabel          site_list_label;
221         JComboBox<AltosSite>    site_list;
222
223         JToggleButton   load_button;
224         boolean         loading;
225         JButton         close_button;
226
227         JCheckBox[]     maptypes = new JCheckBox[AltosSiteMap.maptype_terrain - AltosSiteMap.maptype_hybrid + 1];
228
229         JComboBox<Integer>      min_zoom;
230         JComboBox<Integer>      max_zoom;
231         JComboBox<Integer>      radius;
232
233         Integer[]               zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
234         Integer[]               radii = { 1, 2, 3, 4, 5 };
235
236         static final String[]   lat_hemi_names = { "N", "S" };
237         static final String[]   lon_hemi_names = { "E", "W" };
238
239         class updatePbar implements Runnable {
240                 int             n;
241                 String          s;
242
243                 public updatePbar(int n, String in_s) {
244                         this.n = n;
245                         s = in_s;
246                 }
247
248                 public void run() {
249                         pbar.setValue(n);
250                         pbar.setString(s);
251                         if (n < pbar_max) {
252                                 pbar.setMaximum(pbar_max);
253                                 pbar.setValue(n);
254                                 pbar.setString(s);
255                         } else {
256                                 pbar.setValue(0);
257                                 pbar.setString("");
258                                 load_button.setSelected(false);
259                                 loading = false;
260                         }
261                 }
262         }
263
264         class bgLoad extends Thread {
265
266                 AltosSiteMap    map;
267
268                 double          lat, lon;
269
270                 int             types = 0;
271                 int             r;
272
273                 int     min_z = (Integer) min_zoom.getSelectedItem();
274                 int     max_z = (Integer) max_zoom.getSelectedItem();
275
276                 public bgLoad(AltosSiteMap in_map, double lat, double lon) {
277                         map = in_map;
278                         this.lat = lat;
279                         this.lon = lon;
280                         if (max_z < min_z)
281                                 max_z = min_z;
282                         int ntype = 0;
283                         for (int t = AltosSiteMap.maptype_hybrid; t <= AltosSiteMap.maptype_terrain; t++)
284                                 if (maptypes[t].isSelected()) {
285                                         types |= (1 << t);
286                                         ntype++;
287                                 }
288                         if (ntype == 0) {
289                                 types |= (1 << AltosSiteMap.maptype_hybrid);
290                                 ntype = 1;
291                         }
292                         r = (Integer) radius.getSelectedItem();
293                         pbar_max = (max_z - min_z + 1) * ntype * (r * 2 + 1) * (r * 2 + 1);
294                 }
295
296                 public void run() {
297                         int     i = 0;
298                         for (int z = min_z; z <= max_z; z++) {
299                                 for (int t = AltosSiteMap.maptype_hybrid; t <= AltosSiteMap.maptype_terrain; t++) {
300                                         if ((types & (1 << t)) == 0)
301                                                 continue;
302                                         map.clear_base_location();
303                                         map.set_zoom(z + AltosSiteMap.default_zoom);
304                                         map.set_maptype(t);
305                                         map.set_radius(r);
306                                         map.setBaseLocation(lat, lon);
307                                         map.draw_circle(lat, lon);
308                                         for (int y = -r; y <= r; y++) {
309                                                 for (int x = -r; x <= r; x++) {
310                                                         File    pngfile;
311                                                         pngfile = map.init_map(new Point(x,y),
312                                                                                AltosSiteMap.load_mode_cached|AltosSiteMap.load_mode_uncached);
313                                                         SwingUtilities.invokeLater(new updatePbar(++i, pngfile.toString()));
314                                                 }
315                                         }
316                                 }
317                         }
318                 }
319         }
320
321         public void set_sites() {
322                 int     i = 1;
323                 for (AltosSite site : sites.sites) {
324                         site_list.insertItemAt(site, i);
325                         i++;
326                 }
327         }
328
329         public void itemStateChanged(ItemEvent e) {
330                 int             state = e.getStateChange();
331
332                 if (state == ItemEvent.SELECTED) {
333                         Object  o = e.getItem();
334                         if (o instanceof AltosSite) {
335                                 AltosSite       site = (AltosSite) o;
336                                 lat.set_value(site.latitude);
337                                 lon.set_value(site.longitude);
338                         }
339                 }
340         }
341
342         public void actionPerformed(ActionEvent e) {
343                 String  cmd = e.getActionCommand();
344
345                 if (cmd.equals("close"))
346                         setVisible(false);
347
348                 if (cmd.equals("load")) {
349                         if (!loading) {
350                                 try {
351                                         final double    latitude = lat.get_value();
352                                         final double    longitude = lon.get_value();
353                                         map.clear_base_location();
354                                         map.setBaseLocation(latitude,longitude);
355                                         map.draw_circle(latitude,longitude);
356                                         loading = true;
357                                         bgLoad thread = new bgLoad(map, latitude, longitude);
358                                         thread.start();
359                                 } catch (NumberFormatException ne) {
360                                         load_button.setSelected(false);
361                                 }
362                         }
363                 }
364         }
365
366         public AltosSiteMapPreload(AltosUIFrame in_owner) {
367                 owner = in_owner;
368
369                 Container               pane = getContentPane();
370                 GridBagConstraints      c = new GridBagConstraints();
371                 Insets                  i = new Insets(4,4,4,4);
372
373                 setTitle("AltOS Load Maps");
374
375                 pane.setLayout(new GridBagLayout());
376
377                 map = new AltosSiteMap(2);
378
379                 c.fill = GridBagConstraints.BOTH;
380                 c.anchor = GridBagConstraints.CENTER;
381                 c.insets = i;
382                 c.weightx = 1;
383                 c.weighty = 1;
384
385                 c.gridx = 0;
386                 c.gridy = 0;
387                 c.gridwidth = 10;
388                 c.anchor = GridBagConstraints.CENTER;
389
390                 pane.add(map, c);
391
392                 pbar = new JProgressBar();
393                 pbar.setMinimum(0);
394                 pbar.setMaximum(1);
395                 pbar.setValue(0);
396                 pbar.setString("");
397                 pbar.setStringPainted(true);
398
399                 c.fill = GridBagConstraints.HORIZONTAL;
400                 c.anchor = GridBagConstraints.CENTER;
401                 c.insets = i;
402                 c.weightx = 1;
403                 c.weighty = 0;
404
405                 c.gridx = 0;
406                 c.gridy = 1;
407                 c.gridwidth = 10;
408
409                 pane.add(pbar, c);
410
411                 site_list_label = new JLabel ("Known Launch Sites:");
412
413                 c.fill = GridBagConstraints.NONE;
414                 c.anchor = GridBagConstraints.CENTER;
415                 c.insets = i;
416                 c.weightx = 1;
417                 c.weighty = 0;
418
419                 c.gridx = 0;
420                 c.gridy = 2;
421                 c.gridwidth = 1;
422
423                 pane.add(site_list_label, c);
424
425                 site_list = new JComboBox<AltosSite>(new AltosSite[] { new AltosSite("Site List", 0, 0) });
426                 site_list.addItemListener(this);
427
428                 sites = new AltosSites(this);
429
430                 c.fill = GridBagConstraints.HORIZONTAL;
431                 c.anchor = GridBagConstraints.CENTER;
432                 c.insets = i;
433                 c.weightx = 1;
434                 c.weighty = 0;
435
436                 c.gridx = 1;
437                 c.gridy = 2;
438                 c.gridwidth = 1;
439
440                 pane.add(site_list, c);
441
442                 lat = new AltosMapPos(owner,
443                                       "Latitude:",
444                                       lat_hemi_names,
445                                       37.167833333);
446                 c.fill = GridBagConstraints.NONE;
447                 c.anchor = GridBagConstraints.CENTER;
448                 c.insets = i;
449                 c.weightx = 0;
450                 c.weighty = 0;
451
452                 c.gridx = 0;
453                 c.gridy = 3;
454                 c.gridwidth = 1;
455                 c.anchor = GridBagConstraints.CENTER;
456
457                 pane.add(lat, c);
458
459                 lon = new AltosMapPos(owner,
460                                       "Longitude:",
461                                       lon_hemi_names,
462                                       -97.73975);
463
464                 c.fill = GridBagConstraints.NONE;
465                 c.anchor = GridBagConstraints.CENTER;
466                 c.insets = i;
467                 c.weightx = 0;
468                 c.weighty = 0;
469
470                 c.gridx = 1;
471                 c.gridy = 3;
472                 c.gridwidth = 1;
473                 c.anchor = GridBagConstraints.CENTER;
474
475                 pane.add(lon, c);
476
477                 load_button = new JToggleButton("Load Map");
478                 load_button.addActionListener(this);
479                 load_button.setActionCommand("load");
480
481                 c.fill = GridBagConstraints.NONE;
482                 c.anchor = GridBagConstraints.CENTER;
483                 c.insets = i;
484                 c.weightx = 1;
485                 c.weighty = 0;
486
487                 c.gridx = 0;
488                 c.gridy = 4;
489                 c.gridwidth = 1;
490                 c.anchor = GridBagConstraints.CENTER;
491
492                 pane.add(load_button, c);
493
494                 close_button = new JButton("Close");
495                 close_button.addActionListener(this);
496                 close_button.setActionCommand("close");
497
498                 c.fill = GridBagConstraints.NONE;
499                 c.anchor = GridBagConstraints.CENTER;
500                 c.insets = i;
501                 c.weightx = 1;
502                 c.weighty = 0;
503
504                 c.gridx = 1;
505                 c.gridy = 4;
506                 c.gridwidth = 1;
507                 c.anchor = GridBagConstraints.CENTER;
508
509                 pane.add(close_button, c);
510
511                 JLabel  types_label = new JLabel("Map Types");
512                 c.gridx = 2;
513                 c.gridwidth = 2;
514                 c.gridy = 2;
515                 pane.add(types_label, c);
516
517                 c.gridwidth = 1;
518
519                 for (int type = AltosSiteMap.maptype_hybrid; type <= AltosSiteMap.maptype_terrain; type++) {
520                         maptypes[type] = new JCheckBox(AltosSiteMap.maptype_labels[type],
521                                                        type == AltosSiteMap.maptype_hybrid);
522                         c.gridx = 2 + (type >> 1);
523                         c.fill = GridBagConstraints.HORIZONTAL;
524                         c.gridy = (type & 1) + 3;
525                         pane.add(maptypes[type], c);
526                 }
527
528                 JLabel  min_zoom_label = new JLabel("Minimum Zoom");
529                 c.gridx = 4;
530                 c.gridy = 2;
531                 pane.add(min_zoom_label, c);
532
533                 min_zoom = new JComboBox<Integer>(zooms);
534                 min_zoom.setSelectedItem(zooms[10]);
535                 min_zoom.setEditable(false);
536                 c.gridx = 5;
537                 c.gridy = 2;
538                 pane.add(min_zoom, c);
539
540                 JLabel  max_zoom_label = new JLabel("Maximum Zoom");
541                 c.gridx = 4;
542                 c.gridy = 3;
543                 pane.add(max_zoom_label, c);
544
545                 max_zoom = new JComboBox<Integer>(zooms);
546                 max_zoom.setSelectedItem(zooms[14]);
547                 max_zoom.setEditable(false);
548                 c.gridx = 5;
549                 c.gridy = 3;
550                 pane.add(max_zoom, c);
551
552                 JLabel radius_label = new JLabel("Tile Radius");
553                 c.gridx = 4;
554                 c.gridy = 4;
555                 pane.add(radius_label, c);
556
557                 radius = new JComboBox<Integer>(radii);
558                 radius.setSelectedItem(radii[4]);
559                 radius.setEditable(false);
560                 c.gridx = 5;
561                 c.gridy = 4;
562                 pane.add(radius, c);
563
564                 pack();
565                 setLocationRelativeTo(owner);
566                 setVisible(true);
567         }
568 }