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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altosuilib_14;
22 import java.awt.event.*;
27 import java.lang.Math;
29 import java.net.URLConnection;
30 import org.altusmetrum.altoslib_14.*;
32 class AltosUIMapPos extends Box implements ActionListener {
33 AltosUIMapPreload preload;
42 /* ActionListener interface */
43 public void actionPerformed(ActionEvent e) {
47 public void set_value(double new_value) {
54 new_value = -new_value;
56 d = Math.floor(new_value);
57 deg.setText(String.format("%3.0f", d));
58 m = (new_value - d) * 60.0;
59 min.setText(String.format("%7.4f", m));
60 hemi.setSelectedIndex(h);
63 public double get_value() throws ParseException {
64 int h = hemi.getSelectedIndex();
65 String d_t = deg.getText();
66 String m_t = min.getText();
69 d = AltosParse.parse_double_locale(d_t);
70 } catch (ParseException pe) {
71 JOptionPane.showMessageDialog(owner,
72 String.format("Invalid degrees \"%s\"",
75 JOptionPane.ERROR_MESSAGE);
82 m = AltosParse.parse_double_locale(m_t);
83 } catch (ParseException pe) {
84 JOptionPane.showMessageDialog(owner,
85 String.format("Invalid minutes \"%s\"",
88 JOptionPane.ERROR_MESSAGE);
97 public AltosUIMapPos(AltosUIFrame in_owner,
98 AltosUIMapPreload preload,
101 double default_value) {
102 super(BoxLayout.X_AXIS);
104 this.preload = preload;
105 label = new JLabel(label_value);
106 hemi = new JComboBox<String>(hemi_names);
107 hemi.setEditable(false);
108 deg = new JTextField(5);
109 deg.addActionListener(this);
110 deg.setMinimumSize(deg.getPreferredSize());
111 deg.setHorizontalAlignment(JTextField.RIGHT);
112 deg_label = new JLabel("°");
113 min = new JTextField(9);
114 min.addActionListener(this);
115 min.setMinimumSize(min.getPreferredSize());
116 min_label = new JLabel("'");
117 set_value(default_value);
119 add(Box.createRigidArea(new Dimension(5, 0)));
121 add(Box.createRigidArea(new Dimension(5, 0)));
123 add(Box.createRigidArea(new Dimension(5, 0)));
125 add(Box.createRigidArea(new Dimension(5, 0)));
127 add(Box.createRigidArea(new Dimension(5, 0)));
132 public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, ItemListener, AltosLaunchSiteListener, AltosMapLoaderListener, AltosUnitsListener, AltosFontListener {
141 JLabel site_list_label;
142 java.util.List<AltosLaunchSite> sites;
143 JComboBox<AltosLaunchSite> site_list;
145 JToggleButton load_button;
146 JButton close_button;
149 JCheckBox[] maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1];
152 JComboBox<Integer> min_zoom;
153 JComboBox<Integer> max_zoom;
155 JComboBox<Double> radius;
158 Integer[] zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
160 Double[] radius_mi = { 1.0, 2.0, 5.0, 10.0, 20.0 };
161 Double radius_def_mi = 5.0;
162 Double[] radius_km = { 2.0, 5.0, 10.0, 20.0, 30.0 };
163 Double radius_def_km = 10.0;
165 AltosMapLoader loader;
167 static final String[] lat_hemi_names = { "N", "S" };
168 static final String[] lon_hemi_names = { "E", "W" };
170 double latitude, longitude;
172 long loader_notify_time;
174 /* AltosMapLoaderListener interfaces */
175 public void loader_start(final int max) {
176 loader_notify_time = System.currentTimeMillis();
178 SwingUtilities.invokeLater(new Runnable() {
180 pbar.setMaximum(max);
187 public void loader_notify(final int cur, final int max, final String name) {
188 long now = System.currentTimeMillis();
190 if (now - loader_notify_time < 100)
193 loader_notify_time = now;
195 SwingUtilities.invokeLater(new Runnable() {
198 pbar.setString(name);
203 public void loader_done(int max) {
205 SwingUtilities.invokeLater(new Runnable() {
209 load_button.setSelected(false);
214 public void debug(String format, Object ... arguments) {
215 if (AltosSerial.debug)
216 System.out.printf(format, arguments);
220 private int all_types() {
223 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
224 if (maptypes[t].isSelected())
225 all_types |= (1 << t);
228 return 1 << AltosMap.maptype_hybrid;
231 void add_mark(double lat, double lon, int state, String label) {
232 map.add_mark(lat, lon, state, label);
237 AltosLatLon centre = null;
238 String centre_name = null;
239 if (map != null && map.map != null)
240 centre = map.map.centre;
241 for (AltosLaunchSite site : sites) {
242 if (centre != null && centre.lat == site.latitude && centre.lon == site.longitude)
243 centre_name = site.name;
245 add_mark(site.latitude, site.longitude, AltosLib.ao_flight_main, site.name);
248 add_mark(centre.lat, centre.lon, AltosLib.ao_flight_boost, centre_name);
251 void center_map(double latitude, double longitude) {
252 map.map.centre(new AltosLatLon(latitude, longitude));
258 center_map(lat.get_value(), lon.get_value());
259 } catch (ParseException pe) {
263 public void itemStateChanged(ItemEvent e) {
264 int state = e.getStateChange();
266 if (state == ItemEvent.SELECTED) {
267 Object o = e.getItem();
268 if (o instanceof AltosLaunchSite) {
269 AltosLaunchSite site = (AltosLaunchSite) o;
270 lat.set_value(site.latitude);
271 lon.set_value(site.longitude);
272 center_map(site.latitude, site.longitude);
277 public void actionPerformed(ActionEvent e) {
278 String cmd = e.getActionCommand();
280 if (cmd.equals("close")) {
286 if (cmd.equals("load")) {
287 if (loader == null) {
289 latitude = lat.get_value();
290 longitude = lon.get_value();
291 int min_z = (Integer) min_zoom.getSelectedItem();
292 int max_z = (Integer) max_zoom.getSelectedItem();
295 Double r = (Double) radius.getSelectedItem();
297 if (AltosPreferences.imperial_units())
298 r = AltosConvert.miles_to_meters(r);
302 center_map(latitude, longitude);
304 loader = new AltosMapLoader(this,
309 } catch (ParseException pe) {
310 load_button.setSelected(false);
316 public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
318 SwingUtilities.invokeLater(new Runnable() {
321 for (AltosLaunchSite site : sites) {
322 site_list.insertItemAt(site, i);
330 private void set_radius_values() {
331 radius_label.setText(String.format("Map Radius (%s)",
332 AltosPreferences.imperial_units() ? "mi" : "km"));
336 if (AltosPreferences.imperial_units())
341 radius.removeAllItems();
342 for (Double r : radii) {
345 radius.setSelectedItem(radii[2]);
346 radius.setMaximumRowCount(radii.length);
349 public void units_changed(boolean imperial_units) {
350 map.units_changed(imperial_units);
354 public void font_size_changed(int font_size) {
355 map.font_size_changed(font_size);
358 public AltosUIMapPreload(AltosUIFrame in_owner) {
361 Container pane = getScrollablePane();
362 GridBagConstraints c = new GridBagConstraints();
363 Insets i = new Insets(4,4,4,4);
365 setTitle("AltOS Load Maps");
367 pane.setLayout(new GridBagLayout());
369 addWindowListener(new WindowAdapter() {
371 public void windowClosing(WindowEvent e) {
372 AltosUIPreferences.unregister_font_listener(AltosUIMapPreload.this);
373 AltosPreferences.unregister_units_listener(AltosUIMapPreload.this);
378 AltosPreferences.register_units_listener(this);
379 AltosUIPreferences.register_font_listener(this);
381 map = new AltosUIMap();
383 c.fill = GridBagConstraints.BOTH;
384 c.anchor = GridBagConstraints.CENTER;
392 c.anchor = GridBagConstraints.CENTER;
396 pbar = new JProgressBar();
401 pbar.setStringPainted(true);
403 c.fill = GridBagConstraints.HORIZONTAL;
404 c.anchor = GridBagConstraints.CENTER;
415 site_list_label = new JLabel ("Known Launch Sites:");
417 c.fill = GridBagConstraints.NONE;
418 c.anchor = GridBagConstraints.CENTER;
427 pane.add(site_list_label, c);
429 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
430 site_list.addItemListener(this);
432 new AltosLaunchSites(this);
434 c.fill = GridBagConstraints.HORIZONTAL;
435 c.anchor = GridBagConstraints.CENTER;
444 pane.add(site_list, c);
446 lat = new AltosUIMapPos(owner, this,
450 c.fill = GridBagConstraints.NONE;
451 c.anchor = GridBagConstraints.CENTER;
459 c.anchor = GridBagConstraints.CENTER;
463 lon = new AltosUIMapPos(owner, this,
468 c.fill = GridBagConstraints.NONE;
469 c.anchor = GridBagConstraints.CENTER;
477 c.anchor = GridBagConstraints.CENTER;
481 load_button = new JToggleButton("Load Map");
482 load_button.addActionListener(this);
483 load_button.setActionCommand("load");
485 c.fill = GridBagConstraints.NONE;
486 c.anchor = GridBagConstraints.CENTER;
494 c.anchor = GridBagConstraints.CENTER;
496 pane.add(load_button, c);
498 close_button = new JButton("Close");
499 close_button.addActionListener(this);
500 close_button.setActionCommand("close");
502 c.fill = GridBagConstraints.NONE;
503 c.anchor = GridBagConstraints.CENTER;
511 c.anchor = GridBagConstraints.CENTER;
513 pane.add(close_button, c);
516 JLabel types_label = new JLabel("Map Types");
520 pane.add(types_label, c);
524 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
525 maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
526 type == AltosMap.maptype_hybrid);
527 c.gridx = 2 + (type >> 1);
528 c.fill = GridBagConstraints.HORIZONTAL;
529 c.gridy = (type & 1) + 3;
530 pane.add(maptypes[type], c);
534 JLabel min_zoom_label = new JLabel("Minimum Zoom");
537 pane.add(min_zoom_label, c);
539 min_zoom = new JComboBox<Integer>(zooms);
540 min_zoom.setSelectedItem(zooms[10]);
541 min_zoom.setEditable(false);
544 pane.add(min_zoom, c);
546 JLabel max_zoom_label = new JLabel("Maximum Zoom");
549 pane.add(max_zoom_label, c);
551 max_zoom = new JComboBox<Integer>(zooms);
552 max_zoom.setSelectedItem(zooms[14]);
553 max_zoom.setEditable(false);
556 pane.add(max_zoom, c);
558 radius_label = new JLabel();
562 pane.add(radius_label, c);
564 radius = new JComboBox<Double>();
565 radius.setEditable(true);
573 setLocationRelativeTo(owner);