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 {
221 JLabel site_list_label;
222 JComboBox<AltosUISite> site_list;
224 JToggleButton load_button;
226 JButton close_button;
228 JCheckBox[] maptypes = new JCheckBox[AltosUIMap.maptype_terrain - AltosUIMap.maptype_hybrid + 1];
230 JComboBox<Integer> min_zoom;
231 JComboBox<Integer> max_zoom;
232 JComboBox<Integer> radius;
234 Integer[] zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
235 Integer[] radii = { 1, 2, 3, 4, 5 };
237 static final String[] lat_hemi_names = { "N", "S" };
238 static final String[] lon_hemi_names = { "E", "W" };
240 class updatePbar implements Runnable {
243 public updatePbar(String in_s) {
250 pbar.setMaximum(pbar_max);
256 double latitude, longitude;
270 private void do_load() {
272 map.set_zoom(cur_z + AltosUIMapView.default_zoom);
273 map.set_maptype(cur_type);
274 map.set_load_params(latitude, longitude, r, this);
277 private int next_type(int start) {
279 for (next_type = start;
280 next_type <= AltosUIMap.maptype_terrain && (all_types & (1 << next_type)) == 0;
286 private void next_load() {
287 int next_type = next_type(cur_type + 1);
289 if (next_type > AltosUIMap.maptype_terrain) {
290 if (cur_z == max_z) {
295 next_type = next_type(0);
297 cur_type = next_type;
301 private void start_load() {
305 for (int t = AltosUIMap.maptype_hybrid; t <= AltosUIMap.maptype_terrain; t++)
306 if (maptypes[t].isSelected()) {
307 all_types |= (1 << t);
311 all_types |= (1 << AltosUIMap.maptype_hybrid);
315 cur_type = next_type(0);
316 tiles_per_layer = (r * 2 + 1) * (r * 2 + 1);
317 layers_total = (max_z - min_z + 1) * ntype;
319 pbar_max = layers_total * tiles_per_layer;
323 map.add_mark(latitude,longitude, AltosLib.ao_flight_boost);
327 /* AltosUIMapTileListener methods */
329 public void notify_tile(AltosUIMapTile tile, int status) {
330 if (status == AltosUIMapStore.loading)
333 SwingUtilities.invokeLater(new updatePbar(tile.store.file.toString()));
335 if (tiles_loaded == tiles_per_layer) {
337 if (layers_loaded == layers_total) {
338 SwingUtilities.invokeLater(new Runnable() {
342 load_button.setSelected(false);
347 SwingUtilities.invokeLater(new Runnable() {
356 public void set_sites() {
358 for (AltosUISite site : sites.sites) {
359 site_list.insertItemAt(site, i);
364 public void itemStateChanged(ItemEvent e) {
365 int state = e.getStateChange();
367 if (state == ItemEvent.SELECTED) {
368 Object o = e.getItem();
369 if (o instanceof AltosUISite) {
370 AltosUISite site = (AltosUISite) o;
371 lat.set_value(site.latitude);
372 lon.set_value(site.longitude);
377 public void actionPerformed(ActionEvent e) {
378 String cmd = e.getActionCommand();
380 if (cmd.equals("close"))
383 if (cmd.equals("load")) {
386 latitude = lat.get_value();
387 longitude = lon.get_value();
388 min_z = (Integer) min_zoom.getSelectedItem();
389 max_z = (Integer) max_zoom.getSelectedItem();
392 r = (Integer) radius.getSelectedItem();
394 } catch (NumberFormatException ne) {
395 load_button.setSelected(false);
402 public AltosUIMapPreload(AltosUIFrame in_owner) {
403 System.out.printf("start creating preload ui\n");
407 Container pane = getContentPane();
408 GridBagConstraints c = new GridBagConstraints();
409 Insets i = new Insets(4,4,4,4);
411 setTitle("AltOS Load Maps");
413 pane.setLayout(new GridBagLayout());
415 map = new AltosUIMap();
417 c.fill = GridBagConstraints.BOTH;
418 c.anchor = GridBagConstraints.CENTER;
426 c.anchor = GridBagConstraints.CENTER;
430 pbar = new JProgressBar();
435 pbar.setStringPainted(true);
437 c.fill = GridBagConstraints.HORIZONTAL;
438 c.anchor = GridBagConstraints.CENTER;
449 site_list_label = new JLabel ("Known Launch Sites:");
451 c.fill = GridBagConstraints.NONE;
452 c.anchor = GridBagConstraints.CENTER;
461 pane.add(site_list_label, c);
463 site_list = new JComboBox<AltosUISite>(new AltosUISite[] { new AltosUISite("Site List", 0, 0) });
464 site_list.addItemListener(this);
466 sites = new AltosUISites(this);
468 c.fill = GridBagConstraints.HORIZONTAL;
469 c.anchor = GridBagConstraints.CENTER;
478 pane.add(site_list, c);
480 lat = new AltosUIMapPos(owner,
484 c.fill = GridBagConstraints.NONE;
485 c.anchor = GridBagConstraints.CENTER;
493 c.anchor = GridBagConstraints.CENTER;
497 lon = new AltosUIMapPos(owner,
502 c.fill = GridBagConstraints.NONE;
503 c.anchor = GridBagConstraints.CENTER;
511 c.anchor = GridBagConstraints.CENTER;
515 load_button = new JToggleButton("Load Map");
516 load_button.addActionListener(this);
517 load_button.setActionCommand("load");
519 c.fill = GridBagConstraints.NONE;
520 c.anchor = GridBagConstraints.CENTER;
528 c.anchor = GridBagConstraints.CENTER;
530 pane.add(load_button, c);
532 close_button = new JButton("Close");
533 close_button.addActionListener(this);
534 close_button.setActionCommand("close");
536 c.fill = GridBagConstraints.NONE;
537 c.anchor = GridBagConstraints.CENTER;
545 c.anchor = GridBagConstraints.CENTER;
547 pane.add(close_button, c);
549 JLabel types_label = new JLabel("Map Types");
553 pane.add(types_label, c);
557 for (int type = AltosUIMap.maptype_hybrid; type <= AltosUIMap.maptype_terrain; type++) {
558 maptypes[type] = new JCheckBox(AltosUIMap.maptype_labels[type],
559 type == AltosUIMap.maptype_hybrid);
560 c.gridx = 2 + (type >> 1);
561 c.fill = GridBagConstraints.HORIZONTAL;
562 c.gridy = (type & 1) + 3;
563 pane.add(maptypes[type], c);
566 JLabel min_zoom_label = new JLabel("Minimum Zoom");
569 pane.add(min_zoom_label, c);
571 min_zoom = new JComboBox<Integer>(zooms);
572 min_zoom.setSelectedItem(zooms[10]);
573 min_zoom.setEditable(false);
576 pane.add(min_zoom, c);
578 JLabel max_zoom_label = new JLabel("Maximum Zoom");
581 pane.add(max_zoom_label, c);
583 max_zoom = new JComboBox<Integer>(zooms);
584 max_zoom.setSelectedItem(zooms[14]);
585 max_zoom.setEditable(false);
588 pane.add(max_zoom, c);
590 JLabel radius_label = new JLabel("Tile Radius");
593 pane.add(radius_label, c);
595 radius = new JComboBox<Integer>(radii);
596 radius.setSelectedItem(radii[4]);
597 radius.setEditable(true);
603 setLocationRelativeTo(owner);
606 System.out.printf("done creating preload ui\n");