Bump java lib versions in preparation for 1.9.2
[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_14;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import org.altusmetrum.altoslib_14.*;
25
26 public class AltosRomconfigUI
27         extends AltosUIDialog
28         implements ActionListener
29 {
30         Container       pane;
31         Box             box;
32         JLabel          product_label;
33         JLabel          serial_label;
34         JLabel          radio_calibration_label;
35
36         JLabel          product_value;
37         JFrame          owner;
38         JTextField      serial_value;
39         JTextField      radio_calibration_value;
40
41         JButton         ok;
42         JButton         cancel;
43
44         /* Build the UI using a grid bag */
45         public AltosRomconfigUI(JFrame frame, AltosRomconfig config) {
46                 super (frame, "Configure Rom Values", true);
47
48                 owner = frame;
49                 GridBagConstraints c;
50
51                 Insets il = new Insets(4,4,4,4);
52                 Insets ir = new Insets(4,4,4,4);
53
54                 pane = getContentPane();
55                 pane.setLayout(new GridBagLayout());
56
57                 int y = 0;
58
59                 /* Product name */
60                 /* Serial */
61                 c = new GridBagConstraints();
62                 c.gridx = 0; c.gridy = y;
63                 c.gridwidth = 3;
64                 c.fill = GridBagConstraints.NONE;
65                 c.anchor = GridBagConstraints.LINE_START;
66                 c.insets = il;
67                 product_label = new JLabel("Product:");
68                 pane.add(product_label, c);
69
70                 c = new GridBagConstraints();
71                 c.gridx = 3; c.gridy = y;
72                 c.gridwidth = 3;
73                 c.fill = GridBagConstraints.HORIZONTAL;
74                 c.weightx = 1;
75                 c.anchor = GridBagConstraints.LINE_START;
76                 c.insets = ir;
77                 product_value = new JLabel(config.usb_product);
78                 pane.add(product_value, c);
79
80                 y++;
81
82                 /* Serial */
83                 c = new GridBagConstraints();
84                 c.gridx = 0; c.gridy = y;
85                 c.gridwidth = 3;
86                 c.fill = GridBagConstraints.NONE;
87                 c.anchor = GridBagConstraints.LINE_START;
88                 c.insets = il;
89                 serial_label = new JLabel("Serial:");
90                 pane.add(serial_label, c);
91
92                 c = new GridBagConstraints();
93                 c.gridx = 3; c.gridy = y;
94                 c.gridwidth = 3;
95                 c.fill = GridBagConstraints.HORIZONTAL;
96                 c.weightx = 1;
97                 c.anchor = GridBagConstraints.LINE_START;
98                 c.insets = ir;
99                 serial_value = new JTextField("00000000");
100                 pane.add(serial_value, c);
101
102                 y++;
103
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;
108                         c.gridwidth = 3;
109                         c.fill = GridBagConstraints.NONE;
110                         c.anchor = GridBagConstraints.LINE_START;
111                         c.insets = il;
112                         c.ipady = 5;
113                         radio_calibration_label = new JLabel("Radio Calibration:");
114                         pane.add(radio_calibration_label, c);
115
116                         c = new GridBagConstraints();
117                         c.gridx = 3; c.gridy = y;
118                         c.gridwidth = 3;
119                         c.fill = GridBagConstraints.HORIZONTAL;
120                         c.weightx = 1;
121                         c.anchor = GridBagConstraints.LINE_START;
122                         c.insets = ir;
123                         c.ipady = 5;
124                         radio_calibration_value = new JTextField("00000000");
125                         pane.add(radio_calibration_value, c);
126
127                         y++;
128                 }
129
130                 /* Buttons */
131                 c = new GridBagConstraints();
132                 c.gridx = 0; c.gridy = y;
133                 c.gridwidth = 3;
134                 c.fill = GridBagConstraints.NONE;
135                 c.anchor = GridBagConstraints.CENTER;
136                 c.insets = il;
137                 ok = new JButton("OK");
138                 pane.add(ok, c);
139                 ok.addActionListener(this);
140                 ok.setActionCommand("ok");
141
142                 c = new GridBagConstraints();
143                 c.gridx = 3; c.gridy = y;
144                 c.gridwidth = 3;
145                 c.fill = GridBagConstraints.NONE;
146                 c.anchor = GridBagConstraints.CENTER;
147                 c.insets = il;
148                 cancel = new JButton("Cancel");
149                 pane.add(cancel, c);
150                 cancel.addActionListener(this);
151                 cancel.setActionCommand("cancel");
152
153                 y++;
154
155                 pack();
156                 setLocationRelativeTo(owner);
157                 set(config);
158         }
159
160         boolean selected;
161
162         /* Listen for events from our buttons */
163         public void actionPerformed(ActionEvent e) {
164                 String  cmd = e.getActionCommand();
165
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);
173                                 return;
174                         }
175                         selected = true;
176                 }
177                 setVisible(false);
178         }
179
180         int serial() {
181                 return Integer.parseInt(serial_value.getText());
182         }
183
184         void set_serial(int serial) {
185                 serial_value.setText(String.format("%d", serial));
186         }
187
188         int radio_calibration() {
189                 if (radio_calibration_value == null)
190                         return 0;
191
192                 return Integer.parseInt(radio_calibration_value.getText());
193         }
194
195         void set_radio_calibration(int calibration) {
196                 if (radio_calibration_value == null)
197                         return;
198                 radio_calibration_value.setText(String.format("%d", calibration));
199         }
200
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);
205                 }
206         }
207
208         AltosRomconfig romconfig() {
209                 try {
210                         return new AltosRomconfig(serial(), radio_calibration());
211                 } catch (NumberFormatException ne) {
212                         return null;
213                 }
214         }
215
216         public AltosRomconfig showDialog() {
217                 setVisible(true);
218                 if (selected)
219                         return romconfig();
220                 return null;
221         }
222
223         public static AltosRomconfig show(JFrame frame, AltosRomconfig config) {
224                 AltosRomconfigUI ui = new AltosRomconfigUI(frame, config);
225                 return ui.showDialog();
226         }
227 }