altosui: Eliminate unncessary import altosui lines
[fw/altos] / ao-tools / altosui / AltosUI.java
1 /*
2  * Copyright © 2010 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 javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30
31 import libaltosJNI.*;
32
33 public class AltosUI extends JFrame {
34         public AltosVoice voice = new AltosVoice();
35
36         public static boolean load_library(Frame frame) {
37                 if (!AltosDevice.load_library()) {
38                         JOptionPane.showMessageDialog(frame,
39                                                       String.format("No AltOS library in \"%s\"",
40                                                                     System.getProperty("java.library.path","<undefined>")),
41                                                       "Cannot load device access library",
42                                                       JOptionPane.ERROR_MESSAGE);
43                         return false;
44                 }
45                 return true;
46         }
47
48         void telemetry_window(AltosDevice device) {
49                 try {
50                         AltosFlightReader reader = new AltosTelemetryReader(device);
51                         if (reader != null)
52                                 new AltosFlightUI(voice, reader, device.getSerial());
53                 } catch (FileNotFoundException ee) {
54                         JOptionPane.showMessageDialog(AltosUI.this,
55                                                       String.format("Cannot open device \"%s\"",
56                                                                     device.getPath()),
57                                                       "Cannot open target device",
58                                                       JOptionPane.ERROR_MESSAGE);
59                 } catch (IOException ee) {
60                         JOptionPane.showMessageDialog(AltosUI.this,
61                                                       device.getPath(),
62                                                       "Unkonwn I/O error",
63                                                       JOptionPane.ERROR_MESSAGE);
64                 }
65         }
66
67         Container       pane;
68         GridBagLayout   gridbag;
69
70         JButton addButton(int x, int y, String label) {
71                 GridBagConstraints      c;
72                 JButton                 b;
73
74                 c = new GridBagConstraints();
75                 c.gridx = x; c.gridy = y;
76                 c.fill = GridBagConstraints.BOTH;
77                 c.weightx = 1;
78                 c.weighty = 1;
79                 b = new JButton(label);
80
81                 Dimension ps = b.getPreferredSize();
82
83                 gridbag.setConstraints(b, c);
84                 add(b, c);
85                 return b;
86         }
87
88         public AltosUI() {
89
90                 load_library(null);
91
92                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
93                 if (imgURL != null)
94                         setIconImage(new ImageIcon(imgURL).getImage());
95
96                 AltosPreferences.init(this);
97
98                 pane = getContentPane();
99                 gridbag = new GridBagLayout();
100                 pane.setLayout(gridbag);
101
102                 JButton b;
103
104                 b = addButton(0, 0, "Monitor Flight");
105                 b.addActionListener(new ActionListener() {
106                                         public void actionPerformed(ActionEvent e) {
107                                                 ConnectToDevice();
108                                         }
109                                 });
110                 b = addButton(1, 0, "Save Flight Data");
111                 b.addActionListener(new ActionListener() {
112                                         public void actionPerformed(ActionEvent e) {
113                                                 SaveFlightData();
114                                         }
115                                 });
116                 b = addButton(2, 0, "Replay Flight");
117                 b.addActionListener(new ActionListener() {
118                                         public void actionPerformed(ActionEvent e) {
119                                                 Replay();
120                                         }
121                                 });
122                 b = addButton(0, 1, "Graph Data");
123                 b.addActionListener(new ActionListener() {
124                                         public void actionPerformed(ActionEvent e) {
125                                                 GraphData();
126                                         }
127                                 });
128                 b = addButton(1, 1, "Export Data");
129                 b.addActionListener(new ActionListener() {
130                                         public void actionPerformed(ActionEvent e) {
131                                                 ExportData();
132                                         }
133                                 });
134                 b = addButton(2, 1, "Configure TeleMetrum");
135                 b.addActionListener(new ActionListener() {
136                                         public void actionPerformed(ActionEvent e) {
137                                                 ConfigureTeleMetrum();
138                                         }
139                                 });
140
141                 setTitle("AltOS");
142
143                 createMenu();
144
145                 pane.doLayout();
146                 pane.validate();
147
148                 doLayout();
149                 validate();
150
151                 setVisible(true);
152
153                 Insets i = getInsets();
154                 Dimension ps = rootPane.getPreferredSize();
155                 ps.width += i.left + i.right;
156                 ps.height += i.top + i.bottom;
157                 setPreferredSize(ps);
158                 setSize(ps);
159                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
160                 addWindowListener(new WindowAdapter() {
161                         @Override
162                         public void windowClosing(WindowEvent e) {
163                                 System.exit(0);
164                         }
165                 });
166         }
167
168         private void ConnectToDevice() {
169                 AltosDevice     device = AltosDeviceDialog.show(AltosUI.this,
170                                                                 AltosDevice.product_basestation);
171
172                 if (device != null)
173                         telemetry_window(device);
174         }
175
176         void ConfigureCallsign() {
177                 String  result;
178                 result = JOptionPane.showInputDialog(AltosUI.this,
179                                                      "Configure Callsign",
180                                                      AltosPreferences.callsign());
181                 if (result != null)
182                         AltosPreferences.set_callsign(result);
183         }
184
185         void ConfigureTeleMetrum() {
186                 new AltosConfig(AltosUI.this);
187         }
188
189         void FlashImage() {
190                 new AltosFlashUI(AltosUI.this);
191         }
192
193         /*
194          * Replay a flight from telemetry data
195          */
196         private void Replay() {
197                 AltosLogfileChooser chooser = new AltosLogfileChooser(
198                         AltosUI.this);
199                 AltosRecordIterable iterable = chooser.runDialog();
200                 if (iterable != null) {
201                         AltosFlightReader reader = new AltosReplayReader(iterable.iterator(),
202                                                                          chooser.filename());
203                         new AltosFlightUI(voice, reader);
204                 }
205         }
206
207         /* Connect to TeleMetrum, either directly or through
208          * a TeleDongle over the packet link
209          */
210         private void SaveFlightData() {
211                 new AltosEepromDownload(AltosUI.this);
212         }
213
214         /* Load a flight log file and write out a CSV file containing
215          * all of the data in standard units
216          */
217
218         private void ExportData() {
219                 new AltosCSVUI(AltosUI.this);
220         }
221
222         /* Load a flight log CSV file and display a pretty graph.
223          */
224
225         private void GraphData() {
226                 new AltosGraphUI(AltosUI.this);
227         }
228
229         /* Create the AltosUI menus
230          */
231         private void createMenu() {
232                 JMenuBar menubar = new JMenuBar();
233                 JMenu menu;
234                 JMenuItem item;
235                 JRadioButtonMenuItem radioitem;
236
237                 // File menu
238                 {
239                         menu = new JMenu("File");
240                         menu.setMnemonic(KeyEvent.VK_F);
241                         menubar.add(menu);
242
243                         item = new JMenuItem("Flash Image",KeyEvent.VK_I);
244                         item.addActionListener(new ActionListener() {
245                                         public void actionPerformed(ActionEvent e) {
246                                                 FlashImage();
247                                         }
248                                 });
249                         menu.add(item);
250
251                         item = new JMenuItem("Export Data",KeyEvent.VK_E);
252                         item.addActionListener(new ActionListener() {
253                                         public void actionPerformed(ActionEvent e) {
254                                                 ExportData();
255                                         }
256                                 });
257                         menu.add(item);
258
259                         item = new JMenuItem("Graph Data",KeyEvent.VK_G);
260                         item.addActionListener(new ActionListener() {
261                                         public void actionPerformed(ActionEvent e) {
262                                                 GraphData();
263                                         }
264                                 });
265                         menu.add(item);
266
267                         item = new JMenuItem("Quit",KeyEvent.VK_Q);
268                         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
269                                                                    ActionEvent.CTRL_MASK));
270                         item.addActionListener(new ActionListener() {
271                                         public void actionPerformed(ActionEvent e) {
272                                                 System.out.printf("exiting\n");
273                                                 System.exit(0);
274                                         }
275                                 });
276                         menu.add(item);
277                 }
278
279                 // Device menu
280                 if (false) {
281                         menu = new JMenu("Device");
282                         menu.setMnemonic(KeyEvent.VK_D);
283                         menubar.add(menu);
284
285                         item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
286                         item.addActionListener(new ActionListener() {
287                                         public void actionPerformed(ActionEvent e) {
288                                                 ConnectToDevice();
289                                         }
290                                 });
291                         menu.add(item);
292
293                         menu.addSeparator();
294
295                         item = new JMenuItem("Set Callsign",KeyEvent.VK_S);
296                         item.addActionListener(new ActionListener() {
297                                         public void actionPerformed(ActionEvent e) {
298                                                 ConfigureCallsign();
299                                         }
300                                 });
301
302                         menu.add(item);
303
304                         item = new JMenuItem("Configure TeleMetrum device",KeyEvent.VK_T);
305                         item.addActionListener(new ActionListener() {
306                                         public void actionPerformed(ActionEvent e) {
307                                                 ConfigureTeleMetrum();
308                                         }
309                                 });
310
311                         menu.add(item);
312                 }
313                 // Log menu
314                 {
315                         menu = new JMenu("Log");
316                         menu.setMnemonic(KeyEvent.VK_L);
317                         menubar.add(menu);
318
319                         item = new JMenuItem("New Log",KeyEvent.VK_N);
320                         item.addActionListener(new ActionListener() {
321                                         public void actionPerformed(ActionEvent e) {
322                                         }
323                                 });
324                         menu.add(item);
325
326                         item = new JMenuItem("Configure Log",KeyEvent.VK_C);
327                         item.addActionListener(new ActionListener() {
328                                         public void actionPerformed(ActionEvent e) {
329                                                 AltosPreferences.ConfigureLog();
330                                         }
331                                 });
332                         menu.add(item);
333                 }
334                 // Voice menu
335                 {
336                         menu = new JMenu("Voice", true);
337                         menu.setMnemonic(KeyEvent.VK_V);
338                         menubar.add(menu);
339
340                         radioitem = new JRadioButtonMenuItem("Enable Voice", AltosPreferences.voice());
341                         radioitem.addActionListener(new ActionListener() {
342                                         public void actionPerformed(ActionEvent e) {
343                                                 JRadioButtonMenuItem item = (JRadioButtonMenuItem) e.getSource();
344                                                 boolean enabled = item.isSelected();
345                                                 AltosPreferences.set_voice(enabled);
346                                                 if (enabled)
347                                                         voice.speak_always("Enable voice.");
348                                                 else
349                                                         voice.speak_always("Disable voice.");
350                                         }
351                                 });
352                         menu.add(radioitem);
353                         item = new JMenuItem("Test Voice",KeyEvent.VK_T);
354                         item.addActionListener(new ActionListener() {
355                                         public void actionPerformed(ActionEvent e) {
356                                                 voice.speak("That's one small step for man; one giant leap for mankind.");
357                                         }
358                                 });
359                         menu.add(item);
360                 }
361                 this.setJMenuBar(menubar);
362         }
363
364         static AltosRecordIterable open_logfile(String filename) {
365                 File file = new File (filename);
366                 try {
367                         FileInputStream in;
368
369                         in = new FileInputStream(file);
370                         if (filename.endsWith("eeprom"))
371                                 return new AltosEepromIterable(in);
372                         else
373                                 return new AltosTelemetryIterable(in);
374                 } catch (FileNotFoundException fe) {
375                         System.out.printf("Cannot open '%s'\n", filename);
376                         return null;
377                 }
378         }
379
380         static AltosWriter open_csv(String filename) {
381                 File file = new File (filename);
382                 try {
383                         return new AltosCSV(file);
384                 } catch (FileNotFoundException fe) {
385                         System.out.printf("Cannot open '%s'\n", filename);
386                         return null;
387                 }
388         }
389
390         static AltosWriter open_kml(String filename) {
391                 File file = new File (filename);
392                 try {
393                         return new AltosKML(file);
394                 } catch (FileNotFoundException fe) {
395                         System.out.printf("Cannot open '%s'\n", filename);
396                         return null;
397                 }
398         }
399
400         static final int process_csv = 1;
401         static final int process_kml = 2;
402
403         static void process_file(String input, int process) {
404                 AltosRecordIterable iterable = open_logfile(input);
405                 if (iterable == null)
406                         return;
407                 if (process == 0)
408                         process = process_csv;
409                 if ((process & process_csv) != 0) {
410                         String output = Altos.replace_extension(input,".csv");
411                         System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
412                         if (input.equals(output)) {
413                                 System.out.printf("Not processing '%s'\n", input);
414                         } else {
415                                 AltosWriter writer = open_csv(output);
416                                 if (writer != null) {
417                                         writer.write(iterable);
418                                         writer.close();
419                                 }
420                         }
421                 }
422                 if ((process & process_kml) != 0) {
423                         String output = Altos.replace_extension(input,".kml");
424                         System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
425                         if (input.equals(output)) {
426                                 System.out.printf("Not processing '%s'\n", input);
427                         } else {
428                                 AltosWriter writer = open_kml(output);
429                                 if (writer == null)
430                                         return;
431                                 writer.write(iterable);
432                                 writer.close();
433                         }
434                 }
435         }
436
437         public static void main(final String[] args) {
438                 int     process = 0;
439                 /* Handle batch-mode */
440         if (args.length == 2 && args[0].equals("--replay")) {
441             String filename = args[1];
442             FileInputStream in;
443             try {
444                 in = new FileInputStream(filename);
445             } catch (Exception e) {
446                 System.out.printf("Failed to open file '%s'\n", filename);
447                 return;
448             }
449             AltosRecordIterable recs;
450             AltosReplayReader reader;
451             if (filename.endsWith("eeprom")) {
452               recs = new AltosEepromIterable(in);
453             } else {
454               recs = new AltosTelemetryIterable(in); 
455             }
456             reader = new AltosReplayReader(recs.iterator(), filename);
457             new AltosFlightUI(new AltosVoice(), reader);
458             return;
459         } else if (args.length > 0) {
460                         for (int i = 0; i < args.length; i++) {
461                                 if (args[i].equals("--kml"))
462                                         process |= process_kml;
463                                 else if (args[i].equals("--csv"))
464                                         process |= process_csv;
465                                 else
466                                         process_file(args[i], process);
467                         }
468                 } else {
469                         AltosUI altosui = new AltosUI();
470                         altosui.setVisible(true);
471
472                         AltosDevice[] devices = AltosDevice.list(AltosDevice.product_basestation);
473                         for (int i = 0; i < devices.length; i++)
474                                 altosui.telemetry_window(devices[i]);
475                 }
476         }
477 }