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.
21 import java.awt.event.*;
26 import java.lang.Math;
28 import java.net.URLConnection;
29 import org.altusmetrum.altosuilib.*;
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(AltosUI in_owner,
93 double default_value) {
94 super(BoxLayout.X_AXIS);
96 label = new JLabel(label_value);
97 hemi = new JComboBox(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(), Altos.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(Altos.launch_sites_url);
202 } catch (java.net.MalformedURLException e) {
209 public class AltosSiteMapPreload extends AltosUIDialog implements ActionListener, ItemListener {
216 final static int radius = 4;
217 final static int width = (radius * 2 + 1);
218 final static int height = (radius * 2 + 1);
223 JLabel site_list_label;
226 JToggleButton load_button;
228 JButton close_button;
230 static final String[] lat_hemi_names = { "N", "S" };
231 static final String[] lon_hemi_names = { "E", "W" };
233 class updatePbar implements Runnable {
237 public updatePbar(int x, int y, String in_s) {
238 n = (x + radius) + (y + radius) * width + 1;
245 if (n < width * height) {
251 load_button.setSelected(false);
257 class bgLoad extends Thread {
261 public bgLoad(AltosSiteMap in_map) {
266 for (int y = -map.radius; y <= map.radius; y++) {
267 for (int x = -map.radius; x <= map.radius; x++) {
269 pngfile = map.initMap(new Point(x,y));
270 SwingUtilities.invokeLater(new updatePbar(x, y, pngfile));
276 public void set_sites() {
278 for (AltosSite site : sites.sites) {
279 site_list.insertItemAt(site, i);
284 public void itemStateChanged(ItemEvent e) {
285 int state = e.getStateChange();
287 if (state == ItemEvent.SELECTED) {
288 Object o = e.getItem();
289 if (o instanceof AltosSite) {
290 AltosSite site = (AltosSite) o;
291 lat.set_value(site.latitude);
292 lon.set_value(site.longitude);
297 public void actionPerformed(ActionEvent e) {
298 String cmd = e.getActionCommand();
300 if (cmd.equals("close"))
303 if (cmd.equals("load")) {
306 final double latitude = lat.get_value();
307 final double longitude = lon.get_value();
308 map.setBaseLocation(latitude,longitude);
309 map.draw_circle(latitude,longitude);
311 bgLoad thread = new bgLoad(map);
313 } catch (NumberFormatException ne) {
314 load_button.setSelected(false);
320 public AltosSiteMapPreload(AltosUI in_owner) {
323 Container pane = getContentPane();
324 GridBagConstraints c = new GridBagConstraints();
325 Insets i = new Insets(4,4,4,4);
327 pane.setLayout(new GridBagLayout());
329 map = new AltosSiteMap(4);
331 c.fill = GridBagConstraints.BOTH;
332 c.anchor = GridBagConstraints.CENTER;
340 c.anchor = GridBagConstraints.CENTER;
344 pbar = new JProgressBar();
346 pbar.setMaximum(width * height);
349 pbar.setStringPainted(true);
351 c.fill = GridBagConstraints.HORIZONTAL;
352 c.anchor = GridBagConstraints.CENTER;
363 site_list_label = new JLabel ("Known Launch Sites:");
365 c.fill = GridBagConstraints.NONE;
366 c.anchor = GridBagConstraints.CENTER;
375 pane.add(site_list_label, c);
377 site_list = new JComboBox(new String[] { "Site List" });
378 site_list.addItemListener(this);
380 sites = new AltosSites(this);
382 c.fill = GridBagConstraints.HORIZONTAL;
383 c.anchor = GridBagConstraints.CENTER;
392 pane.add(site_list, c);
394 lat = new AltosMapPos(owner,
398 c.fill = GridBagConstraints.NONE;
399 c.anchor = GridBagConstraints.CENTER;
407 c.anchor = GridBagConstraints.CENTER;
411 lon = new AltosMapPos(owner,
416 c.fill = GridBagConstraints.NONE;
417 c.anchor = GridBagConstraints.CENTER;
425 c.anchor = GridBagConstraints.CENTER;
429 load_button = new JToggleButton("Load Map");
430 load_button.addActionListener(this);
431 load_button.setActionCommand("load");
433 c.fill = GridBagConstraints.NONE;
434 c.anchor = GridBagConstraints.CENTER;
442 c.anchor = GridBagConstraints.CENTER;
444 pane.add(load_button, c);
446 close_button = new JButton("Close");
447 close_button.addActionListener(this);
448 close_button.setActionCommand("close");
450 c.fill = GridBagConstraints.NONE;
451 c.anchor = GridBagConstraints.CENTER;
459 c.anchor = GridBagConstraints.CENTER;
461 pane.add(close_button, c);
464 setLocationRelativeTo(owner);