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