altosui: Complete split out of separate java library
[fw/altos] / altosui / 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import javax.swing.event.*;
26 import java.io.*;
27 import java.util.*;
28 import java.text.*;
29 import java.util.prefs.*;
30 import org.altusmetrum.AltosLib.*;
31
32 public class AltosRomconfigUI
33         extends AltosDialog
34         implements ActionListener
35 {
36         Container       pane;
37         Box             box;
38         JLabel          serial_label;
39         JLabel          radio_calibration_label;
40
41         JFrame          owner;
42         JTextField      serial_value;
43         JTextField      radio_calibration_value;
44
45         JButton         ok;
46         JButton         cancel;
47
48         /* Build the UI using a grid bag */
49         public AltosRomconfigUI(JFrame in_owner) {
50                 super (in_owner, "Configure TeleMetrum Rom Values", true);
51
52                 owner = in_owner;
53                 GridBagConstraints c;
54
55                 Insets il = new Insets(4,4,4,4);
56                 Insets ir = new Insets(4,4,4,4);
57
58                 pane = getContentPane();
59                 pane.setLayout(new GridBagLayout());
60
61                 /* Serial */
62                 c = new GridBagConstraints();
63                 c.gridx = 0; c.gridy = 0;
64                 c.gridwidth = 3;
65                 c.fill = GridBagConstraints.NONE;
66                 c.anchor = GridBagConstraints.LINE_START;
67                 c.insets = il;
68                 serial_label = new JLabel("Serial:");
69                 pane.add(serial_label, c);
70
71                 c = new GridBagConstraints();
72                 c.gridx = 3; c.gridy = 0;
73                 c.gridwidth = 3;
74                 c.fill = GridBagConstraints.HORIZONTAL;
75                 c.weightx = 1;
76                 c.anchor = GridBagConstraints.LINE_START;
77                 c.insets = ir;
78                 serial_value = new JTextField("0");
79                 pane.add(serial_value, c);
80
81                 /* Radio calibration value */
82                 c = new GridBagConstraints();
83                 c.gridx = 0; c.gridy = 1;
84                 c.gridwidth = 3;
85                 c.fill = GridBagConstraints.NONE;
86                 c.anchor = GridBagConstraints.LINE_START;
87                 c.insets = il;
88                 c.ipady = 5;
89                 radio_calibration_label = new JLabel("Radio Calibration:");
90                 pane.add(radio_calibration_label, c);
91
92                 c = new GridBagConstraints();
93                 c.gridx = 3; c.gridy = 1;
94                 c.gridwidth = 3;
95                 c.fill = GridBagConstraints.HORIZONTAL;
96                 c.weightx = 1;
97                 c.anchor = GridBagConstraints.LINE_START;
98                 c.insets = ir;
99                 c.ipady = 5;
100                 radio_calibration_value = new JTextField("1186611");
101                 pane.add(radio_calibration_value, c);
102
103                 /* Buttons */
104                 c = new GridBagConstraints();
105                 c.gridx = 0; c.gridy = 2;
106                 c.gridwidth = 3;
107                 c.fill = GridBagConstraints.NONE;
108                 c.anchor = GridBagConstraints.CENTER;
109                 c.insets = il;
110                 ok = new JButton("OK");
111                 pane.add(ok, c);
112                 ok.addActionListener(this);
113                 ok.setActionCommand("ok");
114
115                 c = new GridBagConstraints();
116                 c.gridx = 3; c.gridy = 2;
117                 c.gridwidth = 3;
118                 c.fill = GridBagConstraints.NONE;
119                 c.anchor = GridBagConstraints.CENTER;
120                 c.insets = il;
121                 cancel = new JButton("Cancel");
122                 pane.add(cancel, c);
123                 cancel.addActionListener(this);
124                 cancel.setActionCommand("cancel");
125
126                 pack();
127                 setLocationRelativeTo(owner);
128         }
129
130         public AltosRomconfigUI(JFrame frame, AltosRomconfig config) {
131                 this(frame);
132                 set(config);
133         }
134
135         boolean selected;
136
137         /* Listen for events from our buttons */
138         public void actionPerformed(ActionEvent e) {
139                 String  cmd = e.getActionCommand();
140
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);
148                                 return;
149                         }
150                         selected = true;
151                 }
152                 setVisible(false);
153         }
154
155         int serial() {
156                 return Integer.parseInt(serial_value.getText());
157         }
158
159         void set_serial(int serial) {
160                 serial_value.setText(String.format("%d", serial));
161         }
162
163         int radio_calibration() {
164                 return Integer.parseInt(radio_calibration_value.getText());
165         }
166
167         void set_radio_calibration(int calibration) {
168                 radio_calibration_value.setText(String.format("%d", calibration));
169         }
170
171         public void set(AltosRomconfig config) {
172                 if (config != null && config.valid()) {
173                         set_serial(config.serial_number);
174                         set_radio_calibration(config.radio_calibration);
175                 }
176         }
177
178         AltosRomconfig romconfig() {
179                 try {
180                         return new AltosRomconfig(serial(), radio_calibration());
181                 } catch (NumberFormatException ne) {
182                         return null;
183                 }
184         }
185
186         public AltosRomconfig showDialog() {
187                 setVisible(true);
188                 if (selected)
189                         return romconfig();
190                 return null;
191         }
192
193         public static AltosRomconfig show(JFrame frame, AltosRomconfig config) {
194                 AltosRomconfigUI ui = new AltosRomconfigUI(frame, config);
195                 return ui.showDialog();
196         }
197 }