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.image.*;
22 import java.awt.event.*;
24 import javax.swing.event.MouseInputAdapter;
25 import javax.imageio.ImageIO;
26 import javax.swing.table.*;
30 import java.util.prefs.*;
31 import java.lang.Math;
32 import java.awt.geom.Point2D;
33 import java.awt.geom.Line2D;
35 import java.net.URLConnection;
36 import org.altusmetrum.AltosLib.*;
38 class AltosMapPos extends Box {
47 public void set_value(double new_value) {
54 new_value = -new_value;
56 d = Math.floor(new_value);
57 deg.setText(String.format("%3.0f", d));
58 m = (new_value - d) * 60.0;
59 min.setText(String.format("%7.4f", m));
60 hemi.setSelectedIndex(h);
63 public double get_value() throws NumberFormatException {
64 int h = hemi.getSelectedIndex();
65 String d_t = deg.getText();
66 String m_t = min.getText();
69 d = Double.parseDouble(d_t);
70 } catch (NumberFormatException ne) {
71 JOptionPane.showMessageDialog(owner,
72 String.format("Invalid degrees \"%s\"",
75 JOptionPane.ERROR_MESSAGE);
82 m = Double.parseDouble(m_t);
83 } catch (NumberFormatException ne) {
84 JOptionPane.showMessageDialog(owner,
85 String.format("Invalid minutes \"%s\"",
88 JOptionPane.ERROR_MESSAGE);
97 public AltosMapPos(AltosUI in_owner,
100 double default_value) {
101 super(BoxLayout.X_AXIS);
103 label = new JLabel(label_value);
104 hemi = new JComboBox(hemi_names);
105 hemi.setEditable(false);
106 deg = new JTextField(5);
107 deg.setMinimumSize(deg.getPreferredSize());
108 deg.setHorizontalAlignment(JTextField.RIGHT);
109 deg_label = new JLabel("°");
110 min = new JTextField(9);
111 min.setMinimumSize(min.getPreferredSize());
112 min_label = new JLabel("'");
113 set_value(default_value);
115 add(Box.createRigidArea(new Dimension(5, 0)));
117 add(Box.createRigidArea(new Dimension(5, 0)));
119 add(Box.createRigidArea(new Dimension(5, 0)));
121 add(Box.createRigidArea(new Dimension(5, 0)));
123 add(Box.createRigidArea(new Dimension(5, 0)));
133 public String toString() {
137 public AltosSite(String in_name, double in_latitude, double in_longitude) {
139 latitude = in_latitude;
140 longitude = in_longitude;
143 public AltosSite(String line) throws ParseException {
144 String[] elements = line.split(":");
146 if (elements.length < 3)
147 throw new ParseException(String.format("Invalid site line %s", line), 0);
152 latitude = Double.parseDouble(elements[1]);
153 longitude = Double.parseDouble(elements[2]);
154 } catch (NumberFormatException ne) {
155 throw new ParseException(String.format("Invalid site line %s", line), 0);
160 class AltosSites extends Thread {
161 AltosSiteMapPreload preload;
163 LinkedList<AltosSite> sites;
165 void notify_complete() {
166 SwingUtilities.invokeLater(new Runnable() {
173 void add(AltosSite site) {
177 void add(String line) {
179 add(new AltosSite(line));
180 } catch (ParseException pe) {
186 URLConnection uc = url.openConnection();
187 int length = uc.getContentLength();
189 InputStreamReader in_stream = new InputStreamReader(uc.getInputStream(), Altos.unicode_set);
190 BufferedReader in = new BufferedReader(in_stream);
193 String line = in.readLine();
198 } catch (IOException e) {
204 public AltosSites(AltosSiteMapPreload in_preload) {
205 sites = new LinkedList<AltosSite>();
206 preload = in_preload;
208 url = new URL(Altos.launch_sites_url);
209 } catch (java.net.MalformedURLException e) {
216 public class AltosSiteMapPreload extends AltosDialog implements ActionListener, ItemListener {
223 final static int radius = 4;
224 final static int width = (radius * 2 + 1);
225 final static int height = (radius * 2 + 1);
230 JLabel site_list_label;
233 JToggleButton load_button;
235 JButton close_button;
237 static final String[] lat_hemi_names = { "N", "S" };
238 static final String[] lon_hemi_names = { "E", "W" };
240 class updatePbar implements Runnable {
244 public updatePbar(int x, int y, String in_s) {
245 n = (x + radius) + (y + radius) * width + 1;
252 if (n < width * height) {
258 load_button.setSelected(false);
264 class bgLoad extends Thread {
268 public bgLoad(AltosSiteMap in_map) {
273 for (int y = -map.radius; y <= map.radius; y++) {
274 for (int x = -map.radius; x <= map.radius; x++) {
276 pngfile = map.initMap(new Point(x,y));
277 SwingUtilities.invokeLater(new updatePbar(x, y, pngfile));
283 public void set_sites() {
285 for (AltosSite site : sites.sites) {
286 site_list.insertItemAt(site, i);
291 public void itemStateChanged(ItemEvent e) {
292 int state = e.getStateChange();
294 if (state == ItemEvent.SELECTED) {
295 Object o = e.getItem();
296 if (o instanceof AltosSite) {
297 AltosSite site = (AltosSite) o;
298 lat.set_value(site.latitude);
299 lon.set_value(site.longitude);
304 public void actionPerformed(ActionEvent e) {
305 String cmd = e.getActionCommand();
307 if (cmd.equals("close"))
310 if (cmd.equals("load")) {
313 final double latitude = lat.get_value();
314 final double longitude = lon.get_value();
315 map.setBaseLocation(latitude,longitude);
316 map.draw_circle(latitude,longitude);
318 bgLoad thread = new bgLoad(map);
320 } catch (NumberFormatException ne) {
321 load_button.setSelected(false);
327 public AltosSiteMapPreload(AltosUI in_owner) {
330 Container pane = getContentPane();
331 GridBagConstraints c = new GridBagConstraints();
332 Insets i = new Insets(4,4,4,4);
334 pane.setLayout(new GridBagLayout());
336 map = new AltosSiteMap(4);
338 c.fill = GridBagConstraints.BOTH;
339 c.anchor = GridBagConstraints.CENTER;
347 c.anchor = GridBagConstraints.CENTER;
351 pbar = new JProgressBar();
353 pbar.setMaximum(width * height);
356 pbar.setStringPainted(true);
358 c.fill = GridBagConstraints.HORIZONTAL;
359 c.anchor = GridBagConstraints.CENTER;
370 site_list_label = new JLabel ("Known Launch Sites:");
372 c.fill = GridBagConstraints.NONE;
373 c.anchor = GridBagConstraints.CENTER;
382 pane.add(site_list_label, c);
384 site_list = new JComboBox(new String[] { "Site List" });
385 site_list.addItemListener(this);
387 sites = new AltosSites(this);
389 c.fill = GridBagConstraints.HORIZONTAL;
390 c.anchor = GridBagConstraints.CENTER;
399 pane.add(site_list, c);
401 lat = new AltosMapPos(owner,
405 c.fill = GridBagConstraints.NONE;
406 c.anchor = GridBagConstraints.CENTER;
414 c.anchor = GridBagConstraints.CENTER;
418 lon = new AltosMapPos(owner,
423 c.fill = GridBagConstraints.NONE;
424 c.anchor = GridBagConstraints.CENTER;
432 c.anchor = GridBagConstraints.CENTER;
436 load_button = new JToggleButton("Load Map");
437 load_button.addActionListener(this);
438 load_button.setActionCommand("load");
440 c.fill = GridBagConstraints.NONE;
441 c.anchor = GridBagConstraints.CENTER;
449 c.anchor = GridBagConstraints.CENTER;
451 pane.add(load_button, c);
453 close_button = new JButton("Close");
454 close_button.addActionListener(this);
455 close_button.setActionCommand("close");
457 c.fill = GridBagConstraints.NONE;
458 c.anchor = GridBagConstraints.CENTER;
466 c.anchor = GridBagConstraints.CENTER;
468 pane.add(close_button, c);
471 setLocationRelativeTo(owner);