add compass bearing during descent
[fw/altos] / ao-tools / altosui / AltosRomconfigUI.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 javax.swing.event.*;
26 import java.io.*;
27 import java.util.*;
28 import java.text.*;
29 import java.util.prefs.*;
30
31 import altosui.AltosRomconfig;
32
33 public class AltosRomconfigUI
34         extends JDialog
35         implements ActionListener
36 {
37         Container       pane;
38         Box             box;
39         JLabel          serial_label;
40         JLabel          radio_calibration_label;
41
42         JFrame          owner;
43         JTextField      serial_value;
44         JTextField      radio_calibration_value;
45
46         JButton         ok;
47         JButton         cancel;
48
49         /* Build the UI using a grid bag */
50         public AltosRomconfigUI(JFrame in_owner) {
51                 super (in_owner, "Configure TeleMetrum Rom Values", true);
52
53                 owner = in_owner;
54                 GridBagConstraints c;
55
56                 Insets il = new Insets(4,4,4,4);
57                 Insets ir = new Insets(4,4,4,4);
58
59                 pane = getContentPane();
60                 pane.setLayout(new GridBagLayout());
61
62                 /* Serial */
63                 c = new GridBagConstraints();
64                 c.gridx = 0; c.gridy = 0;
65                 c.gridwidth = 3;
66                 c.fill = GridBagConstraints.NONE;
67                 c.anchor = GridBagConstraints.LINE_START;
68                 c.insets = il;
69                 serial_label = new JLabel("Serial:");
70                 pane.add(serial_label, c);
71
72                 c = new GridBagConstraints();
73                 c.gridx = 3; c.gridy = 0;
74                 c.gridwidth = 3;
75                 c.fill = GridBagConstraints.HORIZONTAL;
76                 c.weightx = 1;
77                 c.anchor = GridBagConstraints.LINE_START;
78                 c.insets = ir;
79                 serial_value = new JTextField("0");
80                 pane.add(serial_value, c);
81
82                 /* Radio calibration value */
83                 c = new GridBagConstraints();
84                 c.gridx = 0; c.gridy = 1;
85                 c.gridwidth = 3;
86                 c.fill = GridBagConstraints.NONE;
87                 c.anchor = GridBagConstraints.LINE_START;
88                 c.insets = il;
89                 c.ipady = 5;
90                 radio_calibration_label = new JLabel("Radio Calibration:");
91                 pane.add(radio_calibration_label, c);
92
93                 c = new GridBagConstraints();
94                 c.gridx = 3; c.gridy = 1;
95                 c.gridwidth = 3;
96                 c.fill = GridBagConstraints.HORIZONTAL;
97                 c.weightx = 1;
98                 c.anchor = GridBagConstraints.LINE_START;
99                 c.insets = ir;
100                 c.ipady = 5;
101                 radio_calibration_value = new JTextField("1186611");
102                 pane.add(radio_calibration_value, c);
103
104                 /* Buttons */
105                 c = new GridBagConstraints();
106                 c.gridx = 0; c.gridy = 2;
107                 c.gridwidth = 3;
108                 c.fill = GridBagConstraints.NONE;
109                 c.anchor = GridBagConstraints.CENTER;
110                 c.insets = il;
111                 ok = new JButton("OK");
112                 pane.add(ok, c);
113                 ok.addActionListener(this);
114                 ok.setActionCommand("ok");
115
116                 c = new GridBagConstraints();
117                 c.gridx = 3; c.gridy = 2;
118                 c.gridwidth = 3;
119                 c.fill = GridBagConstraints.NONE;
120                 c.anchor = GridBagConstraints.CENTER;
121                 c.insets = il;
122                 cancel = new JButton("Cancel");
123                 pane.add(cancel, c);
124                 cancel.addActionListener(this);
125                 cancel.setActionCommand("cancel");
126
127                 pack();
128                 setLocationRelativeTo(owner);
129         }
130
131         boolean selected;
132
133         /* Listen for events from our buttons */
134         public void actionPerformed(ActionEvent e) {
135                 String  cmd = e.getActionCommand();
136
137                 if (cmd.equals("ok")) {
138                         AltosRomconfig  romconfig = romconfig();
139                         if (romconfig == null || !romconfig.valid()) {
140                                 JOptionPane.showMessageDialog(this,
141                                                               "Invalid serial number or radio calibration value",
142                                                               "Invalid rom configuration",
143                                                               JOptionPane.ERROR_MESSAGE);
144                                 return;
145                         }
146                         selected = true;
147                 }
148                 setVisible(false);
149         }
150
151         int serial() {
152                 return Integer.parseInt(serial_value.getText());
153         }
154
155         void set_serial(int serial) {
156                 serial_value.setText(String.format("%d", serial));
157         }
158
159         int radio_calibration() {
160                 return Integer.parseInt(radio_calibration_value.getText());
161         }
162
163         void set_radio_calibration(int calibration) {
164                 radio_calibration_value.setText(String.format("%d", calibration));
165         }
166
167         public void set(AltosRomconfig config) {
168                 if (config != null && config.valid()) {
169                         set_serial(config.serial_number);
170                         set_radio_calibration(config.radio_calibration);
171                 }
172         }
173
174         AltosRomconfig romconfig() {
175                 try {
176                         return new AltosRomconfig(serial(), radio_calibration());
177                 } catch (NumberFormatException ne) {
178                         return null;
179                 }
180         }
181
182         public AltosRomconfig showDialog() {
183                 setVisible(true);
184                 if (selected)
185                         return romconfig();
186                 return null;
187         }
188 }