20cb3888f611e3b7b92e810fd12f732da1d0cc8c
[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         AltosMapCache   cache;
125
126         AltosUIMapPos   lat;
127         AltosUIMapPos   lon;
128
129         JProgressBar    pbar;
130
131         AltosMapLoader  loader;
132
133         JLabel          site_list_label;
134         JComboBox<AltosLaunchSite>      site_list;
135
136         JToggleButton   load_button;
137         boolean         loading;
138         JButton         close_button;
139
140         JCheckBox[]     maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1];
141
142         JComboBox<Integer>      min_zoom;
143         JComboBox<Integer>      max_zoom;
144         JComboBox<Integer>      radius;
145
146         Integer[]               zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
147         Integer[]               radii = { 1, 2, 3, 4, 5 };
148
149         static final String[]   lat_hemi_names = { "N", "S" };
150         static final String[]   lon_hemi_names = { "E", "W" };
151
152         double  latitude, longitude;
153
154         /* AltosMapLoaderListener interfaces */
155         public void loader_start(final int max) {
156                 SwingUtilities.invokeLater(new Runnable() {
157                                 public void run() {
158                                         pbar.setMaximum(max);
159                                         pbar.setValue(0);
160                                         pbar.setString("");
161                                         map.clear_marks();
162                                         map.add_mark(latitude, longitude, AltosLib.ao_flight_boost);
163                                 }
164                         });
165         }
166
167         public void loader_notify(final int cur, final int max, final String name) {
168                 SwingUtilities.invokeLater(new Runnable() {
169                                 public void run() {
170                                         pbar.setValue(cur);
171                                         pbar.setString(name);
172                                 }
173                         });
174         }
175
176         public void loader_done(int max) {
177                 SwingUtilities.invokeLater(new Runnable() {
178                                 public void run() {
179                                         pbar.setValue(0);
180                                         pbar.setString("");
181                                         load_button.setSelected(false);
182                                         loading = false;
183                                 }
184                         });
185         }
186
187         private int all_types() {
188                 int all_types = 0;
189                 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
190                         if (maptypes[t].isSelected())
191                                 all_types |= (1 << t);
192                 return all_types;
193         }
194
195         public void itemStateChanged(ItemEvent e) {
196                 int             state = e.getStateChange();
197
198                 if (state == ItemEvent.SELECTED) {
199                         Object  o = e.getItem();
200                         if (o instanceof AltosLaunchSite) {
201                                 AltosLaunchSite site = (AltosLaunchSite) o;
202                                 lat.set_value(site.latitude);
203                                 lon.set_value(site.longitude);
204                         }
205                 }
206         }
207
208         public void actionPerformed(ActionEvent e) {
209                 String  cmd = e.getActionCommand();
210
211                 if (cmd.equals("close"))
212                         setVisible(false);
213
214                 if (cmd.equals("load")) {
215                         if (!loading) {
216                                 try {
217                                         latitude = lat.get_value();
218                                         longitude = lon.get_value();
219                                         int min_z = (Integer) min_zoom.getSelectedItem();
220                                         int max_z = (Integer) max_zoom.getSelectedItem();
221                                         if (max_z < min_z)
222                                                 max_z = min_z;
223                                         int r = (Integer) radius.getSelectedItem();
224                                         loading = true;
225
226                                         loader.load(latitude, longitude, min_z, max_z, r, all_types());
227                                 } catch (ParseException pe) {
228                                         load_button.setSelected(false);
229                                 }
230                         }
231                 }
232         }
233
234         public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
235                 SwingUtilities.invokeLater(new Runnable() {
236                                 public void run() {
237                                         int     i = 1;
238                                         for (AltosLaunchSite site : sites) {
239                                                 site_list.insertItemAt(site, i);
240                                                 i++;
241                                         }
242                                 }
243                         });
244         }
245
246         public AltosUIMapPreloadNew(AltosUIFrame in_owner) {
247                 owner = in_owner;
248
249                 Container               pane = getContentPane();
250                 GridBagConstraints      c = new GridBagConstraints();
251                 Insets                  i = new Insets(4,4,4,4);
252
253                 setTitle("AltOS Load Maps");
254
255                 pane.setLayout(new GridBagLayout());
256
257                 map = new AltosUIMapNew();
258                 cache = new AltosMapCache(map);
259
260                 loader = new AltosMapLoader(map.map, cache, this);
261
262                 c.fill = GridBagConstraints.BOTH;
263                 c.anchor = GridBagConstraints.CENTER;
264                 c.insets = i;
265                 c.weightx = 1;
266                 c.weighty = 1;
267
268                 c.gridx = 0;
269                 c.gridy = 0;
270                 c.gridwidth = 10;
271                 c.anchor = GridBagConstraints.CENTER;
272
273                 pane.add(map, c);
274
275                 pbar = new JProgressBar();
276                 pbar.setMinimum(0);
277                 pbar.setMaximum(1);
278                 pbar.setValue(0);
279                 pbar.setString("");
280                 pbar.setStringPainted(true);
281
282                 c.fill = GridBagConstraints.HORIZONTAL;
283                 c.anchor = GridBagConstraints.CENTER;
284                 c.insets = i;
285                 c.weightx = 1;
286                 c.weighty = 0;
287
288                 c.gridx = 0;
289                 c.gridy = 1;
290                 c.gridwidth = 10;
291
292                 pane.add(pbar, c);
293
294                 site_list_label = new JLabel ("Known Launch Sites:");
295
296                 c.fill = GridBagConstraints.NONE;
297                 c.anchor = GridBagConstraints.CENTER;
298                 c.insets = i;
299                 c.weightx = 1;
300                 c.weighty = 0;
301
302                 c.gridx = 0;
303                 c.gridy = 2;
304                 c.gridwidth = 1;
305
306                 pane.add(site_list_label, c);
307
308                 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
309                 site_list.addItemListener(this);
310
311                 new AltosLaunchSites(this);
312
313                 c.fill = GridBagConstraints.HORIZONTAL;
314                 c.anchor = GridBagConstraints.CENTER;
315                 c.insets = i;
316                 c.weightx = 1;
317                 c.weighty = 0;
318
319                 c.gridx = 1;
320                 c.gridy = 2;
321                 c.gridwidth = 1;
322
323                 pane.add(site_list, c);
324
325                 lat = new AltosUIMapPos(owner,
326                                         "Latitude:",
327                                         lat_hemi_names,
328                                         37.167833333);
329                 c.fill = GridBagConstraints.NONE;
330                 c.anchor = GridBagConstraints.CENTER;
331                 c.insets = i;
332                 c.weightx = 0;
333                 c.weighty = 0;
334
335                 c.gridx = 0;
336                 c.gridy = 3;
337                 c.gridwidth = 1;
338                 c.anchor = GridBagConstraints.CENTER;
339
340                 pane.add(lat, c);
341
342                 lon = new AltosUIMapPos(owner,
343                                         "Longitude:",
344                                         lon_hemi_names,
345                                         -97.73975);
346
347                 c.fill = GridBagConstraints.NONE;
348                 c.anchor = GridBagConstraints.CENTER;
349                 c.insets = i;
350                 c.weightx = 0;
351                 c.weighty = 0;
352
353                 c.gridx = 1;
354                 c.gridy = 3;
355                 c.gridwidth = 1;
356                 c.anchor = GridBagConstraints.CENTER;
357
358                 pane.add(lon, c);
359
360                 load_button = new JToggleButton("Load Map");
361                 load_button.addActionListener(this);
362                 load_button.setActionCommand("load");
363
364                 c.fill = GridBagConstraints.NONE;
365                 c.anchor = GridBagConstraints.CENTER;
366                 c.insets = i;
367                 c.weightx = 1;
368                 c.weighty = 0;
369
370                 c.gridx = 0;
371                 c.gridy = 4;
372                 c.gridwidth = 1;
373                 c.anchor = GridBagConstraints.CENTER;
374
375                 pane.add(load_button, c);
376
377                 close_button = new JButton("Close");
378                 close_button.addActionListener(this);
379                 close_button.setActionCommand("close");
380
381                 c.fill = GridBagConstraints.NONE;
382                 c.anchor = GridBagConstraints.CENTER;
383                 c.insets = i;
384                 c.weightx = 1;
385                 c.weighty = 0;
386
387                 c.gridx = 1;
388                 c.gridy = 4;
389                 c.gridwidth = 1;
390                 c.anchor = GridBagConstraints.CENTER;
391
392                 pane.add(close_button, c);
393
394                 JLabel  types_label = new JLabel("Map Types");
395                 c.gridx = 2;
396                 c.gridwidth = 2;
397                 c.gridy = 2;
398                 pane.add(types_label, c);
399
400                 c.gridwidth = 1;
401
402                 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
403                         maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
404                                                        type == AltosMap.maptype_hybrid);
405                         c.gridx = 2 + (type >> 1);
406                         c.fill = GridBagConstraints.HORIZONTAL;
407                         c.gridy = (type & 1) + 3;
408                         pane.add(maptypes[type], c);
409                 }
410
411                 JLabel  min_zoom_label = new JLabel("Minimum Zoom");
412                 c.gridx = 4;
413                 c.gridy = 2;
414                 pane.add(min_zoom_label, c);
415
416                 min_zoom = new JComboBox<Integer>(zooms);
417                 min_zoom.setSelectedItem(zooms[10]);
418                 min_zoom.setEditable(false);
419                 c.gridx = 5;
420                 c.gridy = 2;
421                 pane.add(min_zoom, c);
422
423                 JLabel  max_zoom_label = new JLabel("Maximum Zoom");
424                 c.gridx = 4;
425                 c.gridy = 3;
426                 pane.add(max_zoom_label, c);
427
428                 max_zoom = new JComboBox<Integer>(zooms);
429                 max_zoom.setSelectedItem(zooms[14]);
430                 max_zoom.setEditable(false);
431                 c.gridx = 5;
432                 c.gridy = 3;
433                 pane.add(max_zoom, c);
434
435                 JLabel radius_label = new JLabel("Tile Radius");
436                 c.gridx = 4;
437                 c.gridy = 4;
438                 pane.add(radius_label, c);
439
440                 radius = new JComboBox<Integer>(radii);
441                 radius.setSelectedItem(radii[4]);
442                 radius.setEditable(true);
443                 c.gridx = 5;
444                 c.gridy = 4;
445                 pane.add(radius, c);
446
447                 pack();
448                 setLocationRelativeTo(owner);
449                 setVisible(true);
450         }
451 }