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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altosuilib_13;
22 import java.awt.event.*;
24 import org.altusmetrum.altoslib_13.*;
26 public class AltosRomconfigUI
28 implements ActionListener
33 JLabel radio_calibration_label;
36 JTextField serial_value;
37 JTextField radio_calibration_value;
42 /* Build the UI using a grid bag */
43 public AltosRomconfigUI(JFrame frame, AltosRomconfig config) {
44 super (frame, "Configure Rom Values", true);
49 Insets il = new Insets(4,4,4,4);
50 Insets ir = new Insets(4,4,4,4);
52 pane = getContentPane();
53 pane.setLayout(new GridBagLayout());
58 c = new GridBagConstraints();
59 c.gridx = 0; c.gridy = y;
61 c.fill = GridBagConstraints.NONE;
62 c.anchor = GridBagConstraints.LINE_START;
64 serial_label = new JLabel("Serial:");
65 pane.add(serial_label, c);
67 c = new GridBagConstraints();
68 c.gridx = 3; c.gridy = y;
70 c.fill = GridBagConstraints.HORIZONTAL;
72 c.anchor = GridBagConstraints.LINE_START;
74 serial_value = new JTextField("00000000");
75 pane.add(serial_value, c);
79 if (AltosLib.has_radio(config.usb_id.pid)) {
80 /* Radio calibration value */
81 c = new GridBagConstraints();
82 c.gridx = 0; c.gridy = y;
84 c.fill = GridBagConstraints.NONE;
85 c.anchor = GridBagConstraints.LINE_START;
88 radio_calibration_label = new JLabel("Radio Calibration:");
89 pane.add(radio_calibration_label, c);
91 c = new GridBagConstraints();
92 c.gridx = 3; c.gridy = y;
94 c.fill = GridBagConstraints.HORIZONTAL;
96 c.anchor = GridBagConstraints.LINE_START;
99 radio_calibration_value = new JTextField("00000000");
100 pane.add(radio_calibration_value, c);
106 c = new GridBagConstraints();
107 c.gridx = 0; c.gridy = y;
109 c.fill = GridBagConstraints.NONE;
110 c.anchor = GridBagConstraints.CENTER;
112 ok = new JButton("OK");
114 ok.addActionListener(this);
115 ok.setActionCommand("ok");
117 c = new GridBagConstraints();
118 c.gridx = 3; c.gridy = y;
120 c.fill = GridBagConstraints.NONE;
121 c.anchor = GridBagConstraints.CENTER;
123 cancel = new JButton("Cancel");
125 cancel.addActionListener(this);
126 cancel.setActionCommand("cancel");
131 setLocationRelativeTo(owner);
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 if (radio_calibration_value == null)
167 return Integer.parseInt(radio_calibration_value.getText());
170 void set_radio_calibration(int calibration) {
171 if (radio_calibration_value == null)
173 radio_calibration_value.setText(String.format("%d", calibration));
176 public void set(AltosRomconfig config) {
177 if (config != null && config.valid()) {
178 set_serial(config.serial_number);
179 set_radio_calibration(config.radio_calibration);
183 AltosRomconfig romconfig() {
185 return new AltosRomconfig(serial(), radio_calibration());
186 } catch (NumberFormatException ne) {
191 public AltosRomconfig showDialog() {
198 public static AltosRomconfig show(JFrame frame, AltosRomconfig config) {
199 AltosRomconfigUI ui = new AltosRomconfigUI(frame, config);
200 return ui.showDialog();