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_10;
21 import java.awt.event.*;
26 import java.lang.Math;
28 import java.net.URLConnection;
29 import org.altusmetrum.altoslib_10.*;
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 {
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;
140 JComboBox<Double> radius;
142 Integer[] zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
144 Double[] radius_mi = { 1.0, 2.0, 5.0, 10.0, 20.0 };
145 Double radius_def_mi = 5.0;
146 Double[] radius_km = { 2.0, 5.0, 10.0, 20.0, 30.0 };
147 Double radius_def_km = 10.0;
149 AltosMapLoader loader;
151 static final String[] lat_hemi_names = { "N", "S" };
152 static final String[] lon_hemi_names = { "E", "W" };
154 double latitude, longitude;
156 long loader_notify_time;
158 /* AltosMapLoaderListener interfaces */
159 public void loader_start(final int max) {
160 loader_notify_time = System.currentTimeMillis();
162 SwingUtilities.invokeLater(new Runnable() {
164 pbar.setMaximum(max);
168 map.add_mark(latitude, longitude, AltosLib.ao_flight_boost);
173 public void loader_notify(final int cur, final int max, final String name) {
174 long now = System.currentTimeMillis();
176 if (now - loader_notify_time < 100)
179 loader_notify_time = now;
181 SwingUtilities.invokeLater(new Runnable() {
184 pbar.setString(name);
189 public void loader_done(int max) {
191 SwingUtilities.invokeLater(new Runnable() {
195 load_button.setSelected(false);
200 public void debug(String format, Object ... arguments) {
201 if (AltosSerial.debug)
202 System.out.printf(format, arguments);
206 private int all_types() {
208 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
209 if (maptypes[t].isSelected())
210 all_types |= (1 << t);
214 public void itemStateChanged(ItemEvent e) {
215 int state = e.getStateChange();
217 if (state == ItemEvent.SELECTED) {
218 Object o = e.getItem();
219 if (o instanceof AltosLaunchSite) {
220 AltosLaunchSite site = (AltosLaunchSite) o;
221 lat.set_value(site.latitude);
222 lon.set_value(site.longitude);
227 public void actionPerformed(ActionEvent e) {
228 String cmd = e.getActionCommand();
230 if (cmd.equals("close")) {
236 if (cmd.equals("load")) {
237 if (loader == null) {
239 latitude = lat.get_value();
240 longitude = lon.get_value();
241 int min_z = (Integer) min_zoom.getSelectedItem();
242 int max_z = (Integer) max_zoom.getSelectedItem();
245 Double r = (Double) radius.getSelectedItem();
247 if (AltosPreferences.imperial_units())
248 r = AltosConvert.miles_to_meters(r);
252 loader = new AltosMapLoader(map.map, this,
254 min_z, max_z, r, all_types());
256 } catch (ParseException pe) {
257 load_button.setSelected(false);
263 public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
264 SwingUtilities.invokeLater(new Runnable() {
267 for (AltosLaunchSite site : sites) {
268 site_list.insertItemAt(site, i);
275 public AltosUIMapPreloadNew(AltosUIFrame in_owner) {
278 Container pane = getContentPane();
279 GridBagConstraints c = new GridBagConstraints();
280 Insets i = new Insets(4,4,4,4);
282 setTitle("AltOS Load Maps");
284 pane.setLayout(new GridBagLayout());
286 map = new AltosUIMapNew();
288 c.fill = GridBagConstraints.BOTH;
289 c.anchor = GridBagConstraints.CENTER;
297 c.anchor = GridBagConstraints.CENTER;
301 pbar = new JProgressBar();
306 pbar.setStringPainted(true);
308 c.fill = GridBagConstraints.HORIZONTAL;
309 c.anchor = GridBagConstraints.CENTER;
320 site_list_label = new JLabel ("Known Launch Sites:");
322 c.fill = GridBagConstraints.NONE;
323 c.anchor = GridBagConstraints.CENTER;
332 pane.add(site_list_label, c);
334 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
335 site_list.addItemListener(this);
337 new AltosLaunchSites(this);
339 c.fill = GridBagConstraints.HORIZONTAL;
340 c.anchor = GridBagConstraints.CENTER;
349 pane.add(site_list, c);
351 lat = new AltosUIMapPos(owner,
355 c.fill = GridBagConstraints.NONE;
356 c.anchor = GridBagConstraints.CENTER;
364 c.anchor = GridBagConstraints.CENTER;
368 lon = new AltosUIMapPos(owner,
373 c.fill = GridBagConstraints.NONE;
374 c.anchor = GridBagConstraints.CENTER;
382 c.anchor = GridBagConstraints.CENTER;
386 load_button = new JToggleButton("Load Map");
387 load_button.addActionListener(this);
388 load_button.setActionCommand("load");
390 c.fill = GridBagConstraints.NONE;
391 c.anchor = GridBagConstraints.CENTER;
399 c.anchor = GridBagConstraints.CENTER;
401 pane.add(load_button, c);
403 close_button = new JButton("Close");
404 close_button.addActionListener(this);
405 close_button.setActionCommand("close");
407 c.fill = GridBagConstraints.NONE;
408 c.anchor = GridBagConstraints.CENTER;
416 c.anchor = GridBagConstraints.CENTER;
418 pane.add(close_button, c);
420 JLabel types_label = new JLabel("Map Types");
424 pane.add(types_label, c);
428 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
429 maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
430 type == AltosMap.maptype_hybrid);
431 c.gridx = 2 + (type >> 1);
432 c.fill = GridBagConstraints.HORIZONTAL;
433 c.gridy = (type & 1) + 3;
434 pane.add(maptypes[type], c);
437 JLabel min_zoom_label = new JLabel("Minimum Zoom");
440 pane.add(min_zoom_label, c);
442 min_zoom = new JComboBox<Integer>(zooms);
443 min_zoom.setSelectedItem(zooms[10]);
444 min_zoom.setEditable(false);
447 pane.add(min_zoom, c);
449 JLabel max_zoom_label = new JLabel("Maximum Zoom");
452 pane.add(max_zoom_label, c);
454 max_zoom = new JComboBox<Integer>(zooms);
455 max_zoom.setSelectedItem(zooms[14]);
456 max_zoom.setEditable(false);
459 pane.add(max_zoom, c);
461 JLabel radius_label = new JLabel(String.format("Map Radius (%s)",
462 AltosPreferences.imperial_units() ? "miles" : "km"));
465 pane.add(radius_label, c);
468 Double radius_default;
470 if (AltosPreferences.imperial_units())
474 radius = new JComboBox<Double>(radii);
475 radius.setSelectedItem(radii[2]);
476 radius.setEditable(true);
482 setLocationRelativeTo(owner);