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.
18 package org.altusmetrum.altosuilib_7;
21 import java.awt.event.*;
23 import org.altusmetrum.altoslib_7.*;
25 public class AltosRomconfigUI
27 implements ActionListener
32 JLabel radio_calibration_label;
35 JTextField serial_value;
36 JTextField radio_calibration_value;
41 /* Build the UI using a grid bag */
42 public AltosRomconfigUI(JFrame in_owner) {
43 super (in_owner, "Configure TeleMetrum Rom Values", true);
48 Insets il = new Insets(4,4,4,4);
49 Insets ir = new Insets(4,4,4,4);
51 pane = getContentPane();
52 pane.setLayout(new GridBagLayout());
55 c = new GridBagConstraints();
56 c.gridx = 0; c.gridy = 0;
58 c.fill = GridBagConstraints.NONE;
59 c.anchor = GridBagConstraints.LINE_START;
61 serial_label = new JLabel("Serial:");
62 pane.add(serial_label, c);
64 c = new GridBagConstraints();
65 c.gridx = 3; c.gridy = 0;
67 c.fill = GridBagConstraints.HORIZONTAL;
69 c.anchor = GridBagConstraints.LINE_START;
71 serial_value = new JTextField("00000000");
72 pane.add(serial_value, c);
74 /* Radio calibration value */
75 c = new GridBagConstraints();
76 c.gridx = 0; c.gridy = 1;
78 c.fill = GridBagConstraints.NONE;
79 c.anchor = GridBagConstraints.LINE_START;
82 radio_calibration_label = new JLabel("Radio Calibration:");
83 pane.add(radio_calibration_label, c);
85 c = new GridBagConstraints();
86 c.gridx = 3; c.gridy = 1;
88 c.fill = GridBagConstraints.HORIZONTAL;
90 c.anchor = GridBagConstraints.LINE_START;
93 radio_calibration_value = new JTextField("00000000");
94 pane.add(radio_calibration_value, c);
97 c = new GridBagConstraints();
98 c.gridx = 0; c.gridy = 2;
100 c.fill = GridBagConstraints.NONE;
101 c.anchor = GridBagConstraints.CENTER;
103 ok = new JButton("OK");
105 ok.addActionListener(this);
106 ok.setActionCommand("ok");
108 c = new GridBagConstraints();
109 c.gridx = 3; c.gridy = 2;
111 c.fill = GridBagConstraints.NONE;
112 c.anchor = GridBagConstraints.CENTER;
114 cancel = new JButton("Cancel");
116 cancel.addActionListener(this);
117 cancel.setActionCommand("cancel");
120 setLocationRelativeTo(owner);
123 public AltosRomconfigUI(JFrame frame, AltosRomconfig config) {
130 /* Listen for events from our buttons */
131 public void actionPerformed(ActionEvent e) {
132 String cmd = e.getActionCommand();
134 if (cmd.equals("ok")) {
135 AltosRomconfig romconfig = romconfig();
136 if (romconfig == null || !romconfig.valid()) {
137 JOptionPane.showMessageDialog(this,
138 "Invalid serial number or radio calibration value",
139 "Invalid rom configuration",
140 JOptionPane.ERROR_MESSAGE);
149 return Integer.parseInt(serial_value.getText());
152 void set_serial(int serial) {
153 serial_value.setText(String.format("%d", serial));
156 int radio_calibration() {
157 return Integer.parseInt(radio_calibration_value.getText());
160 void set_radio_calibration(int calibration) {
161 radio_calibration_value.setText(String.format("%d", calibration));
164 public void set(AltosRomconfig config) {
165 if (config != null && config.valid()) {
166 set_serial(config.serial_number);
167 set_radio_calibration(config.radio_calibration);
171 AltosRomconfig romconfig() {
173 return new AltosRomconfig(serial(), radio_calibration());
174 } catch (NumberFormatException ne) {
179 public AltosRomconfig showDialog() {
186 public static AltosRomconfig show(JFrame frame, AltosRomconfig config) {
187 AltosRomconfigUI ui = new AltosRomconfigUI(frame, config);
188 return ui.showDialog();