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_14;
22 import java.awt.event.*;
24 import org.altusmetrum.altoslib_14.*;
26 public class AltosRomconfigUI
28 implements ActionListener
34 JLabel radio_calibration_label;
38 JTextField serial_value;
39 JTextField radio_calibration_value;
44 /* Build the UI using a grid bag */
45 public AltosRomconfigUI(JFrame frame, AltosRomconfig config) {
46 super (frame, "Configure Rom Values", true);
51 Insets il = new Insets(4,4,4,4);
52 Insets ir = new Insets(4,4,4,4);
54 pane = getContentPane();
55 pane.setLayout(new GridBagLayout());
61 c = new GridBagConstraints();
62 c.gridx = 0; c.gridy = y;
64 c.fill = GridBagConstraints.NONE;
65 c.anchor = GridBagConstraints.LINE_START;
67 product_label = new JLabel("Product:");
68 pane.add(product_label, c);
70 c = new GridBagConstraints();
71 c.gridx = 3; c.gridy = y;
73 c.fill = GridBagConstraints.HORIZONTAL;
75 c.anchor = GridBagConstraints.LINE_START;
77 product_value = new JLabel(config.usb_product);
78 pane.add(product_value, c);
83 c = new GridBagConstraints();
84 c.gridx = 0; c.gridy = y;
86 c.fill = GridBagConstraints.NONE;
87 c.anchor = GridBagConstraints.LINE_START;
89 serial_label = new JLabel("Serial:");
90 pane.add(serial_label, c);
92 c = new GridBagConstraints();
93 c.gridx = 3; c.gridy = y;
95 c.fill = GridBagConstraints.HORIZONTAL;
97 c.anchor = GridBagConstraints.LINE_START;
99 serial_value = new JTextField("00000000");
100 pane.add(serial_value, c);
104 if (config == null || AltosLib.has_radio(config.usb_id.pid)) {
105 /* Radio calibration value */
106 c = new GridBagConstraints();
107 c.gridx = 0; c.gridy = y;
109 c.fill = GridBagConstraints.NONE;
110 c.anchor = GridBagConstraints.LINE_START;
113 radio_calibration_label = new JLabel("Radio Calibration:");
114 pane.add(radio_calibration_label, c);
116 c = new GridBagConstraints();
117 c.gridx = 3; c.gridy = y;
119 c.fill = GridBagConstraints.HORIZONTAL;
121 c.anchor = GridBagConstraints.LINE_START;
124 radio_calibration_value = new JTextField("00000000");
125 pane.add(radio_calibration_value, c);
131 c = new GridBagConstraints();
132 c.gridx = 0; c.gridy = y;
134 c.fill = GridBagConstraints.NONE;
135 c.anchor = GridBagConstraints.CENTER;
137 ok = new JButton("OK");
139 ok.addActionListener(this);
140 ok.setActionCommand("ok");
142 c = new GridBagConstraints();
143 c.gridx = 3; c.gridy = y;
145 c.fill = GridBagConstraints.NONE;
146 c.anchor = GridBagConstraints.CENTER;
148 cancel = new JButton("Cancel");
150 cancel.addActionListener(this);
151 cancel.setActionCommand("cancel");
156 setLocationRelativeTo(owner);
162 /* Listen for events from our buttons */
163 public void actionPerformed(ActionEvent e) {
164 String cmd = e.getActionCommand();
166 if (cmd.equals("ok")) {
167 AltosRomconfig romconfig = romconfig();
168 if (romconfig == null || !romconfig.valid()) {
169 JOptionPane.showMessageDialog(this,
170 "Invalid serial number or radio calibration value",
171 "Invalid rom configuration",
172 JOptionPane.ERROR_MESSAGE);
181 return Integer.parseInt(serial_value.getText());
184 void set_serial(int serial) {
185 serial_value.setText(String.format("%d", serial));
188 int radio_calibration() {
189 if (radio_calibration_value == null)
192 return Integer.parseInt(radio_calibration_value.getText());
195 void set_radio_calibration(int calibration) {
196 if (radio_calibration_value == null)
198 radio_calibration_value.setText(String.format("%d", calibration));
201 public void set(AltosRomconfig config) {
202 if (config != null && config.valid()) {
203 set_serial(config.serial_number);
204 set_radio_calibration(config.radio_calibration);
208 AltosRomconfig romconfig() {
210 return new AltosRomconfig(serial(), radio_calibration());
211 } catch (NumberFormatException ne) {
216 public AltosRomconfig showDialog() {
223 public static AltosRomconfig show(JFrame frame, AltosRomconfig config) {
224 AltosRomconfigUI ui = new AltosRomconfigUI(frame, config);
225 return ui.showDialog();