2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import javax.swing.event.*;
29 import java.util.prefs.*;
30 import java.util.concurrent.LinkedBlockingQueue;
34 public class AltosConfigTDUI
36 implements ActionListener, ItemListener, DocumentListener
44 JLabel frequency_label;
45 JLabel radio_calibration_label;
46 JLabel radio_frequency_label;
54 AltosFreqList radio_frequency_value;
55 JLabel radio_calibration_value;
62 ActionListener listener;
65 /* A window listener to catch closing events and tell the config code */
66 class ConfigListener extends WindowAdapter {
69 public ConfigListener(AltosConfigTDUI this_ui) {
73 public void windowClosing(WindowEvent e) {
74 ui.actionPerformed(new ActionEvent(e.getSource(),
75 ActionEvent.ACTION_PERFORMED,
80 /* Build the UI using a grid bag */
81 public AltosConfigTDUI(JFrame in_owner) {
82 super (in_owner, "Configure TeleDongle", false);
87 Insets il = new Insets(4,4,4,4);
88 Insets ir = new Insets(4,4,4,4);
90 pane = getContentPane();
91 pane.setLayout(new GridBagLayout());
94 c = new GridBagConstraints();
95 c.gridx = 0; c.gridy = 0;
97 c.fill = GridBagConstraints.NONE;
98 c.anchor = GridBagConstraints.LINE_START;
100 product_label = new JLabel("Product:");
101 pane.add(product_label, c);
103 c = new GridBagConstraints();
104 c.gridx = 4; c.gridy = 0;
106 c.fill = GridBagConstraints.HORIZONTAL;
108 c.anchor = GridBagConstraints.LINE_START;
110 product_value = new JLabel("");
111 pane.add(product_value, c);
114 c = new GridBagConstraints();
115 c.gridx = 0; c.gridy = 1;
117 c.fill = GridBagConstraints.NONE;
118 c.anchor = GridBagConstraints.LINE_START;
121 version_label = new JLabel("Software version:");
122 pane.add(version_label, c);
124 c = new GridBagConstraints();
125 c.gridx = 4; c.gridy = 1;
127 c.fill = GridBagConstraints.HORIZONTAL;
129 c.anchor = GridBagConstraints.LINE_START;
132 version_value = new JLabel("");
133 pane.add(version_value, c);
136 c = new GridBagConstraints();
137 c.gridx = 0; c.gridy = 2;
139 c.fill = GridBagConstraints.NONE;
140 c.anchor = GridBagConstraints.LINE_START;
143 serial_label = new JLabel("Serial:");
144 pane.add(serial_label, c);
146 c = new GridBagConstraints();
147 c.gridx = 4; c.gridy = 2;
149 c.fill = GridBagConstraints.HORIZONTAL;
151 c.anchor = GridBagConstraints.LINE_START;
154 serial_value = new JLabel("");
155 pane.add(serial_value, c);
158 c = new GridBagConstraints();
159 c.gridx = 0; c.gridy = 5;
161 c.fill = GridBagConstraints.NONE;
162 c.anchor = GridBagConstraints.LINE_START;
165 radio_frequency_label = new JLabel("Frequency:");
166 pane.add(radio_frequency_label, c);
168 c = new GridBagConstraints();
169 c.gridx = 4; c.gridy = 5;
171 c.fill = GridBagConstraints.HORIZONTAL;
173 c.anchor = GridBagConstraints.LINE_START;
176 radio_frequency_value = new AltosFreqList();
177 radio_frequency_value.addItemListener(this);
178 pane.add(radio_frequency_value, c);
179 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
181 /* Radio Calibration */
182 c = new GridBagConstraints();
183 c.gridx = 0; c.gridy = 6;
185 c.fill = GridBagConstraints.NONE;
186 c.anchor = GridBagConstraints.LINE_START;
189 radio_calibration_label = new JLabel("RF Calibration:");
190 pane.add(radio_calibration_label, c);
192 c = new GridBagConstraints();
193 c.gridx = 4; c.gridy = 6;
195 c.fill = GridBagConstraints.HORIZONTAL;
197 c.anchor = GridBagConstraints.LINE_START;
200 radio_calibration_value = new JLabel(String.format("%d", 1186611));
201 pane.add(radio_calibration_value, c);
204 c = new GridBagConstraints();
205 c.gridx = 0; c.gridy = 12;
207 c.fill = GridBagConstraints.NONE;
208 c.anchor = GridBagConstraints.LINE_START;
210 save = new JButton("Save");
212 save.addActionListener(this);
213 save.setActionCommand("Save");
215 c = new GridBagConstraints();
216 c.gridx = 2; c.gridy = 12;
218 c.fill = GridBagConstraints.NONE;
219 c.anchor = GridBagConstraints.CENTER;
221 reset = new JButton("Reset");
223 reset.addActionListener(this);
224 reset.setActionCommand("Reset");
226 c = new GridBagConstraints();
227 c.gridx = 6; c.gridy = 12;
229 c.fill = GridBagConstraints.NONE;
230 c.anchor = GridBagConstraints.LINE_END;
232 close = new JButton("Close");
234 close.addActionListener(this);
235 close.setActionCommand("Close");
237 addWindowListener(new ConfigListener(this));
240 /* Once the initial values are set, the config code will show the dialog */
241 public void make_visible() {
243 setLocationRelativeTo(owner);
247 /* If any values have been changed, confirm before closing */
248 public boolean check_dirty(String operation) {
250 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
252 i = JOptionPane.showOptionDialog(this,
253 String.format("Configuration modified. %s anyway?", operation),
254 "Configuration Modified",
255 JOptionPane.DEFAULT_OPTION,
256 JOptionPane.WARNING_MESSAGE,
257 null, options, options[1]);
264 /* Listen for events from our buttons */
265 public void actionPerformed(ActionEvent e) {
266 String cmd = e.getActionCommand();
268 if (cmd.equals("Close") || cmd.equals("Reboot"))
269 if (!check_dirty(cmd))
271 listener.actionPerformed(e);
272 if (cmd.equals("Close") || cmd.equals("Reboot")) {
279 /* ItemListener interface method */
280 public void itemStateChanged(ItemEvent e) {
284 /* DocumentListener interface methods */
285 public void changedUpdate(DocumentEvent e) {
289 public void insertUpdate(DocumentEvent e) {
293 public void removeUpdate(DocumentEvent e) {
297 /* Let the config code hook on a listener */
298 public void addActionListener(ActionListener l) {
302 /* set and get all of the dialog values */
303 public void set_product(String product) {
304 radio_frequency_value.set_product(product);
305 product_value.setText(product);
308 public void set_version(String version) {
309 version_value.setText(version);
312 public void set_serial(int serial) {
313 radio_frequency_value.set_serial(serial);
314 serial_value.setText(String.format("%d", serial));
317 public void set_radio_frequency(double new_radio_frequency) {
319 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
320 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
322 if (f.close(new_radio_frequency)) {
323 radio_frequency_value.setSelectedIndex(i);
327 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
328 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
330 if (new_radio_frequency < f.frequency)
333 String description = String.format("%s serial %s",
334 product_value.getText(),
335 serial_value.getText());
336 AltosFrequency new_frequency = new AltosFrequency(new_radio_frequency, description);
337 AltosPreferences.add_common_frequency(new_frequency);
338 radio_frequency_value.insertItemAt(new_frequency, i);
339 radio_frequency_value.setSelectedIndex(i);
342 public double radio_frequency() {
343 return radio_frequency_value.frequency();
346 public void set_radio_calibration(int calibration) {
347 radio_calibration_value.setText(String.format("%d", calibration));
350 public void set_clean() {