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