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_7;
21 import java.awt.event.*;
26 import java.lang.Math;
28 import java.net.URLConnection;
29 import org.altusmetrum.altoslib_7.*;
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, AltosMapTileListener, AltosLaunchSiteListener {
133 JLabel site_list_label;
134 JComboBox<AltosLaunchSite> site_list;
136 JToggleButton load_button;
138 JButton close_button;
140 JCheckBox[] maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1];
142 JComboBox<Integer> min_zoom;
143 JComboBox<Integer> max_zoom;
144 JComboBox<Integer> radius;
146 Integer[] zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
147 Integer[] radii = { 1, 2, 3, 4, 5 };
149 static final String[] lat_hemi_names = { "N", "S" };
150 static final String[] lon_hemi_names = { "E", "W" };
152 class updatePbar implements Runnable {
155 public updatePbar(String in_s) {
162 pbar.setMaximum(pbar_max);
168 double latitude, longitude;
182 private void do_load() {
184 map.set_zoom(cur_z + AltosMap.default_zoom);
185 map.set_maptype(cur_type);
186 map.set_load_params(latitude, longitude, r, this);
189 private int next_type(int start) {
191 for (next_type = start;
192 next_type <= AltosMap.maptype_terrain && (all_types & (1 << next_type)) == 0;
198 private void next_load() {
199 int next_type = next_type(cur_type + 1);
201 if (next_type > AltosMap.maptype_terrain) {
202 if (cur_z == max_z) {
207 next_type = next_type(0);
209 cur_type = next_type;
213 private void start_load() {
217 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
218 if (maptypes[t].isSelected()) {
219 all_types |= (1 << t);
223 all_types |= (1 << AltosMap.maptype_hybrid);
227 cur_type = next_type(0);
228 tiles_per_layer = (r * 2 + 1) * (r * 2 + 1);
229 layers_total = (max_z - min_z + 1) * ntype;
231 pbar_max = layers_total * tiles_per_layer;
235 map.add_mark(latitude,longitude, AltosLib.ao_flight_boost);
239 /* AltosMapTileListener methods */
241 public synchronized void notify_tile(AltosMapTile tile, int status) {
242 if (status == AltosMapTile.loading)
245 SwingUtilities.invokeLater(new updatePbar(tile.store.file.toString()));
247 if (tiles_loaded == tiles_per_layer) {
249 if (layers_loaded == layers_total) {
250 SwingUtilities.invokeLater(new Runnable() {
254 load_button.setSelected(false);
259 SwingUtilities.invokeLater(new Runnable() {
268 public AltosMapCache cache() { return cache; }
270 public void itemStateChanged(ItemEvent e) {
271 int state = e.getStateChange();
273 if (state == ItemEvent.SELECTED) {
274 Object o = e.getItem();
275 if (o instanceof AltosLaunchSite) {
276 AltosLaunchSite site = (AltosLaunchSite) o;
277 lat.set_value(site.latitude);
278 lon.set_value(site.longitude);
283 public void actionPerformed(ActionEvent e) {
284 String cmd = e.getActionCommand();
286 if (cmd.equals("close"))
289 if (cmd.equals("load")) {
292 latitude = lat.get_value();
293 longitude = lon.get_value();
294 min_z = (Integer) min_zoom.getSelectedItem();
295 max_z = (Integer) max_zoom.getSelectedItem();
298 r = (Integer) radius.getSelectedItem();
300 } catch (ParseException pe) {
301 load_button.setSelected(false);
308 public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
309 SwingUtilities.invokeLater(new Runnable() {
312 for (AltosLaunchSite site : sites) {
313 site_list.insertItemAt(site, i);
320 public AltosUIMapPreloadNew(AltosUIFrame in_owner) {
323 Container pane = getContentPane();
324 GridBagConstraints c = new GridBagConstraints();
325 Insets i = new Insets(4,4,4,4);
327 setTitle("AltOS Load Maps");
329 pane.setLayout(new GridBagLayout());
331 map = new AltosUIMapNew();
332 cache = new AltosMapCache(map);
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 JLabel radius_label = new JLabel("Tile Radius");
510 pane.add(radius_label, c);
512 radius = new JComboBox<Integer>(radii);
513 radius.setSelectedItem(radii[4]);
514 radius.setEditable(true);
520 setLocationRelativeTo(owner);