docs: Document altosui "Graph Data" button
[fw/altos] / ao-tools / 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
31 public class AltosRomconfigUI
32         extends JDialog
33         implements ActionListener
34 {
35         Container       pane;
36         Box             box;
37         JLabel          serial_label;
38         JLabel          radio_calibration_label;
39
40         JFrame          owner;
41         JTextField      serial_value;
42         JTextField      radio_calibration_value;
43
44         JButton         ok;
45         JButton         cancel;
46
47         /* Build the UI using a grid bag */
48         public AltosRomconfigUI(JFrame in_owner) {
49                 super (in_owner, "Configure TeleMetrum Rom Values", true);
50
51                 owner = in_owner;
52                 GridBagConstraints c;
53
54                 Insets il = new Insets(4,4,4,4);
55                 Insets ir = new Insets(4,4,4,4);
56
57                 pane = getContentPane();
58                 pane.setLayout(new GridBagLayout());
59
60                 /* Serial */
61                 c = new GridBagConstraints();
62                 c.gridx = 0; c.gridy = 0;
63                 c.gridwidth = 3;
64                 c.fill = GridBagConstraints.NONE;
65                 c.anchor = GridBagConstraints.LINE_START;
66                 c.insets = il;
67                 serial_label = new JLabel("Serial:");
68                 pane.add(serial_label, c);
69
70                 c = new GridBagConstraints();
71                 c.gridx = 3; c.gridy = 0;
72                 c.gridwidth = 3;
73                 c.fill = GridBagConstraints.HORIZONTAL;
74                 c.weightx = 1;
75                 c.anchor = GridBagConstraints.LINE_START;
76                 c.insets = ir;
77                 serial_value = new JTextField("0");
78                 pane.add(serial_value, c);
79
80                 /* Radio calibration value */
81                 c = new GridBagConstraints();
82                 c.gridx = 0; c.gridy = 1;
83                 c.gridwidth = 3;
84                 c.fill = GridBagConstraints.NONE;
85                 c.anchor = GridBagConstraints.LINE_START;
86                 c.insets = il;
87                 c.ipady = 5;
88                 radio_calibration_label = new JLabel("Radio Calibration:");
89                 pane.add(radio_calibration_label, c);
90
91                 c = new GridBagConstraints();
92                 c.gridx = 3; c.gridy = 1;
93                 c.gridwidth = 3;
94                 c.fill = GridBagConstraints.HORIZONTAL;
95                 c.weightx = 1;
96                 c.anchor = GridBagConstraints.LINE_START;
97                 c.insets = ir;
98                 c.ipady = 5;
99                 radio_calibration_value = new JTextField("1186611");
100                 pane.add(radio_calibration_value, c);
101
102                 /* Buttons */
103                 c = new GridBagConstraints();
104                 c.gridx = 0; c.gridy = 2;
105                 c.gridwidth = 3;
106                 c.fill = GridBagConstraints.NONE;
107                 c.anchor = GridBagConstraints.CENTER;
108                 c.insets = il;
109                 ok = new JButton("OK");
110                 pane.add(ok, c);
111                 ok.addActionListener(this);
112                 ok.setActionCommand("ok");
113
114                 c = new GridBagConstraints();
115                 c.gridx = 3; c.gridy = 2;
116                 c.gridwidth = 3;
117                 c.fill = GridBagConstraints.NONE;
118                 c.anchor = GridBagConstraints.CENTER;
119                 c.insets = il;
120                 cancel = new JButton("Cancel");
121                 pane.add(cancel, c);
122                 cancel.addActionListener(this);
123                 cancel.setActionCommand("cancel");
124
125                 pack();
126                 setLocationRelativeTo(owner);
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 }