2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 import java.awt.event.*;
24 public class AltosRomconfigUI
26 implements ActionListener
31 JLabel radio_calibration_label;
34 JTextField serial_value;
35 JTextField radio_calibration_value;
40 /* Build the UI using a grid bag */
41 public AltosRomconfigUI(JFrame in_owner) {
42 super (in_owner, "Configure TeleMetrum Rom Values", true);
47 Insets il = new Insets(4,4,4,4);
48 Insets ir = new Insets(4,4,4,4);
50 pane = getContentPane();
51 pane.setLayout(new GridBagLayout());
54 c = new GridBagConstraints();
55 c.gridx = 0; c.gridy = 0;
57 c.fill = GridBagConstraints.NONE;
58 c.anchor = GridBagConstraints.LINE_START;
60 serial_label = new JLabel("Serial:");
61 pane.add(serial_label, c);
63 c = new GridBagConstraints();
64 c.gridx = 3; c.gridy = 0;
66 c.fill = GridBagConstraints.HORIZONTAL;
68 c.anchor = GridBagConstraints.LINE_START;
70 serial_value = new JTextField("0");
71 pane.add(serial_value, c);
73 /* Radio calibration value */
74 c = new GridBagConstraints();
75 c.gridx = 0; c.gridy = 1;
77 c.fill = GridBagConstraints.NONE;
78 c.anchor = GridBagConstraints.LINE_START;
81 radio_calibration_label = new JLabel("Radio Calibration:");
82 pane.add(radio_calibration_label, c);
84 c = new GridBagConstraints();
85 c.gridx = 3; c.gridy = 1;
87 c.fill = GridBagConstraints.HORIZONTAL;
89 c.anchor = GridBagConstraints.LINE_START;
92 radio_calibration_value = new JTextField("1186611");
93 pane.add(radio_calibration_value, c);
96 c = new GridBagConstraints();
97 c.gridx = 0; c.gridy = 2;
99 c.fill = GridBagConstraints.NONE;
100 c.anchor = GridBagConstraints.CENTER;
102 ok = new JButton("OK");
104 ok.addActionListener(this);
105 ok.setActionCommand("ok");
107 c = new GridBagConstraints();
108 c.gridx = 3; c.gridy = 2;
110 c.fill = GridBagConstraints.NONE;
111 c.anchor = GridBagConstraints.CENTER;
113 cancel = new JButton("Cancel");
115 cancel.addActionListener(this);
116 cancel.setActionCommand("cancel");
119 setLocationRelativeTo(owner);
122 public AltosRomconfigUI(JFrame frame, AltosRomconfig config) {
129 /* Listen for events from our buttons */
130 public void actionPerformed(ActionEvent e) {
131 String cmd = e.getActionCommand();
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);
148 return Integer.parseInt(serial_value.getText());
151 void set_serial(int serial) {
152 serial_value.setText(String.format("%d", serial));
155 int radio_calibration() {
156 return Integer.parseInt(radio_calibration_value.getText());
159 void set_radio_calibration(int calibration) {
160 radio_calibration_value.setText(String.format("%d", calibration));
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);
170 AltosRomconfig romconfig() {
172 return new AltosRomconfig(serial(), radio_calibration());
173 } catch (NumberFormatException ne) {
178 public AltosRomconfig showDialog() {
185 public static AltosRomconfig show(JFrame frame, AltosRomconfig config) {
186 AltosRomconfigUI ui = new AltosRomconfigUI(frame, config);
187 return ui.showDialog();