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.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import javax.swing.event.*;
29 import java.util.prefs.*;
30 import org.altusmetrum.AltosLib.*;
32 public class AltosRomconfigUI
34 implements ActionListener
39 JLabel radio_calibration_label;
42 JTextField serial_value;
43 JTextField radio_calibration_value;
48 /* Build the UI using a grid bag */
49 public AltosRomconfigUI(JFrame in_owner) {
50 super (in_owner, "Configure TeleMetrum Rom Values", true);
55 Insets il = new Insets(4,4,4,4);
56 Insets ir = new Insets(4,4,4,4);
58 pane = getContentPane();
59 pane.setLayout(new GridBagLayout());
62 c = new GridBagConstraints();
63 c.gridx = 0; c.gridy = 0;
65 c.fill = GridBagConstraints.NONE;
66 c.anchor = GridBagConstraints.LINE_START;
68 serial_label = new JLabel("Serial:");
69 pane.add(serial_label, c);
71 c = new GridBagConstraints();
72 c.gridx = 3; c.gridy = 0;
74 c.fill = GridBagConstraints.HORIZONTAL;
76 c.anchor = GridBagConstraints.LINE_START;
78 serial_value = new JTextField("0");
79 pane.add(serial_value, c);
81 /* Radio calibration value */
82 c = new GridBagConstraints();
83 c.gridx = 0; c.gridy = 1;
85 c.fill = GridBagConstraints.NONE;
86 c.anchor = GridBagConstraints.LINE_START;
89 radio_calibration_label = new JLabel("Radio Calibration:");
90 pane.add(radio_calibration_label, c);
92 c = new GridBagConstraints();
93 c.gridx = 3; c.gridy = 1;
95 c.fill = GridBagConstraints.HORIZONTAL;
97 c.anchor = GridBagConstraints.LINE_START;
100 radio_calibration_value = new JTextField("1186611");
101 pane.add(radio_calibration_value, c);
104 c = new GridBagConstraints();
105 c.gridx = 0; c.gridy = 2;
107 c.fill = GridBagConstraints.NONE;
108 c.anchor = GridBagConstraints.CENTER;
110 ok = new JButton("OK");
112 ok.addActionListener(this);
113 ok.setActionCommand("ok");
115 c = new GridBagConstraints();
116 c.gridx = 3; c.gridy = 2;
118 c.fill = GridBagConstraints.NONE;
119 c.anchor = GridBagConstraints.CENTER;
121 cancel = new JButton("Cancel");
123 cancel.addActionListener(this);
124 cancel.setActionCommand("cancel");
127 setLocationRelativeTo(owner);
130 public AltosRomconfigUI(JFrame frame, AltosRomconfig config) {
137 /* Listen for events from our buttons */
138 public void actionPerformed(ActionEvent e) {
139 String cmd = e.getActionCommand();
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);
156 return Integer.parseInt(serial_value.getText());
159 void set_serial(int serial) {
160 serial_value.setText(String.format("%d", serial));
163 int radio_calibration() {
164 return Integer.parseInt(radio_calibration_value.getText());
167 void set_radio_calibration(int calibration) {
168 radio_calibration_value.setText(String.format("%d", calibration));
171 public void set(AltosRomconfig config) {
172 if (config != null && config.valid()) {
173 set_serial(config.serial_number);
174 set_radio_calibration(config.radio_calibration);
178 AltosRomconfig romconfig() {
180 return new AltosRomconfig(serial(), radio_calibration());
181 } catch (NumberFormatException ne) {
186 public AltosRomconfig showDialog() {
193 public static AltosRomconfig show(JFrame frame, AltosRomconfig config) {
194 AltosRomconfigUI ui = new AltosRomconfigUI(frame, config);
195 return ui.showDialog();