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, AltosUnitsListener, AltosFontListener {
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;
141 JComboBox<Double> radius;
144 Integer[] zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
146 Double[] radius_mi = { 1.0, 2.0, 5.0, 10.0, 20.0 };
147 Double radius_def_mi = 5.0;
148 Double[] radius_km = { 2.0, 5.0, 10.0, 20.0, 30.0 };
149 Double radius_def_km = 10.0;
151 AltosMapLoader loader;
153 static final String[] lat_hemi_names = { "N", "S" };
154 static final String[] lon_hemi_names = { "E", "W" };
156 double latitude, longitude;
158 long loader_notify_time;
160 /* AltosMapLoaderListener interfaces */
161 public void loader_start(final int max) {
162 loader_notify_time = System.currentTimeMillis();
164 SwingUtilities.invokeLater(new Runnable() {
166 pbar.setMaximum(max);
170 map.add_mark(latitude, longitude, AltosLib.ao_flight_boost);
175 public void loader_notify(final int cur, final int max, final String name) {
176 long now = System.currentTimeMillis();
178 if (now - loader_notify_time < 100)
181 loader_notify_time = now;
183 SwingUtilities.invokeLater(new Runnable() {
186 pbar.setString(name);
191 public void loader_done(int max) {
193 SwingUtilities.invokeLater(new Runnable() {
197 load_button.setSelected(false);
202 public void debug(String format, Object ... arguments) {
203 if (AltosSerial.debug)
204 System.out.printf(format, arguments);
208 private int all_types() {
210 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
211 if (maptypes[t].isSelected())
212 all_types |= (1 << t);
216 public void itemStateChanged(ItemEvent e) {
217 int state = e.getStateChange();
219 if (state == ItemEvent.SELECTED) {
220 Object o = e.getItem();
221 if (o instanceof AltosLaunchSite) {
222 AltosLaunchSite site = (AltosLaunchSite) o;
223 lat.set_value(site.latitude);
224 lon.set_value(site.longitude);
229 public void actionPerformed(ActionEvent e) {
230 String cmd = e.getActionCommand();
232 if (cmd.equals("close")) {
238 if (cmd.equals("load")) {
239 if (loader == null) {
241 latitude = lat.get_value();
242 longitude = lon.get_value();
243 int min_z = (Integer) min_zoom.getSelectedItem();
244 int max_z = (Integer) max_zoom.getSelectedItem();
247 Double r = (Double) radius.getSelectedItem();
249 if (AltosPreferences.imperial_units())
250 r = AltosConvert.miles_to_meters(r);
254 map.map.centre(new AltosLatLon(latitude, longitude));
256 loader = new AltosMapLoader(this,
261 } catch (ParseException pe) {
262 load_button.setSelected(false);
268 public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
269 SwingUtilities.invokeLater(new Runnable() {
272 for (AltosLaunchSite site : sites) {
273 site_list.insertItemAt(site, i);
280 private void set_radius_values() {
281 radius_label.setText(String.format("Map Radius (%s)",
282 AltosPreferences.imperial_units() ? "mi" : "km"));
286 if (AltosPreferences.imperial_units())
291 radius.removeAllItems();
292 for (Double r : radii) {
293 System.out.printf("adding radius %f\n",r);
296 radius.setSelectedItem(radii[2]);
297 radius.setMaximumRowCount(radii.length);
300 public void units_changed(boolean imperial_units) {
301 map.units_changed(imperial_units);
305 public void font_size_changed(int font_size) {
306 map.font_size_changed(font_size);
309 public AltosUIMapPreloadNew(AltosUIFrame in_owner) {
312 Container pane = getContentPane();
313 GridBagConstraints c = new GridBagConstraints();
314 Insets i = new Insets(4,4,4,4);
316 setTitle("AltOS Load Maps");
318 pane.setLayout(new GridBagLayout());
320 addWindowListener(new WindowAdapter() {
322 public void windowClosing(WindowEvent e) {
323 AltosUIPreferences.unregister_font_listener(AltosUIMapPreloadNew.this);
324 AltosPreferences.unregister_units_listener(AltosUIMapPreloadNew.this);
329 AltosPreferences.register_units_listener(this);
330 AltosUIPreferences.register_font_listener(this);
332 map = new AltosUIMapNew();
334 c.fill = GridBagConstraints.BOTH;
335 c.anchor = GridBagConstraints.CENTER;
343 c.anchor = GridBagConstraints.CENTER;
347 pbar = new JProgressBar();
352 pbar.setStringPainted(true);
354 c.fill = GridBagConstraints.HORIZONTAL;
355 c.anchor = GridBagConstraints.CENTER;
366 site_list_label = new JLabel ("Known Launch Sites:");
368 c.fill = GridBagConstraints.NONE;
369 c.anchor = GridBagConstraints.CENTER;
378 pane.add(site_list_label, c);
380 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
381 site_list.addItemListener(this);
383 new AltosLaunchSites(this);
385 c.fill = GridBagConstraints.HORIZONTAL;
386 c.anchor = GridBagConstraints.CENTER;
395 pane.add(site_list, c);
397 lat = new AltosUIMapPos(owner,
401 c.fill = GridBagConstraints.NONE;
402 c.anchor = GridBagConstraints.CENTER;
410 c.anchor = GridBagConstraints.CENTER;
414 lon = new AltosUIMapPos(owner,
419 c.fill = GridBagConstraints.NONE;
420 c.anchor = GridBagConstraints.CENTER;
428 c.anchor = GridBagConstraints.CENTER;
432 load_button = new JToggleButton("Load Map");
433 load_button.addActionListener(this);
434 load_button.setActionCommand("load");
436 c.fill = GridBagConstraints.NONE;
437 c.anchor = GridBagConstraints.CENTER;
445 c.anchor = GridBagConstraints.CENTER;
447 pane.add(load_button, c);
449 close_button = new JButton("Close");
450 close_button.addActionListener(this);
451 close_button.setActionCommand("close");
453 c.fill = GridBagConstraints.NONE;
454 c.anchor = GridBagConstraints.CENTER;
462 c.anchor = GridBagConstraints.CENTER;
464 pane.add(close_button, c);
466 JLabel types_label = new JLabel("Map Types");
470 pane.add(types_label, c);
474 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
475 maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
476 type == AltosMap.maptype_hybrid);
477 c.gridx = 2 + (type >> 1);
478 c.fill = GridBagConstraints.HORIZONTAL;
479 c.gridy = (type & 1) + 3;
480 pane.add(maptypes[type], c);
483 JLabel min_zoom_label = new JLabel("Minimum Zoom");
486 pane.add(min_zoom_label, c);
488 min_zoom = new JComboBox<Integer>(zooms);
489 min_zoom.setSelectedItem(zooms[10]);
490 min_zoom.setEditable(false);
493 pane.add(min_zoom, c);
495 JLabel max_zoom_label = new JLabel("Maximum Zoom");
498 pane.add(max_zoom_label, c);
500 max_zoom = new JComboBox<Integer>(zooms);
501 max_zoom.setSelectedItem(zooms[14]);
502 max_zoom.setEditable(false);
505 pane.add(max_zoom, c);
507 radius_label = new JLabel();
511 pane.add(radius_label, c);
513 radius = new JComboBox<Double>();
514 radius.setEditable(true);
522 setLocationRelativeTo(owner);