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