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_11;
22 import java.awt.event.*;
24 import org.altusmetrum.altoslib_11.*;
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 in_owner) {
44 super (in_owner, "Configure TeleMetrum 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());
56 c = new GridBagConstraints();
57 c.gridx = 0; c.gridy = 0;
59 c.fill = GridBagConstraints.NONE;
60 c.anchor = GridBagConstraints.LINE_START;
62 serial_label = new JLabel("Serial:");
63 pane.add(serial_label, c);
65 c = new GridBagConstraints();
66 c.gridx = 3; c.gridy = 0;
68 c.fill = GridBagConstraints.HORIZONTAL;
70 c.anchor = GridBagConstraints.LINE_START;
72 serial_value = new JTextField("00000000");
73 pane.add(serial_value, c);
75 /* Radio calibration value */
76 c = new GridBagConstraints();
77 c.gridx = 0; c.gridy = 1;
79 c.fill = GridBagConstraints.NONE;
80 c.anchor = GridBagConstraints.LINE_START;
83 radio_calibration_label = new JLabel("Radio Calibration:");
84 pane.add(radio_calibration_label, c);
86 c = new GridBagConstraints();
87 c.gridx = 3; c.gridy = 1;
89 c.fill = GridBagConstraints.HORIZONTAL;
91 c.anchor = GridBagConstraints.LINE_START;
94 radio_calibration_value = new JTextField("00000000");
95 pane.add(radio_calibration_value, c);
98 c = new GridBagConstraints();
99 c.gridx = 0; c.gridy = 2;
101 c.fill = GridBagConstraints.NONE;
102 c.anchor = GridBagConstraints.CENTER;
104 ok = new JButton("OK");
106 ok.addActionListener(this);
107 ok.setActionCommand("ok");
109 c = new GridBagConstraints();
110 c.gridx = 3; c.gridy = 2;
112 c.fill = GridBagConstraints.NONE;
113 c.anchor = GridBagConstraints.CENTER;
115 cancel = new JButton("Cancel");
117 cancel.addActionListener(this);
118 cancel.setActionCommand("cancel");
121 setLocationRelativeTo(owner);
124 public AltosRomconfigUI(JFrame frame, AltosRomconfig config) {
131 /* Listen for events from our buttons */
132 public void actionPerformed(ActionEvent e) {
133 String cmd = e.getActionCommand();
135 if (cmd.equals("ok")) {
136 AltosRomconfig romconfig = romconfig();
137 if (romconfig == null || !romconfig.valid()) {
138 JOptionPane.showMessageDialog(this,
139 "Invalid serial number or radio calibration value",
140 "Invalid rom configuration",
141 JOptionPane.ERROR_MESSAGE);
150 return Integer.parseInt(serial_value.getText());
153 void set_serial(int serial) {
154 serial_value.setText(String.format("%d", serial));
157 int radio_calibration() {
158 return Integer.parseInt(radio_calibration_value.getText());
161 void set_radio_calibration(int calibration) {
162 radio_calibration_value.setText(String.format("%d", calibration));
165 public void set(AltosRomconfig config) {
166 if (config != null && config.valid()) {
167 set_serial(config.serial_number);
168 set_radio_calibration(config.radio_calibration);
172 AltosRomconfig romconfig() {
174 return new AltosRomconfig(serial(), radio_calibration());
175 } catch (NumberFormatException ne) {
180 public AltosRomconfig showDialog() {
187 public static AltosRomconfig show(JFrame frame, AltosRomconfig config) {
188 AltosRomconfigUI ui = new AltosRomconfigUI(frame, config);
189 return ui.showDialog();