]> git.gag.com Git - fw/altos/blob - altosuilib/AltosRomconfigUI.java
altos/ao_rn4678: Send '$$$' for new devices. Fix name setting code.
[fw/altos] / altosuilib / AltosRomconfigUI.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.altosuilib_11;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import org.altusmetrum.altoslib_11.*;
25
26 public class AltosRomconfigUI
27         extends AltosUIDialog
28         implements ActionListener
29 {
30         Container       pane;
31         Box             box;
32         JLabel          serial_label;
33         JLabel          radio_calibration_label;
34
35         JFrame          owner;
36         JTextField      serial_value;
37         JTextField      radio_calibration_value;
38
39         JButton         ok;
40         JButton         cancel;
41
42         /* Build the UI using a grid bag */
43         public AltosRomconfigUI(JFrame in_owner) {
44                 super (in_owner, "Configure TeleMetrum Rom Values", true);
45
46                 owner = in_owner;
47                 GridBagConstraints c;
48
49                 Insets il = new Insets(4,4,4,4);
50                 Insets ir = new Insets(4,4,4,4);
51
52                 pane = getContentPane();
53                 pane.setLayout(new GridBagLayout());
54
55                 /* Serial */
56                 c = new GridBagConstraints();
57                 c.gridx = 0; c.gridy = 0;
58                 c.gridwidth = 3;
59                 c.fill = GridBagConstraints.NONE;
60                 c.anchor = GridBagConstraints.LINE_START;
61                 c.insets = il;
62                 serial_label = new JLabel("Serial:");
63                 pane.add(serial_label, c);
64
65                 c = new GridBagConstraints();
66                 c.gridx = 3; c.gridy = 0;
67                 c.gridwidth = 3;
68                 c.fill = GridBagConstraints.HORIZONTAL;
69                 c.weightx = 1;
70                 c.anchor = GridBagConstraints.LINE_START;
71                 c.insets = ir;
72                 serial_value = new JTextField("00000000");
73                 pane.add(serial_value, c);
74
75                 /* Radio calibration value */
76                 c = new GridBagConstraints();
77                 c.gridx = 0; c.gridy = 1;
78                 c.gridwidth = 3;
79                 c.fill = GridBagConstraints.NONE;
80                 c.anchor = GridBagConstraints.LINE_START;
81                 c.insets = il;
82                 c.ipady = 5;
83                 radio_calibration_label = new JLabel("Radio Calibration:");
84                 pane.add(radio_calibration_label, c);
85
86                 c = new GridBagConstraints();
87                 c.gridx = 3; c.gridy = 1;
88                 c.gridwidth = 3;
89                 c.fill = GridBagConstraints.HORIZONTAL;
90                 c.weightx = 1;
91                 c.anchor = GridBagConstraints.LINE_START;
92                 c.insets = ir;
93                 c.ipady = 5;
94                 radio_calibration_value = new JTextField("00000000");
95                 pane.add(radio_calibration_value, c);
96
97                 /* Buttons */
98                 c = new GridBagConstraints();
99                 c.gridx = 0; c.gridy = 2;
100                 c.gridwidth = 3;
101                 c.fill = GridBagConstraints.NONE;
102                 c.anchor = GridBagConstraints.CENTER;
103                 c.insets = il;
104                 ok = new JButton("OK");
105                 pane.add(ok, c);
106                 ok.addActionListener(this);
107                 ok.setActionCommand("ok");
108
109                 c = new GridBagConstraints();
110                 c.gridx = 3; c.gridy = 2;
111                 c.gridwidth = 3;
112                 c.fill = GridBagConstraints.NONE;
113                 c.anchor = GridBagConstraints.CENTER;
114                 c.insets = il;
115                 cancel = new JButton("Cancel");
116                 pane.add(cancel, c);
117                 cancel.addActionListener(this);
118                 cancel.setActionCommand("cancel");
119
120                 pack();
121                 setLocationRelativeTo(owner);
122         }
123
124         public AltosRomconfigUI(JFrame frame, AltosRomconfig config) {
125                 this(frame);
126                 set(config);
127         }
128
129         boolean selected;
130
131         /* Listen for events from our buttons */
132         public void actionPerformed(ActionEvent e) {
133                 String  cmd = e.getActionCommand();
134
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);
142                                 return;
143                         }
144                         selected = true;
145                 }
146                 setVisible(false);
147         }
148
149         int serial() {
150                 return Integer.parseInt(serial_value.getText());
151         }
152
153         void set_serial(int serial) {
154                 serial_value.setText(String.format("%d", serial));
155         }
156
157         int radio_calibration() {
158                 return Integer.parseInt(radio_calibration_value.getText());
159         }
160
161         void set_radio_calibration(int calibration) {
162                 radio_calibration_value.setText(String.format("%d", calibration));
163         }
164
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);
169                 }
170         }
171
172         AltosRomconfig romconfig() {
173                 try {
174                         return new AltosRomconfig(serial(), radio_calibration());
175                 } catch (NumberFormatException ne) {
176                         return null;
177                 }
178         }
179
180         public AltosRomconfig showDialog() {
181                 setVisible(true);
182                 if (selected)
183                         return romconfig();
184                 return null;
185         }
186
187         public static AltosRomconfig show(JFrame frame, AltosRomconfig config) {
188                 AltosRomconfigUI ui = new AltosRomconfigUI(frame, config);
189                 return ui.showDialog();
190         }
191 }