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_12;
22 import java.awt.event.*;
27 import java.lang.Math;
29 import java.net.URLConnection;
30 import org.altusmetrum.altoslib_12.*;
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;
147 JCheckBox[] maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1];
149 JComboBox<Integer> min_zoom;
150 JComboBox<Integer> max_zoom;
152 JComboBox<Double> radius;
155 Integer[] zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
157 Double[] radius_mi = { 1.0, 2.0, 5.0, 10.0, 20.0 };
158 Double radius_def_mi = 5.0;
159 Double[] radius_km = { 2.0, 5.0, 10.0, 20.0, 30.0 };
160 Double radius_def_km = 10.0;
162 AltosMapLoader loader;
164 static final String[] lat_hemi_names = { "N", "S" };
165 static final String[] lon_hemi_names = { "E", "W" };
167 double latitude, longitude;
169 long loader_notify_time;
171 /* AltosMapLoaderListener interfaces */
172 public void loader_start(final int max) {
173 loader_notify_time = System.currentTimeMillis();
175 SwingUtilities.invokeLater(new Runnable() {
177 pbar.setMaximum(max);
184 public void loader_notify(final int cur, final int max, final String name) {
185 long now = System.currentTimeMillis();
187 if (now - loader_notify_time < 100)
190 loader_notify_time = now;
192 SwingUtilities.invokeLater(new Runnable() {
195 pbar.setString(name);
200 public void loader_done(int max) {
202 SwingUtilities.invokeLater(new Runnable() {
206 load_button.setSelected(false);
211 public void debug(String format, Object ... arguments) {
212 if (AltosSerial.debug)
213 System.out.printf(format, arguments);
217 private int all_types() {
219 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
220 if (maptypes[t].isSelected())
221 all_types |= (1 << t);
225 void center_map(double latitude, double longitude) {
226 map.map.centre(new AltosLatLon(latitude, longitude));
228 map.add_mark(latitude, longitude, AltosLib.ao_flight_boost);
233 center_map(lat.get_value(), lon.get_value());
234 } catch (ParseException pe) {
238 public void itemStateChanged(ItemEvent e) {
239 int state = e.getStateChange();
241 if (state == ItemEvent.SELECTED) {
242 Object o = e.getItem();
243 if (o instanceof AltosLaunchSite) {
244 AltosLaunchSite site = (AltosLaunchSite) o;
245 lat.set_value(site.latitude);
246 lon.set_value(site.longitude);
247 center_map(site.latitude, site.longitude);
252 public void actionPerformed(ActionEvent e) {
253 String cmd = e.getActionCommand();
255 if (cmd.equals("close")) {
261 if (cmd.equals("load")) {
262 if (loader == null) {
264 latitude = lat.get_value();
265 longitude = lon.get_value();
266 int min_z = (Integer) min_zoom.getSelectedItem();
267 int max_z = (Integer) max_zoom.getSelectedItem();
270 Double r = (Double) radius.getSelectedItem();
272 if (AltosPreferences.imperial_units())
273 r = AltosConvert.miles_to_meters(r);
277 center_map(latitude, longitude);
279 loader = new AltosMapLoader(this,
284 } catch (ParseException pe) {
285 load_button.setSelected(false);
291 public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
292 SwingUtilities.invokeLater(new Runnable() {
295 for (AltosLaunchSite site : sites) {
296 site_list.insertItemAt(site, i);
303 private void set_radius_values() {
304 radius_label.setText(String.format("Map Radius (%s)",
305 AltosPreferences.imperial_units() ? "mi" : "km"));
309 if (AltosPreferences.imperial_units())
314 radius.removeAllItems();
315 for (Double r : radii) {
318 radius.setSelectedItem(radii[2]);
319 radius.setMaximumRowCount(radii.length);
322 public void units_changed(boolean imperial_units) {
323 map.units_changed(imperial_units);
327 public void font_size_changed(int font_size) {
328 map.font_size_changed(font_size);
331 public AltosUIMapPreload(AltosUIFrame in_owner) {
334 Container pane = getContentPane();
335 GridBagConstraints c = new GridBagConstraints();
336 Insets i = new Insets(4,4,4,4);
338 setTitle("AltOS Load Maps");
340 pane.setLayout(new GridBagLayout());
342 addWindowListener(new WindowAdapter() {
344 public void windowClosing(WindowEvent e) {
345 AltosUIPreferences.unregister_font_listener(AltosUIMapPreload.this);
346 AltosPreferences.unregister_units_listener(AltosUIMapPreload.this);
351 AltosPreferences.register_units_listener(this);
352 AltosUIPreferences.register_font_listener(this);
354 map = new AltosUIMap();
356 c.fill = GridBagConstraints.BOTH;
357 c.anchor = GridBagConstraints.CENTER;
365 c.anchor = GridBagConstraints.CENTER;
369 pbar = new JProgressBar();
374 pbar.setStringPainted(true);
376 c.fill = GridBagConstraints.HORIZONTAL;
377 c.anchor = GridBagConstraints.CENTER;
388 site_list_label = new JLabel ("Known Launch Sites:");
390 c.fill = GridBagConstraints.NONE;
391 c.anchor = GridBagConstraints.CENTER;
400 pane.add(site_list_label, c);
402 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
403 site_list.addItemListener(this);
405 new AltosLaunchSites(this);
407 c.fill = GridBagConstraints.HORIZONTAL;
408 c.anchor = GridBagConstraints.CENTER;
417 pane.add(site_list, c);
419 lat = new AltosUIMapPos(owner, this,
423 c.fill = GridBagConstraints.NONE;
424 c.anchor = GridBagConstraints.CENTER;
432 c.anchor = GridBagConstraints.CENTER;
436 lon = new AltosUIMapPos(owner, this,
441 c.fill = GridBagConstraints.NONE;
442 c.anchor = GridBagConstraints.CENTER;
450 c.anchor = GridBagConstraints.CENTER;
454 load_button = new JToggleButton("Load Map");
455 load_button.addActionListener(this);
456 load_button.setActionCommand("load");
458 c.fill = GridBagConstraints.NONE;
459 c.anchor = GridBagConstraints.CENTER;
467 c.anchor = GridBagConstraints.CENTER;
469 pane.add(load_button, c);
471 close_button = new JButton("Close");
472 close_button.addActionListener(this);
473 close_button.setActionCommand("close");
475 c.fill = GridBagConstraints.NONE;
476 c.anchor = GridBagConstraints.CENTER;
484 c.anchor = GridBagConstraints.CENTER;
486 pane.add(close_button, c);
488 JLabel types_label = new JLabel("Map Types");
492 pane.add(types_label, c);
496 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
497 maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
498 type == AltosMap.maptype_hybrid);
499 c.gridx = 2 + (type >> 1);
500 c.fill = GridBagConstraints.HORIZONTAL;
501 c.gridy = (type & 1) + 3;
502 pane.add(maptypes[type], c);
505 JLabel min_zoom_label = new JLabel("Minimum Zoom");
508 pane.add(min_zoom_label, c);
510 min_zoom = new JComboBox<Integer>(zooms);
511 min_zoom.setSelectedItem(zooms[10]);
512 min_zoom.setEditable(false);
515 pane.add(min_zoom, c);
517 JLabel max_zoom_label = new JLabel("Maximum Zoom");
520 pane.add(max_zoom_label, c);
522 max_zoom = new JComboBox<Integer>(zooms);
523 max_zoom.setSelectedItem(zooms[14]);
524 max_zoom.setEditable(false);
527 pane.add(max_zoom, c);
529 radius_label = new JLabel();
533 pane.add(radius_label, c);
535 radius = new JComboBox<Double>();
536 radius.setEditable(true);
544 setLocationRelativeTo(owner);