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_2;
21 import java.awt.event.*;
26 import java.lang.Math;
28 import java.net.URLConnection;
29 import org.altusmetrum.altoslib_4.*;
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 NumberFormatException {
57 int h = hemi.getSelectedIndex();
58 String d_t = deg.getText();
59 String m_t = min.getText();
62 d = Double.parseDouble(d_t);
63 } catch (NumberFormatException ne) {
64 JOptionPane.showMessageDialog(owner,
65 String.format("Invalid degrees \"%s\"",
68 JOptionPane.ERROR_MESSAGE);
75 m = Double.parseDouble(m_t);
76 } catch (NumberFormatException ne) {
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 = Double.parseDouble(elements[1]);
146 longitude = Double.parseDouble(elements[2]);
147 } catch (NumberFormatException ne) {
148 throw new ParseException(String.format("Invalid site line %s", line), 0);
153 class AltosUISites extends Thread {
154 AltosUIMapPreload 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) {
179 URLConnection uc = url.openConnection();
180 //int length = uc.getContentLength();
182 InputStreamReader in_stream = new InputStreamReader(uc.getInputStream(), AltosLib.unicode_set);
183 BufferedReader in = new BufferedReader(in_stream);
186 String line = in.readLine();
191 } catch (IOException e) {
197 public AltosUISites(AltosUIMapPreload in_preload) {
198 sites = new LinkedList<AltosUISite>();
199 preload = in_preload;
201 url = new URL(AltosLib.launch_sites_url);
202 } catch (java.net.MalformedURLException e) {
209 public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, ItemListener, AltosUIMapTileListener {
212 AltosUIMapCache cache = new AltosUIMapCache();
222 JLabel site_list_label;
223 JComboBox<AltosUISite> site_list;
225 JToggleButton load_button;
227 JButton close_button;
229 JCheckBox[] maptypes = new JCheckBox[AltosUIMap.maptype_terrain - AltosUIMap.maptype_hybrid + 1];
231 JComboBox<Integer> min_zoom;
232 JComboBox<Integer> max_zoom;
233 JComboBox<Integer> radius;
235 Integer[] zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
236 Integer[] radii = { 1, 2, 3, 4, 5 };
238 static final String[] lat_hemi_names = { "N", "S" };
239 static final String[] lon_hemi_names = { "E", "W" };
241 class updatePbar implements Runnable {
244 public updatePbar(String in_s) {
251 pbar.setMaximum(pbar_max);
257 double latitude, longitude;
271 private void do_load() {
273 map.set_zoom(cur_z + AltosUIMapView.default_zoom);
274 map.set_maptype(cur_type);
275 map.set_load_params(latitude, longitude, r, this);
278 private int next_type(int start) {
280 for (next_type = start;
281 next_type <= AltosUIMap.maptype_terrain && (all_types & (1 << next_type)) == 0;
287 private void next_load() {
288 int next_type = next_type(cur_type + 1);
290 if (next_type > AltosUIMap.maptype_terrain) {
291 if (cur_z == max_z) {
296 next_type = next_type(0);
298 cur_type = next_type;
302 private void start_load() {
306 for (int t = AltosUIMap.maptype_hybrid; t <= AltosUIMap.maptype_terrain; t++)
307 if (maptypes[t].isSelected()) {
308 all_types |= (1 << t);
312 all_types |= (1 << AltosUIMap.maptype_hybrid);
316 cur_type = next_type(0);
317 tiles_per_layer = (r * 2 + 1) * (r * 2 + 1);
318 layers_total = (max_z - min_z + 1) * ntype;
320 pbar_max = layers_total * tiles_per_layer;
324 map.add_mark(latitude,longitude, AltosLib.ao_flight_boost);
328 /* AltosUIMapTileListener methods */
330 public void notify_tile(AltosUIMapTile tile, int status) {
331 if (status == AltosUIMapStore.loading)
334 SwingUtilities.invokeLater(new updatePbar(tile.store.file.toString()));
336 if (tiles_loaded == tiles_per_layer) {
338 if (layers_loaded == layers_total) {
339 SwingUtilities.invokeLater(new Runnable() {
343 load_button.setSelected(false);
348 SwingUtilities.invokeLater(new Runnable() {
357 public AltosUIMapCache cache() { return cache; }
359 public void set_sites() {
361 for (AltosUISite site : sites.sites) {
362 site_list.insertItemAt(site, i);
367 public void itemStateChanged(ItemEvent e) {
368 int state = e.getStateChange();
370 if (state == ItemEvent.SELECTED) {
371 Object o = e.getItem();
372 if (o instanceof AltosUISite) {
373 AltosUISite site = (AltosUISite) o;
374 lat.set_value(site.latitude);
375 lon.set_value(site.longitude);
380 public void actionPerformed(ActionEvent e) {
381 String cmd = e.getActionCommand();
383 if (cmd.equals("close"))
386 if (cmd.equals("load")) {
389 latitude = lat.get_value();
390 longitude = lon.get_value();
391 min_z = (Integer) min_zoom.getSelectedItem();
392 max_z = (Integer) max_zoom.getSelectedItem();
395 r = (Integer) radius.getSelectedItem();
397 } catch (NumberFormatException ne) {
398 load_button.setSelected(false);
405 public AltosUIMapPreload(AltosUIFrame in_owner) {
406 System.out.printf("start creating preload ui\n");
410 Container pane = getContentPane();
411 GridBagConstraints c = new GridBagConstraints();
412 Insets i = new Insets(4,4,4,4);
414 setTitle("AltOS Load Maps");
416 pane.setLayout(new GridBagLayout());
418 map = new AltosUIMap();
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 = AltosUIMap.maptype_hybrid; type <= AltosUIMap.maptype_terrain; type++) {
561 maptypes[type] = new JCheckBox(AltosUIMap.maptype_labels[type],
562 type == AltosUIMap.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);
609 System.out.printf("done creating preload ui\n");