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