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)));
126 public String toString() {
130 public AltosUISite(String in_name, double in_latitude, double in_longitude) {
132 latitude = in_latitude;
133 longitude = in_longitude;
136 public AltosUISite(String line) throws ParseException {
137 String[] elements = line.split(":");
139 if (elements.length < 3)
140 throw new ParseException(String.format("Invalid site line %s", line), 0);
145 latitude = AltosParse.parse_double_net(elements[1]);
146 longitude = AltosParse.parse_double_net(elements[2]);
147 } catch (ParseException pe) {
148 throw new ParseException(String.format("Invalid site line %s", line), 0);
153 class AltosUISites extends Thread {
154 AltosUIMapPreloadNew preload;
156 LinkedList<AltosUISite> sites;
158 void notify_complete() {
159 SwingUtilities.invokeLater(new Runnable() {
166 void add(AltosUISite site) {
170 void add(String line) {
172 add(new AltosUISite(line));
173 } catch (ParseException pe) {
174 System.out.printf("parse exception %s\n", pe.toString());
180 URLConnection uc = url.openConnection();
181 //int length = uc.getContentLength();
183 InputStreamReader in_stream = new InputStreamReader(uc.getInputStream(), AltosLib.unicode_set);
184 BufferedReader in = new BufferedReader(in_stream);
187 String line = in.readLine();
192 } catch (IOException e) {
198 public AltosUISites(AltosUIMapPreloadNew in_preload) {
199 sites = new LinkedList<AltosUISite>();
200 preload = in_preload;
202 url = new URL(AltosLib.launch_sites_url);
203 } catch (java.net.MalformedURLException e) {
210 public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener, ItemListener, AltosMapTileListener {
223 JLabel site_list_label;
224 JComboBox<AltosUISite> site_list;
226 JToggleButton load_button;
228 JButton close_button;
230 JCheckBox[] maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1];
232 JComboBox<Integer> min_zoom;
233 JComboBox<Integer> max_zoom;
234 JComboBox<Integer> radius;
236 Integer[] zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
237 Integer[] radii = { 1, 2, 3, 4, 5 };
239 static final String[] lat_hemi_names = { "N", "S" };
240 static final String[] lon_hemi_names = { "E", "W" };
242 class updatePbar implements Runnable {
245 public updatePbar(String in_s) {
252 pbar.setMaximum(pbar_max);
258 double latitude, longitude;
272 private void do_load() {
274 map.set_zoom(cur_z + AltosMap.default_zoom);
275 map.set_maptype(cur_type);
276 map.set_load_params(latitude, longitude, r, this);
279 private int next_type(int start) {
281 for (next_type = start;
282 next_type <= AltosMap.maptype_terrain && (all_types & (1 << next_type)) == 0;
288 private void next_load() {
289 int next_type = next_type(cur_type + 1);
291 if (next_type > AltosMap.maptype_terrain) {
292 if (cur_z == max_z) {
297 next_type = next_type(0);
299 cur_type = next_type;
303 private void start_load() {
307 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
308 if (maptypes[t].isSelected()) {
309 all_types |= (1 << t);
313 all_types |= (1 << AltosMap.maptype_hybrid);
317 cur_type = next_type(0);
318 tiles_per_layer = (r * 2 + 1) * (r * 2 + 1);
319 layers_total = (max_z - min_z + 1) * ntype;
321 pbar_max = layers_total * tiles_per_layer;
325 map.add_mark(latitude,longitude, AltosLib.ao_flight_boost);
329 /* AltosMapTileListener methods */
331 public synchronized void notify_tile(AltosMapTile tile, int status) {
332 if (status == AltosMapTile.loading)
335 SwingUtilities.invokeLater(new updatePbar(tile.store.file.toString()));
337 if (tiles_loaded == tiles_per_layer) {
339 if (layers_loaded == layers_total) {
340 SwingUtilities.invokeLater(new Runnable() {
344 load_button.setSelected(false);
349 SwingUtilities.invokeLater(new Runnable() {
358 public AltosMapCache cache() { return cache; }
360 public void set_sites() {
362 for (AltosUISite site : sites.sites) {
363 site_list.insertItemAt(site, i);
368 public void itemStateChanged(ItemEvent e) {
369 int state = e.getStateChange();
371 if (state == ItemEvent.SELECTED) {
372 Object o = e.getItem();
373 if (o instanceof AltosUISite) {
374 AltosUISite site = (AltosUISite) o;
375 lat.set_value(site.latitude);
376 lon.set_value(site.longitude);
381 public void actionPerformed(ActionEvent e) {
382 String cmd = e.getActionCommand();
384 if (cmd.equals("close"))
387 if (cmd.equals("load")) {
390 latitude = lat.get_value();
391 longitude = lon.get_value();
392 min_z = (Integer) min_zoom.getSelectedItem();
393 max_z = (Integer) max_zoom.getSelectedItem();
396 r = (Integer) radius.getSelectedItem();
398 } catch (ParseException pe) {
399 load_button.setSelected(false);
406 public AltosUIMapPreloadNew(AltosUIFrame in_owner) {
409 Container pane = getContentPane();
410 GridBagConstraints c = new GridBagConstraints();
411 Insets i = new Insets(4,4,4,4);
413 setTitle("AltOS Load Maps");
415 pane.setLayout(new GridBagLayout());
417 map = new AltosUIMapNew();
418 cache = new AltosMapCache(map);
420 c.fill = GridBagConstraints.BOTH;
421 c.anchor = GridBagConstraints.CENTER;
429 c.anchor = GridBagConstraints.CENTER;
433 pbar = new JProgressBar();
438 pbar.setStringPainted(true);
440 c.fill = GridBagConstraints.HORIZONTAL;
441 c.anchor = GridBagConstraints.CENTER;
452 site_list_label = new JLabel ("Known Launch Sites:");
454 c.fill = GridBagConstraints.NONE;
455 c.anchor = GridBagConstraints.CENTER;
464 pane.add(site_list_label, c);
466 site_list = new JComboBox<AltosUISite>(new AltosUISite[] { new AltosUISite("Site List", 0, 0) });
467 site_list.addItemListener(this);
469 sites = new AltosUISites(this);
471 c.fill = GridBagConstraints.HORIZONTAL;
472 c.anchor = GridBagConstraints.CENTER;
481 pane.add(site_list, c);
483 lat = new AltosUIMapPos(owner,
487 c.fill = GridBagConstraints.NONE;
488 c.anchor = GridBagConstraints.CENTER;
496 c.anchor = GridBagConstraints.CENTER;
500 lon = new AltosUIMapPos(owner,
505 c.fill = GridBagConstraints.NONE;
506 c.anchor = GridBagConstraints.CENTER;
514 c.anchor = GridBagConstraints.CENTER;
518 load_button = new JToggleButton("Load Map");
519 load_button.addActionListener(this);
520 load_button.setActionCommand("load");
522 c.fill = GridBagConstraints.NONE;
523 c.anchor = GridBagConstraints.CENTER;
531 c.anchor = GridBagConstraints.CENTER;
533 pane.add(load_button, c);
535 close_button = new JButton("Close");
536 close_button.addActionListener(this);
537 close_button.setActionCommand("close");
539 c.fill = GridBagConstraints.NONE;
540 c.anchor = GridBagConstraints.CENTER;
548 c.anchor = GridBagConstraints.CENTER;
550 pane.add(close_button, c);
552 JLabel types_label = new JLabel("Map Types");
556 pane.add(types_label, c);
560 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
561 maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
562 type == AltosMap.maptype_hybrid);
563 c.gridx = 2 + (type >> 1);
564 c.fill = GridBagConstraints.HORIZONTAL;
565 c.gridy = (type & 1) + 3;
566 pane.add(maptypes[type], c);
569 JLabel min_zoom_label = new JLabel("Minimum Zoom");
572 pane.add(min_zoom_label, c);
574 min_zoom = new JComboBox<Integer>(zooms);
575 min_zoom.setSelectedItem(zooms[10]);
576 min_zoom.setEditable(false);
579 pane.add(min_zoom, c);
581 JLabel max_zoom_label = new JLabel("Maximum Zoom");
584 pane.add(max_zoom_label, c);
586 max_zoom = new JComboBox<Integer>(zooms);
587 max_zoom.setSelectedItem(zooms[14]);
588 max_zoom.setEditable(false);
591 pane.add(max_zoom, c);
593 JLabel radius_label = new JLabel("Tile Radius");
596 pane.add(radius_label, c);
598 radius = new JComboBox<Integer>(radii);
599 radius.setSelectedItem(radii[4]);
600 radius.setEditable(true);
606 setLocationRelativeTo(owner);