Add version numbers to java libraries
[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 org.altusmetrum.altosuilib_1.*;
24
25 public class AltosRomconfigUI
26         extends AltosUIDialog
27         implements ActionListener
28 {
29         Container       pane;
30         Box             box;
31         JLabel          serial_label;
32         JLabel          radio_calibration_label;
33
34         JFrame          owner;
35         JTextField      serial_value;
36         JTextField      radio_calibration_value;
37
38         JButton         ok;
39         JButton         cancel;
40
41         /* Build the UI using a grid bag */
42         public AltosRomconfigUI(JFrame in_owner) {
43                 super (in_owner, "Configure TeleMetrum Rom Values", true);
44
45                 owner = in_owner;
46                 GridBagConstraints c;
47
48                 Insets il = new Insets(4,4,4,4);
49                 Insets ir = new Insets(4,4,4,4);
50
51                 pane = getContentPane();
52                 pane.setLayout(new GridBagLayout());
53
54                 /* Serial */
55                 c = new GridBagConstraints();
56                 c.gridx = 0; c.gridy = 0;
57                 c.gridwidth = 3;
58                 c.fill = GridBagConstraints.NONE;
59                 c.anchor = GridBagConstraints.LINE_START;
60                 c.insets = il;
61                 serial_label = new JLabel("Serial:");
62                 pane.add(serial_label, c);
63
64                 c = new GridBagConstraints();
65                 c.gridx = 3; c.gridy = 0;
66                 c.gridwidth = 3;
67                 c.fill = GridBagConstraints.HORIZONTAL;
68                 c.weightx = 1;
69                 c.anchor = GridBagConstraints.LINE_START;
70                 c.insets = ir;
71                 serial_value = new JTextField("0");
72                 pane.add(serial_value, c);
73
74                 /* Radio calibration value */
75                 c = new GridBagConstraints();
76                 c.gridx = 0; c.gridy = 1;
77                 c.gridwidth = 3;
78                 c.fill = GridBagConstraints.NONE;
79                 c.anchor = GridBagConstraints.LINE_START;
80                 c.insets = il;
81                 c.ipady = 5;
82                 radio_calibration_label = new JLabel("Radio Calibration:");
83                 pane.add(radio_calibration_label, c);
84
85                 c = new GridBagConstraints();
86                 c.gridx = 3; c.gridy = 1;
87                 c.gridwidth = 3;
88                 c.fill = GridBagConstraints.HORIZONTAL;
89                 c.weightx = 1;
90                 c.anchor = GridBagConstraints.LINE_START;
91                 c.insets = ir;
92                 c.ipady = 5;
93                 radio_calibration_value = new JTextField("1186611");
94                 pane.add(radio_calibration_value, c);
95
96                 /* Buttons */
97                 c = new GridBagConstraints();
98                 c.gridx = 0; c.gridy = 2;
99                 c.gridwidth = 3;
100                 c.fill = GridBagConstraints.NONE;
101                 c.anchor = GridBagConstraints.CENTER;
102                 c.insets = il;
103                 ok = new JButton("OK");
104                 pane.add(ok, c);
105                 ok.addActionListener(this);
106                 ok.setActionCommand("ok");
107
108                 c = new GridBagConstraints();
109                 c.gridx = 3; c.gridy = 2;
110                 c.gridwidth = 3;
111                 c.fill = GridBagConstraints.NONE;
112                 c.anchor = GridBagConstraints.CENTER;
113                 c.insets = il;
114                 cancel = new JButton("Cancel");
115                 pane.add(cancel, c);
116                 cancel.addActionListener(this);
117                 cancel.setActionCommand("cancel");
118
119                 pack();
120                 setLocationRelativeTo(owner);
121         }
122
123         public AltosRomconfigUI(JFrame frame, AltosRomconfig config) {
124                 this(frame);
125                 set(config);
126         }
127
128         boolean selected;
129
130         /* Listen for events from our buttons */
131         public void actionPerformed(ActionEvent e) {
132                 String  cmd = e.getActionCommand();
133
134                 if (cmd.equals("ok")) {
135                         AltosRomconfig  romconfig = romconfig();
136                         if (romconfig == null || !romconfig.valid()) {
137                                 JOptionPane.showMessageDialog(this,
138                                                               "Invalid serial number or radio calibration value",
139                                                               "Invalid rom configuration",
140                                                               JOptionPane.ERROR_MESSAGE);
141                                 return;
142                         }
143                         selected = true;
144                 }
145                 setVisible(false);
146         }
147
148         int serial() {
149                 return Integer.parseInt(serial_value.getText());
150         }
151
152         void set_serial(int serial) {
153                 serial_value.setText(String.format("%d", serial));
154         }
155
156         int radio_calibration() {
157                 return Integer.parseInt(radio_calibration_value.getText());
158         }
159
160         void set_radio_calibration(int calibration) {
161                 radio_calibration_value.setText(String.format("%d", calibration));
162         }
163
164         public void set(AltosRomconfig config) {
165                 if (config != null && config.valid()) {
166                         set_serial(config.serial_number);
167                         set_radio_calibration(config.radio_calibration);
168                 }
169         }
170
171         AltosRomconfig romconfig() {
172                 try {
173                         return new AltosRomconfig(serial(), radio_calibration());
174                 } catch (NumberFormatException ne) {
175                         return null;
176                 }
177         }
178
179         public AltosRomconfig showDialog() {
180                 setVisible(true);
181                 if (selected)
182                         return romconfig();
183                 return null;
184         }
185
186         public static AltosRomconfig show(JFrame frame, AltosRomconfig config) {
187                 AltosRomconfigUI ui = new AltosRomconfigUI(frame, config);
188                 return ui.showDialog();
189         }
190 }