2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
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.
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.
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.
18 package org.altusmetrum.altosuilib_11;
21 import java.awt.event.*;
26 import java.lang.Math;
28 import java.net.URLConnection;
29 import org.altusmetrum.altoslib_11.*;
31 class AltosUIMapPos extends Box {
40 public void set_value(double new_value) {
47 new_value = -new_value;
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);
56 public double get_value() throws ParseException {
57 int h = hemi.getSelectedIndex();
58 String d_t = deg.getText();
59 String m_t = min.getText();
62 d = AltosParse.parse_double_locale(d_t);
63 } catch (ParseException pe) {
64 JOptionPane.showMessageDialog(owner,
65 String.format("Invalid degrees \"%s\"",
68 JOptionPane.ERROR_MESSAGE);
75 m = AltosParse.parse_double_locale(m_t);
76 } catch (ParseException pe) {
77 JOptionPane.showMessageDialog(owner,
78 String.format("Invalid minutes \"%s\"",
81 JOptionPane.ERROR_MESSAGE);
90 public AltosUIMapPos(AltosUIFrame in_owner,
93 double default_value) {
94 super(BoxLayout.X_AXIS);
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);
108 add(Box.createRigidArea(new Dimension(5, 0)));
110 add(Box.createRigidArea(new Dimension(5, 0)));
112 add(Box.createRigidArea(new Dimension(5, 0)));
114 add(Box.createRigidArea(new Dimension(5, 0)));
116 add(Box.createRigidArea(new Dimension(5, 0)));
121 public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener, ItemListener, AltosLaunchSiteListener, AltosMapLoaderListener {
130 JLabel site_list_label;
131 JComboBox<AltosLaunchSite> site_list;
133 JToggleButton load_button;
134 JButton close_button;
136 JCheckBox[] maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1];
138 JComboBox<Integer> min_zoom;
139 JComboBox<Integer> max_zoom;
140 JComboBox<Double> radius;
143 Integer[] zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
145 Double[] radius_mi = { 1.0, 2.0, 5.0, 10.0, 20.0 };
146 Double radius_def_mi = 5.0;
147 Double[] radius_km = { 2.0, 5.0, 10.0, 20.0, 30.0 };
148 Double radius_def_km = 10.0;
150 AltosMapLoader loader;
152 static final String[] lat_hemi_names = { "N", "S" };
153 static final String[] lon_hemi_names = { "E", "W" };
155 double latitude, longitude;
157 long loader_notify_time;
159 /* AltosMapLoaderListener interfaces */
160 public void loader_start(final int max) {
161 loader_notify_time = System.currentTimeMillis();
163 SwingUtilities.invokeLater(new Runnable() {
165 pbar.setMaximum(max);
169 map.add_mark(latitude, longitude, AltosLib.ao_flight_boost);
174 public void loader_notify(final int cur, final int max, final String name) {
175 long now = System.currentTimeMillis();
177 if (now - loader_notify_time < 100)
180 loader_notify_time = now;
182 SwingUtilities.invokeLater(new Runnable() {
185 pbar.setString(name);
190 public void loader_done(int max) {
192 SwingUtilities.invokeLater(new Runnable() {
196 load_button.setSelected(false);
201 public void debug(String format, Object ... arguments) {
202 if (AltosSerial.debug)
203 System.out.printf(format, arguments);
207 private int all_types() {
209 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
210 if (maptypes[t].isSelected())
211 all_types |= (1 << t);
215 public void itemStateChanged(ItemEvent e) {
216 int state = e.getStateChange();
218 if (state == ItemEvent.SELECTED) {
219 Object o = e.getItem();
220 if (o instanceof AltosLaunchSite) {
221 AltosLaunchSite site = (AltosLaunchSite) o;
222 lat.set_value(site.latitude);
223 lon.set_value(site.longitude);
228 public void actionPerformed(ActionEvent e) {
229 String cmd = e.getActionCommand();
231 if (cmd.equals("close")) {
237 if (cmd.equals("load")) {
238 if (loader == null) {
240 latitude = lat.get_value();
241 longitude = lon.get_value();
242 int min_z = (Integer) min_zoom.getSelectedItem();
243 int max_z = (Integer) max_zoom.getSelectedItem();
246 Double r = (Double) radius.getSelectedItem();
248 if (AltosPreferences.imperial_units())
249 r = AltosConvert.miles_to_meters(r);
253 map.map.centre(new AltosLatLon(latitude, longitude));
255 loader = new AltosMapLoader(this,
260 } catch (ParseException pe) {
261 load_button.setSelected(false);
267 public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
268 SwingUtilities.invokeLater(new Runnable() {
271 for (AltosLaunchSite site : sites) {
272 site_list.insertItemAt(site, i);
279 public AltosUIMapPreloadNew(AltosUIFrame in_owner) {
282 Container pane = getContentPane();
283 GridBagConstraints c = new GridBagConstraints();
284 Insets i = new Insets(4,4,4,4);
286 setTitle("AltOS Load Maps");
288 pane.setLayout(new GridBagLayout());
290 map = new AltosUIMapNew();
292 c.fill = GridBagConstraints.BOTH;
293 c.anchor = GridBagConstraints.CENTER;
301 c.anchor = GridBagConstraints.CENTER;
305 pbar = new JProgressBar();
310 pbar.setStringPainted(true);
312 c.fill = GridBagConstraints.HORIZONTAL;
313 c.anchor = GridBagConstraints.CENTER;
324 site_list_label = new JLabel ("Known Launch Sites:");
326 c.fill = GridBagConstraints.NONE;
327 c.anchor = GridBagConstraints.CENTER;
336 pane.add(site_list_label, c);
338 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
339 site_list.addItemListener(this);
341 new AltosLaunchSites(this);
343 c.fill = GridBagConstraints.HORIZONTAL;
344 c.anchor = GridBagConstraints.CENTER;
353 pane.add(site_list, c);
355 lat = new AltosUIMapPos(owner,
359 c.fill = GridBagConstraints.NONE;
360 c.anchor = GridBagConstraints.CENTER;
368 c.anchor = GridBagConstraints.CENTER;
372 lon = new AltosUIMapPos(owner,
377 c.fill = GridBagConstraints.NONE;
378 c.anchor = GridBagConstraints.CENTER;
386 c.anchor = GridBagConstraints.CENTER;
390 load_button = new JToggleButton("Load Map");
391 load_button.addActionListener(this);
392 load_button.setActionCommand("load");
394 c.fill = GridBagConstraints.NONE;
395 c.anchor = GridBagConstraints.CENTER;
403 c.anchor = GridBagConstraints.CENTER;
405 pane.add(load_button, c);
407 close_button = new JButton("Close");
408 close_button.addActionListener(this);
409 close_button.setActionCommand("close");
411 c.fill = GridBagConstraints.NONE;
412 c.anchor = GridBagConstraints.CENTER;
420 c.anchor = GridBagConstraints.CENTER;
422 pane.add(close_button, c);
424 JLabel types_label = new JLabel("Map Types");
428 pane.add(types_label, c);
432 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
433 maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
434 type == AltosMap.maptype_hybrid);
435 c.gridx = 2 + (type >> 1);
436 c.fill = GridBagConstraints.HORIZONTAL;
437 c.gridy = (type & 1) + 3;
438 pane.add(maptypes[type], c);
441 JLabel min_zoom_label = new JLabel("Minimum Zoom");
444 pane.add(min_zoom_label, c);
446 min_zoom = new JComboBox<Integer>(zooms);
447 min_zoom.setSelectedItem(zooms[10]);
448 min_zoom.setEditable(false);
451 pane.add(min_zoom, c);
453 JLabel max_zoom_label = new JLabel("Maximum Zoom");
456 pane.add(max_zoom_label, c);
458 max_zoom = new JComboBox<Integer>(zooms);
459 max_zoom.setSelectedItem(zooms[14]);
460 max_zoom.setEditable(false);
463 pane.add(max_zoom, c);
465 JLabel radius_label = new JLabel(String.format("Map Radius (%s)",
466 AltosPreferences.imperial_units() ? "miles" : "km"));
469 pane.add(radius_label, c);
472 Double radius_default;
474 if (AltosPreferences.imperial_units())
478 radius = new JComboBox<Double>(radii);
479 radius.setSelectedItem(radii[2]);
480 radius.setEditable(true);
486 setLocationRelativeTo(owner);