altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / altosuilib / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altosuilib_14;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import org.altusmetrum.altoslib_14.*;
25
26 public class AltosRomconfigUI
27         extends AltosUIDialog
28         implements ActionListener
29 {
30         Container       pane;
31         Box             box;
32         JLabel          product_label;
33         JLabel          serial_label;
34         JLabel          radio_calibration_label;
35
36         JLabel          product_value;
37         JFrame          owner;
38         JTextField      serial_value;
39         JTextField      radio_calibration_value;
40
41         JButton         ok;
42         JButton         cancel;
43
44         /* Build the UI using a grid bag */
45         public AltosRomconfigUI(JFrame frame, AltosRomconfig config) {
46                 super (frame, "Configure Rom Values", true);
47
48                 owner = frame;
49                 GridBagConstraints c;
50
51                 Insets il = new Insets(4,4,4,4);
52                 Insets ir = new Insets(4,4,4,4);
53
54                 pane = getScrollablePane();
55                 pane.setLayout(new GridBagLayout());
56
57                 int y = 0;
58
59                 /* Product name */
60                 /* Serial */
61                 c = new GridBagConstraints();
62                 c.gridx = 0; c.gridy = y;
63                 c.gridwidth = 3;
64                 c.fill = GridBagConstraints.NONE;
65                 c.anchor = GridBagConstraints.LINE_START;
66                 c.insets = il;
67                 product_label = new JLabel("Product:");
68                 pane.add(product_label, c);
69
70                 c = new GridBagConstraints();
71                 c.gridx = 3; c.gridy = y;
72                 c.gridwidth = 3;
73                 c.fill = GridBagConstraints.HORIZONTAL;
74                 c.weightx = 1;
75                 c.anchor = GridBagConstraints.LINE_START;
76                 c.insets = ir;
77                 String product = "unknown";
78                 if (config != null)
79                         product = config.usb_product;
80                 product_value = new JLabel(product);
81                 pane.add(product_value, c);
82
83                 y++;
84
85                 /* Serial */
86                 c = new GridBagConstraints();
87                 c.gridx = 0; c.gridy = y;
88                 c.gridwidth = 3;
89                 c.fill = GridBagConstraints.NONE;
90                 c.anchor = GridBagConstraints.LINE_START;
91                 c.insets = il;
92                 serial_label = new JLabel("Serial:");
93                 pane.add(serial_label, c);
94
95                 c = new GridBagConstraints();
96                 c.gridx = 3; c.gridy = y;
97                 c.gridwidth = 3;
98                 c.fill = GridBagConstraints.HORIZONTAL;
99                 c.weightx = 1;
100                 c.anchor = GridBagConstraints.LINE_START;
101                 c.insets = ir;
102                 serial_value = new JTextField("00000000");
103                 pane.add(serial_value, c);
104
105                 y++;
106
107                 if (config == null || AltosLib.has_radio(config.usb_id.pid)) {
108                         /* Radio calibration value */
109                         c = new GridBagConstraints();
110                         c.gridx = 0; c.gridy = y;
111                         c.gridwidth = 3;
112                         c.fill = GridBagConstraints.NONE;
113                         c.anchor = GridBagConstraints.LINE_START;
114                         c.insets = il;
115                         c.ipady = 5;
116                         radio_calibration_label = new JLabel("Radio Calibration:");
117                         pane.add(radio_calibration_label, c);
118
119                         c = new GridBagConstraints();
120                         c.gridx = 3; c.gridy = y;
121                         c.gridwidth = 3;
122                         c.fill = GridBagConstraints.HORIZONTAL;
123                         c.weightx = 1;
124                         c.anchor = GridBagConstraints.LINE_START;
125                         c.insets = ir;
126                         c.ipady = 5;
127                         radio_calibration_value = new JTextField("00000000");
128                         pane.add(radio_calibration_value, c);
129
130                         y++;
131                 }
132
133                 /* Buttons */
134                 c = new GridBagConstraints();
135                 c.gridx = 0; c.gridy = y;
136                 c.gridwidth = 3;
137                 c.fill = GridBagConstraints.NONE;
138                 c.anchor = GridBagConstraints.CENTER;
139                 c.insets = il;
140                 ok = new JButton("OK");
141                 pane.add(ok, c);
142                 ok.addActionListener(this);
143                 ok.setActionCommand("ok");
144
145                 c = new GridBagConstraints();
146                 c.gridx = 3; c.gridy = y;
147                 c.gridwidth = 3;
148                 c.fill = GridBagConstraints.NONE;
149                 c.anchor = GridBagConstraints.CENTER;
150                 c.insets = il;
151                 cancel = new JButton("Cancel");
152                 pane.add(cancel, c);
153                 cancel.addActionListener(this);
154                 cancel.setActionCommand("cancel");
155
156                 y++;
157
158                 pack();
159                 setLocationRelativeTo(owner);
160                 set(config);
161         }
162
163         boolean selected;
164
165         /* Listen for events from our buttons */
166         public void actionPerformed(ActionEvent e) {
167                 String  cmd = e.getActionCommand();
168
169                 if (cmd.equals("ok")) {
170                         AltosRomconfig  romconfig = romconfig();
171                         if (romconfig == null || !romconfig.valid()) {
172                                 JOptionPane.showMessageDialog(this,
173                                                               "Invalid serial number or radio calibration value",
174                                                               "Invalid rom configuration",
175                                                               JOptionPane.ERROR_MESSAGE);
176                                 return;
177                         }
178                         selected = true;
179                 }
180                 setVisible(false);
181         }
182
183         int serial() {
184                 return Integer.parseInt(serial_value.getText());
185         }
186
187         void set_serial(int serial) {
188                 serial_value.setText(String.format("%d", serial));
189         }
190
191         int radio_calibration() {
192                 if (radio_calibration_value == null)
193                         return 0;
194
195                 return Integer.parseInt(radio_calibration_value.getText());
196         }
197
198         void set_radio_calibration(int calibration) {
199                 if (radio_calibration_value == null)
200                         return;
201                 radio_calibration_value.setText(String.format("%d", calibration));
202         }
203
204         public void set(AltosRomconfig config) {
205                 if (config != null && config.valid()) {
206                         set_serial(config.serial_number);
207                         set_radio_calibration(config.radio_calibration);
208                 }
209         }
210
211         AltosRomconfig romconfig() {
212                 try {
213                         return new AltosRomconfig(serial(), radio_calibration());
214                 } catch (NumberFormatException ne) {
215                         return null;
216                 }
217         }
218
219         public AltosRomconfig showDialog() {
220                 setVisible(true);
221                 if (selected)
222                         return romconfig();
223                 return null;
224         }
225
226         public static AltosRomconfig show(JFrame frame, AltosRomconfig config) {
227                 AltosRomconfigUI ui = new AltosRomconfigUI(frame, config);
228                 return ui.showDialog();
229         }
230 }