Clean up some altosui comments
[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 gnu.io.*;
30
31 import altosui.AltosSerial;
32 import altosui.AltosSerialMonitor;
33 import altosui.AltosTelemetry;
34 import altosui.AltosState;
35
36 class AltosUIMonitor implements AltosSerialMonitor {
37         public void data(String data) {
38                 System.out.println(data);
39         }
40 }
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 serialLine;
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 AltosUI() {
145
146                 String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
147                 Object[][] statusData = { { "0", "pad", "-50", "0" } };
148
149                 vbox = Box.createVerticalBox();
150                 this.add(vbox);
151
152                 flightStatusModel = new AltosFlightStatusTableModel();
153                 flightStatus = new JTable(flightStatusModel);
154                 flightStatus.setFont(statusFont);
155                 TableColumnModel tcm = flightStatus.getColumnModel();
156                 for (int i = 0; i < flightStatusModel.getColumnCount(); i++) {
157                         DefaultTableCellRenderer       r = new DefaultTableCellRenderer();
158                         r.setFont(statusFont);
159                         r.setHorizontalAlignment(SwingConstants.CENTER);
160                         tcm.getColumn(i).setCellRenderer(r);
161                 }
162
163                 FontMetrics     statusMetrics = flightStatus.getFontMetrics(statusFont);
164                 int statusHeight = (statusMetrics.getHeight() + statusMetrics.getLeading()) * 15 / 10;
165                 flightStatus.setRowHeight(statusHeight);
166                 flightStatus.setShowGrid(false);
167
168                 vbox.add(flightStatus);
169
170                 hbox = Box.createHorizontalBox();
171                 vbox.add(hbox);
172
173                 flightInfo = new JTable[3];
174                 flightInfoModel = new AltosFlightInfoTableModel[3];
175                 ibox = new Box[3];
176                 FontMetrics     infoValueMetrics = flightStatus.getFontMetrics(infoValueFont);
177                 int infoHeight = (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 20 / 10;
178
179                 for (int i = 0; i < info_columns; i++) {
180                         ibox[i] = Box.createVerticalBox();
181                         flightInfoModel[i] = new AltosFlightInfoTableModel();
182                         flightInfo[i] = new JTable(flightInfoModel[i]);
183                         flightInfo[i].setFont(infoValueFont);
184                         flightInfo[i].setRowHeight(infoHeight);
185                         flightInfo[i].setShowGrid(true);
186                         ibox[i].add(flightInfo[i].getTableHeader());
187                         ibox[i].add(flightInfo[i]);
188                         hbox.add(ibox[i]);
189                 }
190
191                 setTitle("AltOS");
192
193                 createMenu();
194
195                 serialLine = new AltosSerial();
196                 serialLine.monitor(new AltosUIMonitor());
197                 int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
198                 this.setSize(new Dimension (infoValueMetrics.charWidth('0') * 6 * 20,
199                                             statusHeight * 4 + infoHeight * 17));
200                 this.validate();
201                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
202                 addWindowListener(new WindowAdapter() {
203                         @Override
204                         public void windowClosing(WindowEvent e) {
205                                 System.exit(0);
206                         }
207                 });
208         }
209
210         public void info_reset() {
211                 for (int i = 0; i < info_columns; i++)
212                         flightInfoModel[i].resetRow();
213         }
214
215         public void info_add_row(int col, String name, String value) {
216                 flightInfoModel[col].addRow(name, value);
217         }
218
219         public void info_add_row(int col, String name, String format, Object value) {
220                 flightInfoModel[col].addRow(name, String.format(format, value));
221         }
222
223         public void info_add_row(int col, String name, String format, Object v1, Object v2) {
224                 flightInfoModel[col].addRow(name, String.format(format, v1, v2));
225         }
226
227         public void info_add_row(int col, String name, String format, Object v1, Object v2, Object v3) {
228                 flightInfoModel[col].addRow(name, String.format(format, v1, v2, v3));
229         }
230
231         public void info_add_deg(int col, String name, double v, int pos, int neg) {
232                 int     c = pos;
233                 if (v < 0) {
234                         c = neg;
235                         v = -v;
236                 }
237                 double  deg = Math.floor(v);
238                 double  min = (v - deg) * 60;
239
240                 flightInfoModel[col].addRow(name, String.format("%3.0f°%08.5f'", deg, min));
241         }
242
243         public void info_finish() {
244                 for (int i = 0; i < info_columns; i++)
245                         flightInfoModel[i].finish();
246         }
247
248         static final int MIN_PAD_SAMPLES = 10;
249
250         public void show(AltosState state) {
251                 flightStatusModel.set(state);
252
253                 info_reset();
254                 if (state.npad >= MIN_PAD_SAMPLES)
255                         info_add_row(0, "Ground state", "%s", "ready");
256                 else
257                         info_add_row(0, "Ground state", "wait (%d)",
258                                      MIN_PAD_SAMPLES - state.npad);
259                 info_add_row(0, "Rocket state", "%s", state.data.state);
260                 info_add_row(0, "Callsign", "%s", state.data.callsign);
261                 info_add_row(0, "Rocket serial", "%6d", state.data.serial);
262                 info_add_row(0, "Rocket flight", "%6d", state.data.flight);
263
264                 info_add_row(0, "RSSI", "%6d    dBm", state.data.rssi);
265                 info_add_row(0, "Height", "%6.0f    m", state.height);
266                 info_add_row(0, "Max height", "%6.0f    m", state.max_height);
267                 info_add_row(0, "Acceleration", "%8.1f  m/s²", state.acceleration);
268                 info_add_row(0, "Max acceleration", "%8.1f  m/s²", state.max_acceleration);
269                 info_add_row(0, "Speed", "%8.1f  m/s", state.ascent ? state.speed : state.baro_speed);
270                 info_add_row(0, "Max Speed", "%8.1f  m/s", state.max_speed);
271                 info_add_row(0, "Temperature", "%9.2f °C", state.temperature);
272                 info_add_row(0, "Battery", "%9.2f V", state.battery);
273                 info_add_row(0, "Drogue", "%9.2f V", state.drogue_sense);
274                 info_add_row(0, "Main", "%9.2f V", state.main_sense);
275                 info_add_row(0, "Pad altitude", "%6.0f    m", state.ground_altitude);
276                 if (state.gps == null) {
277                         info_add_row(1, "GPS", "not available");
278                 } else {
279                         if (state.data.gps.gps_locked)
280                                 info_add_row(1, "GPS", "   locked");
281                         else if (state.data.gps.gps_connected)
282                                 info_add_row(1, "GPS", " unlocked");
283                         else
284                                 info_add_row(1, "GPS", "  missing");
285                         info_add_row(1, "Satellites", "%6d", state.data.gps.nsat);
286                         info_add_deg(1, "Latitude", state.gps.lat, 'N', 'S');
287                         info_add_deg(1, "Longitude", state.gps.lon, 'E', 'W');
288                         info_add_row(1, "GPS altitude", "%6d", state.gps.alt);
289                         info_add_row(1, "GPS height", "%6.0f", state.gps_height);
290
291                         /* The SkyTraq GPS doesn't report these values */
292                         if (false) {
293                                 info_add_row(1, "GPS ground speed", "%8.1f m/s %3d°",
294                                              state.gps.ground_speed,
295                                              state.gps.course);
296                                 info_add_row(1, "GPS climb rate", "%8.1f m/s",
297                                              state.gps.climb_rate);
298                                 info_add_row(1, "GPS error", "%6d m(h)%3d m(v)",
299                                              state.gps.h_error, state.gps.v_error);
300                         }
301                         info_add_row(1, "GPS hdop", "%8.1f", state.gps.hdop);
302
303                         if (state.npad > 0) {
304                                 if (state.from_pad != null) {
305                                         info_add_row(1, "Distance from pad", "%6.0f m", state.from_pad.distance);
306                                         info_add_row(1, "Direction from pad", "%6.0f°", state.from_pad.bearing);
307                                 } else {
308                                         info_add_row(1, "Distance from pad", "unknown");
309                                         info_add_row(1, "Direction from pad", "unknown");
310                                 }
311                                 info_add_deg(1, "Pad latitude", state.pad_lat, 'N', 'S');
312                                 info_add_deg(1, "Pad longitude", state.pad_lon, 'E', 'W');
313                                 info_add_row(1, "Pad GPS alt", "%6.0f m", state.pad_alt);
314                         }
315                         info_add_row(1, "GPS date", "%04d-%02d-%02d",
316                                        state.gps.gps_time.year,
317                                        state.gps.gps_time.month,
318                                        state.gps.gps_time.day);
319                         info_add_row(1, "GPS time", "  %02d:%02d:%02d",
320                                        state.gps.gps_time.hour,
321                                        state.gps.gps_time.minute,
322                                        state.gps.gps_time.second);
323                         int     nsat_vis = 0;
324                         int     c;
325
326                         if (state.gps.cc_gps_sat == null)
327                                 info_add_row(2, "Satellites Visible", "%4d", 0);
328                         else {
329                                 info_add_row(2, "Satellites Visible", "%4d", state.gps.cc_gps_sat.length);
330                                 for (c = 0; c < state.gps.cc_gps_sat.length; c++) {
331                                         info_add_row(2, "Satellite id,C/N0",
332                                                      "%4d, %4d",
333                                                      state.gps.cc_gps_sat[c].svid,
334                                                      state.gps.cc_gps_sat[c].c_n0);
335                                 }
336                         }
337                 }
338                 info_finish();
339         }
340
341         /* User Preferences */
342         Preferences altosui_preferences = Preferences.userNodeForPackage(this.getClass());
343
344         /* Log directory */
345         private File logdir = null;
346
347         /* logdir preference name */
348         final static String logdirPreference = "LOGDIR";
349
350         /* Default logdir is ~/TeleMetrum */
351         final static String logdirName = "TeleMetrum";
352
353         /* Initialize logdir from preferences */
354         {
355                 String logdir_string = altosui_preferences.get(logdirPreference, null);
356                 if (logdir_string != null)
357                         logdir = new File(logdir_string);
358                 else
359                         /* a hack -- make the file chooser tell us what the default directory
360                          * would be and stick our logdir in a subdirectory of that.
361                          */
362                         logdir = new File(new JFileChooser().getCurrentDirectory(), logdirName);
363         }
364
365         private void set_logdir(File new_logdir) {
366                 logdir = new_logdir;
367                 System.out.printf("Set logdir to %s\n", logdir.toString());
368                 synchronized (altosui_preferences) {
369                         altosui_preferences.put(logdirPreference, logdir.getPath());
370                         try {
371                                 altosui_preferences.flush();
372                         } catch (BackingStoreException ee) {
373                                 JOptionPane.showMessageDialog(AltosUI.this,
374                                                               altosui_preferences.absolutePath(),
375                                                               "Cannot save prefernces",
376                                                               JOptionPane.ERROR_MESSAGE);
377                         }
378                 }
379         }
380
381         private boolean check_dir(File dir) {
382                 if (!dir.exists()) {
383                         if (!dir.mkdirs()) {
384                                 JOptionPane.showMessageDialog(AltosUI.this,
385                                                               dir.getName(),
386                                                               "Cannot create directory",
387                                                               JOptionPane.ERROR_MESSAGE);
388                                 return false;
389                         }
390                 } else if (!dir.isDirectory()) {
391                         JOptionPane.showMessageDialog(AltosUI.this,
392                                                       dir.getName(),
393                                                       "Is not a directory",
394                                                       JOptionPane.ERROR_MESSAGE);
395                         return false;
396                 }
397                 return true;
398         }
399
400         private void PickSerialDevice() {
401                 java.util.Enumeration<CommPortIdentifier> port_list = CommPortIdentifier.getPortIdentifiers();
402                 while (port_list.hasMoreElements()) {
403                         CommPortIdentifier identifier = port_list.nextElement();
404                         System.out.println("Serial port " + identifier.getName());
405                 }
406         }
407
408         private void ConnectToDevice() {
409                 JFileChooser    device_chooser = new JFileChooser();
410                 int returnVal = device_chooser.showOpenDialog(AltosUI.this);
411
412                 if (returnVal == JFileChooser.APPROVE_OPTION) {
413                         File file = device_chooser.getSelectedFile();
414                         try {
415                                 serialLine.connect(file.getCanonicalPath());
416                         } catch (FileNotFoundException ee) {
417                                 JOptionPane.showMessageDialog(AltosUI.this,
418                                                               file.getName(),
419                                                               "Cannot open serial port",
420                                                               JOptionPane.ERROR_MESSAGE);
421                         } catch (NoSuchPortException ee) {
422                                 JOptionPane.showMessageDialog(AltosUI.this,
423                                                               file.getName(),
424                                                               "No such serial port",
425                                                               JOptionPane.ERROR_MESSAGE);
426                         } catch (PortInUseException ee) {
427                                 JOptionPane.showMessageDialog(AltosUI.this,
428                                                               file.getName(),
429                                                               "Port in use",
430                                                               JOptionPane.ERROR_MESSAGE);
431                         } catch (IOException ee) {
432                                 JOptionPane.showMessageDialog(AltosUI.this,
433                                                               file.getName(),
434                                                               "Unkonwn I/O error",
435                                                               JOptionPane.ERROR_MESSAGE);
436                         }
437                 }
438         }
439
440         String readline(FileInputStream s) throws IOException {
441                 int c;
442                 String  line = "";
443
444                 while ((c = s.read()) != -1) {
445                         if (c == '\r')
446                                 continue;
447                         if (c == '\n')
448                                 return line;
449                         line = line + (char) c;
450                 }
451                 return null;
452         }
453
454         /*
455          * Open an existing telemetry file and replay it in realtime
456          */
457
458         class ReplayThread extends Thread {
459                 FileInputStream replay;
460                 String filename;
461
462                 ReplayThread(FileInputStream in, String name) {
463                         replay = in;
464                         filename = name;
465                 }
466
467                 /* Run the replay in a separate thread
468                  * so that the UI can update
469                  */
470                 public void run() {
471                         String          line;
472                         AltosState      state = null;
473                         try {
474                                 while ((line = readline(replay)) != null) {
475                                         try {
476                                                 AltosTelemetry  t = new AltosTelemetry(line);
477                                                 state = new AltosState(t, state);
478                                                 show(state);
479
480                                                 /* Make it run in realtime after the rocket leaves the pad */
481                                                 try {
482                                                         if (state.state > AltosTelemetry.ao_flight_pad)
483                                                                 Thread.sleep((int) (state.time_change * 1000));
484                                                 } catch (InterruptedException e) {}
485                                         } catch (ParseException pp) {
486                                                 JOptionPane.showMessageDialog(AltosUI.this,
487                                                                               line,
488                                                                               "error parsing",
489                                                                               JOptionPane.ERROR_MESSAGE);
490                                                 break;
491                                         }
492                                 }
493                         } catch (IOException ee) {
494                                 JOptionPane.showMessageDialog(AltosUI.this,
495                                                               filename,
496                                                               "error reading",
497                                                               JOptionPane.ERROR_MESSAGE);
498                         } finally {
499                                 try {
500                                         replay.close();
501                                 } catch (IOException e) {}
502                         }
503                 }
504         }
505
506         /*
507          * Replay a flight from telemetry data
508          */
509         private void Replay() {
510                 JFileChooser    logfile_chooser = new JFileChooser();
511
512                 logfile_chooser.setDialogTitle("Select Telemetry File");
513                 logfile_chooser.setFileFilter(new FileNameExtensionFilter("Telemetry file", "telem"));
514                 logfile_chooser.setCurrentDirectory(logdir);
515                 int returnVal = logfile_chooser.showOpenDialog(AltosUI.this);
516
517                 if (returnVal == JFileChooser.APPROVE_OPTION) {
518                         File file = logfile_chooser.getSelectedFile();
519                         if (file == null)
520                                 System.out.println("No file selected?");
521                         String  filename = file.getName();
522                         try {
523                                 FileInputStream replay = new FileInputStream(file);
524                                 ReplayThread    thread = new ReplayThread(replay, filename);
525                                 thread.start();
526                         } catch (FileNotFoundException ee) {
527                                 JOptionPane.showMessageDialog(AltosUI.this,
528                                                               filename,
529                                                               "Cannot open serial port",
530                                                               JOptionPane.ERROR_MESSAGE);
531                         }
532                 }
533         }
534
535         /* Connect to TeleMetrum, either directly or through
536          * a TeleDongle over the packet link
537          */
538         private void SaveFlightData() {
539         }
540
541         /* Configure the log directory. This is where all telemetry and eeprom files
542          * will be written to, and where replay will look for telemetry files
543          */
544         private void ConfigureLog() {
545                 JFileChooser    logdir_chooser = new JFileChooser();
546
547                 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
548                 logdir_chooser.setCurrentDirectory(logdir.getParentFile());
549                 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
550
551                 if (logdir_chooser.showDialog(AltosUI.this, "Select Directory") == JFileChooser.APPROVE_OPTION) {
552                         File dir = logdir_chooser.getSelectedFile();
553                         if (check_dir(dir))
554                                 set_logdir(dir);
555                 }
556         }
557
558         /* Create the AltosUI menus
559          */
560         private void createMenu() {
561                 JMenuBar menubar = new JMenuBar();
562                 JMenu menu;
563                 JMenuItem item;
564                 JRadioButtonMenuItem radioitem;
565
566                 // File menu
567                 {
568                         menu = new JMenu("File");
569                         menu.setMnemonic(KeyEvent.VK_F);
570                         menubar.add(menu);
571
572                         item = new JMenuItem("Quit",KeyEvent.VK_Q);
573                         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
574                                                                    ActionEvent.CTRL_MASK));
575                         item.addActionListener(new ActionListener() {
576                                         public void actionPerformed(ActionEvent e) {
577                                                 System.exit(0);
578                                         }
579                                 });
580                         menu.add(item);
581                 }
582
583                 // Device menu
584                 {
585                         menu = new JMenu("Device");
586                         menu.setMnemonic(KeyEvent.VK_D);
587                         menubar.add(menu);
588
589                         item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
590                         item.addActionListener(new ActionListener() {
591                                         public void actionPerformed(ActionEvent e) {
592                                                 ConnectToDevice();
593                                         }
594                                 });
595                         menu.add(item);
596
597                         item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
598                         item.addActionListener(new ActionListener() {
599                                         public void actionPerformed(ActionEvent e) {
600                                                 serialLine.close();
601                                         }
602                                 });
603                         menu.add(item);
604
605                         menu.addSeparator();
606
607                         item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
608                         item.addActionListener(new ActionListener() {
609                                         public void actionPerformed(ActionEvent e) {
610                                                 SaveFlightData();
611                                         }
612                                 });
613                         menu.add(item);
614
615                         item = new JMenuItem("Replay",KeyEvent.VK_R);
616                         item.addActionListener(new ActionListener() {
617                                         public void actionPerformed(ActionEvent e) {
618                                                 Replay();
619                                         }
620                                 });
621                         menu.add(item);
622                 }
623                 // Log menu
624                 {
625                         menu = new JMenu("Log");
626                         menu.setMnemonic(KeyEvent.VK_L);
627                         menubar.add(menu);
628
629                         item = new JMenuItem("New Log",KeyEvent.VK_N);
630                         item.addActionListener(new ActionListener() {
631                                         public void actionPerformed(ActionEvent e) {
632                                         }
633                                 });
634                         menu.add(item);
635
636                         item = new JMenuItem("Configure Log",KeyEvent.VK_C);
637                         item.addActionListener(new ActionListener() {
638                                         public void actionPerformed(ActionEvent e) {
639                                                 ConfigureLog();
640                                         }
641                                 });
642                         menu.add(item);
643                 }
644                 // Voice menu
645                 {
646                         menu = new JMenu("Voice", true);
647                         menu.setMnemonic(KeyEvent.VK_V);
648                         menubar.add(menu);
649
650                         radioitem = new JRadioButtonMenuItem("Enable Voice");
651                         radioitem.addActionListener(new ActionListener() {
652                                         public void actionPerformed(ActionEvent e) {
653                                         }
654                                 });
655                         menu.add(radioitem);
656                 }
657
658                 // Channel menu
659                 {
660                         menu = new JMenu("Channel", true);
661                         menu.setMnemonic(KeyEvent.VK_C);
662                         menubar.add(menu);
663                         ButtonGroup group = new ButtonGroup();
664
665                         for (int c = 0; c <= 9; c++) {
666                                 radioitem = new JRadioButtonMenuItem(String.format("Channel %1d (%7.3fMHz)", c,
667                                                                                    434.550 + c * 0.1),
668                                                                      c == 0);
669                                 radioitem.setActionCommand(String.format("%d", c));
670                                 radioitem.addActionListener(new ActionListener() {
671                                                 public void actionPerformed(ActionEvent e) {
672                                                         System.out.println("Command: " + e.getActionCommand() + " param: " +
673                                                                            e.paramString());
674                                                 }
675                                         });
676                                 menu.add(radioitem);
677                                 group.add(radioitem);
678                         }
679                 }
680
681                 this.setJMenuBar(menubar);
682
683         }
684         public static void main(final String[] args) {
685                 AltosUI altosui = new AltosUI();
686                 altosui.setVisible(true);
687         }
688 }