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 AltosMapPos 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 AltosMapPos(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 AltosSite(String in_name, double in_latitude, double in_longitude) {
132 latitude = in_latitude;
133 longitude = in_longitude;
136 public AltosSite(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 AltosSites extends Thread {
154 AltosSiteMapPreload preload;
156 LinkedList<AltosSite> sites;
158 void notify_complete() {
159 SwingUtilities.invokeLater(new Runnable() {
166 void add(AltosSite site) {
170 void add(String line) {
172 add(new AltosSite(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 AltosSites(AltosSiteMapPreload in_preload) {
198 sites = new LinkedList<AltosSite>();
199 preload = in_preload;
201 url = new URL(AltosLib.launch_sites_url);
202 } catch (java.net.MalformedURLException e) {
209 public class AltosSiteMapPreload extends AltosUIFrame implements ActionListener, ItemListener {
220 JLabel site_list_label;
221 JComboBox<AltosSite> site_list;
223 JToggleButton load_button;
225 JButton close_button;
227 JCheckBox[] maptypes = new JCheckBox[AltosSiteMap.maptype_terrain - AltosSiteMap.maptype_hybrid + 1];
229 JComboBox<Integer> min_zoom;
230 JComboBox<Integer> max_zoom;
231 JComboBox<Integer> radius;
233 Integer[] zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
234 Integer[] radii = { 1, 2, 3, 4, 5 };
236 static final String[] lat_hemi_names = { "N", "S" };
237 static final String[] lon_hemi_names = { "E", "W" };
239 class updatePbar implements Runnable {
243 public updatePbar(int n, String in_s) {
252 pbar.setMaximum(pbar_max);
258 load_button.setSelected(false);
264 class bgLoad extends Thread {
273 int min_z = (Integer) min_zoom.getSelectedItem();
274 int max_z = (Integer) max_zoom.getSelectedItem();
276 public bgLoad(AltosSiteMap in_map, double lat, double lon) {
283 for (int t = AltosSiteMap.maptype_hybrid; t <= AltosSiteMap.maptype_terrain; t++)
284 if (maptypes[t].isSelected()) {
289 types |= (1 << AltosSiteMap.maptype_hybrid);
292 r = (Integer) radius.getSelectedItem();
293 pbar_max = (max_z - min_z + 1) * ntype * (r * 2 + 1) * (r * 2 + 1);
298 for (int z = min_z; z <= max_z; z++) {
299 for (int t = AltosSiteMap.maptype_hybrid; t <= AltosSiteMap.maptype_terrain; t++) {
300 if ((types & (1 << t)) == 0)
302 map.clear_base_location();
303 map.set_zoom(z + AltosSiteMap.default_zoom);
306 map.setBaseLocation(lat, lon);
307 map.draw_circle(lat, lon);
308 for (int y = -r; y <= r; y++) {
309 for (int x = -r; x <= r; x++) {
311 pngfile = map.init_map(new Point(x,y),
312 AltosSiteMap.load_mode_cached|AltosSiteMap.load_mode_uncached);
313 SwingUtilities.invokeLater(new updatePbar(++i, pngfile.toString()));
321 public void set_sites() {
323 for (AltosSite site : sites.sites) {
324 site_list.insertItemAt(site, i);
329 public void itemStateChanged(ItemEvent e) {
330 int state = e.getStateChange();
332 if (state == ItemEvent.SELECTED) {
333 Object o = e.getItem();
334 if (o instanceof AltosSite) {
335 AltosSite site = (AltosSite) o;
336 lat.set_value(site.latitude);
337 lon.set_value(site.longitude);
342 public void actionPerformed(ActionEvent e) {
343 String cmd = e.getActionCommand();
345 if (cmd.equals("close"))
348 if (cmd.equals("load")) {
351 final double latitude = lat.get_value();
352 final double longitude = lon.get_value();
353 map.clear_base_location();
354 map.setBaseLocation(latitude,longitude);
355 map.draw_circle(latitude,longitude);
357 bgLoad thread = new bgLoad(map, latitude, longitude);
359 } catch (NumberFormatException ne) {
360 load_button.setSelected(false);
366 public AltosSiteMapPreload(AltosUIFrame in_owner) {
369 Container pane = getContentPane();
370 GridBagConstraints c = new GridBagConstraints();
371 Insets i = new Insets(4,4,4,4);
373 setTitle("AltOS Load Maps");
375 pane.setLayout(new GridBagLayout());
377 map = new AltosSiteMap(2);
379 c.fill = GridBagConstraints.BOTH;
380 c.anchor = GridBagConstraints.CENTER;
388 c.anchor = GridBagConstraints.CENTER;
392 pbar = new JProgressBar();
397 pbar.setStringPainted(true);
399 c.fill = GridBagConstraints.HORIZONTAL;
400 c.anchor = GridBagConstraints.CENTER;
411 site_list_label = new JLabel ("Known Launch Sites:");
413 c.fill = GridBagConstraints.NONE;
414 c.anchor = GridBagConstraints.CENTER;
423 pane.add(site_list_label, c);
425 site_list = new JComboBox<AltosSite>(new AltosSite[] { new AltosSite("Site List", 0, 0) });
426 site_list.addItemListener(this);
428 sites = new AltosSites(this);
430 c.fill = GridBagConstraints.HORIZONTAL;
431 c.anchor = GridBagConstraints.CENTER;
440 pane.add(site_list, c);
442 lat = new AltosMapPos(owner,
446 c.fill = GridBagConstraints.NONE;
447 c.anchor = GridBagConstraints.CENTER;
455 c.anchor = GridBagConstraints.CENTER;
459 lon = new AltosMapPos(owner,
464 c.fill = GridBagConstraints.NONE;
465 c.anchor = GridBagConstraints.CENTER;
473 c.anchor = GridBagConstraints.CENTER;
477 load_button = new JToggleButton("Load Map");
478 load_button.addActionListener(this);
479 load_button.setActionCommand("load");
481 c.fill = GridBagConstraints.NONE;
482 c.anchor = GridBagConstraints.CENTER;
490 c.anchor = GridBagConstraints.CENTER;
492 pane.add(load_button, c);
494 close_button = new JButton("Close");
495 close_button.addActionListener(this);
496 close_button.setActionCommand("close");
498 c.fill = GridBagConstraints.NONE;
499 c.anchor = GridBagConstraints.CENTER;
507 c.anchor = GridBagConstraints.CENTER;
509 pane.add(close_button, c);
511 JLabel types_label = new JLabel("Map Types");
515 pane.add(types_label, c);
519 for (int type = AltosSiteMap.maptype_hybrid; type <= AltosSiteMap.maptype_terrain; type++) {
520 maptypes[type] = new JCheckBox(AltosSiteMap.maptype_labels[type],
521 type == AltosSiteMap.maptype_hybrid);
522 c.gridx = 2 + (type >> 1);
523 c.fill = GridBagConstraints.HORIZONTAL;
524 c.gridy = (type & 1) + 3;
525 pane.add(maptypes[type], c);
528 JLabel min_zoom_label = new JLabel("Minimum Zoom");
531 pane.add(min_zoom_label, c);
533 min_zoom = new JComboBox<Integer>(zooms);
534 min_zoom.setSelectedItem(zooms[10]);
535 min_zoom.setEditable(false);
538 pane.add(min_zoom, c);
540 JLabel max_zoom_label = new JLabel("Maximum Zoom");
543 pane.add(max_zoom_label, c);
545 max_zoom = new JComboBox<Integer>(zooms);
546 max_zoom.setSelectedItem(zooms[14]);
547 max_zoom.setEditable(false);
550 pane.add(max_zoom, c);
552 JLabel radius_label = new JLabel("Tile Radius");
555 pane.add(radius_label, c);
557 radius = new JComboBox<Integer>(radii);
558 radius.setSelectedItem(radii[4]);
559 radius.setEditable(false);
565 setLocationRelativeTo(owner);