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_9;
21 import java.awt.event.*;
26 import java.lang.Math;
28 import java.net.URLConnection;
29 import org.altusmetrum.altoslib_9.*;
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 AltosMapLoader loader;
132 JLabel site_list_label;
133 JComboBox<AltosLaunchSite> site_list;
135 JToggleButton load_button;
137 JButton close_button;
139 JCheckBox[] maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1];
141 JComboBox<Integer> min_zoom;
142 JComboBox<Integer> max_zoom;
143 JComboBox<Double> radius;
145 Integer[] zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
147 Double[] radius_mi = { 1.0, 2.0, 5.0, 10.0, 20.0 };
148 Double radius_def_mi = 5.0;
149 Double[] radius_km = { 2.0, 5.0, 10.0, 20.0, 30.0 };
150 Double radius_def_km = 10.0;
153 static final String[] lat_hemi_names = { "N", "S" };
154 static final String[] lon_hemi_names = { "E", "W" };
156 double latitude, longitude;
158 /* AltosMapLoaderListener interfaces */
159 public void loader_start(final int max) {
160 SwingUtilities.invokeLater(new Runnable() {
162 pbar.setMaximum(max);
166 map.add_mark(latitude, longitude, AltosLib.ao_flight_boost);
171 public void loader_notify(final int cur, final int max, final String name) {
172 SwingUtilities.invokeLater(new Runnable() {
175 pbar.setString(name);
180 public void loader_done(int max) {
181 SwingUtilities.invokeLater(new Runnable() {
185 load_button.setSelected(false);
191 public void debug(String format, Object ... arguments) {
192 System.out.printf(format, arguments);
196 private int all_types() {
198 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
199 if (maptypes[t].isSelected())
200 all_types |= (1 << t);
204 public void itemStateChanged(ItemEvent e) {
205 int state = e.getStateChange();
207 if (state == ItemEvent.SELECTED) {
208 Object o = e.getItem();
209 if (o instanceof AltosLaunchSite) {
210 AltosLaunchSite site = (AltosLaunchSite) o;
211 lat.set_value(site.latitude);
212 lon.set_value(site.longitude);
217 public void actionPerformed(ActionEvent e) {
218 String cmd = e.getActionCommand();
220 if (cmd.equals("close"))
223 if (cmd.equals("load")) {
226 latitude = lat.get_value();
227 longitude = lon.get_value();
228 int min_z = (Integer) min_zoom.getSelectedItem();
229 int max_z = (Integer) max_zoom.getSelectedItem();
232 Double r = (Double) radius.getSelectedItem();
234 if (AltosPreferences.imperial_units())
235 r = AltosConvert.distance.inverse(r);
240 loader.load(latitude, longitude, min_z, max_z, r, all_types());
241 } catch (ParseException pe) {
242 load_button.setSelected(false);
248 public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
249 SwingUtilities.invokeLater(new Runnable() {
252 for (AltosLaunchSite site : sites) {
253 site_list.insertItemAt(site, i);
260 public AltosUIMapPreloadNew(AltosUIFrame in_owner) {
263 Container pane = getContentPane();
264 GridBagConstraints c = new GridBagConstraints();
265 Insets i = new Insets(4,4,4,4);
267 setTitle("AltOS Load Maps");
269 pane.setLayout(new GridBagLayout());
271 map = new AltosUIMapNew();
273 loader = new AltosMapLoader(map.map, this);
275 c.fill = GridBagConstraints.BOTH;
276 c.anchor = GridBagConstraints.CENTER;
284 c.anchor = GridBagConstraints.CENTER;
288 pbar = new JProgressBar();
293 pbar.setStringPainted(true);
295 c.fill = GridBagConstraints.HORIZONTAL;
296 c.anchor = GridBagConstraints.CENTER;
307 site_list_label = new JLabel ("Known Launch Sites:");
309 c.fill = GridBagConstraints.NONE;
310 c.anchor = GridBagConstraints.CENTER;
319 pane.add(site_list_label, c);
321 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
322 site_list.addItemListener(this);
324 new AltosLaunchSites(this);
326 c.fill = GridBagConstraints.HORIZONTAL;
327 c.anchor = GridBagConstraints.CENTER;
336 pane.add(site_list, c);
338 lat = new AltosUIMapPos(owner,
342 c.fill = GridBagConstraints.NONE;
343 c.anchor = GridBagConstraints.CENTER;
351 c.anchor = GridBagConstraints.CENTER;
355 lon = new AltosUIMapPos(owner,
360 c.fill = GridBagConstraints.NONE;
361 c.anchor = GridBagConstraints.CENTER;
369 c.anchor = GridBagConstraints.CENTER;
373 load_button = new JToggleButton("Load Map");
374 load_button.addActionListener(this);
375 load_button.setActionCommand("load");
377 c.fill = GridBagConstraints.NONE;
378 c.anchor = GridBagConstraints.CENTER;
386 c.anchor = GridBagConstraints.CENTER;
388 pane.add(load_button, c);
390 close_button = new JButton("Close");
391 close_button.addActionListener(this);
392 close_button.setActionCommand("close");
394 c.fill = GridBagConstraints.NONE;
395 c.anchor = GridBagConstraints.CENTER;
403 c.anchor = GridBagConstraints.CENTER;
405 pane.add(close_button, c);
407 JLabel types_label = new JLabel("Map Types");
411 pane.add(types_label, c);
415 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
416 maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
417 type == AltosMap.maptype_hybrid);
418 c.gridx = 2 + (type >> 1);
419 c.fill = GridBagConstraints.HORIZONTAL;
420 c.gridy = (type & 1) + 3;
421 pane.add(maptypes[type], c);
424 JLabel min_zoom_label = new JLabel("Minimum Zoom");
427 pane.add(min_zoom_label, c);
429 min_zoom = new JComboBox<Integer>(zooms);
430 min_zoom.setSelectedItem(zooms[10]);
431 min_zoom.setEditable(false);
434 pane.add(min_zoom, c);
436 JLabel max_zoom_label = new JLabel("Maximum Zoom");
439 pane.add(max_zoom_label, c);
441 max_zoom = new JComboBox<Integer>(zooms);
442 max_zoom.setSelectedItem(zooms[14]);
443 max_zoom.setEditable(false);
446 pane.add(max_zoom, c);
448 JLabel radius_label = new JLabel(String.format("Map Radius (%s)",
449 AltosPreferences.imperial_units() ? "miles" : "km"));
452 pane.add(radius_label, c);
455 Double radius_default;
457 if (AltosPreferences.imperial_units())
461 radius = new JComboBox<Double>(radii);
462 radius.setSelectedItem(radii[2]);
463 radius.setEditable(true);
469 setLocationRelativeTo(owner);