altosdroid: Add map types and map preloading UIs
[fw/altos] / altosuilib / AltosUIMapPreloadNew.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altosuilib_7;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import java.io.*;
24 import java.util.*;
25 import java.text.*;
26 import java.lang.Math;
27 import java.net.URL;
28 import java.net.URLConnection;
29 import org.altusmetrum.altoslib_7.*;
30
31 class AltosUIMapPos extends Box {
32         AltosUIFrame    owner;
33         JLabel          label;
34         JComboBox       hemi;
35         JTextField      deg;
36         JLabel          deg_label;
37         JTextField      min;
38         JLabel          min_label;
39
40         public void set_value(double new_value) {
41                 double  d, m;
42                 int     h;
43
44                 h = 0;
45                 if (new_value < 0) {
46                         h = 1;
47                         new_value = -new_value;
48                 }
49                 d = Math.floor(new_value);
50                 deg.setText(String.format("%3.0f", d));
51                 m = (new_value - d) * 60.0;
52                 min.setText(String.format("%7.4f", m));
53                 hemi.setSelectedIndex(h);
54         }
55
56         public double get_value() throws ParseException {
57                 int     h = hemi.getSelectedIndex();
58                 String  d_t = deg.getText();
59                 String  m_t = min.getText();
60                 double  d, m, v;
61                 try {
62                         d = AltosParse.parse_double_locale(d_t);
63                 } catch (ParseException pe) {
64                         JOptionPane.showMessageDialog(owner,
65                                                       String.format("Invalid degrees \"%s\"",
66                                                                     d_t),
67                                                       "Invalid number",
68                                                       JOptionPane.ERROR_MESSAGE);
69                         throw pe;
70                 }
71                 try {
72                         if (m_t.equals(""))
73                                 m = 0;
74                         else
75                                 m = AltosParse.parse_double_locale(m_t);
76                 } catch (ParseException pe) {
77                         JOptionPane.showMessageDialog(owner,
78                                                       String.format("Invalid minutes \"%s\"",
79                                                                     m_t),
80                                                       "Invalid number",
81                                                       JOptionPane.ERROR_MESSAGE);
82                         throw pe;
83                 }
84                 v = d + m/60.0;
85                 if (h == 1)
86                         v = -v;
87                 return v;
88         }
89
90         public AltosUIMapPos(AltosUIFrame in_owner,
91                            String label_value,
92                            String[] hemi_names,
93                            double default_value) {
94                 super(BoxLayout.X_AXIS);
95                 owner = in_owner;
96                 label = new JLabel(label_value);
97                 hemi = new JComboBox<String>(hemi_names);
98                 hemi.setEditable(false);
99                 deg = new JTextField(5);
100                 deg.setMinimumSize(deg.getPreferredSize());
101                 deg.setHorizontalAlignment(JTextField.RIGHT);
102                 deg_label = new JLabel("°");
103                 min = new JTextField(9);
104                 min.setMinimumSize(min.getPreferredSize());
105                 min_label = new JLabel("'");
106                 set_value(default_value);
107                 add(label);
108                 add(Box.createRigidArea(new Dimension(5, 0)));
109                 add(hemi);
110                 add(Box.createRigidArea(new Dimension(5, 0)));
111                 add(deg);
112                 add(Box.createRigidArea(new Dimension(5, 0)));
113                 add(deg_label);
114                 add(Box.createRigidArea(new Dimension(5, 0)));
115                 add(min);
116                 add(Box.createRigidArea(new Dimension(5, 0)));
117                 add(min_label);
118         }
119 }
120
121 public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener, ItemListener, AltosLaunchSiteListener, AltosMapLoaderListener  {
122         AltosUIFrame    owner;
123         AltosUIMapNew   map;
124
125         AltosUIMapPos   lat;
126         AltosUIMapPos   lon;
127
128         JProgressBar    pbar;
129
130         AltosMapLoader  loader;
131
132         JLabel          site_list_label;
133         JComboBox<AltosLaunchSite>      site_list;
134
135         JToggleButton   load_button;
136         boolean         loading;
137         JButton         close_button;
138
139         JCheckBox[]     maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1];
140
141         JComboBox<Integer>      min_zoom;
142         JComboBox<Integer>      max_zoom;
143         JComboBox<Integer>      radius;
144
145         Integer[]               zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
146         Integer[]               radii = { 1, 2, 3, 4, 5 };
147
148         static final String[]   lat_hemi_names = { "N", "S" };
149         static final String[]   lon_hemi_names = { "E", "W" };
150
151         double  latitude, longitude;
152
153         /* AltosMapLoaderListener interfaces */
154         public void loader_start(final int max) {
155                 SwingUtilities.invokeLater(new Runnable() {
156                                 public void run() {
157                                         pbar.setMaximum(max);
158                                         pbar.setValue(0);
159                                         pbar.setString("");
160                                         map.clear_marks();
161                                         map.add_mark(latitude, longitude, AltosLib.ao_flight_boost);
162                                 }
163                         });
164         }
165
166         public void loader_notify(final int cur, final int max, final String name) {
167                 SwingUtilities.invokeLater(new Runnable() {
168                                 public void run() {
169                                         pbar.setValue(cur);
170                                         pbar.setString(name);
171                                 }
172                         });
173         }
174
175         public void loader_done(int max) {
176                 SwingUtilities.invokeLater(new Runnable() {
177                                 public void run() {
178                                         pbar.setValue(0);
179                                         pbar.setString("");
180                                         load_button.setSelected(false);
181                                         loading = false;
182                                 }
183                         });
184         }
185
186         private int all_types() {
187                 int all_types = 0;
188                 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
189                         if (maptypes[t].isSelected())
190                                 all_types |= (1 << t);
191                 return all_types;
192         }
193
194         public void itemStateChanged(ItemEvent e) {
195                 int             state = e.getStateChange();
196
197                 if (state == ItemEvent.SELECTED) {
198                         Object  o = e.getItem();
199                         if (o instanceof AltosLaunchSite) {
200                                 AltosLaunchSite site = (AltosLaunchSite) o;
201                                 lat.set_value(site.latitude);
202                                 lon.set_value(site.longitude);
203                         }
204                 }
205         }
206
207         public void actionPerformed(ActionEvent e) {
208                 String  cmd = e.getActionCommand();
209
210                 if (cmd.equals("close"))
211                         setVisible(false);
212
213                 if (cmd.equals("load")) {
214                         if (!loading) {
215                                 try {
216                                         latitude = lat.get_value();
217                                         longitude = lon.get_value();
218                                         int min_z = (Integer) min_zoom.getSelectedItem();
219                                         int max_z = (Integer) max_zoom.getSelectedItem();
220                                         if (max_z < min_z)
221                                                 max_z = min_z;
222                                         int r = (Integer) radius.getSelectedItem();
223                                         loading = true;
224
225                                         loader.load(latitude, longitude, min_z, max_z, r, all_types());
226                                 } catch (ParseException pe) {
227                                         load_button.setSelected(false);
228                                 }
229                         }
230                 }
231         }
232
233         public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
234                 SwingUtilities.invokeLater(new Runnable() {
235                                 public void run() {
236                                         int     i = 1;
237                                         for (AltosLaunchSite site : sites) {
238                                                 site_list.insertItemAt(site, i);
239                                                 i++;
240                                         }
241                                 }
242                         });
243         }
244
245         public AltosUIMapPreloadNew(AltosUIFrame in_owner) {
246                 owner = in_owner;
247
248                 Container               pane = getContentPane();
249                 GridBagConstraints      c = new GridBagConstraints();
250                 Insets                  i = new Insets(4,4,4,4);
251
252                 setTitle("AltOS Load Maps");
253
254                 pane.setLayout(new GridBagLayout());
255
256                 map = new AltosUIMapNew();
257
258                 loader = new AltosMapLoader(map.map, this);
259
260                 c.fill = GridBagConstraints.BOTH;
261                 c.anchor = GridBagConstraints.CENTER;
262                 c.insets = i;
263                 c.weightx = 1;
264                 c.weighty = 1;
265
266                 c.gridx = 0;
267                 c.gridy = 0;
268                 c.gridwidth = 10;
269                 c.anchor = GridBagConstraints.CENTER;
270
271                 pane.add(map, c);
272
273                 pbar = new JProgressBar();
274                 pbar.setMinimum(0);
275                 pbar.setMaximum(1);
276                 pbar.setValue(0);
277                 pbar.setString("");
278                 pbar.setStringPainted(true);
279
280                 c.fill = GridBagConstraints.HORIZONTAL;
281                 c.anchor = GridBagConstraints.CENTER;
282                 c.insets = i;
283                 c.weightx = 1;
284                 c.weighty = 0;
285
286                 c.gridx = 0;
287                 c.gridy = 1;
288                 c.gridwidth = 10;
289
290                 pane.add(pbar, c);
291
292                 site_list_label = new JLabel ("Known Launch Sites:");
293
294                 c.fill = GridBagConstraints.NONE;
295                 c.anchor = GridBagConstraints.CENTER;
296                 c.insets = i;
297                 c.weightx = 1;
298                 c.weighty = 0;
299
300                 c.gridx = 0;
301                 c.gridy = 2;
302                 c.gridwidth = 1;
303
304                 pane.add(site_list_label, c);
305
306                 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
307                 site_list.addItemListener(this);
308
309                 new AltosLaunchSites(this);
310
311                 c.fill = GridBagConstraints.HORIZONTAL;
312                 c.anchor = GridBagConstraints.CENTER;
313                 c.insets = i;
314                 c.weightx = 1;
315                 c.weighty = 0;
316
317                 c.gridx = 1;
318                 c.gridy = 2;
319                 c.gridwidth = 1;
320
321                 pane.add(site_list, c);
322
323                 lat = new AltosUIMapPos(owner,
324                                         "Latitude:",
325                                         lat_hemi_names,
326                                         37.167833333);
327                 c.fill = GridBagConstraints.NONE;
328                 c.anchor = GridBagConstraints.CENTER;
329                 c.insets = i;
330                 c.weightx = 0;
331                 c.weighty = 0;
332
333                 c.gridx = 0;
334                 c.gridy = 3;
335                 c.gridwidth = 1;
336                 c.anchor = GridBagConstraints.CENTER;
337
338                 pane.add(lat, c);
339
340                 lon = new AltosUIMapPos(owner,
341                                         "Longitude:",
342                                         lon_hemi_names,
343                                         -97.73975);
344
345                 c.fill = GridBagConstraints.NONE;
346                 c.anchor = GridBagConstraints.CENTER;
347                 c.insets = i;
348                 c.weightx = 0;
349                 c.weighty = 0;
350
351                 c.gridx = 1;
352                 c.gridy = 3;
353                 c.gridwidth = 1;
354                 c.anchor = GridBagConstraints.CENTER;
355
356                 pane.add(lon, c);
357
358                 load_button = new JToggleButton("Load Map");
359                 load_button.addActionListener(this);
360                 load_button.setActionCommand("load");
361
362                 c.fill = GridBagConstraints.NONE;
363                 c.anchor = GridBagConstraints.CENTER;
364                 c.insets = i;
365                 c.weightx = 1;
366                 c.weighty = 0;
367
368                 c.gridx = 0;
369                 c.gridy = 4;
370                 c.gridwidth = 1;
371                 c.anchor = GridBagConstraints.CENTER;
372
373                 pane.add(load_button, c);
374
375                 close_button = new JButton("Close");
376                 close_button.addActionListener(this);
377                 close_button.setActionCommand("close");
378
379                 c.fill = GridBagConstraints.NONE;
380                 c.anchor = GridBagConstraints.CENTER;
381                 c.insets = i;
382                 c.weightx = 1;
383                 c.weighty = 0;
384
385                 c.gridx = 1;
386                 c.gridy = 4;
387                 c.gridwidth = 1;
388                 c.anchor = GridBagConstraints.CENTER;
389
390                 pane.add(close_button, c);
391
392                 JLabel  types_label = new JLabel("Map Types");
393                 c.gridx = 2;
394                 c.gridwidth = 2;
395                 c.gridy = 2;
396                 pane.add(types_label, c);
397
398                 c.gridwidth = 1;
399
400                 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
401                         maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
402                                                        type == AltosMap.maptype_hybrid);
403                         c.gridx = 2 + (type >> 1);
404                         c.fill = GridBagConstraints.HORIZONTAL;
405                         c.gridy = (type & 1) + 3;
406                         pane.add(maptypes[type], c);
407                 }
408
409                 JLabel  min_zoom_label = new JLabel("Minimum Zoom");
410                 c.gridx = 4;
411                 c.gridy = 2;
412                 pane.add(min_zoom_label, c);
413
414                 min_zoom = new JComboBox<Integer>(zooms);
415                 min_zoom.setSelectedItem(zooms[10]);
416                 min_zoom.setEditable(false);
417                 c.gridx = 5;
418                 c.gridy = 2;
419                 pane.add(min_zoom, c);
420
421                 JLabel  max_zoom_label = new JLabel("Maximum Zoom");
422                 c.gridx = 4;
423                 c.gridy = 3;
424                 pane.add(max_zoom_label, c);
425
426                 max_zoom = new JComboBox<Integer>(zooms);
427                 max_zoom.setSelectedItem(zooms[14]);
428                 max_zoom.setEditable(false);
429                 c.gridx = 5;
430                 c.gridy = 3;
431                 pane.add(max_zoom, c);
432
433                 JLabel radius_label = new JLabel("Tile Radius");
434                 c.gridx = 4;
435                 c.gridy = 4;
436                 pane.add(radius_label, c);
437
438                 radius = new JComboBox<Integer>(radii);
439                 radius.setSelectedItem(radii[4]);
440                 radius.setEditable(true);
441                 c.gridx = 5;
442                 c.gridy = 4;
443                 pane.add(radius, c);
444
445                 pack();
446                 setLocationRelativeTo(owner);
447                 setVisible(true);
448         }
449 }