altoslib: Create AltosProgrammer class
[fw/altos] / altosui / AltosSiteMapPreload.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import java.io.*;
24 import java.util.*;
25 import java.text.*;
26 import java.lang.Math;
27 import java.net.URL;
28 import java.net.URLConnection;
29 import org.altusmetrum.altosuilib_1.*;
30
31 class AltosMapPos extends Box {
32         AltosUI         owner;
33         JLabel          label;
34         JComboBox       hemi;
35         JTextField      deg;
36         JLabel          deg_label;
37         JTextField      min;
38         JLabel          min_label;
39
40         public void set_value(double new_value) {
41                 double  d, m;
42                 int     h;
43
44                 h = 0;
45                 if (new_value < 0) {
46                         h = 1;
47                         new_value = -new_value;
48                 }
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);
54         }
55
56         public double get_value() throws NumberFormatException {
57                 int     h = hemi.getSelectedIndex();
58                 String  d_t = deg.getText();
59                 String  m_t = min.getText();
60                 double  d, m, v;
61                 try {
62                         d = Double.parseDouble(d_t);
63                 } catch (NumberFormatException ne) {
64                         JOptionPane.showMessageDialog(owner,
65                                                       String.format("Invalid degrees \"%s\"",
66                                                                     d_t),
67                                                       "Invalid number",
68                                                       JOptionPane.ERROR_MESSAGE);
69                         throw ne;
70                 }
71                 try {
72                         if (m_t.equals(""))
73                                 m = 0;
74                         else
75                                 m = Double.parseDouble(m_t);
76                 } catch (NumberFormatException ne) {
77                         JOptionPane.showMessageDialog(owner,
78                                                       String.format("Invalid minutes \"%s\"",
79                                                                     m_t),
80                                                       "Invalid number",
81                                                       JOptionPane.ERROR_MESSAGE);
82                         throw ne;
83                 }
84                 v = d + m/60.0;
85                 if (h == 1)
86                         v = -v;
87                 return v;
88         }
89
90         public AltosMapPos(AltosUI in_owner,
91                            String label_value,
92                            String[] hemi_names,
93                            double default_value) {
94                 super(BoxLayout.X_AXIS);
95                 owner = in_owner;
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);
107                 add(label);
108                 add(Box.createRigidArea(new Dimension(5, 0)));
109                 add(hemi);
110                 add(Box.createRigidArea(new Dimension(5, 0)));
111                 add(deg);
112                 add(Box.createRigidArea(new Dimension(5, 0)));
113                 add(deg_label);
114                 add(Box.createRigidArea(new Dimension(5, 0)));
115                 add(min);
116                 add(Box.createRigidArea(new Dimension(5, 0)));
117                 add(min_label);
118         }
119 }
120
121 class AltosSite {
122         String  name;
123         double  latitude;
124         double  longitude;
125
126         public String toString() {
127                 return name;
128         }
129
130         public AltosSite(String in_name, double in_latitude, double in_longitude) {
131                 name = in_name;
132                 latitude = in_latitude;
133                 longitude = in_longitude;
134         }
135
136         public AltosSite(String line) throws ParseException {
137                 String[]        elements = line.split(":");
138
139                 if (elements.length < 3)
140                         throw new ParseException(String.format("Invalid site line %s", line), 0);
141
142                 name = elements[0];
143
144                 try {
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);
149                 }
150         }
151 }
152
153 class AltosSites extends Thread {
154         AltosSiteMapPreload     preload;
155         URL                     url;
156         LinkedList<AltosSite>   sites;
157
158         void notify_complete() {
159                 SwingUtilities.invokeLater(new Runnable() {
160                                 public void run() {
161                                         preload.set_sites();
162                                 }
163                         });
164         }
165
166         void add(AltosSite site) {
167                 sites.add(site);
168         }
169
170         void add(String line) {
171                 try {
172                         add(new AltosSite(line));
173                 } catch (ParseException pe) {
174                 }
175         }
176
177         public void run() {
178                 try {
179                         URLConnection uc = url.openConnection();
180                         //int length = uc.getContentLength();
181                         
182                         InputStreamReader in_stream = new InputStreamReader(uc.getInputStream(), Altos.unicode_set);
183                         BufferedReader in = new BufferedReader(in_stream);
184
185                         for (;;) {
186                                 String line = in.readLine();
187                                 if (line == null)
188                                         break;
189                                 add(line);
190                         }
191                 } catch (IOException e) {
192                 } finally {
193                         notify_complete();
194                 }
195         }
196
197         public AltosSites(AltosSiteMapPreload in_preload) {
198                 sites = new LinkedList<AltosSite>();
199                 preload = in_preload;
200                 try {
201                         url = new URL(Altos.launch_sites_url);
202                 } catch (java.net.MalformedURLException e) {
203                         notify_complete();
204                 }
205                 start();
206         }
207 }
208
209 public class AltosSiteMapPreload extends AltosUIDialog implements ActionListener, ItemListener {
210         AltosUI         owner;
211         AltosSiteMap    map;
212
213         AltosMapPos     lat;
214         AltosMapPos     lon;
215
216         final static int        radius = 4;
217         final static int        width = (radius * 2 + 1);
218         final static int        height = (radius * 2 + 1);
219
220         JProgressBar    pbar;
221
222         AltosSites      sites;
223         JLabel          site_list_label;
224         JComboBox       site_list;
225                 
226         JToggleButton   load_button;
227         boolean         loading;
228         JButton         close_button;
229
230         static final String[]   lat_hemi_names = { "N", "S" };
231         static final String[]   lon_hemi_names = { "E", "W" };
232
233         class updatePbar implements Runnable {
234                 int             n;
235                 String          s;
236
237                 public updatePbar(int x, int y, String in_s) {
238                         n = (x + radius) + (y + radius) * width + 1;
239                         s = in_s;
240                 }
241
242                 public void run() {
243                         pbar.setValue(n);
244                         pbar.setString(s);
245                         if (n < width * height) {
246                                 pbar.setValue(n);
247                                 pbar.setString(s);
248                         } else {
249                                 pbar.setValue(0);
250                                 pbar.setString("");
251                                 load_button.setSelected(false);
252                                 loading = false;
253                         }
254                 }
255         }
256
257         class bgLoad extends Thread {
258
259                 AltosSiteMap    map;
260
261                 public bgLoad(AltosSiteMap in_map) {
262                         map = in_map;
263                 }
264
265                 public void run() {
266                         for (int y = -map.radius; y <= map.radius; y++) {
267                                 for (int x = -map.radius; x <= map.radius; x++) {
268                                         String  pngfile;
269                                         pngfile = map.initMap(new Point(x,y));
270                                         SwingUtilities.invokeLater(new updatePbar(x, y, pngfile));
271                                 }
272                         }
273                 }
274         }
275
276         public void set_sites() {
277                 int     i = 1;
278                 for (AltosSite site : sites.sites) {
279                         site_list.insertItemAt(site, i);
280                         i++;
281                 }
282         }
283
284         public void itemStateChanged(ItemEvent e) {
285                 int             state = e.getStateChange();
286
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);
293                         }
294                 }
295         }
296
297         public void actionPerformed(ActionEvent e) {
298                 String  cmd = e.getActionCommand();
299
300                 if (cmd.equals("close"))
301                         setVisible(false);
302
303                 if (cmd.equals("load")) {
304                         if (!loading) {
305                                 try {
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);
310                                         loading = true;
311                                         bgLoad thread = new bgLoad(map);
312                                         thread.start();
313                                 } catch (NumberFormatException ne) {
314                                         load_button.setSelected(false);
315                                 }
316                         }
317                 }
318         }
319
320         public AltosSiteMapPreload(AltosUI in_owner) {
321                 owner = in_owner;
322
323                 Container               pane = getContentPane();
324                 GridBagConstraints      c = new GridBagConstraints();
325                 Insets                  i = new Insets(4,4,4,4);
326
327                 pane.setLayout(new GridBagLayout());
328
329                 map = new AltosSiteMap(4);
330
331                 c.fill = GridBagConstraints.BOTH;
332                 c.anchor = GridBagConstraints.CENTER;
333                 c.insets = i;
334                 c.weightx = 1;
335                 c.weighty = 1;
336
337                 c.gridx = 0;
338                 c.gridy = 0;
339                 c.gridwidth = 2;
340                 c.anchor = GridBagConstraints.CENTER;
341
342                 pane.add(map, c);
343
344                 pbar = new JProgressBar();
345                 pbar.setMinimum(0);
346                 pbar.setMaximum(width * height);
347                 pbar.setValue(0);
348                 pbar.setString("");
349                 pbar.setStringPainted(true);
350                 
351                 c.fill = GridBagConstraints.HORIZONTAL;
352                 c.anchor = GridBagConstraints.CENTER;
353                 c.insets = i;
354                 c.weightx = 1;
355                 c.weighty = 0;
356
357                 c.gridx = 0;
358                 c.gridy = 1;
359                 c.gridwidth = 2;
360
361                 pane.add(pbar, c);
362
363                 site_list_label = new JLabel ("Known Launch Sites:");
364
365                 c.fill = GridBagConstraints.NONE;
366                 c.anchor = GridBagConstraints.CENTER;
367                 c.insets = i;
368                 c.weightx = 1;
369                 c.weighty = 0;
370
371                 c.gridx = 0;
372                 c.gridy = 2;
373                 c.gridwidth = 1;
374
375                 pane.add(site_list_label, c);
376                 
377                 site_list = new JComboBox(new String[] { "Site List" });
378                 site_list.addItemListener(this);
379
380                 sites = new AltosSites(this);
381
382                 c.fill = GridBagConstraints.HORIZONTAL;
383                 c.anchor = GridBagConstraints.CENTER;
384                 c.insets = i;
385                 c.weightx = 1;
386                 c.weighty = 0;
387
388                 c.gridx = 1;
389                 c.gridy = 2;
390                 c.gridwidth = 1;
391
392                 pane.add(site_list, c);
393                 
394                 lat = new AltosMapPos(owner,
395                                       "Latitude:",
396                                       lat_hemi_names,
397                                       37.167833333);
398                 c.fill = GridBagConstraints.NONE;
399                 c.anchor = GridBagConstraints.CENTER;
400                 c.insets = i;
401                 c.weightx = 0;
402                 c.weighty = 0;
403
404                 c.gridx = 0;
405                 c.gridy = 3;
406                 c.gridwidth = 1;
407                 c.anchor = GridBagConstraints.CENTER;
408
409                 pane.add(lat, c);
410                 
411                 lon = new AltosMapPos(owner,
412                                       "Longitude:",
413                                       lon_hemi_names,
414                                       -97.73975);
415
416                 c.fill = GridBagConstraints.NONE;
417                 c.anchor = GridBagConstraints.CENTER;
418                 c.insets = i;
419                 c.weightx = 0;
420                 c.weighty = 0;
421
422                 c.gridx = 1;
423                 c.gridy = 3;
424                 c.gridwidth = 1;
425                 c.anchor = GridBagConstraints.CENTER;
426
427                 pane.add(lon, c);
428
429                 load_button = new JToggleButton("Load Map");
430                 load_button.addActionListener(this);
431                 load_button.setActionCommand("load");
432                 
433                 c.fill = GridBagConstraints.NONE;
434                 c.anchor = GridBagConstraints.CENTER;
435                 c.insets = i;
436                 c.weightx = 1;
437                 c.weighty = 0;
438
439                 c.gridx = 0;
440                 c.gridy = 4;
441                 c.gridwidth = 1;
442                 c.anchor = GridBagConstraints.CENTER;
443
444                 pane.add(load_button, c);
445
446                 close_button = new JButton("Close");
447                 close_button.addActionListener(this);
448                 close_button.setActionCommand("close");
449                 
450                 c.fill = GridBagConstraints.NONE;
451                 c.anchor = GridBagConstraints.CENTER;
452                 c.insets = i;
453                 c.weightx = 1;
454                 c.weighty = 0;
455
456                 c.gridx = 1;
457                 c.gridy = 4;
458                 c.gridwidth = 1;
459                 c.anchor = GridBagConstraints.CENTER;
460
461                 pane.add(close_button, c);
462
463                 pack();
464                 setLocationRelativeTo(owner);
465                 setVisible(true);
466         }
467 }