altosui: Catch errors opening USB devices. Limit list to relevant devices
[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 altosui.AltosSerial;
32 import altosui.AltosSerialMonitor;
33 import altosui.AltosTelemetry;
34 import altosui.AltosState;
35 import altosui.AltosDeviceDialog;
36 import altosui.AltosPreferences;
37 import altosui.AltosLog;
38 import altosui.AltosVoice;
39
40 import libaltosJNI.*;
41
42 class AltosFlightStatusTableModel extends AbstractTableModel {
43         private String[] columnNames = {"Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
44         private Object[] data = { 0, "idle", 0, 0 };
45
46         public int getColumnCount() { return columnNames.length; }
47         public int getRowCount() { return 2; }
48         public Object getValueAt(int row, int col) {
49                 if (row == 0)
50                         return columnNames[col];
51                 return data[col];
52         }
53
54         public void setValueAt(Object value, int col) {
55                 data[col] = value;
56                 fireTableCellUpdated(1, col);
57         }
58
59         public void setValueAt(Object value, int row, int col) {
60                 setValueAt(value, col);
61         }
62
63         public void set(AltosState state) {
64                 setValueAt(String.format("%1.0f", state.height), 0);
65                 setValueAt(state.data.state, 1);
66                 setValueAt(state.data.rssi, 2);
67                 double speed = state.baro_speed;
68                 if (state.ascent)
69                         speed = state.speed;
70                 setValueAt(String.format("%1.0f", speed), 3);
71         }
72 }
73
74 class AltosFlightInfoTableModel extends AbstractTableModel {
75         private String[] columnNames = {"Field", "Value"};
76
77         class InfoLine {
78                 String  name;
79                 String  value;
80
81                 public InfoLine(String n, String v) {
82                         name = n;
83                         value = v;
84                 }
85         }
86
87         private ArrayList<InfoLine> rows = new ArrayList<InfoLine>();
88
89         public int getColumnCount() { return columnNames.length; }
90         public String getColumnName(int col) { return columnNames[col]; }
91
92         public int getRowCount() { return 20; }
93
94         public Object getValueAt(int row, int col) {
95                 if (row >= rows.size())
96                         return "";
97                 if (col == 0)
98                         return rows.get(row).name;
99                 else
100                         return rows.get(row).value;
101         }
102
103         int     current_row = 0;
104         int     prev_num_rows = 0;
105
106         public void resetRow() {
107                 current_row = 0;
108         }
109         public void addRow(String name, String value) {
110                 if (current_row >= rows.size())
111                         rows.add(current_row, new InfoLine(name, value));
112                 else
113                         rows.set(current_row, new InfoLine(name, value));
114                 current_row++;
115         }
116         public void finish() {
117                 if (current_row > prev_num_rows) {
118                         fireTableRowsInserted(prev_num_rows, current_row - 1);
119                         prev_num_rows = current_row;
120                 }
121                 fireTableDataChanged();
122         }
123 }
124
125 public class AltosUI extends JFrame {
126         private int channel = -1;
127
128         private AltosFlightStatusTableModel flightStatusModel;
129         private JTable flightStatus;
130
131         static final int info_columns = 3;
132
133         private AltosFlightInfoTableModel[] flightInfoModel;
134         private JTable[] flightInfo;
135         private AltosSerial serial_line;
136         private AltosLog altos_log;
137         private Box[] ibox;
138         private Box vbox;
139         private Box hbox;
140
141         private Font statusFont = new Font("SansSerif", Font.BOLD, 24);
142         private Font infoLabelFont = new Font("SansSerif", Font.PLAIN, 14);
143         private Font infoValueFont = new Font("Monospaced", Font.PLAIN, 14);
144
145         public AltosVoice voice = new AltosVoice();
146
147         public AltosUI() {
148
149                 String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
150                 Object[][] statusData = { { "0", "pad", "-50", "0" } };
151
152                 AltosPreferences.init(this);
153
154                 vbox = Box.createVerticalBox();
155                 this.add(vbox);
156
157                 flightStatusModel = new AltosFlightStatusTableModel();
158                 flightStatus = new JTable(flightStatusModel);
159                 flightStatus.setFont(statusFont);
160                 TableColumnModel tcm = flightStatus.getColumnModel();
161                 for (int i = 0; i < flightStatusModel.getColumnCount(); i++) {
162                         DefaultTableCellRenderer       r = new DefaultTableCellRenderer();
163                         r.setFont(statusFont);
164                         r.setHorizontalAlignment(SwingConstants.CENTER);
165                         tcm.getColumn(i).setCellRenderer(r);
166                 }
167
168                 FontMetrics     statusMetrics = flightStatus.getFontMetrics(statusFont);
169                 int statusHeight = (statusMetrics.getHeight() + statusMetrics.getLeading()) * 15 / 10;
170                 flightStatus.setRowHeight(statusHeight);
171                 flightStatus.setShowGrid(false);
172
173                 vbox.add(flightStatus);
174
175                 hbox = Box.createHorizontalBox();
176                 vbox.add(hbox);
177
178                 flightInfo = new JTable[3];
179                 flightInfoModel = new AltosFlightInfoTableModel[3];
180                 ibox = new Box[3];
181                 FontMetrics     infoValueMetrics = flightStatus.getFontMetrics(infoValueFont);
182                 int infoHeight = (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 20 / 10;
183
184                 for (int i = 0; i < info_columns; i++) {
185                         ibox[i] = Box.createVerticalBox();
186                         flightInfoModel[i] = new AltosFlightInfoTableModel();
187                         flightInfo[i] = new JTable(flightInfoModel[i]);
188                         flightInfo[i].setFont(infoValueFont);
189                         flightInfo[i].setRowHeight(infoHeight);
190                         flightInfo[i].setShowGrid(true);
191                         ibox[i].add(flightInfo[i].getTableHeader());
192                         ibox[i].add(flightInfo[i]);
193                         hbox.add(ibox[i]);
194                 }
195
196                 setTitle("AltOS");
197
198                 createMenu();
199
200                 serial_line = new AltosSerial();
201                 altos_log = new AltosLog(serial_line);
202                 int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
203                 this.setSize(new Dimension (infoValueMetrics.charWidth('0') * 6 * 20,
204                                             statusHeight * 4 + infoHeight * 17));
205                 this.validate();
206                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
207                 addWindowListener(new WindowAdapter() {
208                         @Override
209                         public void windowClosing(WindowEvent e) {
210                                 System.exit(0);
211                         }
212                 });
213         }
214
215         public void info_reset() {
216                 for (int i = 0; i < info_columns; i++)
217                         flightInfoModel[i].resetRow();
218         }
219
220         public void info_add_row(int col, String name, String value) {
221                 flightInfoModel[col].addRow(name, value);
222         }
223
224         public void info_add_row(int col, String name, String format, Object... parameters) {
225                 flightInfoModel[col].addRow(name, String.format(format, parameters));
226         }
227
228         public void info_add_deg(int col, String name, double v, int pos, int neg) {
229                 int     c = pos;
230                 if (v < 0) {
231                         c = neg;
232                         v = -v;
233                 }
234                 double  deg = Math.floor(v);
235                 double  min = (v - deg) * 60;
236
237                 flightInfoModel[col].addRow(name, String.format("%3.0f°%08.5f'", deg, min));
238         }
239
240         public void info_finish() {
241                 for (int i = 0; i < info_columns; i++)
242                         flightInfoModel[i].finish();
243         }
244
245         static final int MIN_PAD_SAMPLES = 10;
246
247         public void show(AltosState state) {
248                 flightStatusModel.set(state);
249
250                 info_reset();
251                 if (state.npad >= MIN_PAD_SAMPLES)
252                         info_add_row(0, "Ground state", "%s", "ready");
253                 else
254                         info_add_row(0, "Ground state", "wait (%d)",
255                                      MIN_PAD_SAMPLES - state.npad);
256                 info_add_row(0, "Rocket state", "%s", state.data.state);
257                 info_add_row(0, "Callsign", "%s", state.data.callsign);
258                 info_add_row(0, "Rocket serial", "%6d", state.data.serial);
259                 info_add_row(0, "Rocket flight", "%6d", state.data.flight);
260
261                 info_add_row(0, "RSSI", "%6d    dBm", state.data.rssi);
262                 info_add_row(0, "Height", "%6.0f    m", state.height);
263                 info_add_row(0, "Max height", "%6.0f    m", state.max_height);
264                 info_add_row(0, "Acceleration", "%8.1f  m/s²", state.acceleration);
265                 info_add_row(0, "Max acceleration", "%8.1f  m/s²", state.max_acceleration);
266                 info_add_row(0, "Speed", "%8.1f  m/s", state.ascent ? state.speed : state.baro_speed);
267                 info_add_row(0, "Max Speed", "%8.1f  m/s", state.max_speed);
268                 info_add_row(0, "Temperature", "%9.2f °C", state.temperature);
269                 info_add_row(0, "Battery", "%9.2f V", state.battery);
270                 info_add_row(0, "Drogue", "%9.2f V", state.drogue_sense);
271                 info_add_row(0, "Main", "%9.2f V", state.main_sense);
272                 info_add_row(0, "Pad altitude", "%6.0f    m", state.ground_altitude);
273                 if (state.gps == null) {
274                         info_add_row(1, "GPS", "not available");
275                 } else {
276                         if (state.data.gps.gps_locked)
277                                 info_add_row(1, "GPS", "   locked");
278                         else if (state.data.gps.gps_connected)
279                                 info_add_row(1, "GPS", " unlocked");
280                         else
281                                 info_add_row(1, "GPS", "  missing");
282                         info_add_row(1, "Satellites", "%6d", state.data.gps.nsat);
283                         info_add_deg(1, "Latitude", state.gps.lat, 'N', 'S');
284                         info_add_deg(1, "Longitude", state.gps.lon, 'E', 'W');
285                         info_add_row(1, "GPS altitude", "%6d", state.gps.alt);
286                         info_add_row(1, "GPS height", "%6.0f", state.gps_height);
287
288                         /* The SkyTraq GPS doesn't report these values */
289                         if (false) {
290                                 info_add_row(1, "GPS ground speed", "%8.1f m/s %3d°",
291                                              state.gps.ground_speed,
292                                              state.gps.course);
293                                 info_add_row(1, "GPS climb rate", "%8.1f m/s",
294                                              state.gps.climb_rate);
295                                 info_add_row(1, "GPS error", "%6d m(h)%3d m(v)",
296                                              state.gps.h_error, state.gps.v_error);
297                         }
298                         info_add_row(1, "GPS hdop", "%8.1f", state.gps.hdop);
299
300                         if (state.npad > 0) {
301                                 if (state.from_pad != null) {
302                                         info_add_row(1, "Distance from pad", "%6.0f m", state.from_pad.distance);
303                                         info_add_row(1, "Direction from pad", "%6.0f°", state.from_pad.bearing);
304                                 } else {
305                                         info_add_row(1, "Distance from pad", "unknown");
306                                         info_add_row(1, "Direction from pad", "unknown");
307                                 }
308                                 info_add_deg(1, "Pad latitude", state.pad_lat, 'N', 'S');
309                                 info_add_deg(1, "Pad longitude", state.pad_lon, 'E', 'W');
310                                 info_add_row(1, "Pad GPS alt", "%6.0f m", state.pad_alt);
311                         }
312                         info_add_row(1, "GPS date", "%04d-%02d-%02d",
313                                        state.gps.gps_time.year,
314                                        state.gps.gps_time.month,
315                                        state.gps.gps_time.day);
316                         info_add_row(1, "GPS time", "  %02d:%02d:%02d",
317                                        state.gps.gps_time.hour,
318                                        state.gps.gps_time.minute,
319                                        state.gps.gps_time.second);
320                         int     nsat_vis = 0;
321                         int     c;
322
323                         if (state.gps.cc_gps_sat == null)
324                                 info_add_row(2, "Satellites Visible", "%4d", 0);
325                         else {
326                                 info_add_row(2, "Satellites Visible", "%4d", state.gps.cc_gps_sat.length);
327                                 for (c = 0; c < state.gps.cc_gps_sat.length; c++) {
328                                         info_add_row(2, "Satellite id,C/N0",
329                                                      "%4d, %4d",
330                                                      state.gps.cc_gps_sat[c].svid,
331                                                      state.gps.cc_gps_sat[c].c_n0);
332                                 }
333                         }
334                 }
335                 info_finish();
336         }
337
338         class IdleThread extends Thread {
339
340                 private AltosState state;
341                 int     reported_landing;
342
343                 public void report(boolean last) {
344                         if (state == null)
345                                 return;
346
347                         /* reset the landing count once we hear about a new flight */
348                         if (state.state < AltosTelemetry.ao_flight_drogue)
349                                 reported_landing = 0;
350
351                         /* Shut up once the rocket is on the ground */
352                         if (reported_landing > 2) {
353                                 return;
354                         }
355
356                         /* If the rocket isn't on the pad, then report height */
357                         if (state.state > AltosTelemetry.ao_flight_pad) {
358                                 voice.speak("%d meters", (int) (state.height + 0.5));
359                         } else {
360                                 reported_landing = 0;
361                         }
362
363                         /* If the rocket is coming down, check to see if it has landed;
364                          * either we've got a landed report or we haven't heard from it in
365                          * a long time
366                          */
367                         if (!state.ascent &&
368                             (last ||
369                              System.currentTimeMillis() - state.report_time >= 15000 ||
370                              state.state == AltosTelemetry.ao_flight_landed))
371                         {
372                                 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
373                                         voice.speak("rocket landed safely");
374                                 else
375                                         voice.speak("rocket may have crashed");
376                                 if (state.gps != null)
377                                         voice.speak("bearing %d degrees, range %d meters",
378                                                     (int) (state.from_pad.bearing + 0.5),
379                                                     (int) (state.from_pad.distance + 0.5));
380                                 ++reported_landing;
381                         }
382                 }
383
384                 public void run () {
385
386                         reported_landing = 0;
387                         state = null;
388                         try {
389                                 for (;;) {
390                                         Thread.sleep(10000);
391                                         report(false);
392                                 }
393                         } catch (InterruptedException ie) {
394                         }
395                 }
396
397                 public void notice(AltosState new_state) {
398                         state = new_state;
399                 }
400         }
401
402         private void tell(AltosState state, AltosState old_state) {
403                 if (old_state == null || old_state.state != state.state) {
404                         voice.speak(state.data.state);
405                         if ((old_state == null || old_state.state <= AltosTelemetry.ao_flight_boost) &&
406                             state.state > AltosTelemetry.ao_flight_boost) {
407                                 voice.speak("max speed: %d meters per second.",
408                                             (int) (state.max_speed + 0.5));
409                         } else if ((old_state == null || old_state.state < AltosTelemetry.ao_flight_drogue) &&
410                                    state.state >= AltosTelemetry.ao_flight_drogue) {
411                                 voice.speak("max height: %d meters.",
412                                             (int) (state.max_height + 0.5));
413                         }
414                 }
415                 old_state = state;
416         }
417
418         class DisplayThread extends Thread {
419                 IdleThread      idle_thread;
420
421                 String read() throws InterruptedException { return null; }
422
423                 void close() { }
424
425                 void update(AltosState state) throws InterruptedException { }
426
427                 public void run() {
428                         String          line;
429                         AltosState      state = null;
430                         AltosState      old_state = null;
431
432                         idle_thread = new IdleThread();
433
434                         info_reset();
435                         info_finish();
436                         idle_thread.start();
437                         try {
438                                 while ((line = read()) != null) {
439                                         try {
440                                                 AltosTelemetry  t = new AltosTelemetry(line);
441                                                 old_state = state;
442                                                 state = new AltosState(t, state);
443                                                 update(state);
444                                                 show(state);
445                                                 tell(state, old_state);
446                                                 idle_thread.notice(state);
447                                         } catch (ParseException pp) {
448                                                 System.out.printf("Parse error on %s\n", line);
449                                                 System.out.println("exception " + pp);
450                                         }
451                                 }
452                         } catch (InterruptedException ee) {
453                         } finally {
454                                 close();
455                                 idle_thread.interrupt();
456                         }
457                 }
458
459                 public void report() {
460                         if (idle_thread != null)
461                                 idle_thread.report(true);
462                 }
463         }
464
465         class DeviceThread extends DisplayThread {
466                 AltosSerial     serial;
467                 LinkedBlockingQueue<String> telem;
468
469                 String read() throws InterruptedException {
470                         return telem.take();
471                 }
472
473                 void close() {
474                         serial.close();
475                         serial.remove_monitor(telem);
476                         System.out.println("DisplayThread done");
477                 }
478
479                 public DeviceThread(AltosSerial s) {
480                         serial = s;
481                         telem = new LinkedBlockingQueue<String>();
482                         serial.add_monitor(telem);
483                 }
484         }
485
486         private void ConnectToDevice() {
487                 altos_device    device = AltosDeviceDialog.show(AltosUI.this, "TeleDongle");
488
489                 if (device != null) {
490                         try {
491                                 serial_line.open(device);
492                                 DeviceThread thread = new DeviceThread(serial_line);
493                                 run_display(thread);
494                         } catch (FileNotFoundException ee) {
495                                 JOptionPane.showMessageDialog(AltosUI.this,
496                                                               String.format("Cannot open device \"%s\"",
497                                                                             device.getPath()),
498                                                               "Cannot open target device",
499                                                               JOptionPane.ERROR_MESSAGE);
500                         } catch (IOException ee) {
501                                 JOptionPane.showMessageDialog(AltosUI.this,
502                                                               device.getPath(),
503                                                               "Unkonwn I/O error",
504                                                               JOptionPane.ERROR_MESSAGE);
505                         }
506                 }
507         }
508
509         void DisconnectFromDevice () {
510                 stop_display();
511         }
512
513         String readline(FileInputStream s) throws IOException {
514                 int c;
515                 String  line = "";
516
517                 while ((c = s.read()) != -1) {
518                         if (c == '\r')
519                                 continue;
520                         if (c == '\n') {
521                                 return line;
522                         }
523                         line = line + (char) c;
524                 }
525                 return null;
526         }
527
528         /*
529          * Open an existing telemetry file and replay it in realtime
530          */
531
532         class ReplayThread extends DisplayThread {
533                 FileInputStream replay;
534                 String filename;
535
536                 ReplayThread(FileInputStream in, String name) {
537                         replay = in;
538                         filename = name;
539                 }
540
541                 String read() {
542                         try {
543                                 return readline(replay);
544                         } catch (IOException ee) {
545                                 JOptionPane.showMessageDialog(AltosUI.this,
546                                                               filename,
547                                                               "error reading",
548                                                               JOptionPane.ERROR_MESSAGE);
549                         }
550                         return null;
551                 }
552
553                 void close () {
554                         try {
555                                 replay.close();
556                         } catch (IOException ee) {
557                         }
558                         report();
559                 }
560
561                 void update(AltosState state) throws InterruptedException {
562                         /* Make it run in realtime after the rocket leaves the pad */
563                         if (state.state > AltosTelemetry.ao_flight_pad)
564                                 Thread.sleep((int) (Math.min(state.time_change,10) * 1000));
565                 }
566         }
567
568         Thread          display_thread;
569
570         private void stop_display() {
571                 if (display_thread != null && display_thread.isAlive())
572                         display_thread.interrupt();
573                 display_thread = null;
574         }
575
576         private void run_display(Thread thread) {
577                 stop_display();
578                 display_thread = thread;
579                 display_thread.start();
580         }
581
582         /*
583          * Replay a flight from telemetry data
584          */
585         private void Replay() {
586                 JFileChooser    logfile_chooser = new JFileChooser();
587
588                 logfile_chooser.setDialogTitle("Select Telemetry File");
589                 logfile_chooser.setFileFilter(new FileNameExtensionFilter("Telemetry file", "telem"));
590                 logfile_chooser.setCurrentDirectory(AltosPreferences.logdir());
591                 int returnVal = logfile_chooser.showOpenDialog(AltosUI.this);
592
593                 if (returnVal == JFileChooser.APPROVE_OPTION) {
594                         File file = logfile_chooser.getSelectedFile();
595                         if (file == null)
596                                 System.out.println("No file selected?");
597                         String  filename = file.getName();
598                         try {
599                                 FileInputStream replay = new FileInputStream(file);
600                                 ReplayThread    thread = new ReplayThread(replay, filename);
601                                 run_display(thread);
602                         } catch (FileNotFoundException ee) {
603                                 JOptionPane.showMessageDialog(AltosUI.this,
604                                                               filename,
605                                                               "Cannot open telemetry file",
606                                                               JOptionPane.ERROR_MESSAGE);
607                         }
608                 }
609         }
610
611         /* Connect to TeleMetrum, either directly or through
612          * a TeleDongle over the packet link
613          */
614         private void SaveFlightData() {
615         }
616
617         /* Create the AltosUI menus
618          */
619         private void createMenu() {
620                 JMenuBar menubar = new JMenuBar();
621                 JMenu menu;
622                 JMenuItem item;
623                 JRadioButtonMenuItem radioitem;
624
625                 // File menu
626                 {
627                         menu = new JMenu("File");
628                         menu.setMnemonic(KeyEvent.VK_F);
629                         menubar.add(menu);
630
631                         item = new JMenuItem("Quit",KeyEvent.VK_Q);
632                         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
633                                                                    ActionEvent.CTRL_MASK));
634                         item.addActionListener(new ActionListener() {
635                                         public void actionPerformed(ActionEvent e) {
636                                                 System.exit(0);
637                                         }
638                                 });
639                         menu.add(item);
640                 }
641
642                 // Device menu
643                 {
644                         menu = new JMenu("Device");
645                         menu.setMnemonic(KeyEvent.VK_D);
646                         menubar.add(menu);
647
648                         item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
649                         item.addActionListener(new ActionListener() {
650                                         public void actionPerformed(ActionEvent e) {
651                                                 ConnectToDevice();
652                                         }
653                                 });
654                         menu.add(item);
655
656                         item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
657                         item.addActionListener(new ActionListener() {
658                                         public void actionPerformed(ActionEvent e) {
659                                                 DisconnectFromDevice();
660                                         }
661                                 });
662                         menu.add(item);
663
664                         menu.addSeparator();
665
666                         item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
667                         item.addActionListener(new ActionListener() {
668                                         public void actionPerformed(ActionEvent e) {
669                                                 SaveFlightData();
670                                         }
671                                 });
672                         menu.add(item);
673
674                         item = new JMenuItem("Replay",KeyEvent.VK_R);
675                         item.addActionListener(new ActionListener() {
676                                         public void actionPerformed(ActionEvent e) {
677                                                 Replay();
678                                         }
679                                 });
680                         menu.add(item);
681                 }
682                 // Log menu
683                 {
684                         menu = new JMenu("Log");
685                         menu.setMnemonic(KeyEvent.VK_L);
686                         menubar.add(menu);
687
688                         item = new JMenuItem("New Log",KeyEvent.VK_N);
689                         item.addActionListener(new ActionListener() {
690                                         public void actionPerformed(ActionEvent e) {
691                                         }
692                                 });
693                         menu.add(item);
694
695                         item = new JMenuItem("Configure Log",KeyEvent.VK_C);
696                         item.addActionListener(new ActionListener() {
697                                         public void actionPerformed(ActionEvent e) {
698                                                 AltosPreferences.ConfigureLog();
699                                         }
700                                 });
701                         menu.add(item);
702                 }
703                 // Voice menu
704                 {
705                         menu = new JMenu("Voice", true);
706                         menu.setMnemonic(KeyEvent.VK_V);
707                         menubar.add(menu);
708
709                         radioitem = new JRadioButtonMenuItem("Enable Voice");
710                         radioitem.addActionListener(new ActionListener() {
711                                         public void actionPerformed(ActionEvent e) {
712                                         }
713                                 });
714                         menu.add(radioitem);
715                 }
716
717                 // Channel menu
718                 {
719                         menu = new JMenu("Channel", true);
720                         menu.setMnemonic(KeyEvent.VK_C);
721                         menubar.add(menu);
722                         ButtonGroup group = new ButtonGroup();
723
724                         for (int c = 0; c <= 9; c++) {
725                                 radioitem = new JRadioButtonMenuItem(String.format("Channel %1d (%7.3fMHz)", c,
726                                                                                    434.550 + c * 0.1),
727                                                                      c == 0);
728                                 radioitem.setActionCommand(String.format("%d", c));
729                                 radioitem.addActionListener(new ActionListener() {
730                                                 public void actionPerformed(ActionEvent e) {
731                                                         System.out.println("Command: " + e.getActionCommand() + " param: " +
732                                                                            e.paramString());
733                                                 }
734                                         });
735                                 menu.add(radioitem);
736                                 group.add(radioitem);
737                         }
738                 }
739
740                 this.setJMenuBar(menubar);
741
742         }
743         public static void main(final String[] args) {
744                 AltosUI altosui = new AltosUI();
745                 altosui.setVisible(true);
746         }
747 }