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 = getScrollablePane();
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 String product = "unknown";
79 product = config.usb_product;
80 product_value = new JLabel(product);
81 pane.add(product_value, c);
86 c = new GridBagConstraints();
87 c.gridx = 0; c.gridy = y;
89 c.fill = GridBagConstraints.NONE;
90 c.anchor = GridBagConstraints.LINE_START;
92 serial_label = new JLabel("Serial:");
93 pane.add(serial_label, c);
95 c = new GridBagConstraints();
96 c.gridx = 3; c.gridy = y;
98 c.fill = GridBagConstraints.HORIZONTAL;
100 c.anchor = GridBagConstraints.LINE_START;
102 serial_value = new JTextField("00000000");
103 pane.add(serial_value, c);
107 if (config == null || AltosLib.has_radio(config.usb_id.pid)) {
108 /* Radio calibration value */
109 c = new GridBagConstraints();
110 c.gridx = 0; c.gridy = y;
112 c.fill = GridBagConstraints.NONE;
113 c.anchor = GridBagConstraints.LINE_START;
116 radio_calibration_label = new JLabel("Radio Calibration:");
117 pane.add(radio_calibration_label, c);
119 c = new GridBagConstraints();
120 c.gridx = 3; c.gridy = y;
122 c.fill = GridBagConstraints.HORIZONTAL;
124 c.anchor = GridBagConstraints.LINE_START;
127 radio_calibration_value = new JTextField("00000000");
128 pane.add(radio_calibration_value, c);
134 c = new GridBagConstraints();
135 c.gridx = 0; c.gridy = y;
137 c.fill = GridBagConstraints.NONE;
138 c.anchor = GridBagConstraints.CENTER;
140 ok = new JButton("OK");
142 ok.addActionListener(this);
143 ok.setActionCommand("ok");
145 c = new GridBagConstraints();
146 c.gridx = 3; c.gridy = y;
148 c.fill = GridBagConstraints.NONE;
149 c.anchor = GridBagConstraints.CENTER;
151 cancel = new JButton("Cancel");
153 cancel.addActionListener(this);
154 cancel.setActionCommand("cancel");
159 setLocationRelativeTo(owner);
165 /* Listen for events from our buttons */
166 public void actionPerformed(ActionEvent e) {
167 String cmd = e.getActionCommand();
169 if (cmd.equals("ok")) {
170 AltosRomconfig romconfig = romconfig();
171 if (romconfig == null || !romconfig.valid()) {
172 JOptionPane.showMessageDialog(this,
173 "Invalid serial number or radio calibration value",
174 "Invalid rom configuration",
175 JOptionPane.ERROR_MESSAGE);
184 return Integer.parseInt(serial_value.getText());
187 void set_serial(int serial) {
188 serial_value.setText(String.format("%d", serial));
191 int radio_calibration() {
192 if (radio_calibration_value == null)
195 return Integer.parseInt(radio_calibration_value.getText());
198 void set_radio_calibration(int calibration) {
199 if (radio_calibration_value == null)
201 radio_calibration_value.setText(String.format("%d", calibration));
204 public void set(AltosRomconfig config) {
205 if (config != null && config.valid()) {
206 set_serial(config.serial_number);
207 set_radio_calibration(config.radio_calibration);
211 AltosRomconfig romconfig() {
213 return new AltosRomconfig(serial(), radio_calibration());
214 } catch (NumberFormatException ne) {
219 public AltosRomconfig showDialog() {
226 public static AltosRomconfig show(JFrame frame, AltosRomconfig config) {
227 AltosRomconfigUI ui = new AltosRomconfigUI(frame, config);
228 return ui.showDialog();