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_3;
21 import java.awt.event.*;
26 import java.lang.Math;
28 import java.net.URLConnection;
29 import org.altusmetrum.altoslib_5.*;
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 synchronized 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) {
408 Container pane = getContentPane();
409 GridBagConstraints c = new GridBagConstraints();
410 Insets i = new Insets(4,4,4,4);
412 setTitle("AltOS Load Maps");
414 pane.setLayout(new GridBagLayout());
416 map = new AltosUIMap();
418 c.fill = GridBagConstraints.BOTH;
419 c.anchor = GridBagConstraints.CENTER;
427 c.anchor = GridBagConstraints.CENTER;
431 pbar = new JProgressBar();
436 pbar.setStringPainted(true);
438 c.fill = GridBagConstraints.HORIZONTAL;
439 c.anchor = GridBagConstraints.CENTER;
450 site_list_label = new JLabel ("Known Launch Sites:");
452 c.fill = GridBagConstraints.NONE;
453 c.anchor = GridBagConstraints.CENTER;
462 pane.add(site_list_label, c);
464 site_list = new JComboBox<AltosUISite>(new AltosUISite[] { new AltosUISite("Site List", 0, 0) });
465 site_list.addItemListener(this);
467 sites = new AltosUISites(this);
469 c.fill = GridBagConstraints.HORIZONTAL;
470 c.anchor = GridBagConstraints.CENTER;
479 pane.add(site_list, c);
481 lat = new AltosUIMapPos(owner,
485 c.fill = GridBagConstraints.NONE;
486 c.anchor = GridBagConstraints.CENTER;
494 c.anchor = GridBagConstraints.CENTER;
498 lon = new AltosUIMapPos(owner,
503 c.fill = GridBagConstraints.NONE;
504 c.anchor = GridBagConstraints.CENTER;
512 c.anchor = GridBagConstraints.CENTER;
516 load_button = new JToggleButton("Load Map");
517 load_button.addActionListener(this);
518 load_button.setActionCommand("load");
520 c.fill = GridBagConstraints.NONE;
521 c.anchor = GridBagConstraints.CENTER;
529 c.anchor = GridBagConstraints.CENTER;
531 pane.add(load_button, c);
533 close_button = new JButton("Close");
534 close_button.addActionListener(this);
535 close_button.setActionCommand("close");
537 c.fill = GridBagConstraints.NONE;
538 c.anchor = GridBagConstraints.CENTER;
546 c.anchor = GridBagConstraints.CENTER;
548 pane.add(close_button, c);
550 JLabel types_label = new JLabel("Map Types");
554 pane.add(types_label, c);
558 for (int type = AltosUIMap.maptype_hybrid; type <= AltosUIMap.maptype_terrain; type++) {
559 maptypes[type] = new JCheckBox(AltosUIMap.maptype_labels[type],
560 type == AltosUIMap.maptype_hybrid);
561 c.gridx = 2 + (type >> 1);
562 c.fill = GridBagConstraints.HORIZONTAL;
563 c.gridy = (type & 1) + 3;
564 pane.add(maptypes[type], c);
567 JLabel min_zoom_label = new JLabel("Minimum Zoom");
570 pane.add(min_zoom_label, c);
572 min_zoom = new JComboBox<Integer>(zooms);
573 min_zoom.setSelectedItem(zooms[10]);
574 min_zoom.setEditable(false);
577 pane.add(min_zoom, c);
579 JLabel max_zoom_label = new JLabel("Maximum Zoom");
582 pane.add(max_zoom_label, c);
584 max_zoom = new JComboBox<Integer>(zooms);
585 max_zoom.setSelectedItem(zooms[14]);
586 max_zoom.setEditable(false);
589 pane.add(max_zoom, c);
591 JLabel radius_label = new JLabel("Tile Radius");
594 pane.add(radius_label, c);
596 radius = new JComboBox<Integer>(radii);
597 radius.setSelectedItem(radii[4]);
598 radius.setEditable(true);
604 setLocationRelativeTo(owner);