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_13;
22 import java.awt.event.*;
27 import java.lang.Math;
29 import java.net.URLConnection;
30 import org.altusmetrum.altoslib_13.*;
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 JComboBox<AltosLaunchSite> site_list;
144 JToggleButton load_button;
145 JButton close_button;
148 JCheckBox[] maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1];
151 JComboBox<Integer> min_zoom;
152 JComboBox<Integer> max_zoom;
154 JComboBox<Double> radius;
157 Integer[] zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
159 Double[] radius_mi = { 1.0, 2.0, 5.0, 10.0, 20.0 };
160 Double radius_def_mi = 5.0;
161 Double[] radius_km = { 2.0, 5.0, 10.0, 20.0, 30.0 };
162 Double radius_def_km = 10.0;
164 AltosMapLoader loader;
166 static final String[] lat_hemi_names = { "N", "S" };
167 static final String[] lon_hemi_names = { "E", "W" };
169 double latitude, longitude;
171 long loader_notify_time;
173 /* AltosMapLoaderListener interfaces */
174 public void loader_start(final int max) {
175 loader_notify_time = System.currentTimeMillis();
177 SwingUtilities.invokeLater(new Runnable() {
179 pbar.setMaximum(max);
186 public void loader_notify(final int cur, final int max, final String name) {
187 long now = System.currentTimeMillis();
189 if (now - loader_notify_time < 100)
192 loader_notify_time = now;
194 SwingUtilities.invokeLater(new Runnable() {
197 pbar.setString(name);
202 public void loader_done(int max) {
204 SwingUtilities.invokeLater(new Runnable() {
208 load_button.setSelected(false);
213 public void debug(String format, Object ... arguments) {
214 if (AltosSerial.debug)
215 System.out.printf(format, arguments);
219 private int all_types() {
222 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
223 if (maptypes[t].isSelected())
224 all_types |= (1 << t);
227 return 1 << AltosMap.maptype_hybrid;
230 void center_map(double latitude, double longitude) {
231 map.map.centre(new AltosLatLon(latitude, longitude));
233 map.add_mark(latitude, longitude, AltosLib.ao_flight_boost);
238 center_map(lat.get_value(), lon.get_value());
239 } catch (ParseException pe) {
243 public void itemStateChanged(ItemEvent e) {
244 int state = e.getStateChange();
246 if (state == ItemEvent.SELECTED) {
247 Object o = e.getItem();
248 if (o instanceof AltosLaunchSite) {
249 AltosLaunchSite site = (AltosLaunchSite) o;
250 lat.set_value(site.latitude);
251 lon.set_value(site.longitude);
252 center_map(site.latitude, site.longitude);
257 public void actionPerformed(ActionEvent e) {
258 String cmd = e.getActionCommand();
260 if (cmd.equals("close")) {
266 if (cmd.equals("load")) {
267 if (loader == null) {
269 latitude = lat.get_value();
270 longitude = lon.get_value();
271 int min_z = (Integer) min_zoom.getSelectedItem();
272 int max_z = (Integer) max_zoom.getSelectedItem();
275 Double r = (Double) radius.getSelectedItem();
277 if (AltosPreferences.imperial_units())
278 r = AltosConvert.miles_to_meters(r);
282 center_map(latitude, longitude);
284 loader = new AltosMapLoader(this,
289 } catch (ParseException pe) {
290 load_button.setSelected(false);
296 public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
297 SwingUtilities.invokeLater(new Runnable() {
300 for (AltosLaunchSite site : sites) {
301 site_list.insertItemAt(site, i);
308 private void set_radius_values() {
309 radius_label.setText(String.format("Map Radius (%s)",
310 AltosPreferences.imperial_units() ? "mi" : "km"));
314 if (AltosPreferences.imperial_units())
319 radius.removeAllItems();
320 for (Double r : radii) {
323 radius.setSelectedItem(radii[2]);
324 radius.setMaximumRowCount(radii.length);
327 public void units_changed(boolean imperial_units) {
328 map.units_changed(imperial_units);
332 public void font_size_changed(int font_size) {
333 map.font_size_changed(font_size);
336 public AltosUIMapPreload(AltosUIFrame in_owner) {
339 Container pane = getContentPane();
340 GridBagConstraints c = new GridBagConstraints();
341 Insets i = new Insets(4,4,4,4);
343 setTitle("AltOS Load Maps");
345 pane.setLayout(new GridBagLayout());
347 addWindowListener(new WindowAdapter() {
349 public void windowClosing(WindowEvent e) {
350 AltosUIPreferences.unregister_font_listener(AltosUIMapPreload.this);
351 AltosPreferences.unregister_units_listener(AltosUIMapPreload.this);
356 AltosPreferences.register_units_listener(this);
357 AltosUIPreferences.register_font_listener(this);
359 map = new AltosUIMap();
361 c.fill = GridBagConstraints.BOTH;
362 c.anchor = GridBagConstraints.CENTER;
370 c.anchor = GridBagConstraints.CENTER;
374 pbar = new JProgressBar();
379 pbar.setStringPainted(true);
381 c.fill = GridBagConstraints.HORIZONTAL;
382 c.anchor = GridBagConstraints.CENTER;
393 site_list_label = new JLabel ("Known Launch Sites:");
395 c.fill = GridBagConstraints.NONE;
396 c.anchor = GridBagConstraints.CENTER;
405 pane.add(site_list_label, c);
407 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
408 site_list.addItemListener(this);
410 new AltosLaunchSites(this);
412 c.fill = GridBagConstraints.HORIZONTAL;
413 c.anchor = GridBagConstraints.CENTER;
422 pane.add(site_list, c);
424 lat = new AltosUIMapPos(owner, this,
428 c.fill = GridBagConstraints.NONE;
429 c.anchor = GridBagConstraints.CENTER;
437 c.anchor = GridBagConstraints.CENTER;
441 lon = new AltosUIMapPos(owner, this,
446 c.fill = GridBagConstraints.NONE;
447 c.anchor = GridBagConstraints.CENTER;
455 c.anchor = GridBagConstraints.CENTER;
459 load_button = new JToggleButton("Load Map");
460 load_button.addActionListener(this);
461 load_button.setActionCommand("load");
463 c.fill = GridBagConstraints.NONE;
464 c.anchor = GridBagConstraints.CENTER;
472 c.anchor = GridBagConstraints.CENTER;
474 pane.add(load_button, c);
476 close_button = new JButton("Close");
477 close_button.addActionListener(this);
478 close_button.setActionCommand("close");
480 c.fill = GridBagConstraints.NONE;
481 c.anchor = GridBagConstraints.CENTER;
489 c.anchor = GridBagConstraints.CENTER;
491 pane.add(close_button, c);
494 JLabel types_label = new JLabel("Map Types");
498 pane.add(types_label, c);
502 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
503 maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
504 type == AltosMap.maptype_hybrid);
505 c.gridx = 2 + (type >> 1);
506 c.fill = GridBagConstraints.HORIZONTAL;
507 c.gridy = (type & 1) + 3;
508 pane.add(maptypes[type], c);
512 JLabel min_zoom_label = new JLabel("Minimum Zoom");
515 pane.add(min_zoom_label, c);
517 min_zoom = new JComboBox<Integer>(zooms);
518 min_zoom.setSelectedItem(zooms[10]);
519 min_zoom.setEditable(false);
522 pane.add(min_zoom, c);
524 JLabel max_zoom_label = new JLabel("Maximum Zoom");
527 pane.add(max_zoom_label, c);
529 max_zoom = new JComboBox<Integer>(zooms);
530 max_zoom.setSelectedItem(zooms[14]);
531 max_zoom.setEditable(false);
534 pane.add(max_zoom, c);
536 radius_label = new JLabel();
540 pane.add(radius_label, c);
542 radius = new JComboBox<Double>();
543 radius.setEditable(true);
551 setLocationRelativeTo(owner);