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);
234 map.add_mark(lat, lon + 360, state, label);
236 map.add_mark(lat, lon - 360, state, label);
241 AltosLatLon centre = null;
242 String centre_name = null;
243 if (map != null && map.map != null)
244 centre = map.map.centre;
245 for (AltosLaunchSite site : sites) {
246 if (centre != null && centre.lat == site.latitude && centre.lon == site.longitude)
247 centre_name = site.name;
249 add_mark(site.latitude, site.longitude, AltosLib.ao_flight_main, site.name);
252 add_mark(centre.lat, centre.lon, AltosLib.ao_flight_boost, centre_name);
255 void center_map(double latitude, double longitude) {
256 map.map.centre(new AltosLatLon(latitude, longitude));
262 center_map(lat.get_value(), lon.get_value());
263 } catch (ParseException pe) {
267 public void itemStateChanged(ItemEvent e) {
268 int state = e.getStateChange();
270 if (state == ItemEvent.SELECTED) {
271 Object o = e.getItem();
272 if (o instanceof AltosLaunchSite) {
273 AltosLaunchSite site = (AltosLaunchSite) o;
274 lat.set_value(site.latitude);
275 lon.set_value(site.longitude);
276 center_map(site.latitude, site.longitude);
281 public void actionPerformed(ActionEvent e) {
282 String cmd = e.getActionCommand();
284 if (cmd.equals("close")) {
290 if (cmd.equals("load")) {
291 if (loader == null) {
293 latitude = lat.get_value();
294 longitude = lon.get_value();
295 int min_z = (Integer) min_zoom.getSelectedItem();
296 int max_z = (Integer) max_zoom.getSelectedItem();
299 Double r = (Double) radius.getSelectedItem();
301 if (AltosPreferences.imperial_units())
302 r = AltosConvert.miles_to_meters(r);
306 center_map(latitude, longitude);
308 loader = new AltosMapLoader(this,
313 } catch (ParseException pe) {
314 load_button.setSelected(false);
320 public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
322 SwingUtilities.invokeLater(new Runnable() {
325 for (AltosLaunchSite site : sites) {
326 site_list.insertItemAt(site, i);
334 private void set_radius_values() {
335 radius_label.setText(String.format("Map Radius (%s)",
336 AltosPreferences.imperial_units() ? "mi" : "km"));
340 if (AltosPreferences.imperial_units())
345 radius.removeAllItems();
346 for (Double r : radii) {
349 radius.setSelectedItem(radii[2]);
350 radius.setMaximumRowCount(radii.length);
353 public void units_changed(boolean imperial_units) {
354 map.units_changed(imperial_units);
358 public void font_size_changed(int font_size) {
359 map.font_size_changed(font_size);
362 public AltosUIMapPreload(AltosUIFrame in_owner) {
365 Container pane = getScrollablePane();
366 GridBagConstraints c = new GridBagConstraints();
367 Insets i = new Insets(4,4,4,4);
369 setTitle("AltOS Load Maps");
371 pane.setLayout(new GridBagLayout());
373 addWindowListener(new WindowAdapter() {
375 public void windowClosing(WindowEvent e) {
376 AltosUIPreferences.unregister_font_listener(AltosUIMapPreload.this);
377 AltosPreferences.unregister_units_listener(AltosUIMapPreload.this);
382 AltosPreferences.register_units_listener(this);
383 AltosUIPreferences.register_font_listener(this);
385 map = new AltosUIMap();
387 c.fill = GridBagConstraints.BOTH;
388 c.anchor = GridBagConstraints.CENTER;
396 c.anchor = GridBagConstraints.CENTER;
400 pbar = new JProgressBar();
405 pbar.setStringPainted(true);
407 c.fill = GridBagConstraints.HORIZONTAL;
408 c.anchor = GridBagConstraints.CENTER;
419 site_list_label = new JLabel ("Known Launch Sites:");
421 c.fill = GridBagConstraints.NONE;
422 c.anchor = GridBagConstraints.CENTER;
431 pane.add(site_list_label, c);
433 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
434 site_list.addItemListener(this);
436 new AltosLaunchSites(this);
438 c.fill = GridBagConstraints.HORIZONTAL;
439 c.anchor = GridBagConstraints.CENTER;
448 pane.add(site_list, c);
450 lat = new AltosUIMapPos(owner, this,
454 c.fill = GridBagConstraints.NONE;
455 c.anchor = GridBagConstraints.CENTER;
463 c.anchor = GridBagConstraints.CENTER;
467 lon = new AltosUIMapPos(owner, this,
472 c.fill = GridBagConstraints.NONE;
473 c.anchor = GridBagConstraints.CENTER;
481 c.anchor = GridBagConstraints.CENTER;
485 load_button = new JToggleButton("Load Map");
486 load_button.addActionListener(this);
487 load_button.setActionCommand("load");
489 c.fill = GridBagConstraints.NONE;
490 c.anchor = GridBagConstraints.CENTER;
498 c.anchor = GridBagConstraints.CENTER;
500 pane.add(load_button, c);
502 close_button = new JButton("Close");
503 close_button.addActionListener(this);
504 close_button.setActionCommand("close");
506 c.fill = GridBagConstraints.NONE;
507 c.anchor = GridBagConstraints.CENTER;
515 c.anchor = GridBagConstraints.CENTER;
517 pane.add(close_button, c);
520 JLabel types_label = new JLabel("Map Types");
524 pane.add(types_label, c);
528 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
529 maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
530 type == AltosMap.maptype_hybrid);
531 c.gridx = 2 + (type >> 1);
532 c.fill = GridBagConstraints.HORIZONTAL;
533 c.gridy = (type & 1) + 3;
534 pane.add(maptypes[type], c);
538 JLabel min_zoom_label = new JLabel("Minimum Zoom");
541 pane.add(min_zoom_label, c);
543 min_zoom = new JComboBox<Integer>(zooms);
544 min_zoom.setSelectedItem(zooms[10]);
545 min_zoom.setEditable(false);
548 pane.add(min_zoom, c);
550 JLabel max_zoom_label = new JLabel("Maximum Zoom");
553 pane.add(max_zoom_label, c);
555 max_zoom = new JComboBox<Integer>(zooms);
556 max_zoom.setSelectedItem(zooms[14]);
557 max_zoom.setEditable(false);
560 pane.add(max_zoom, c);
562 radius_label = new JLabel();
566 pane.add(radius_label, c);
568 radius = new JComboBox<Double>();
569 radius.setEditable(true);
577 setLocationRelativeTo(owner);